///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//-----------------------------------------------------------------------------------------------------------------------------------------
//  Memory2X version 0.1 by GuZzO             02-11-2010
//-----------------------------------------------------------------------------------------------------------------------------------------
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Program memory;


Const
	timer_in_menu=7;
	timer_between_menu=15;
   
Global
	timer_touch=timer_in_menu;
	//start_game=0;
	taille_plateau=4;
	choix_tileset=0;
	nb_validated_card;
	
	//GP2X Keys
	__A = _control;
	__B = _alt;
	__X = _x;
	__Y = _y;
	__SELECT = _space;
	__R = _tab;
	__L = _backspace;
	__START = _enter;
	__VolP = _a;
	__VolM = _s;
	
	//Sprites & Fonts
	fpg1;
	fnt1;
	fnt2;
	snd1;

	//Test choix carte
	check_collision_card;
	nb_card_selected = 0;
	check_rotate_global;
	check_first_card;
	check_first_card_id;
	check_first_card_x;
	check_first_card_y;
	check_second_card;
	check_second_card_id;
	check_second_card_x;
	check_second_card_y;

	Struct memory_gameboard[6][4]
		int card_id;
		int card_gfx;
		int card_selected;
		int card_validated;
	End;

//DEBUG
tmp_graph;


BEGIN

		//Initialisation de l'écran
		set_mode(320,240,16);
		set_fps(30,0);
		
		//Chargement des gfx
		fpg1 = load_fpg("gfx_memory.fpg");
		fnt1 = load_fnt("font_small_001.fnt");
		fnt2 = load_fnt("font_large_001.fnt");
                set_text_color(rgb(0,0,0));


		//Chargement du splash-screen
		put_screen(fpg1,998); 
		frame;

		patiente(100);
		fade(0,0,0,1);
		while (fading)
			frame;
		End
		//Fin du splash-screen

		//Fire up the menu
		main_menu();
      
End



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MENU - RAF
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PROCESS main_menu()


Private
id_text;
string strg_text;
i;

Begin

	clear_screen();
	let_me_alone();
	graph=994;
	x=160; y=103;
	Set_text_color(255);
	
	strg_text = "Easy";
	taille_plateau=4;

	id_text = write_string(fnt1,160,103,4,&strg_text);
	write(fnt1,160,133,4,"Credits");
	write(fnt1,160,163,4,"Exit");
	Write(fnt1, 10,215,0,"by GuZzO - 2010");

	menu_title();

		Loop
				If(key(_esc)) exit(); End; 
				timer_touch--;
				If(timer_touch<=0) timer_touch=0; End;
				//***
				If(key(_down) AND y==103 AND timer_touch==0) y=133; graph=995; timer_touch=timer_in_menu; End;
				If(key(_down) AND y==133 AND timer_touch==0) y=163; graph=995; timer_touch=timer_in_menu; End;
				If(key(_down) AND y==163 AND timer_touch==0) y=103; graph=994; timer_touch=timer_in_menu; End;
				//***
				If(key(_up) AND y==163 AND timer_touch==0) y=133; graph=995; timer_touch=timer_in_menu; End;
				If(key(_up) AND y==133 AND timer_touch==0) y=103; graph=994; timer_touch=timer_in_menu; End;
				If(key(_up) AND y==103 AND timer_touch==0) y=163; graph=995; timer_touch=timer_in_menu; End;
				//***
				If(key(_left) AND y==103 AND timer_touch==0 AND strg_text=="Easy") strg_text = "Hard"; taille_plateau=6; timer_touch=timer_in_menu; End;
				If(key(_left) AND y==103 AND timer_touch==0 AND strg_text=="Medium") strg_text = "Easy"; taille_plateau=4; timer_touch=timer_in_menu; End;
				If(key(_left) AND y==103 AND timer_touch==0 AND strg_text=="Hard") strg_text = "Medium"; taille_plateau=5; timer_touch=timer_in_menu; End;
				//***
				If(key(_right) AND y==103 AND timer_touch==0 AND strg_text=="Easy") strg_text = "Medium"; taille_plateau=5; timer_touch=timer_in_menu; End;
				If(key(_right) AND y==103 AND timer_touch==0 AND strg_text=="Medium") strg_text = "Hard"; taille_plateau=6; timer_touch=timer_in_menu; End;
				If(key(_right) AND y==103 AND timer_touch==0 AND strg_text=="Hard") strg_text = "Easy"; taille_plateau=4; timer_touch=timer_in_menu; End;
				//***
				If(y==103 AND key(__A) AND timer_touch==0) timer_touch=timer_in_menu; delete_text(0); menu_level(); Break; End;
				If(y==133 AND key(__A) AND timer_touch==0) timer_touch=timer_in_menu; delete_text(0); menu_credits(); Break; End;
				If(y==163 AND key(__A) AND timer_touch==0) exit(); End;
				//***
				If(key(_esc)) exit(); End;
				Frame;
		End;
		
End;
				

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Temporiser - RAF
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Process menu_title();

Private
   taille=80;
   cptr_incrementation=0;

Begin
		graph=996;
		x=160; y=30;
		Loop
				size=taille;
				cptr_incrementation++;      
				If(cptr_incrementation>0 AND cptr_incrementation<=10) taille=taille+2; End;
				If(cptr_incrementation>10 AND cptr_incrementation<=20) taille=taille-2; End;
				If(cptr_incrementation==20) cptr_incrementation=0; End;
				//If(start_game==1) Break; End;
				Frame;
		End;
End;


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// menu_credits - RAF
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Process menu_credits(); //Credits

Begin	
		graph=995;
		x=160; y=193;
		write(fnt1,160,110,1,"Memory 2X - GuZzO");
		write(fnt1,160,130,1,"Graphismes - GuZzO & Guyfawkes");
		write(fnt1,160,150,1,"Menu - GuZzO & Saruman");
		write(fnt1,160,193,4,"Back");
		
		Loop
				timer_touch--;
				If(timer_touch<=0) timer_touch=0; End;
				If(y==193 AND key(__A) AND timer_touch==0) timer_touch=timer_between_menu; delete_text(0); main_menu(); Break; End;
				Frame;
		End;
End;


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Menu Level - RAF
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Process menu_level(); //Select difficulty level

Begin
	graph=995;
	x=160;
	y=83;
	
	write(fnt1,160,83,4,"Tileset 1");
	write(fnt1,160,103,4,"Tileset 2");
	write(fnt1,160,123,4,"Tileset 3");
	write(fnt1,160,143,4,"Tileset 4");
	write(fnt1,160,163,4,"Tileset 5");
	write(fnt1,160,193,4,"Back");
	Display_mini_card(1);
		
   Loop
      timer_touch--;
      If(timer_touch<=0) timer_touch=0; End;
         //***
      If(key(_down) AND y==83 AND timer_touch==0) y=103; signal(type Display_mini_card, s_kill); Display_mini_card(13); timer_touch=timer_in_menu; End;
      If(key(_down) AND y==103 AND timer_touch==0) y=123; signal(type Display_mini_card, s_kill); Display_mini_card(25); timer_touch=timer_in_menu; End;
      If(key(_down) AND y==123 AND timer_touch==0) y=143; signal(type Display_mini_card, s_kill); Display_mini_card(37); timer_touch=timer_in_menu; End;
      If(key(_down) AND y==143 AND timer_touch==0) y=163; signal(type Display_mini_card, s_kill); Display_mini_card(49); timer_touch=timer_in_menu; End;
      If(key(_down) AND y==163 AND timer_touch==0) y=193; signal(type Display_mini_card, s_kill); Display_mini_card(0); timer_touch=timer_in_menu; End;
      If(key(_down) AND y==193 AND timer_touch==0) y=83; signal(type Display_mini_card, s_kill); Display_mini_card(1); timer_touch=timer_in_menu; End;
         //***
      If(key(_up) AND y==193 AND timer_touch==0) y=163; signal(type Display_mini_card, s_kill); Display_mini_card(49); timer_touch=timer_in_menu; End;
      If(key(_up) AND y==163 AND timer_touch==0) y=143; signal(type Display_mini_card, s_kill); Display_mini_card(37); timer_touch=timer_in_menu; End;
      If(key(_up) AND y==143 AND timer_touch==0) y=123; signal(type Display_mini_card, s_kill); Display_mini_card(25); timer_touch=timer_in_menu; End;
      If(key(_up) AND y==123 AND timer_touch==0) y=103; signal(type Display_mini_card, s_kill); Display_mini_card(13); timer_touch=timer_in_menu; End;
      If(key(_up) AND y==103 AND timer_touch==0) y=83; signal(type Display_mini_card, s_kill); Display_mini_card(1); timer_touch=timer_in_menu; End;
      If(key(_up) AND y==83 AND timer_touch==0) y=193; signal(type Display_mini_card, s_kill); Display_mini_card(0); timer_touch=timer_in_menu; End;

         //***
      If(y==83 AND key(__A) AND timer_touch==0)
         choix_tileset = 0;
         delete_text(0);
         game();
         Break;
      End;

      If(y==103 AND key(__A) AND timer_touch==0) 
         choix_tileset = 1;
         delete_text(0);
         game();
         Break;
      End;

      If(y==123 AND key(__A) AND timer_touch==0)
         choix_tileset = 2;
         delete_text(0);
         game();
         Break;
      End;

      If(y==143 AND key(__A) AND timer_touch==0)
         choix_tileset = 3;
         delete_text(0);
         game();
         Break;
      End;

      If(y==163 AND key(__A) AND timer_touch==0)
         choix_tileset = 4;
         delete_text(0);
         game();
         Break;
      End;

      If(y==193 AND key(__A) AND timer_touch==0)
         timer_touch=timer_between_menu;
         delete_text(0);
         main_menu();
         Break;
      End;
      Frame;
   End;
End;


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Game - RAF
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Process game();

Private
string time_in_game;
int text_id_1;
int text_id_2;
case_x;
case_Y;

Begin
		let_me_alone();
		snd1 = load_wav ("sound_01.wav");
		timer[0] = 0;

                Curseur();
                mouse.x = 0;
                mouse.y = 0;
                Create_gameboard();

		Loop

				If (key(__START))
					Pause();
					Loop	
						Frame;	
						If (key(__L) AND key(__R)) 
							Go_back_menu();	
						Elsif (key(__SELECT))
							signal(type Curseur, s_wakeup);
							signal(type Pause, s_kill);
							Frame;
							Break;
						End;
					End;		
				End;

				nb_validated_card = 0;                
				For(case_x=1;case_x<=taille_plateau;case_x++)
					For(case_y=1;case_y<=4;case_y++)
						If (memory_gameboard[case_x][case_y].card_validated == 1)
							nb_validated_card++;
						End;
					End;
				End;
				
				If (nb_validated_card == (taille_plateau * 4))
					play_wav (snd1,1);
					write(fnt2,160,120,4,"YOU WIN !!");
					Frame;
					Patiente(536);
					Go_back_menu();
					unload_wav(snd1);
				End;

				Frame;
		End;

		

End


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Plateau JEUX - RAF
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PROCESS Display_mini_card(dalle)

Begin

	x = 260;
	y = 120;
	Graph = dalle;
	
	Loop
		Frame;
	End;

End
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Plateau JEUX - RAF
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PROCESS Display_card(x,y,dalle)

Private
case_x;
case_y;
check_rotate_local;
uncheck_card = 2;
cptr_turn = 0;
i;

BEGIN

		//Chargement du sprite
		graph = dalle;
		z = -255;
		
		
		
		loop    
				If (check_collision_card != 0 AND graph == 993 AND nb_card_selected < 2)
						
						If (id == check_collision_card) 
								check_rotate_local = true; 
								check_rotate_global = true; 
						End;
						
						If (check_rotate_local == true)//Vitesse de rotation
								i+=6000;
								
								//Taille du texte qui tourne
								size_x = abs(get_distx(i,100));
								size_y = 100;
								
								//Rotation du texte
								If (get_distx(i,100)<0)
										flags = 1;
								Else
										flags = 0;
								End
								
								If (get_distx(i,100) == 100) cptr_turn++; End; //Compte le nombre de tour complet
								If (cptr_turn >= 1) 
										mouse.x = 0;
                                                                                mouse.y = 0;
                                                                                cptr_turn = 0;
										check_rotate_local = false; //Stoppe la rotation après x tours
										check_rotate_global = false; //Stoppe la rotation après x tours
										nb_card_selected++;
		
										For(case_x=1;case_x<=taille_plateau;case_x++)
											For(case_y=1;case_y<=4;case_y++)
												If (memory_gameboard[case_x][case_y].card_id == id)
													If (memory_gameboard[case_x][case_y].card_selected == 0 )
				                                        graph = memory_gameboard[case_x][case_y].card_gfx;
				                                        memory_gameboard[case_x][case_y].card_selected = 1;
				                                        If (nb_card_selected == 1)
				                                        		check_first_card = memory_gameboard[case_x][case_y].card_gfx;
				                                        		check_first_card_id = id;
				                                        		check_first_card_x = case_x;
				                                        		check_first_card_y = case_y;
				                                        Elsif (nb_card_selected == 2)
				                                        		check_second_card = memory_gameboard[case_x][case_y].card_gfx;
				                                        		check_second_card_id = id;
				                                        		check_second_card_x = case_x;
				                                        		check_second_card_y	= case_y;
				                                        End;
				                                    End;
												End;
											End;
										End;
								End; 
						End;
				End;

				If (nb_card_selected == 2)
				//delete_text(debug_txt);
				//debug_txt = write(0,300,225,0,nb_card_selected);

						If (check_first_card == check_second_card)
								memory_gameboard[check_first_card_x][check_first_card_y].card_validated = 1;
								memory_gameboard[check_second_card_x][check_second_card_y].card_validated = 1;
								nb_card_selected = 0;
								uncheck_card = 2;
						Else								
								patiente (100 * uncheck_card);//Temps d'affichage des cartes avant de les recacher
								If ((id == check_first_card_id OR id == check_second_card_id) AND uncheck_card > 0)
										graph = 993;
										memory_gameboard[check_first_card_x][check_first_card_y].card_selected = 0;
										memory_gameboard[check_second_card_x][check_second_card_y].card_selected = 0;
										uncheck_card--;
										If (uncheck_card == 0)
												uncheck_card = 2;
												nb_card_selected = 0;
										End;
								End;
						End;
				End;
		
		Frame;
		End
		

End


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Plateau JEUX - RAF
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PROCESS Create_gameboard()

Private
case_x;
case_y;
tmp_x;
tmp_y;
rand_x;
rand_y;
rand_pics;
int used_pics[12];


BEGIN

		For(case_x=1;case_x<=taille_plateau;case_x++)
				For(case_y=1;case_y<=4;case_y++)
						tmp_x = ((case_x-1)*50)+((case_x)*3)+25;
						tmp_y = ((case_y-1)*50)+((case_y)*3)+25;
						memory_gameboard[case_x][case_y].card_id = Display_card(tmp_x,tmp_y,993);
                        memory_gameboard[case_x][case_y].card_gfx = 0;
                        memory_gameboard[case_x][case_y].card_selected = 0;
                        memory_gameboard[case_x][case_y].card_validated = 0;
				End;
		End;


		For(case_x=1;case_x<=taille_plateau;case_x++)
				For(case_y=1;case_y<=4;case_y++)
						loop
								If (memory_gameboard[case_x][case_y].card_gfx != 0)	break;	End;

								rand_x = rand2(1,taille_plateau);
								rand_y = rand2(1,4);
								rand_pics = rand2(1,12); 
								
								while ((rand_x == case_x) AND (rand_y == case_y)) 
										rand_x = rand2(1,taille_plateau);
										rand_y = rand2(1,4);
								End; 
								
								while (used_pics[rand_pics] == 1)
										rand_pics = rand2(1,12);
								End; 
								
		                        If ((memory_gameboard[case_x][case_y].card_gfx == 0) AND(memory_gameboard[rand_x][rand_y].card_gfx == 0) AND (used_pics[rand_pics] == 0))
				                        memory_gameboard[rand_x][rand_y].card_gfx = (choix_tileset * 12) + rand_pics;
				                        memory_gameboard[case_x][case_y].card_gfx = (choix_tileset * 12) + rand_pics;
				                        used_pics[rand_pics] = 1;
				                        break; 
		                        End;
                        End;
                        
                        frame;
                        
				End;
		End;
		

End


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Temporiser - RAF
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FUNCTION patiente(int tps)

BEGIN

		timer[0]=0;
		while (timer[0] <= tps ) 	frame;	End

End


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Function Random_modified - RAF
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define RAND_MAX 32767
#define DRAND_RANGE (1.0/((RAND_MAX + 1)*(RAND_MAX + 1)))
#define irand(x) ((unsigned int) ((x) * drand ()))
Function float drand ()
Private
    float f;
Begin
    Repeat
       f = (rand (0,RAND_MAX) * (RAND_MAX + 1.0)
          + rand (0,RAND_MAX)) * DRAND_RANGE;
    Until (f < 1); /* Round off */
    return f;
End

Function rand2(int lowerlimit, int upperlimit)
Begin
    return (lowerlimit+irand(upperlimit-lowerlimit+1));
End


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Pointeur - OK
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PROCESS Curseur();

BEGIN

		graph = 999;
		
		Loop
			x = mouse.x;
			y = mouse.y;
			z = -256;

            if (check_rotate_global == false)
            	check_collision_card = COLLISION(type Display_card);
            End;

			Frame;
		End;
      
End


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Go_back_menu - OK
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PROCESS Go_back_menu();

BEGIN

		//RESTART GAME
		//============
		Fade_off();
		delete_text(all_text);
		main_menu ();
		signal(type Game ,s_kill);
      
END


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//PAUSE - OK
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
PROCESS Pause();


Begin

	graph = 997;
	x = 160;
	y = 120;
	z = -256;
	
	Loop
		signal( Type Curseur , s_freeze );
		Frame;
	End;
	
End


