#include <stdio.h>
#include <system.h>
#include <io.h>
#include "functions.h"
#include <stdlib.h>
#include <time.h>
#include <sys/alt_irq.h> // the irq functions

#define random(x) (rand()%x)

int i=0;
int j;
int m;
int n;
unsigned data_temp;
int state = 0;
int score0 = 0;
int score1 = 0;
int score2 = 0;
int life = 3;
long int panel_command;
long int mole_command;
int level_mode = 0;
int mole_command1=0;
int mole_command2=0;
int mole_command3=0;
int bomb=0;
int bottle=0;
int animal=0;
int animal2=0;

int hex_to_ten(int hex)
{
    char buf[100];
    sprintf(buf,"%d",hex);
    return atoi(buf);
}



int touch(int state_in){
	int x;
	int y;
	x = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,0);
	y = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,1);
	if(state_in == 0x4 || state_in == 0x5 || state_in == 0x6 || state_in == 0x7 )
	{
		if(x>0xA10 && x< 0xED0 && y> 0x430 && y< 0x7F0)
			return 0;//mole0
		else if(x>0xA10 && x< 0xED0 && y>0x7F1 && y< 0xBB0)
			return 1;
		else if(x>0xA10 && x< 0xED0 && y>0xBB0 && y< 0xF40)
			return 2;
		else if(x>0x530 && x< 0xA10 && y> 0x430 && y< 0x7F0)
			return 3;
		else if(x>0x530 && x< 0xA10 && y>0x7F1 && y< 0xBB0)
			return 4;
		else if(x>0x530 && x< 0xA10 && y>0xBB0 && y< 0xF40)
			return 5;
		else if(x>0x0C0 && x< 0x530 && y> 0x430 && y< 0x7F0)
			return 6;
		else if(x>0x0C0 && x< 0x530 && y>0x7F1 && y< 0xBB0)
			return 7;
		else if(x>0x0C0 && x< 0x530 && y>0xBB0 && y< 0xF40)
			return 8;//mole8
		else if(x>0x530 && x<0x790 && y>0x0 && y<0x430)
			return 9; //start key
		else if(x>0x2F0 && x< 0x530 && y>0x0 && y<0x430)
			return 0xA;//pause key
		else if(x>0x0 && x< 0x2F0 && y>0x0 && y<0x430)
			return 0xB; //quit key
		else
			return 0xC;
	}

	else if(state_in == 0x0 || state_in == 0x1 || state_in == 0x2 || state_in == 0x3)
	{
		if(x>0x810 && x< 0xA90 && y> 0x580 && y< 0xA10)
			return 0;// easy
		else if(x>0x610 && x< 0x810 && y> 0x580 && y< 0xA10)
			return 1; // normal
		else if(x>0x370 && x< 0x610 && y> 0x580 && y< 0xA10)
			return 2; // hard
		else if(x>0x0 && x< 0x350 && y> 0xCD0 && y< 0xF98)
			return 3; //go
		else
			return 4;
	}
}

int mole1(int num){

    if (num==0) return 0x000000;
    else if (num==1) return 0x000002;
    else if (num==2) return 0x000004;
    else if (num==3) return 0x000008;
    else if (num==4) return 0x000010;
    else if (num==5) return 0x000020;
    else if (num==6) return 0x000040;
    else if (num==7) return 0x000080;
    else if (num==8) return 0x000100;

    else if (num==9) return 0x001;
    else if (num==10) return 0x002;
    else if (num==11) return 0x004;
    else if (num==12) return 0x008;
    else if (num==13) return 0x010;
    else if (num==14) return 0x020;
    else if (num==15) return 0x040;
    else if (num==16) return 0x080;
    else if (num==17) return 0x100;

    else if (num==18) return 0x001;
    else if (num==19) return 0x002;
    else if (num==20) return 0x004;
    else if (num==21) return 0x008;
    else if (num==22) return 0x010;
    else if (num==23) return 0x020;
    else if (num==24) return 0x040;
    else if (num==25) return 0x080;
    else if (num==26) return 0x100;

}

int mole_data(int num){

    switch(num%3){
        case 0: animal=1;
            break;
        case 1: bottle=1;
            break;
        case 2: bomb=1;
            break;
    }

    while (num%3!=0){
        num--;
    }
    int i=num/3;

    switch(i)
    {
        case 0: return mole1(num);
            break;
        case 1: return mole1(num)*8;
            break;
        case 2: return mole1(num)*8*8;
            break;
        case 3: return mole1(num)*8*8*8;
            break;
        case 4: return mole1(num)*8*8*8*8;
            break;
        case 5: return mole1(num)*8*8*8*8*8;
            break;
        case 6: return mole1(num)*8*8*8*8*8*8;
            break;
        case 7: return mole1(num)*8*8*8*8*8*8*8;
            break;
        case 8: return mole1(num)*8*8*8*8*8*8*8*8;
            break;

    }
}



//Music interrupt function
static void standbymusic(){
    for(j=0;j<256;j++)
    {
        IOWR_16DIRECT(MUSICCONTROLLER_0_BASE,j*2,standby[i]);
        i++;
        if(i >24533 ){
            i = 0;
        }
    }
}


void state_sel(int state_in)
{
	switch(state_in)
	{
            //Welcome-Easy
        case 0x0:
            if(touch(state_in) != 4)
            {
                if(touch(state_in)== 0)
                    state = 0x0; //easy
                else if(touch(state_in) == 1)
                    state = 0x1; // normal
                else if(touch(state_in) == 2)
                    state = 0x2; // hard
                else if(touch(state_in)==3){
                    state = 0x4;
                    level_mode=0;
                }
            }

            break;
            //WELCOME-NORMAL
        case 0x1:
            if(touch(state_in) != 4)
            {
                if(touch(state_in)== 0)
                    state = 0;
                else if(touch(state_in) == 1)
                    state = 0x1;
                else if(touch(state_in) == 2)
                    state = 0x2;
                else if(touch(state_in)==3){
                    state = 0x4;
                    level_mode=1;
                }
            }
            break;
            //WELCOME-HARD
        case 0x2:
            if(touch(state_in) != 4)
            {
                if(touch(state_in)== 0)
                    state = 0;
                else if(touch(state_in) == 1)
                    state = 0x1;
                else if(touch(state_in) == 2)
                    state = 0x2;
                else if(touch(state_in)==3){
                    state = 0x4;
                    level_mode=2;
                }
            }
            break;
            //unused state
        case 0x3:
            break;

            //PLAY-INIT
        case 0x4:
            if(touch(state_in) == 0xB)
                state = 0x0;
            else if(touch(state_in) == 0x9)
                state = 0x5;
            break;

            //PLAY- ONGOING
        case 0x5:
            if(touch(state_in) == 0xB)
                state = 0x0;
            else if(touch(state_in) == 0xA)
                state = 0x6;

            break;

            //PLAY- PAUSE
        case 0x6:
            if(touch(state_in) == 0xB)
                state = 0x0;
            else if(touch(state_in) == 0x9)
                state = 0x5;
            break;

        case 0x7:
            break;
        case 0x8:
            break;
        case 0x9:
            break;
        case 0xA:
            break;
        case 0xB:
            break;
        case 0xC:
            break;
        case 0xD:
            break;
        case 0xE:
            break;
        case 0xF:
            break;
	}
}

/*

int void mole_data2(int num2){

    animal2=1;

    while (num2%3!=0){
        num2--;
    }
    int i=num2/3;

    switch(i)
    {
        case 0: mole_command1=mole1(num2);
            break;
        case 1: mole_command1=mole1(num2)*8;
            break;
        case 2: mole_command1=mole1(num2)*8*8;
            break;
        case 3: mole_command1=mole1(num2)*8*8*8;
            break;
        case 4: mole_command1=mole1(num2)*8*8*8*8;
            break;
        case 5: mole_command1=mole1(num2)*8*8*8*8*8;
            break;
        case 6: mole_command1=mole1(num2)*8*8*8*8*8*8;
            break;
        case 7: mole_command1=mole1(num2)*8*8*8*8*8*8*8;
            break;
        case 8: mole_command1=mole1(num2)*8*8*8*8*8*8*8*8;
            break;

    }
}

*/
typedef enum {True, False} bool;

static void statedetect()
{
	state_sel(state);
	panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
	IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
	printf("interrupt state: %d\n", state);
}


int main (int argc, const char * argv[])
{
	printf("ni da ye????? %d\n");
	//Music interrupt
	alt_irq_register(MUSICCONTROLLER_0_IRQ, NULL,(void*)standbymusic);
	//touch interrupt
	alt_irq_register(COMMAND_0_IRQ, NULL,(void*)statedetect);

	init();
	int previousx;
	int previousy;
	int currentx;
	int currenty;
	int counter = 0;
	int random_number=0;
    int random_number2=0;
	int mole_position=0;
	int moleP=0;

//    bool flag = False;
//	clock_t start;
//	clock_t end;

//	previousx = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,0);
//	previousy = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,1);
	IOWR_CONTROL(COMMAND_0_BASE,0,0x30);
	IOWR_CONTROL(COMMAND_0_BASE,1,0);
	printf("how come????? \n");
    while (1){
    	//printf("in or out? \n");
    if (state==0x0 || state==0x1 || state==0x2){
    	life=3;
    	score0=0;
    	score1=0;
    	score2=0;
    }


	if (state==0x4){
		printf("level = %d", level_mode);
		if (level_mode==0)
		{
			printf("state = %d", state);
			while(state == 0x4 || state == 0x5 || state ==0x6)
			{
			if (state==0x5)
			{
				IOWR_CONTROL(COMMAND_0_BASE,1,0x4);
				panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
				printf("state: %d\n",state);
//				printf("life: %d\n",life);
//				printf("panel: 0x%d\n",panel_command);
			}
			}
		}
	}
    }
 return 0;
}
//				mole_command = mole_data(random_number);
//				IOWR_CONTROL(COMMAND_0_BASE,1,0x4);
//				IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//				start=clock();
//				printf("000");
//
//			while(1){
//				//random_number = rand()%27;
//				random_number=0;
//				mole_command = mole_data(random_number);
//
//
//				while (counter<100000){
//
//					counter++;
//					currentx = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,0);
//					currenty = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,1);
//					//pause
//					if (state==0xA){
//						printf("111");
//						panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//						IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//						IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//
//
//                        fflush(stdout);
//                        while(state==0x5){
//                            panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//                            IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//                            IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                            break;
//                        }
//
//
//					}else if (state==0xB){
//						printf("222");
//						panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//						score0=0;
//						score1=0;
//						score2=0;
//						life=3;
//                        IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//						IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//
//					}else{
//					//	printf("333");
//                    if(previousx!=currentx || previousy!=currenty){
//                        if (random_number==0 || random_number==1 || random_number==2){
//                            moleP=0;
//                        }else if (random_number==3 || random_number==4 || random_number==5){
//                            moleP=1;
//                        }else if (random_number==6 || random_number==7 || random_number==8){
//                            moleP=2;
//                        }else if (random_number==9 || random_number==10 || random_number==11){
//                            moleP=3;
//                        }else if (random_number==12 || random_number==13 || random_number==14){
//                            moleP=4;
//                        }else if (random_number==15 || random_number==16 || random_number==17){
//                            moleP=5;
//                        }else if (random_number==18 || random_number==19 || random_number==20){
//                            moleP=6;
//                        }else if (random_number==21 || random_number==22 || random_number==23){
//                            moleP=7;
//                        }else if (random_number==24 || random_number==25 || random_number==26){
//                            moleP=8;
//                        }
//                        //printf("moleP, %d",moleP);
//                       // printf("touch, %d",touch(state));
//
//                        if (touch(state)==moleP){
//                        	printf("444");
//                            if (animal==1){
//                            	printf("555");
//                               score0=score0+5;
//                                if (score0>=10){
//                                    score1++;
//                                    score0=score0%10;
//                                }
//                                if (score1>=10){
//                                    score2++;
//                                    score1=score1%10;
//                                }
//                                panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//
//                                IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//                                IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                                animal=0;
//
//                            }else if (bottle==1){
//                                if (life==3){
//                                    IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//                                    IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                                }else {
//                                    life++;
//                                    panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//
//                                    IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//                                    IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                                }
//                                bottle=0;
//                            }else if (bomb==1){
//                                life--;
//                                if (life==0){
//                                    panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//                                    IOWR_CONTROL(COMMAND_0_BASE,1,0x0);
//                                    IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                                }else{
//                                    panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//                                    IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//                                    IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//
//                                }
//                                bomb=0;
//                            }
//
//                        }else{
//                            IOWR_CONTROL(COMMAND_0_BASE,1,mole_position);
//                            life--;
//                            if (life==0){
//                                panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//                                IOWR_CONTROL(COMMAND_0_BASE,1,0x0);
//                                IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                            }else{
//                                panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//                                IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//                                IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//
//                            }
//                        }
//
//                        previousx=currentx;
//                        previousy=currenty;
//
//                    }else{
//                        IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                        IOWR_CONTROL(COMMAND_0_BASE,1,mole_position);
//                    }
//                }
//
//                end=clock();
//                if ((int)((end-start)/CLOCKS_PER_SEC)>=120){
//                    // next round
//                    state=0x0;
//                    score0=0;
//                    score1=0;
//                    score2=0;
//                    mole_position=0;
//                    panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//                    IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                    IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//
//                }
//			}
//
//            IOWR_CONTROL(COMMAND_0_BASE,1,0x0);
//            counter = 0;
//		}
//		}
//
//
//	}else if (level_mode==1){
//        // normal mode
//        if (state==0x5){
//            panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//            mole_command = mole_data(random_number);
//            IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//            IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//            start=clock();
//        }
//        while(1){
//            random_number = rand()%27;
//            mole_command = mole_data(random_number);
//
//
//            while (counter<70000){
//                counter++;
//                currentx = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,0);
//                currenty = IORD_XY_DATA(XYCOORD_INTERFACE_0_BASE,1);
//                //pause
//                if (touch(state)==0xA){
//                    panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//                    IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//                    IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//
//                    // 该命令需要调试确定
//                    fflush(stdout);
//                    while(touch(state)==0x5){
//                        panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//                        IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//                        IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                        break;
//                    }
//
//
//                }else if (touch(state)==0xB){
//                    panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//                    score0=0;
//                    score1=0;
//                    score2=0;
//                    life=3;
//                    IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//                    IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//
//                }else{
//                    // 第二只老鼠跳起来，command变化
//
//                    if (counter>40000){
//                        if (!flag){
//                            random_number2 = rand()%27;
//               //             mole_command = mole_data2(random_number2);
//                        }
//                    }
//
//                    if(previousx!=currentx || previousy!=currenty){
//                        if (random_number==0 || random_number==1 || random_number==2 || random_number2==0 || random_number2==1 || random_number2==2){
//                            moleP=0;
//                        }else if (random_number==3 || random_number==4 || random_number==5 || random_number2==3 || random_number2==4 || random_number2==5){
//                            moleP=1;
//                        }else if (random_number==6 || random_number==7 || random_number==8 || random_number2==6 || random_number2==7 || random_number2==8){
//                            moleP=2;
//                        }else if (random_number==9 || random_number==10 || random_number==11 || random_number2==9 || random_number2==10 || random_number2==11){
//                            moleP=3;
//                        }else if (random_number==12 || random_number==13 || random_number==14 || random_number2==12 || random_number2==13 || random_number2==14){
//                            moleP=4;
//                        }else if (random_number==15 || random_number==16 || random_number==17 || random_number2==15 || random_number2==16 || random_number2==17){
//                            moleP=5;
//                        }else if (random_number==18 || random_number==19 || random_number==20 || random_number2==18 || random_number2==19 || random_number2==20){
//                            moleP=6;
//                        }else if (random_number==21 || random_number==22 || random_number==23 || random_number2==21 || random_number2==22 || random_number2==23){
//                            moleP=7;
//                        }else if (random_number==24 || random_number==25 || random_number==26 || random_number2==24 || random_number2==25 || random_number2==26){
//                            moleP=8;
//                        }
//
//                    if (touch(state)==moleP){
//                        if (animal==1){
//                            score0=score0+5;
//                            if (score0>=10){
//                                score1++;
//                                score0=score0%10;
//                            }
//                            if (score1>=10){
//                                score2++;
//                                score1=score1%10;
//                            }
//                            panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//
//                            IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//                            IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//
//
//                        }else if (bottle==1){
//                            if (life==3){
//                                IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//                                IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                            }else {
//                                life++;
//                                panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//
//                                IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//                                IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                            }
//
//                        }else if (bomb==1){
//                            life--;
//                            if (life==0){
//                                panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//                                IOWR_CONTROL(COMMAND_0_BASE,1,0x0);
//                                IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                            }else{
//                                panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//                                IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//                                IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//
//                            }
//
//                        }
//
//                    }else{
//                        IOWR_CONTROL(COMMAND_0_BASE,1,mole_position);
//                        life--;
//                        if (life==0){
//                            panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//                            IOWR_CONTROL(COMMAND_0_BASE,1,0x0);
//                            IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                        }else{
//                            panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//                            IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//                            IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//
//                        }
//                    }
//
//
//
//                    previousx=currentx;
//                    previousy=currenty;
//
//                }else{
//                    IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                    IOWR_CONTROL(COMMAND_0_BASE,1,mole_position);
//                }
//
//            }
//
//            end=clock();
//            if ((int)((end-start)/CLOCKS_PER_SEC)>=120){
//                // next round
//                state=0x0;
//                score0=0;
//                score1=0;
//                score2=0;
//                mole_position=0;
//                panel_command = state+ life*16 + score0*16*16 + score1*16*16*16 + score2*16*16*16*16;
//                IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
//                IOWR_CONTROL(COMMAND_0_BASE,1,mole_command);
//
//            }
//        }
//
//        IOWR_CONTROL(COMMAND_0_BASE,1,0x0);
//        counter = 0;
//    }
//
//
//
//
//    }else if (level_mode==2){
//
//        // hard mode
//
//
//
//    }
//
//}
//}
//    return 0;
//}
//
