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

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

int i=0;
int j;
int jj;
int m;
int mm;
int n;
int time_interval=0;
unsigned data_temp;
int state = 0;
int score0 = 0;
int score1 = 0;
int score2 = 0;
int game_time=0;
int life = 3;
int moleP=0;
int showtime=0;
int counter = 0;
int counter_previous = 0;
int random_number=0;
long int panel_command=0;
long int mole_command=0;
int mole_status=0;
int level_mode = 0;
int mole_command1=0;
int mole_command2=0;
int mole_command3=0;
int item=0;
int animal2=0;
int whichmole;
clock_t start_t;
clock_t end_t;
clock_t total_t;

int hit_count;
int miss_count;
int life_count =3;
int state;
int time_counter;



short audio_to_play;
short *music_to_play;
short music_to_play_temp;
short sound_to_play_temp;
short *remember_music_to_play;
short *sound_to_play;
int music = 0;
int remember_music;
int remember_musiclength;
int music_length;
int sound = 0;
int sound_length;
int a,b,c,d,e;
int aa,bb,cc,dd,ee,ff;
int pause = 0;
int pre_pause = 0;

static void standbymusic(){
//	switch(music){
//	case 0: music_length = 368139; break;
//	case 1:	music_length = 78912;  break;
//	case 2:	music_length = 368140;	break;
//	case 3:	music_length = 80640;	break;
//	case 4:	music_length = 87552;	break;
//	default:	break;
//	}
	if (b != 1 && d != 1 && e != 1){
	switch(music){
			 	case 0: music_length = 368139; if(a==0){i=0;} a=1; b=0 ;c=0;d=0;e = 0;
			 	                                  music_to_play = standby;    break;
			 	case 1:	music_length = 78912;  if(b==0){i=0;} b=1; a=0;c=0;d=0;e = 0;
			 	                                  music_to_play = lady_go;    break;
			 	case 2:	music_length = 368140; if(c==0){i=0;} c=1; a=0;b=0;d=0;e = 0;
			 	                                  music_to_play = background_music; break;
			 	case 3:	music_length = 67026;  if(d==0){i=0;} d=1; a=0;b=0;c=0;e = 0;
			 	                                  music_to_play = victory;    break;
			 	case 4:	music_length = 63295;  if(e==0){i=0;} e=1; a=0;b=0;c=0;d = 0;
			 	                                  music_to_play = defeat;       break;
//			 	        case 5: music_length = 5790;  if(aa==0) m=1; aa=1; bb=0; dd=0; ee=0; ff = 0;
//			 	                                 				 		 music_to_play = hit;     break;
//			 	      	case 6:	music_length = 14008;  if(bb==0){m=1;} bb=1; aa=0;cc=0;dd=0;ee=0;ff = 0;
//			 	                                 				 		 music_to_play = gold;    break;
//			 	     	case 7:	music_length = 13540; if(cc==0){m=1;} cc=1; aa=0;bb=0;dd=0;ee=0;ff = 0;
//			 	                                 				 		 music_to_play = explo;   break;
//			 	        case 8:	music_length = 6571;  if(dd==0){m=1;} dd=1; aa=0;bb=0;cc=0;ee=0;ff = 0;
//			 	                                 				 		 music_to_play = miss;    break;
//			 	        case 9:	music_length = 1961;  if(ee==0){m=1;} ee=1; aa=0;bb=0;cc=0;dd=0;ff = 0;
//			 	                                 				 		 music_to_play = button_chose; break;
//			 	        case 10:	music_length = 1128;  if(ff==0){m=1;} ff=1; aa=0;bb=0;cc=0;dd=0;ee = 0;
//			 	                                 				 		 music_to_play = button_wood;  break;
			 	default:	break;
			 	}
	}
					 switch(sound){
					 		 	case 0: sound_length = 5790;
			 	 	 	 	 	 	 	 if(aa==0) m=0;
			 	 	 	 	 	 	 	 aa=1;
			 	 	 	 	 	 	 	 bb=0;
			 	 	 	 	 	 	 	 cc=0;
			 	 	 	 	 	 	 	 dd=0;
			 	 	 	 	 	 	 	 ee=0;
			 	 	 	 	 	 	 	 ff = 0;
					 		 	         sound_to_play = hit;
			 	 	 	 	 	 	 	 break;
					 		 	case 1:	sound_length = 14008;  if(bb==0){m=0;} bb=1; aa=0;cc=0;dd=0;ee=0;ff = 0;
					 		 	                                  sound_to_play = gold;    break;
					 		 	case 2:	sound_length = 13540; if(cc==0){m=0;} cc=1; aa=0;bb=0;dd=0;ee=0;ff = 0;
					 		 	                                  sound_to_play = explo;   break;
					 		 	case 3:	sound_length = 6571;  if(dd==0){m=0;} dd=1; aa=0;bb=0;cc=0;ee=0;ff = 0;
					 		 	                                  sound_to_play = miss;    break;
					 		 	case 4:	sound_length = 1961;  if(ee==0){m=0;} ee=1; aa=0;bb=0;cc=0;dd=0;ff = 0;
					 		 					 		 	      sound_to_play = button_chose; break;
					 		 	case 5:	sound_length = 1128;  if(ff==0){m=0;} ff=1; aa=0;bb=0;cc=0;dd=0;ee = 0;
					 		 					 		 		  sound_to_play = button_wood;  break;
					 		 	default: aa=0;bb=0;cc=0;dd=0;ee=0;ff = 0; m = 0;  break;
					 		 	}





	 for(j=0;j<2048;j++)
	     {
//		 switch(music){
//		 	case 0: music_length = 368139; if(a==0){i=0;} a=1; b=0 ;c=0;d=0;e = 0;
//		 	                                  music_to_play = standby[i];    break;
//		 	case 1:	music_length = 78912;  if(b==0){i=0;} b=1; a=0;c=0;d=0;e = 0;
//		 	                                  music_to_play = lady_go[i];    break;
//		 	case 2:	music_length = 368140; if(c==0){i=0;} c=1; a=0;b=0;d=0;e = 0;
//		 	                                  music_to_play = background_music[i]; break;
//		 	case 3:	music_length = 67026;  if(d==0){i=0;} d=1; a=0;b=0;c=0;e = 0;
//		 	                                  music_to_play = victory[i];    break;
//		 	case 4:	music_length = 63295;  if(e==0){i=0;} e=1; a=0;b=0;c=0;d = 0;
//		 	                                  music_to_play = defeat[i];       break;
//		 	        case 5: music_length = 5790;  if(aa==0) m=0; aa=1; bb=0; dd=0; ee=0; ff = 0;
//		 	                                 				 		 music_to_play = hit[m];     break;
//		 	      	case 6:	music_length = 14008;  if(bb==0){m=0;} bb=1; aa=0;cc=0;dd=0;ee=0;ff = 0;
//		 	                                 				 		 music_to_play = gold[m];    break;
//		 	     	case 7:	music_length = 13540; if(cc==0){m=0;} cc=1; aa=0;bb=0;dd=0;ee=0;ff = 0;
//		 	                                 				 		 music_to_play = explo[m];   break;
//		 	        case 8:	music_length = 6571;  if(dd==0){m=0;} dd=1; aa=0;bb=0;cc=0;ee=0;ff = 0;
//		 	                                 				 		 music_to_play = miss[m];    break;
//		 	        case 9:	music_length = 1961;  if(ee==0){m=0;} ee=1; aa=0;bb=0;cc=0;dd=0;ff = 0;
//		 	                                 				 		 music_to_play = button_chose[m]; break;
//		 	        case 10:	music_length = 1128;  if(ff==0){m=0;} ff=1; aa=0;bb=0;cc=0;dd=0;ee = 0;
//		 	                                 				 		 music_to_play = button_wood[m];  break;
//		 	default:	break;
//		 	}

//
//				 switch(sound){
//				 		 	case 0: sound_length = 5790;
//		 	 	 	 	 	 	 	 if(aa==0) m=0;
//		 	 	 	 	 	 	 	 aa=1;
//		 	 	 	 	 	 	 	 bb=0;
//		 	 	 	 	 	 	 	 cc=0;
//		 	 	 	 	 	 	 	 dd=0;
//		 	 	 	 	 	 	 	 ee=0;
//		 	 	 	 	 	 	 	 ff = 0;
//				 		 	         sound_to_play = hit[m];
//		 	 	 	 	 	 	 	 break;
//				 		 	case 1:	sound_length = 14008;  if(bb==0){m=0;} bb=1; aa=0;cc=0;dd=0;ee=0;ff = 0;
//				 		 	                                  sound_to_play = gold[m];    break;
//				 		 	case 2:	sound_length = 13540; if(cc==0){m=0;} cc=1; aa=0;bb=0;dd=0;ee=0;ff = 0;
//				 		 	                                  sound_to_play = explo[m];   break;
//				 		 	case 3:	sound_length = 6571;  if(dd==0){m=0;} dd=1; aa=0;bb=0;cc=0;ee=0;ff = 0;
//				 		 	                                  sound_to_play = miss[m];    break;
//				 		 	case 4:	sound_length = 1961;  if(ee==0){m=0;} ee=1; aa=0;bb=0;cc=0;dd=0;ff = 0;
//				 		 					 		 	      sound_to_play = button_chose[m]; break;
//				 		 	case 5:	sound_length = 1128;  if(ff==0){m=0;} ff=1; aa=0;bb=0;cc=0;dd=0;ee = 0;
//				 		 					 		 		  sound_to_play = button_wood[m];  break;
//				 		 	default: sound_to_play = 0; aa=0;bb=0;cc=0;dd=0;ee=0;ff = 0; m = 0;  break;
//				 		 	}

//		  if (music != 5 && 6 && 7 && 8 && 9 && 10){remember_music = music;remember_musiclength = music_length;}


          if (pause == 1){audio_to_play=0;}
//          else if (m!=0){audio_to_play = music_to_play[m];}
//          else audio_to_play = music_to_play[i];
          else if (sound !=6) {
        	  music_to_play_temp = music_to_play[i];
        	  sound_to_play_temp = sound_to_play[m];
        	  audio_to_play = (music_to_play_temp >> 2) + ((sound_to_play_temp>>2)*3);
          }
          else audio_to_play = music_to_play[i];

//          else if (music == 0 || music == 1 || music == 2 || music == 3 || music == 4){
//        	   audio_to_play = music_to_play[i];
//          		  }
//          else audio_to_play = music_to_play[m];



		   IOWR_16DIRECT(MUSICCONTROLLER_0_BASE,j*2,audio_to_play);
		     i++;
	         m++;


//	         if (music == 0 || music == 1 || music == 2 || music == 3 || music == 4){
//	        	 remember_music = music;remember_musiclength = music_length;remember_music_to_play = music_to_play;
//	        	 if(i == music_length){ i = 0;}
//	         }
//	         else {
//	        	 if(i == remember_musiclength){ i= 0;}
//	        	 if (m == music_length){ m = 0; music = remember_music; music_length = remember_musiclength;music_to_play = remember_music_to_play;}
//	         }


//	         if (music != 0 && 1 && 2 && 3 && 4 && 5){
//	        	 if(i == remember_musiclength){ i= 0;}
//	         else if (i == music_length) {i= 0;}
//	         if (m == music_length){ m = 0; music = remember_music; }
//
//	         }

		  if(i == music_length){ i = 0;
		    if (music == 1) {music = 2; b = 0;}
		    else if (music == 3 || music == 4) {state = 0;d = 0; e = 0;}

		  }
			//	  if(i == 368139){ i = 0;}
		  if(m == sound_length){
			  if (pre_pause == 1){pause = 1;}
			  m = 0; sound = 6;}
		 }

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


int touch(int state_in){
	int x;
	int y;
    if (time_counter > 10000000){


	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 )
	{   music = 2;
		if(x>0xA10  && x< 0xED0 && y> 0x000 && y< 0x7F0)
			music = 0; //area1
		else if(x>0xA10 && x< 0xED0 && y>0x7F1 && y< 0xF40)
			music = 2; //area2
		else if(x>0x530 && x< 0xA10 && y>0x430 && y< 0xBB0)
			{if (pre_pause == 1) {pre_pause = 0; pause = 0;sound = 5;}
			else {pre_pause = 1;sound = 5;}
			}         //area3 (pause)
		else if(x>0x0C0 && x< 0x530 && y> 0x000 && y< 0x430)
			{ sound = 0;
			   hit_count ++;
			   if (hit_count == 10)
			      music = 3;
			}            //area4 (hit)
		else if(x>0x0C0 && x< 0x530 && y> 0x430 && y< 0x7F0)
		    {  sound = 3;
				miss_count ++;
				if (miss_count == 10)
				  music = 4;
					}      //area5 (miss)
		else if(x>0x0C0 && x< 0x530 && y>0x7F1 && y< 0xBB0)
		{      sound = 1;
			   if (life_count < 3) life_count ++;
					}    //area6 (life)
		else if(x>0x0C0 && x< 0x530 && y>0xBB0 && y< 0xF40)
		{	 sound = 2;
		       if (life_count == 0) music = 4;
		       else life_count --;
		}                   //area7 (skull)
		else
			return 0xC;
	}

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

}




int mole_position(int num,int status){

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

    while (num%3!=0){
        num--;
    }
    int i=num/3;
    whichmole = i;
   mole_command = item*pow(8,i)+status*pow(8,i)*4;
   return mole_command;
}


void check_mole(){


	 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;
	 }
}

void state_sel(int state_in)
{
	check_mole();
	switch(state_in)
	{
            //Welcome-Easy
        case 0x0:
            if(touch(state_in) != 4)
            {
                if(touch(state_in)== 0)
                {
                    state = 0x0; //easy
//                    music = 9;
                	sound = 4;
//                	sound_length = 1961;
//                	if(ee==0){m=0;}
//                	ee=1;
//                	aa=0;
//                	bb=0;
//                	cc=0;
//                	dd=0;
//                	ff = 0;
//                    sound_to_play = button_chose;
                }
                else if(touch(state_in) == 1)
                {
                    state = 0x1; // normal
//                   music = 9;
                	sound = 4;
                }
                else if(touch(state_in) == 2)
                {
                    state = 0x2; // hard
//                  music = 9;
                	sound = 4;
                }
                else if(touch(state_in)==3){
                    state = 0x4;
                    level_mode=0;
 //                   music = 9;
                    sound = 4;
                }
            }

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

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

            //PLAY- ONGOING
        case 0x5:
            if(touch(state_in) == 0xB)
            {
                state = 0x0;
                sound =5;
            }
            else if(touch(state_in) == 0xA)
            {
                state = 0x6;
                sound =5;
            }
            else if(touch(state_in) == moleP){
            	mole_status = 0;
            	if(item == 0)
            		sound = 0;
            	else if(item == 1)
            		sound =2;
            	else
            		sound = 1;
            }
            else
            	sound = 3;
            break;

            //PLAY- PAUSE
        case 0x6:
            if(touch(state_in) == 0xB)
            {
                state = 0x0;
                sound =5;
            }
            else if(touch(state_in) == 0x9)
            {
                state = 0x5;
                sound =5;
            }
            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;
	}
}




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(INTERRUPT_0_BASE,0,0);
	IOWR_CONTROL(COMMAND_0_BASE,0,panel_command);
}


int main (int argc, const char * argv[])
{



//
//	//Music interrupt
//	alt_irq_register(MUSICCONTROLLER_0_IRQ, NULL,(void*)standbymusic);
//	//touch interrupt
//	alt_irq_register(INTERRUPT_0_IRQ, NULL,(void*)statedetect);

	init();


    // initialization
	IOWR_CONTROL(COMMAND_0_BASE,0,0x30);
	IOWR_CONTROL(COMMAND_0_BASE,1,0);

	for(;;)
	{
		//Music interrupt
		alt_irq_register(MUSICCONTROLLER_0_IRQ, NULL,(void*)standbymusic);
		//touch interrupt
		alt_irq_register(INTERRUPT_0_IRQ, NULL,(void*)statedetect);

        time_counter ++;
	}
    return 0;
}



