//D:BattleField1944   :^XԲ
//Ƽ:Achung Panzer!!
//aϥTileK,tile size=20X20 pixel
//eѪR:320X240
//Map size=320X200pixel , W15PixelTC,U25PixelTC
//Cu԰Ͳ, ƶqMOƥ]wn. 


#define DELAY_TIME 10  //Dealyɶ: PCW]100. 


char map1_index[16][10]=0;  //aindex,0~15ӧP_ne@i.
                            //0~4a 5~6sa 7~8 9 10@ū 11Gū 
                            //12@ž 13Gž 14@Ŵf 15GŴf
char map1_OnOff[16][10]=0;  //P_aϦmO_; Ʀr0=S 
                            //1~10:ڤBL  101~110:ĤBL
char AirMap1_OnOff[8][5]=0; //Ťa,P_mO_; Ʀr0=S 
                            //1~3:ڤԾ   10~13'Ծ
                            
char map1_DFplus[16][10]=0; //aΨm[;
char map1_Fog[16][10]=0;  //P_o϶O_i.


struct TROOP            //aWMWƵc
{
    char ID;            //0~9  
    char Life;
    char AT;
    char AT_Range;
    char DF;
    char FPW;           //`X귽 = U+h
    char FPWcost;       //C^ʮɮӪI
    char State;         //A : 0=ʧ , 1=|
    char Sight_Range;   //dd.    //pFL+3
    char GroundMove;    //WʤO.
    char SeaMove;       //WʤO.
    char GroundTake;    //IO.
    char Frame;         //ʵei
    int  Xpos,Ypos;     //XYyX=0~320 Y=0~200
    char Cost;          //ͲO=}
    char Night;         //]ԧL   
    //char MaxTroop={10;}     //̤jLO;  
    //char EXP;         //g=԰1X+2X} , 255ɵ+1,gk0   
    //char Level;        // 0=w] 1=LOMax+1 2=LOMax+1 ̦h5
                        //4iHiLɯ,ɯųyun1b,k0g+128
};

struct AIR              //Ƶc
{
    char ID;            //0~4  
    char Life;
    char ATair;
    char ATland;        //a
    char DFair;         //
    char DFland;        //am
    char FPW;           //`X귽 = U+h+u, C25ɥu^.C0ɳ
    char FPWcost;       //C^ʮɮӪI
    char State;         //A : 0=ʧ , 1=|
    char Sight_Range;   //dd.(Ť1=a4
    char AirMove;       //
    char frame;         //ʵei
    int  Xpos,Ypos;     //XYyX=0~320 Y=0~200
    char Cost;          //ͲO=}
    char Night;         //]ԧL, 0=鶡L, 1=]jƧLAT+50%, 2=]M~LAT+100%  
    //char MaxTroop={10;}     //̤jLO;    
    //char EXP;         //g=԰1X++2X} 255ɵ+1gk0   
    //char Level        // 0=w] 1=LOMax+1 2=LOMax+1 ̦h5
};

struct BUILDING         //ؿv 0=(@[15) 1=1ū(@[10) 2=2ū(@[5)
{                       //3=1ž(@[10,4)  4=2ž(@[5,2) 5=3ž(@[2,1)
                        //6=1Ŵf(@[10)  7=2Ŵf(@[5)
    char ID;            //0~9  
    char Life;          //
    char State;         //A : 0=LH , 1=1P, 2=2P.
    char Sight_Range;   //dd.    //pFL+3
    char frame;         //ʵei
    int  Xpos,Ypos;     //XYyX=0~320 Y=0~200      
};

struct STAGEDATA
{
    char ID;            //0~255  
    int  TopScore;       //
    char MaxTurn;       //̤j^X,W255
    char MaxLand;       //̤jW,W255
    char MaxSea;        //̤jW,W255
    char MaxAir;        //̤jŤ,W255
};

int i,j;                //j
int select_posX=0;      //1PܮتXy
int select_posY=0;      //1PܮتYy
int select2P_posX=15;   //2PܮتXy
int select2P_posY=0;   //2PܮتYy
char TroopID1P=0;       //1PQ쪺ID (0~9)w]0
char TroopID2P=0;       //2PQ쪺ID (0~9)w]0
char Turn1P2P=1;        //ثeO1P2P
int Turn=1;             //ĴX^X
char GroundAir=0;        //ܦaW0ΪŤ1 
char DayNight;         //]   0= am06:00  1=Uam12:00 2=ıpm06:00 3= pm12:00(]Lا+50%~100%)
int Score1P=0;         //1P,ĭxl=ۭx
int Score2P=0;         //2P
int high=0;            //MainMenu,w]0=sC,1=Ūs.....
int MenuFlag = 1;      //MenujX,}lJ
int MenuSelect = 0;    //ԳMenu,w]0=,1=ݩR.....
int AirMapCoord=0;
char key;              //LJ
int keyin;             //LJ
char gameover = 1;     //O_}MainMenu(),iJExitGame()j
int move;
int Fight=0;           //O_԰
int TurnEnd=1;         //1=^Xi椤 0=^X;
char StageNum=1;       //Хܥثed 0=ut 1Z 2֧]J 3֧]aQ
                       //4i 5بF 6Ca 7ըu 8JJ 9.kn
                       //10CAž 11縥 12.p1;@õn 13.p2;n@
                       //15.n 16.ڤù 17.di 18.[ 19.vFL 20. 
                       //21.BQ 22._D 23.㺸Ա 24.}ù 25.qn1943
                       //26.jv1944K 27.ŤF1944L 27.n1944 28.𲤾1945K
                       //29.ï] 30.ĬhBe 31.泮 (@ð򺸴䪺BV222_)
                       //32.հҦan1944
//========y{functionC=========//
void ShowTeamLOGO();
void ShowStory(); 
void MainMenu();
void NewGame();
void LoadGame();
void How2Play();
void ExitGame();
void DrawMap();          //øs
void DrawTroops();       //øs
void DrawSelect();       //e
void GameSub();
void MoveSelect(int keyin);
void MoveTroops(int keyin);
void Battle();
void DrawInfo();
void DrawBattleMenu();
void DrawTroopMenu();
char AttackResult(char AT, char Life1,char DF, char Life2, char map_DF);
void TurnAdd();          //^Xƥ[1
void DrawRange();        //øsʽd,Md
void CheckTurnEnd();     //ˬdҦʤO,^XO_
void FPWcost();          //FPW Op
void ShowData();         //ܪZ
void DrawAirMap();
//===========MainD{============//
void main()
{
    SetLCDMode(0);    //bwİø(1Oø)
    SetBkColor(15);    
    ClearScreen(); 
       
    Refresh();     
    ShowTeamLOGO();   //TeamLogoMOP

    Delay(DELAY_TIME*5);
    ClearScreen();
    
    MainMenu();       //D 
    ExitGame();
}





//=========function{X==========//
void ShowTeamLOGO()
{
    SetFontSize(12);  //]wrjp12
    SetFgColor(0);
    SetBkColor(15);    
    ClearScreen();    
    TextOut(90,100,"Present By");
    TextOut(100,120,"Team:One Bear A Army");
    TextOut(280,228,"v0.1a"); 
    Refresh();
}


void MainMenu()
{
	//PlaySndID(5);  
	Delay(DELAY_TIME);
		
    while(gameover)    
    {
        MenuFlag=1;
        while(MenuFlag)  
        {   
            //eϩMGameLOGO
            ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
	        ShowImage(IDL0,33,33,256,64,0,0,1,15);        //LOGO:¦⬰z
	        ShowImage(IDL0,80,228,142,14,0,178,1,15);     //U:¦⬰z
	        ShowImage(IDL0,221,122,109,118,147,138,1,15); //kxx:¦⬰z
	        DigitOut(1,0,Getms()); 
	          	    
	        //e
	        SetFgColor(0);
	        Rectangle(20,138,130,220,1);  //զ
     	    SetFgColor(15);
	        Rectangle(22,140,128,218,0);  //¦u
	        //er թ¦r
	        SetFgColor(15);
	        SetBkColor(0);
	        TextOut(51,149,"s C ");      //high=0
	        TextOut(51,161,"Ūs");      //high=1
	        TextOut(51,173,"L");      //high=2
	        TextOut(51,185,"C");      //high=3
	        TextOut(51,197,"}C");      //high=4
	        RectangleNot(50,149+high*12,101,161+high*12,2);  //e

	        Refresh();
	        
	        if(Inkey())
	        {MenuFlag=0;}
	        ReleaseKey (128);
        }

        MenuFlag=1;
        key=getchar();
    	if(key==KEY_UP && high==0)      //W
    	{
    	    high=4;
    	    Beep ();
    	}
    	else if(key==KEY_UP && high!=0)
    	{
    	    high=high-1;
    	    Beep ();
    	}
	
    	if(key==KEY_DOWN && high!=4)    //U
    	{
    	    high=high+1;
    	    Beep ();
    	}
    	else if(key==KEY_DOWN && high==4)
    	{
    	    high=0;
    	    Beep ();
    	}    	
	
    	if(key==KEY_ENTER && high==0)  //s C 
    	{
    	    Beep ();	
    	    NewGame();
        }
    
    	if(key==KEY_ENTER && high==1)  //Ūs
    	{
            Beep ();    	  
            LoadGame();
    	}
	
    	if(key==KEY_ENTER && high==2)  //L
    	{
    	    Beep ();
    	    ShowData();
    	}

    	if(key==KEY_ENTER && high==3)  //C
    	{
    	    Beep ();
    	    How2Play();
    	}    	
	
    	if(key==KEY_ENTER && high==4)  //}C
    	{
            Beep ();    	
    	    ExitGame();
            gameover=0;
    	}
    }
}


void NewGame()
{  
    ShowStory();   //d}ler 

    //==========l==========//
    struct TROOP MyTank[10];
    for (i=0;i<10;i++)           //10ӾԨ
    {
        MyTank[i].ID=i;          //s0~9
        MyTank[i].Life=10;       //lͩR
        MyTank[i].AT=5;          //lO
        MyTank[i].AT_Range=2;    //lZ
        MyTank[i].DF=2;          //lmO
        MyTank[i].FPW=100;       //lFPW
        MyTank[i].FPWcost=1;    //C^ʮɮӪI
        MyTank[i].State=1;       //A   ,1=iHʤ@
        MyTank[i].Sight_Range=2; //dd.
        MyTank[i].GroundMove=4;  //WʤO.
        MyTank[i].SeaMove=1;     //WʤO.
        MyTank[i].GroundTake=2;  //IO.
        MyTank[i].Frame=0;       //ʵei
        MyTank[i].Xpos=0;        //ùy
        MyTank[i].Ypos=i*20;  
        MyTank[i].Cost=100; 
        MyTank[i].Night=0;  
              
        map1_OnOff[0][i]=i+1;    //1~10
    }

    struct TROOP EnemyTank[10];
    for (i=0;i<10;i++)
    {
        EnemyTank[i].ID=i;       //s0~9
        EnemyTank[i].Life=10;    //lͩR
        EnemyTank[i].AT=5;
        EnemyTank[i].AT_Range=2;
        EnemyTank[i].DF=2;
        EnemyTank[i].FPW=100; 
        EnemyTank[i].FPWcost=1;    //C^ʮɮӪI
        EnemyTank[i].State=1;       //A : 
        EnemyTank[i].Sight_Range=2; //dd.
        EnemyTank[i].GroundMove=4;  //WʤO.
        EnemyTank[i].SeaMove=1;     //WʤO.
        EnemyTank[i].GroundTake=2;  //IO.
        EnemyTank[i].Frame=0;       //ʵei
        EnemyTank[i].Xpos=15*20;    //ùy
        EnemyTank[i].Ypos=i*20; 
        EnemyTank[i].Cost=100; 
        EnemyTank[i].Night=0; 
                
        map1_OnOff[15][9-i]=i+101; //101~110
    }    
    //============lƦa============//
    //====Ū@i16X104bitǶBMP====//
    //====Ωt(0-15ƭ)wTile====//
    for (i=0;i<16;i++)        //0~15
    {
        for (j=0;j<10;j++)    //0~9
        {
            map1_index[i][j]=rand()%4;  //ζüƼ0~3
            
            map1_DFplus[i][j]=rand()%5-2; //aΨ
            if (map1_DFplus[i][j]<0)
            {map1_DFplus[i][j]=0;}
        }
    }
    
    //============lƪŤIndexMap============//    
    for (i=0;i<8;i++)
    {
        for (j=0;j<5;j++)
        {
            AirMap1_OnOff[i][j]=0; //Ťa,P_mO_; Ʀr0=S        
        }
    }                  
                              
    GameSub();             //iJC
}

void LoadGame()
{
     //Ū
    TextOut(24,122," e L   ");
    Refresh();
    Delay(DELAY_TIME*5);
}

void How2Play()
{
    SetFontSize(16);  //]wrjp12or16
    SetFgColor(0);
    SetBkColor(15); 
          
    ClearScreen();
    ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
    SetFontSize(16);            
    TextOut(66,10,"H O W    T O    P L A Y");
    SetFontSize(12);            
    TextOut(0,210,"Ob's sturmt oder schneit, Ob die Sonne uns lacht,"); 
    TextOut(20,226,"L׬OgΤjA٬OӶڭ̷LA");        
    Delay(DELAY_TIME*20);
    Refresh();  

    ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
    SetFontSize(16);            
    TextOut(66,10,"H O W    T O    P L A Y");
    SetFontSize(12);        
    TextOut(0,210,"Der Tag gluhend heis, Oder eiskalt die Nacht."); 
    TextOut(20,226,"L׬OձުB٬O]ߴHNpBC");        
    Delay(DELAY_TIME*20);
    Refresh(); 
                
    ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
    SetFontSize(16);            
    TextOut(66,10,"H O W    T O    P L A Y");
    SetFontSize(12);  
    TextOut(0,210,"Ob's sturmt oder schneit, Ob die Sonne uns lacht,"); 
    TextOut(20,226,"L׬OgΤjA٬OӶڭ̷LA");        
    Delay(DELAY_TIME*20);
    Refresh(); 
                
    ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
    SetFontSize(16);            
    TextOut(66,10,"H O W    T O    P L A Y");
    SetFontSize(12);        
    TextOut(0,210,"Bestaubt sind die Gesichter, Doch froh ist unser Sinn,"); 
    TextOut(20,226,"M۴HyШFAOڭ̪߱Or֪A");        
    Delay(DELAY_TIME*20);
    Refresh(); 
                
    ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
    SetFontSize(16);            
    TextOut(66,10,"H O W    T O    P L A Y");
    SetFontSize(12);        
    TextOut(0,210,"Ist unser Sinn; Es braust unser Panzer, Im Sturmwind dahin. "); 
    TextOut(20,226,"Lڭ̪ԨbeVeiI");        
    Delay(DELAY_TIME*20);
    Refresh(); 
                
    ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
    SetFontSize(16);            
    TextOut(66,10,"H O W    T O    P L A Y");
    SetFontSize(12);        
    TextOut(0,210,"Mit donnernden Motoren, Geschwind wie der Blitz,"); 
    TextOut(20,226,"qnpAp{qA");        
    Delay(DELAY_TIME*20);
    Refresh(); 
                
    ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
    SetFontSize(16);            
    TextOut(66,10,"H O W    T O    P L A Y");
    SetFontSize(12);        
    TextOut(0,210,"Dem Feinde entgegen, Im Panzer geschutzt."); 
    TextOut(20,226,"bԨO@UĤHڭ̵Li`C");        
    Delay(DELAY_TIME*20);
    Refresh(); 
                
    ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
    SetFontSize(16);            
    TextOut(66,10,"H O W    T O    P L A Y");
    SetFontSize(12);        
    TextOut(0,210,"Voraus den Kameraden, Im Kampf steh'n wir allein, "); 
    TextOut(20,226,"eiI̥S̡Ib԰ڭ`OY}A");        
    Delay(DELAY_TIME*20);
    Refresh(); 
                
    ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
    SetFontSize(16);            
    TextOut(66,10,"H O W    T O    P L A Y");
    SetFontSize(12);        
    TextOut(0,210,"Steh'n wir allein, So stosen wir tief, In die feindlichen Reihn."); 
    TextOut(20,226,"Oڭ̽ĶiĤHԽuC");        
    Delay(DELAY_TIME*20);
    Refresh(); 
                
    ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
    SetFontSize(16);            
    TextOut(66,10,"H O W    T O    P L A Y");
    SetFontSize(12);        
    TextOut(0,210,"Und last uns im Stich, Einst das treulose Gluck, "); 
    TextOut(20,226,"pGnBwɡA");        
    Delay(DELAY_TIME*20);
    Refresh(); 
                
    ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
    SetFontSize(16);            
    TextOut(66,10,"H O W    T O    P L A Y");
    SetFontSize(12);        
    TextOut(0,210,"Und kehren wir nicht mehr, Zur Heimat zuruck, "); 
    TextOut(20,226,"ڭ̱N|A^amA");        
    Delay(DELAY_TIME*20);
    Refresh(); 
                
    ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
    SetFontSize(16);            
    TextOut(66,10,"H O W    T O    P L A Y");
    SetFontSize(12);        
    TextOut(0,210,"Trifft uns die Todeskugel, Ruft uns das Schicksal ab, "); 
    TextOut(20,226,"PRuڭ̡A");        
    Delay(DELAY_TIME*20);
    Refresh(); 
                
    ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
    SetFontSize(16);            
    TextOut(66,10,"H O W    T O    P L A Y");
    SetFontSize(12);        
    TextOut(0,210,"Ja Schicksal ab, Dann wird uns der Panzer "); 
    TextOut(20,226,"RBbVڭ̥lA");        
    Delay(DELAY_TIME*20);
    Refresh(); 
                
    ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
    SetFontSize(16);            
    TextOut(66,10,"H O W    T O    P L A Y");
    SetFontSize(12);        
    TextOut(20,210,"Ein ehernes Grab."); 
    TextOut(20,226,"OԨNڭ̺aģäC");        
    Delay(DELAY_TIME*20);
    Refresh(); 
                   
    DigitOut(1,0,Getms()); 
}

void ExitGame()
{ 
 
   int a,b,c,d,e,f;
   int a1,b1,c1,d1,e1;  
   int cola,colb,colc,cold,cole,colf,cola1,colb1,colc1,cold1,cole1;     
   int temp;
  


   ClearScreen();
   SetFgColor (0);
   SetBkColor (15);
   SetBkMode(0);


   i=0;
   cola=1; colb=0; colc=0; cold=0; cole=0;  colf=0;
   cola1=1; colb1=0; colc1=0; cold1=0; cole1=0;
   temp=1;
   while(temp)
   {    
       ShowImage(IDL1,0,0,320,240,0,0,0,0);          //
       SetFontSize(16);  //]wrjp12or16

       a=105-i;  //abcde...Nr, a>0,r٦be~øs.
       if (a>0) //raW,BrC⤣O 
       {        //rCCܶ
           if (a<16 && cola<14) 
           {
               cola=cola+1;  
           } 
           SetFgColor(cola);
           TextOut( 85, a,"   S  T  A  F  F   ");
       }
       
       SetFontSize(12);  
       b=155-i;
       if (b>0)
       {
           if (b<=16 && colb<14)
           {
               colb=colb+1;
           }     
           SetFgColor(colb); 
           TextOut(105, b,"Programmer         ");
       }  
       
       c=170-i;
       if (c>0) 
       {
           if (c<=16 && colc<14)
           {
               colc=colc+1;
           }          
           SetFgColor(colc);
           TextOut(105, c,"           WaffenSS");
       }
       
       d=200-i;
       if (d>0)
       { 
           if (d<=16 && cold<14)
           {
               cold=cold+1;
           }    
           SetFgColor(cold);
           TextOut(105, d,"Lead Artist        "); 
       }
       
       e=215-i;
       if (e>0)  
       {
           if (e<=16 && cole<14)
           {
               cole=cole+1;
           }    
           SetFgColor(cole);       
           TextOut(105, e,"           WaffenSS");
       }
       
       a1=245-i;
       if (a1>0)
       {  
           if (a1<=16 && cola1<14)
           {
               cola1=cola1+1;
           }    
           SetFgColor(cola1);       
           TextOut(105,a1,"GameDesigner       "); 
       }
       
       b1=260-i;
       if (b1>0)         
       {
           if (b1<=16 && colb1<14)
           {
               colb1=colb1+1;
           }    
           SetFgColor(colb1);       
           TextOut(105,b1,"           WaffenSS");
       }
       
       c1=290-i;
       if (c1>0)
       {
           if (c1<=16 && colc1<14)
           {
               colc1=colc1+1;
           }    
           SetFgColor(colc1);       
           TextOut(105,c1,"MusicComposer      ");  
       }
       
       d1=305-i;
       if (d1>0)
       {   
           if (d1<=16 && cold1<14)
           {
               cold1=cold1+1;
           }    
           SetFgColor(cold1);            
           TextOut(105,d1,"           WaffenSS");
       }
       
       e1=335-i;
       if (e1>0)   
       {
           if (e1<=16 && cole1<14)
           {
               cole1=cole1+1;
           }    
           SetFgColor(cole1);                     
           TextOut(105,e1,"Game Tester        ");  
       }

       f=350-i;
       if (f>0)    
       {
           if (f<=16 && colf<14)
           {
               colf=colf+1;
           }    
           SetFgColor(colf);           
           TextOut(105,f,"           WaffenSS");
       }


       i=i+1;
       Refresh();      
       Delay(DELAY_TIME);
       
       if (Inkey())
       {
         temp=0;
       }

   }
   
   ShowImage(IDL1,0,0,320,240,0,0,0,0);          //   
   SetFontSize(16);  //]wrjp12or16
   TextOut(88,105,"  T H E    E N D  "); 
   SetFontSize(12);  //]wrjp12or16   
   TextOut(108,125," See you next time");        
   TextOut(188,226,"Press Any Key To Exit");
   Refresh();
    
   getchar();
   ClearScreen();
   Refresh();   
   exit(0);
}

void DrawMap()            //øs
{
   int temp;

   //jMmap1_indexP_aϬOέ@Tile
   for (i=0; i<16; i++)
   {
       for  (j=0; j<10; j++)
       {	    
           temp = map1_index[i][j];    //0~3
           
           if (temp==0)
               ShowImage(TileMap,0+20*i,15+20*j,20,20,0,0,0,0);   //TileGrass1 
           else if(temp==1)
               ShowImage(TileMap,0+20*i,15+20*j,20,20,20,0,0,0);   //TileGrass2  
           else if(temp==2)         
               ShowImage(TileMap,0+20*i,15+20*j,20,20,0,20,0,0);   //TileGrass3 
           else      
               ShowImage(TileMap,0+20*i,15+20*j,20,20,20,20,0,0);  //TileGrass4      
       }  
   }
}
          
void DrawTroops()         //øs
{  
   int temp;

   for (i=0; i<16; i++)
   {
       for  (j=0; j<10; j++)
       {   //jMmap1_OnOffP_aϦmO_
           temp = map1_OnOff[i][j];

           //===================e1P===================//
           if (temp>0 && temp<=100) //pTileWw1P
           {                
               SetFgColor(15);              //]weCw]¦
               SetBkColor(8);               //]wICw]զ           
               SetBkMode(1);                //rIz 
               
               if (MyTank[temp-1].State>=1) //pTileW1P٦ʤO
               {
                   SetFgColor(2);           //ܫeC⬰¦ 
               }
               ShowImage(IDL0,0+20*i,15+20*j,20,20,105,64,1,15);   //eXTank1Pϧ                
               DigitOut(7+20*i,19+20*j,MyTank[temp-1].ID);         //eXos
               
               //===============Ϭۿ1P==============//                           
               if (Turn1P2P==1 && (TroopID1P==(temp-1)) )   // p1PʥBTileW=1P
               {
                   RectangleNot (0+20*i,15+20*j,20+20*i,35+20*j,1);                                                              
               }               
           }
                      
           //===================e2P===================//                      
           if (temp>100)  //pTileWw2P
           {
               SetFgColor(15);              //]weCw]¦
               SetBkColor(8);               //]wICw]զ           
               SetBkMode(1);                //rIz 
               
               if (EnemyTank[temp-1].State>=1)
               {
                   SetFgColor(2);          //ܫeC⬰¦ 
               }               
               ShowImage(IDL0,0+20*i,15+20*j,20,20,125,64,1,15);   //eXTank2Pϧ
               DigitOut(7+20*i,19+20*j,EnemyTank[temp-101].ID);    //eXos
              
               //===============Ϭۿ2P==============//            
               if (Turn1P2P==2 && (TroopID2P==(temp-101)))  // p2PʥBTileW=2P
               {         
                   RectangleNot (0+20*i,15+20*j,20+20*i,35+20*j,1);  
               }               
           }    
        }     
    }
}       

void DrawSelect()         //e
{
   if(Turn1P2P==1)
   {
       SetFgColor(0);     //]weC⬰զ
       //e, Yn[15,]select_posYOTiley.+15~Oey.
       Rectangle(  select_posX,   select_posY+15, select_posX+20, select_posY+35,0);
       Rectangle(2+select_posX, 2+select_posY+15, select_posX+18, select_posY+33,0);
   }
   
   if(Turn1P2P==2)
   {
       SetFgColor(0);     //]weC⬰զ
       //e, Yn[15,]select2P_posYOTiley.+15~Oey.
       Rectangle(  select2P_posX,   select2P_posY+15, select2P_posX+20, select2P_posY+35,0);
       Rectangle(2+select2P_posX, 2+select2P_posY+15, select2P_posX+18, select2P_posY+33,0);
   }   
}       

void GameSub()            //iJC
{
    int temp;
        temp=0;
        move=1;
        GroundAir=0;
    while(gameover)
    {
        while(TurnEnd)
        {
            while(move)   //ʿ
            {
                ClearScreen();
                ReleaseKey (128);           

                if (GroundAir==1)
                {
                    DrawAirMap();
                    //DrawAirTroops();   //øs
                    //DrawAirSelect();   //e
                    //DrawAirRange();    //øsʽdMd
	                //DrawAirInfo();     //øsT
	                TextOut(50,121," LŤ,SPACEťaja ");
		            DrawInfo();     //øsT                
	                keyin=Inkey();
	                if (keyin==KEY_SPACE )
	                {    GroundAir=0;    };	
	                
	                Delay(DELAY_TIME*2);  
                } 
                
                else if(GroundAir==0)
                {
                    DrawMap();      //øs
                    DrawTroops();   //øs
                    DrawSelect();   //e
                    DrawRange();    //øsʽdMd
	                DrawInfo();     //øsT
	            
	                TextOut(80,21," MyTank[TroopID1P].Xpos ");	            
                    DigitOut(251,21,MyTank[TroopID1P].Xpos) ;
	                TextOut(80,38," MyTank[TroopID1P].Ypos ");
                    DigitOut(251,38,MyTank[TroopID1P].Ypos) ; 
	                TextOut(80,51," EnemyTank[TroopID2P].Xpos ");	            
                    DigitOut(251,51,EnemyTank[TroopID2P].Xpos) ;
	                TextOut(80,66," EnemyTank[TroopID2P].Ypos ");
                    DigitOut(251,66,EnemyTank[TroopID2P].Ypos) ; 
                
	                TextOut(111,79," select_posX/20 ");                
                    DigitOut(251,79,select_posX/20); 
	                TextOut(111,92," select_posY/20 ");                 
                    DigitOut(251,92,select_posY/20); 
	                TextOut(25,104," map1_OnOff[select_posX/20][select_posY/20] ");                                   
                    DigitOut(291,104,map1_OnOff[select_posX/20][select_posY/20]);
                
	                TextOut(80,213,"SPACEťŤa");               
                	        
                    keyin=Inkey(); 
                    MoveSelect(keyin);  //ʿ
                                        //Enter move=0|Xwhile(move)j   

	                if (keyin==KEY_SPACE )
	                {    GroundAir=1;    };	
                }                                                                                        
                Refresh();
                //MoveTroops(keyin);  
            }

            MoveTroops(keyin);      //ʳ. move=1
            move=1;
            FPWcost();              //FPWp            
            TurnEnd=1;
        }
        
        if (Fight)                  //po;԰
        {   
          // Battle();
        }

        TurnAdd();//^X       
        CheckTurnEnd(); 
        
	    Refresh();
	    Delay( DELAY_TIME*2);        
   
    }
}       

void MoveSelect(int keyin)
{
    int temp;

    if (Turn1P2P==1)
    {    
        if (keyin==KEY_UP && select_posY>15)
        {   
            select_posY = select_posY - 20;           
        }

        if (keyin==KEY_LEFT && select_posX>=20)
        {   
            select_posX = select_posX - 20;  
            
	    SetFgColor(1);            
        DigitOut(151,131,TroopID1P) ;
        //DigitOut(151,111,select_posX) ; 
        Refresh();             
                     
        }

        if (keyin==KEY_DOWN && select_posY<=175)
        {   
            select_posY = select_posY + 20;           
        }

        if (keyin==KEY_RIGHT && select_posX<=280)
        {   
            select_posX = select_posX + 20;           
        }

        if (keyin==KEY_ENTER)
        {              
            move=0;           
        }
    
        if (keyin==KEY_ESC)
        {
            DrawTroopMenu();
        }
    }
    else if (Turn1P2P==2)
    {    
        if (keyin==KEY_UP && select2P_posY>=15)
        {   
            select2P_posY = select2P_posY - 20;           
        }

        if (keyin==KEY_LEFT && select2P_posX>=20)
        {   
            select2P_posX = select2P_posX - 20;           
        }        

        if (keyin==KEY_DOWN && select2P_posY<=175)
        {   
            select2P_posY = select2P_posY + 20;           
        }

        if (keyin==KEY_RIGHT && select2P_posX<=280)
        {   
            select2P_posX = select2P_posX + 20;           
        }

        if (keyin==KEY_ENTER)
        {     
            move=0;
        }
    
        if (keyin==KEY_ESC)
        {
            DrawTroopMenu();
        }
	                
	    if (keyin==KEY_SPACE )
	    {    GroundAir=0;    };	          
    }    
}


void MoveTroops(int keyin)
{   
    int temp;   //ȦsCЦmO_P_ƭ
    int s,t;
    int AllState;
        
    //jMmap1_OnOffP_nʪTileO_
    temp = map1_OnOff[select_posX/20][select_posY/20];  
    
    //1Pʪ    
    //pYTileWS, 1P,B1PԨ|ʤO               
    if (temp==0 && Turn1P2P==1 && MyTank[TroopID1P].State>0)
    {                                                      
        s= abs(MyTank[TroopID1P].Xpos - select_posX); //1PԨتXbZ
        t= abs(MyTank[TroopID1P].Ypos - select_posY); //1PԨتYbZ  
  
        //P_nʪTileO_bʪZ.
        if ( (s+t) <= (MyTank[TroopID1P].GroundMove * 20) )   
        {
            //쥻map1_OnOffאּ0, ܳF.
            map1_OnOff[MyTank[TroopID1P].Xpos/20][MyTank[TroopID1P].Ypos/20]=0;
           
            //ثe1P쪺yЧ令ثeSelectm
            MyTank[TroopID1P].Xpos = select_posX;
            MyTank[TroopID1P].Ypos = select_posY; 

            //ثeSelectmID]ثe1P쪺
            map1_OnOff[select_posX/20][(select_posY)/20]=TroopID1P+1;
            //ʤO1
            MyTank[TroopID1P].State=MyTank[TroopID1P].State-1;
        }               
    }

    //2Pʪ      
    //pYTileWS, 2P,B2PԨ|ʤO               
    if (temp==0 && Turn1P2P==2 && EnemyTank[TroopID2P].State>0)
    {                                                      
        s= abs(EnemyTank[TroopID2P].Xpos - select2P_posX); //2PԨتXbZ
        t= abs(EnemyTank[TroopID2P].Ypos - select2P_posY); //2PԨتYbZ
  
        //P_nʪTileO_bʪZ.
        if ( (s+t) <= (EnemyTank[TroopID2P].GroundMove * 20) )   
        {
            //쥻map1_OnOffאּ0, ܳF.
            map1_OnOff[EnemyTank[TroopID2P].Xpos/20][EnemyTank[TroopID2P].Ypos/20]=0;
           
            //ثe2P쪺yЧ令ثeSelectm
            EnemyTank[TroopID2P].Xpos = select2P_posX;
            EnemyTank[TroopID2P].Ypos = select2P_posY; 

            //ثeSelectmID]ثe1P쪺
            map1_OnOff[select2P_posX/20][(select2P_posY)/20]=TroopID2P+1;
            //ʤO1
            EnemyTank[TroopID2P].State=EnemyTank[TroopID2P].State-1;
        }               
    }
    

    
    //ӦaIwg
    if(Turn1P2P==1 && temp>=1 && temp<=100)
    {   //1PʥBʦmw1P
        TroopID1P=temp-1;   //令ؿ쪺     
    }

    if(Turn1P2P==1 && temp>=101 && temp<=200)
    {   //1PʥBʦmw2P
        DrawBattleMenu();  //""""         
    }

    if(Turn1P2P==2 && temp>=101 && temp<=200)
    {   //2PʥBʦmw2P
        TroopID2P=temp-101;  //令ؿ쪺                  
    }

    if(Turn1P2P==2 && temp>=1 && temp<=10)
    {   //2PʥBʦmw1P
        DrawBattleMenu(); //""""    
    }
    move=1;
}

void Battle()
{  
    int temp;
    char AT,DF;
    char Life1,Life2;
    char map_DF;
    int x,y;
    
    if(Turn1P2P==1)  //1P Attack 2P
    {
        AT= MyTank[TroopID1P].AT;
        Life1=MyTank[TroopID1P].Life;
          
        temp = map1_OnOff[select_posX/20][select_posY/20]-101;
          DF = EnemyTank[temp].DF;
        Life2=EnemyTank[temp].Life; 
           

        x = EnemyTank[temp].Xpos/20;
        y = EnemyTank[temp].Ypos/20;  
        map_DF= map1_DFplus[x][y];   
        EnemyTank[temp].Life = AttackResult( AT,  Life1, DF,  Life2, map_DF);     
    } 
        
    if(Turn1P2P==2)  //2P Attack 1P
    {
        AT= EnemyTank[TroopID2P].AT;
        Life1=EnemyTank[TroopID2P].Life;
           
        temp = map1_OnOff[select2P_posX/20][select2P_posY/20]-1;
        DF= MyTank[temp].DF;
        Life2=MyTank[temp].Life; 
           
        x = MyTank[temp].Xpos/20;
        y = MyTank[temp].Ypos/20;  
        map_DF= map1_DFplus[x][y];   
        MyTank[temp].Life = AttackResult( AT,  Life1, DF,  Life2,  map_DF);     
    } 
}  

void DrawInfo()
{
    //øsT
    //W,q,AT,DF.FPW
    if (Turn1P2P)
    {
        SetFgColor(0);
	    SetBkColor(15);
	    TextOut(0,0," Turn ");
	    DigitOut(33,0,Turn);
	    DigitOut(80,0,Getms());
	    DigitOut(125,0,Turn1P2P);
	    TextOut(130,0,"P is playing");    
    
    
        SetFgColor(0);
     	SetBkColor(15);
    	TextOut(2,228,"  ˥Үv:LO   O   uO   FPW");
    	DigitOut(17,228,MyTank[TroopID1P].ID);  
    	DigitOut(93,228,MyTank[TroopID1P].Life); 
        DigitOut(148,228,MyTank[TroopID1P].AT);  
        DigitOut(202,228,MyTank[TroopID1P].DF);
        DigitOut(237,228,MyTank[TroopID1P].FPW);
        TextOut(286,216,"SCORE");
        DigitOut(286,228,Score1P);
        
    }

}

void DrawBattleMenu()
{
    int temp;
        temp=1;
        MenuSelect=0;
        MenuFlag=1;
        SetFontSize(12); 

   while(MenuFlag)
   {
      ReleaseKey(128);    
      SetFgColor(0);
      Rectangle (111,110,192,168,1);
      SetFgColor(15);          
      Rectangle (113,112,190,166,0);          
      SetFgColor(15);
      SetBkMode(0);     
      TextOut(128,118,"oʧ");
      TextOut(128,138,"aݩR");
      RectangleNot(125,118+MenuSelect*20 , 178 , 130+MenuSelect*20,1);                       
 
      Delay(DELAY_TIME*2);
      keyin= Inkey();
	      
      SetFgColor(10);
      DigitOut(161,158,keyin); 
	  Refresh();  
	              	      
      if(keyin==KEY_UP && MenuSelect==0)
      {
          MenuSelect = MenuSelect+1;
          MenuFlag=1;
          Beep ();
      }
      else if(keyin==KEY_UP && MenuSelect==1)
      {
          MenuSelect = MenuSelect-1;          
          MenuFlag=1;
          Beep ();
      }
      else if (keyin==KEY_DOWN && MenuSelect==0)
      { 
          MenuSelect = MenuSelect+1;
          MenuFlag=1;
          Beep ();
      }
      else if (keyin==KEY_DOWN && MenuSelect==1)
      {         
          MenuSelect = MenuSelect-1;
          MenuFlag=1;
          Beep ();              
      }

      if (keyin==KEY_ENTER && MenuSelect==0)
      {
          Beep ();          
          Fight=1;
          MenuFlag=0;
      }
           
      if (keyin==KEY_ENTER && MenuSelect==1)
      {
          Beep ();
          Fight=0;
          MenuFlag=0;
      }
   }                  
}

void DrawTroopMenu()
{
    int temp;
        temp=1;
        MenuFlag=1;
        MenuSelect=0;
    while (temp)
    {       
        while(MenuFlag)  
        {
            //e
            ReleaseKey (128);
            Delay( DELAY_TIME);
    	    SetFgColor(0);
	        Rectangle(120,90,200,200,1);  //զ
	        SetFgColor(15);
	        Rectangle(122,92,198,198,0);  //¦u
	        //er թ¦r
	        SetFgColor(15);
	        SetBkColor(0);
            TextOut(138, 96,"S A V E");
            TextOut(138,113,"L O A D");
            TextOut(138,130,"ɵ");
            TextOut(138,147,"ɥRLO");
            TextOut(138,164,"˵L");
            TextOut(138,181,"^X");
            SetBkColor(15);
	        RectangleNot(125,95+(MenuSelect*17),195,111+(MenuSelect*17),2);  //e
        	Refresh();

            Delay( DELAY_TIME);
            key=Inkey();	   
            if (key);
            {  MenuFlag=0; } 
        }
        
        MenuFlag=1;
        if (key==KEY_UP && MenuSelect!=0)
        {MenuSelect=MenuSelect-1;}

        if (key==KEY_DOWN && MenuSelect!=5)
        {MenuSelect=MenuSelect+1;}

        
        if (key==KEY_ENTER && MenuSelect==0)  //Save game
        {temp=0;} 
        if (key==KEY_ENTER && MenuSelect==1)  //Load Game
        {temp=0;}
        if (key==KEY_ENTER && MenuSelect==2)  //Get Supply
        {temp=0;}
        if (key==KEY_ENTER && MenuSelect==3)  //Enforcement
        {temp=0;}
        if (key==KEY_ENTER && MenuSelect==4)  //WeaponData
        { 
            ShowData();
        }        
        
        if (key==KEY_ENTER && MenuSelect==5)   //^X
        {
           temp=0;
           TurnEnd=0;
           gameover=0;
           
           SetFgColor(1);
           TextOut(150,100,"Ҧʧ");
           Refresh();
        }   
        
        if (key==KEY_ESC)
        {
            temp=0;
            ReleaseKey (128);
           
        }
    }
}


char AttackResult(char AT, char Life1,char DF, char Life2, char map_DF)
{
    char temp;
    char AT_temp;
    char DF_temp;
    
         DF_temp = DF + map_DF;    //+aΨm
    if ((Life1>=8) && (Life2<=5))  //uէLO[
    { 
         AT_temp = AT + 1;
    }

    temp = (AT_temp*Life1) - (DF_temp*Life2);
    temp = temp+1/Life2;  
    
    if (temp<=0 && temp>=-2 )
    { temp = rand()%2; }     //O(0~-2),50%vlucky shot
    else if(temp<=-3)
    { temp = 0 ; }
    
    temp = Life2 - temp;
    if (temp<0){temp=0;}
    return temp;
} 


void ShowStory()
{
    int temp;
        temp=1;
    ReleaseKey (128);
    SetFgColor(0);
    SetBkColor(15);
    while(temp)
    {
        ShowImage(IDL1,0,0,320,240,0,0,0,0);    //
        TextOut(45, 65,"wӨ꨾x˥ҧLǮ,bY檺Vm");  
        TextOut(45, 85,"AN[JUwNӰ꨾x,åBPNx"); 
        TextOut(45,105,"@_tOaðꪺd!");
        TextOut(45,125,"utߧYNi},btߤiHA");
        TextOut(45,145,"OPi,qLիAN[Jxx");
        TextOut(45,165,",@ӧQPaģûPAPb.");
        
        TextOut(45,205,"  10   ^X20   ؼ:ĭx  ");        
        Refresh();
        Delay( DELAY_TIME);
        temp=Inkey();
        if (temp==0)
        { temp=1;}
        else {temp=0;}
    }    
    
    ShowImage(IDL1,0,0,320,240,0,0,0,0);    //
    TextOut(48,115,"       u   t      }   l!!    ");
    Refresh();
    Delay( DELAY_TIME*10);
} 



void TurnAdd()
{
    Turn=Turn+1;
    if(Turn1P2P==1) 
    {
        Turn1P2P=2;
    }
    else if(Turn1P2P==2) 
    {
        Turn1P2P=1;
    }    
    TextOut(80,116,"^X+1");
    Delay( DELAY_TIME*3);
    Refresh();
}


void DrawRange()
{
   int s,t;
   int temp;

    TextOut(80,116,"øsd");
    Delay(DELAY_TIME);
    Refresh();
    TextOut(70,129,"TroopID1P");
    DigitOut(130,129,TroopID1P);
    
    for (i=0; i<16; i++)
    {
        for  (j=0; j<10; j++)
        {	 
            //eʽdMd
            //jMmap1_OnOffP_aϤWO_
            temp = map1_OnOff[i][j];
           
            //================e1Pʽd================//
            if (Turn1P2P==1 && MyTank[TroopID1P].State>=1 )     //p1PʥB1PԨ٦ʤO.
            {               
                s=abs(MyTank[TroopID1P].Xpos-20*i);  //1PԨYTileXbZ
                t=abs(MyTank[TroopID1P].Ypos-20*j);  //1PԨYTileYbZ
      
                if (s+t <= (MyTank[TroopID1P].GroundMove*20 && temp==0)) //pYTilebʽd                
                {                                                        //BYTileWS~eʽd 
                    SetFgColor(0);            //]wʽdتC⬰զ          
                    Rectangle (2+20*i , 17+20*j , 18+20*i , 33+20*j ,0);                    
                }
                
                //============e1Pd============//    
                if (s+t <= (MyTank[TroopID1P].AT_Range * 20))    //p1PʥB1PԨ٦ʤO.             
                {                                                //BYTilebd
                    if (temp>100)     //paϤWĤ(2P)
                    {
                        SetFgColor(0);  //]wdتC⬰զ 
                        Rectangle (8+20*i,23+20*j,12+20*i,27+20*j,0);
                    }                                      
                }               
            } 
               
           //================e2P&d================//
           if (Turn1P2P==2 && EnemyTank[TroopID2P].State>=1 )   //p2PʥB1PԨ٦ʤO.
           {               
               s=abs(EnemyTank[TroopID2P].Xpos-20*i);  //2PԨYTileXbZ
               t=abs(EnemyTank[TroopID2P].Ypos-20*j);  //2PԨYTileYbZ
               
               if (s+t <= (EnemyTank[TroopID2P].GroundMove * 20 && temp==0)) //pYTilebʽd򤺥B    
               {                                                             //YTileWS~eʽd 
                   SetFgColor(0);//]wʽdتC⬰զ   
                   Rectangle (2+20*i,17+20*j,18+20*i,33+20*j,0);                 
               }
               //============e2Pd============//               
               if (s+t <= (EnemyTank[TroopID1P].AT_Range * 20)) //p1PʥB1PԨ٦ʤO.  
               {                                                //BYTilebd
                   if (temp<=100)     //paϤWĤ(1P)
                   {                     
                      SetFgColor(0);  //]wdتC⬰զ 
                      Rectangle (8+20*i,23+20*j,12+20*i,27+20*j,0);
                   }                                      
               }               
           } 
       }
    }
}


void CheckTurnEnd()  //ˬd1P2PO_٦|ʩҦʧ,_hTurnEnd=0
{
    int AllState;
        AllState = 0;
    if (Turn1P2P==1)  //ˬd1pO_٦|
    {  
        AllState = 0;    
        for (i=0;i<10;i++)
        {
            AllState = AllState + MyTank[i].State;
        }
        TextOut(150,100,"ˬd1PʤO");
        Refresh();        
    }
    
    else if(Turn1P2P==2) //ˬd2pO_٦|
    {  
        AllState = 0;
        for (i=0;i<10;i++)
        {
            AllState = AllState + EnemyTank[i].State;
        }
        TextOut(150,100,"ˬd2PʤO");
        Refresh(); 
    }
        
    if (AllState ==0)   //Ҧʧ
    {
        TurnEnd=0;
        TextOut(150,100,"Ҧʧ");
        Refresh();
    }
}    

void FPWcost()
{
    int temp;
    if(Turn1P2P==1)
    {
       temp = MyTank[TroopID1P].FPW - (MyTank[TroopID1P].FPWcost * MyTank[TroopID1P].Life);
       MyTank[TroopID1P].FPW = temp;
    }
        
    if(Turn1P2P==2)
    {
       temp = EnemyTank[TroopID2P].FPW - (EnemyTank[TroopID2P].FPWcost * EnemyTank[TroopID2P].Life);
       EnemyTank[TroopID2P].FPW = temp;
    }
    
    TextOut(150,100,"FPWI");
    Refresh();    
}

void ShowData()         //ܪZ
{
    ShowImage(DataBG,0,0,320,240,0,0,0,0);    //
    ShowImage(IDL0,220,105,64,64,105,84,1,15);    //
    Refresh();
    Delay(DELAY_TIME);  
    getchar();  
}

void DrawAirMap()
{ 
    int U,V,S,T;
  
    U= 0 + AirMapCoord;
    S= 320 - AirMapCoord;
    V=   0;
    ShowImage(SkyMap,0,15,320- AirMapCoord,200, U , V ,0,15);    //     
    ShowImage(SkyMap,320-AirMapCoord,15,0+AirMapCoord,200, 0 , V ,0,15);    // 
    AirMapCoord=AirMapCoord+1;
    if (AirMapCoord==320){AirMapCoord=0;}  

        SetFgColor(8);        
        HLine( 0,320, 55);
        HLine( 0,320, 95);
        HLine( 0,320,135);
        HLine( 0,320,175);
        VLine(40,15,214);
        VLine(80,15,214);
        VLine(120,15,214);
        VLine(160,15,214);
        VLine(200,15,214);
        VLine(240,15,214);
        VLine(280,15,214); 
}