4180 fire embed / Mbed 2 deprecated Fire_Mbed_2_0

Dependencies:   4DGL-uLCD-SE Motor SparkfunAnalogJoystick mbed

Fork of Fire_Mbed_2_0 by Jake Ramey

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SparkfunAnalogJoystick.h"
00003 #include "uLCD_4DGL.h"
00004 #include "FEmain.h"
00005 #define MapSecA 123
00006 #define MapSecB 123
00007 
00008 #include "Motor.h"
00009 int units_received = 0;
00010 
00011 Motor m(p21, p24, p25); // pwm, fwd, rev
00012 
00013 #define FORTRESS 0
00014 #define HOUSE 1
00015 #define KEEP 2
00016 #define GRASS 3
00017 #define TREES 4
00018 #define COMP 2
00019 #define USER 1
00020 
00021 
00022 DigitalOut myled(LED1);
00023 uLCD_4DGL uLCD(p9,p10,p11);
00024 DigitalIn pb5(p5);
00025 DigitalIn pb6(p6);
00026 DigitalIn pb7(p7);
00027 DigitalIn pb8(p8);
00028 DigitalOut led1(LED1);
00029 DigitalOut led2(LED2);
00030 DigitalOut led3(LED3);
00031 DigitalOut led4(LED4);
00032 #define M_PI 3.14159265358979323846
00033 Serial pc(USBTX, USBRX);
00034 Serial Julio_Mbed(p28,p27);
00035 SparkfunAnalogJoystick joysttick(p18, p19, p20);
00036 #define x_sector 0x003B
00037 int analog2Keypad(SparkfunAnalogJoystick *joystick);
00038 int player_turn;
00039 char stagenumber = 0;
00040 
00041 struct tiles {
00042         char v;
00043         int x;
00044         int y;
00045         char passable;
00046         tiles(char v1 =0, int x1=0, int y1=0, char p1=0) : v(v1), x(x1), y(y1), passable(p1){}}; //fortress_t,house_t,keep_t,grass_t,trees_t;
00047 const tiles fortress_t(0,x_sector,0x5009,0);
00048 const tiles house_t(1,x_sector,0x5021,1);
00049 const tiles keep_t(2,x_sector,0x501B,1);
00050 const tiles grass_t(3,x_sector,0x501D,1);
00051 const tiles trees_t(4,x_sector,0x5019,1);
00052 const tiles hill_t(5,x_sector,0x501F,1);
00053 
00054 tiles tile_array[6] = {fortress_t,house_t,keep_t,grass_t,trees_t,hill_t};
00055 
00056 
00057 typedef struct units {
00058         job_t job;
00059         char v;
00060         int FoF;//1 = USER // 2= COMP
00061         int xloc;
00062         int yloc;
00063         int xlast;
00064         int ylast;
00065         char remain_moves;
00066         char health;
00067         char status;
00068     } person_t;
00069 
00070     
00071 static person_t Mc;
00072 static person_t Ak;
00073 static person_t Ap;
00074 static person_t Healer;
00075 static person_t Brigand;    
00076 static person_t Thief;
00077 static person_t Soldier;
00078 static person_t Wolf;
00079 static person_t Wolfa;
00080 static person_t Wolfb;
00081 static person_t Wolfc;
00082 
00083 static person_t p_Princess;
00084 static person_t p_Thief;
00085 static person_t p_WhiteMage;
00086 static person_t p_Pirate;
00087 static person_t p_Lancer;
00088 static person_t p_Archer;
00089 static person_t p_Dragon;
00090 static person_t p_Sage;
00091 static person_t p_Caster;
00092 static person_t p_Assassin;
00093 static person_t p_Medic;
00094 static person_t p_Brigand;
00095 static person_t p_Wolf_a;
00096 static person_t p_Wolf_b;
00097 static person_t p_Wolf_c;
00098 static person_t p_Assassin_e;
00099 static person_t p_Fighter_a;
00100 static person_t p_Fighter_b;
00101 static person_t p_Fighter_c;
00102 static person_t p_RedSpider;
00103 static person_t p_Spider_a;
00104 static person_t p_Spider_b;
00105 static person_t p_Spider_c;
00106 static person_t p_FallenHero;
00107 static person_t p_Mage_4a;
00108 static person_t p_Mage_4b;
00109 static person_t p_Mage_4c;
00110 static person_t p_TrueAssassin;
00111 static person_t p_Zombie_5a;
00112 static person_t p_Zombie_5b;
00113 static person_t p_Zombie_5c;
00114 static person_t p_General;
00115 static person_t p_Zombie_6a;
00116 static person_t p_Zombie_6b;
00117 static person_t p_Mage_6a;
00118 static person_t p_Mage_6b;
00119 static person_t p_Summoner;
00120 static person_t p_Zombie_7a;
00121 static person_t p_Zombie_7b;
00122 static person_t p_Zombie_7c;
00123 static person_t p_Zombie_7d;
00124 
00125 person_t* person_array[42] = {NULL, &p_Princess, &p_Thief, &p_WhiteMage, &p_Pirate, &p_Lancer, &p_Archer, &p_Dragon, &p_Sage, &p_Caster, &p_Assassin, &p_Medic, 
00126                                     &p_Brigand, &p_Wolf_a, &p_Wolf_b, &p_Wolf_c, 
00127                                     &p_Assassin_e, &p_Fighter_a, &p_Fighter_b, &p_Fighter_c, 
00128                                     &p_RedSpider, &p_Spider_a, &p_Spider_b, &p_Spider_c, 
00129                                     &p_FallenHero, &p_Mage_4a, &p_Mage_4b, &p_Mage_4c, 
00130                                     &p_TrueAssassin, &p_Zombie_5a, &p_Zombie_5b, &p_Zombie_5c, 
00131                                     &p_General, &p_Zombie_6a, &p_Zombie_6b, &p_Mage_6a, &p_Mage_6b, 
00132                                     &p_Summoner, &p_Zombie_7a, &p_Zombie_7b, &p_Zombie_7c, &p_Zombie_7d};
00133     
00134 char Map1_1[8][8] = {
00135     {FORTRESS, FORTRESS, FORTRESS,  GRASS,  GRASS,  GRASS,  HOUSE,  GRASS},
00136     {FORTRESS, FORTRESS, FORTRESS,  GRASS,  GRASS,  GRASS,  GRASS,  GRASS},
00137     {FORTRESS, FORTRESS, FORTRESS,  GRASS,  TREES,  GRASS,  GRASS,  GRASS},
00138     {GRASS,    GRASS,    GRASS,     GRASS,  GRASS,  GRASS,  GRASS,  GRASS},
00139     {GRASS,    GRASS,    GRASS,     GRASS,  GRASS,  GRASS,  GRASS,  GRASS},
00140     {GRASS,    GRASS,    GRASS,     GRASS,  TREES,  TREES,  TREES,  GRASS},
00141     {GRASS,    GRASS,    HOUSE,     TREES,  GRASS,  KEEP,   GRASS,  GRASS},
00142     {GRASS,    GRASS,    GRASS,     GRASS,  GRASS,  GRASS,  GRASS,  GRASS} };
00143 
00144 char unitsMap1_1[8][8] = {
00145     {0, 0, 0, 0, 0, 0, 0, 0},
00146     {0, 0, 0, 0, 0, 0, 0, 0},
00147     {0, 0, 0, 0, 0, 0, 0, 0},
00148     {0, 0, 0, 0, 0, 0, 0, 0},
00149     {0, 0, 0, 0, 0, 0, 0, 0},
00150     {0, 0, 0, 0, 0, 0, 0, 0},
00151     {0, 0, 0, 0, 0, 0, 0, 0},
00152     {0, 0, 0, 0, 0, 0, 0, 0} };
00153     
00154     
00155     
00156         
00157 void initialize(void);
00158 //void load_game_state
00159 int user_selecting(void);//return selection
00160 int user_action(int selection);
00161 int display_status(int selection); 
00162 int user_movement(int selection);
00163 int combat(person_t* attacker, person_t* defender);
00164 int heal(person_t* attacker, person_t* defender);
00165 void find_player(person_t* ai_pred);
00166 void hunt_player(person_t* predator, person_t* prey, int prey_x, int prey_y, int predator_x, int predator_y);
00167 void ai_movement(void);    
00168 void ask_nextstage(void);
00169 void load_units_map(void);
00170 void combatPhase(char a, char d);
00171 void sendMap(void);
00172 void sendUnits (void);
00173 void receiveMap (void);
00174 void receiveUnits (void);
00175 void waitForAck (void);
00176 int state;
00177 int cursor_x;
00178 int cursor_y;
00179 void kill_unit(person_t* dead_unit);
00180     
00181 int main() {
00182     uLCD.media_init();
00183     pb5.mode(PullUp);
00184     pb6.mode(PullUp);
00185     pb7.mode(PullUp);
00186     pb8.mode(PullUp);
00187     initialize();
00188     int selection;
00189     int action;
00190     player_turn =1;
00191     cursor_x = 0;
00192     cursor_y = 0;
00193     state = 0;
00194     person_t* temp_persona;
00195     while(1) {
00196         if (state == 0){
00197             selection = user_selecting();}
00198         //else if (state ==1)
00199             //ret_val = display_status(selection); 
00200         else if (state == 2){
00201             action = user_movement(selection);}
00202         else if (state == 3){
00203             if (player_turn ==1){
00204                 player_turn = 2;
00205                 ai_movement();
00206                 }
00207             else {
00208                 player_turn =1;
00209                 }
00210             for(int i=1; i<42;i++){
00211                 temp_persona = person_array[i]; 
00212                 temp_persona->remain_moves = temp_persona->job.movement; 
00213                 }
00214             state = 0;
00215             }
00216         //else if (state == 4)
00217     }
00218 }
00219 //cleric - lord - ax
00220 //noth - spear
00221 
00222 
00223 
00224 void initialize(void){
00225     init_jobs();
00226 //    keep_t.x = x_sector;
00227 //    house_t.x = x_sector;
00228 //    grass_t.x = x_sector;
00229 //    trees_t.x = x_sector;
00230 //    fortress_t.x = x_sector;
00231 //    fortress_t.y = 0x5042;
00232 //    trees_t.y = 0x5052;
00233 //    keep_t.y = 0x5054;
00234 //    grass_t.y = 0x5056;
00235 //    house_t.y = 0x5058;
00236     //wait for ack function
00237     /*while(!pc.readable()){
00238         wait(.001);
00239     }
00240     for (int y_num = 0; y_num < 8; y_num ++){
00241         for(int x_num = 0; x_num<8; x_num ++){
00242             Map1_1[y_num][x_num] = pc.getc();
00243         }
00244     }
00245     tiles tile_draw;
00246     int castledrawn = 0;
00247     int drawn;
00248     for (int y_num = 0; y_num < 8; y_num ++){
00249         for(int x_num = 0; x_num<8; x_num ++){
00250             drawn = Map1_1[y_num][x_num];
00251             if (! ((drawn == CASTLE) && (castledrawn == 1))){
00252                 tile_draw = tile_array[Map1_1[y_num][x_num]];
00253                 uLCD.set_sector_address(tile_draw.x,tile_draw.y);
00254                 uLCD.display_image((16*x_num),(16*y_num));
00255             }
00256             if (drawn == CASTLE)
00257                 castledrawn = 1;
00258         }
00259     }*/
00260     //uLCD.set_sector_address(Mapx, Mapy);
00261     //uLCD.display_image(0,0);
00262     p_Princess.job = j_Erika;
00263     p_Thief.job = j_Thief;
00264     p_WhiteMage.job = j_Cleric;
00265     p_Pirate.job = j_Pirate;
00266     p_Lancer.job = j_Knight;
00267     p_Archer.job = j_Archer;
00268     p_Dragon.job = j_Myrrh;
00269     p_Sage.job = j_Sage_M;
00270     p_Caster.job = j_Sage_F;
00271     p_Assassin.job = j_Assassin_F;
00272     p_Medic.job = j_Bard;
00273     p_Brigand.job = j_Brigand;
00274     p_Wolf_a.job = j_Wolf;
00275     p_Wolf_b.job = j_Wolf;
00276     p_Wolf_c.job = j_Wolf;
00277     p_Assassin_e.job = j_Boss_Assassin_F;
00278     p_Fighter_a.job = j_Fighter;
00279     p_Fighter_b.job = j_Fighter;
00280     p_Fighter_c.job = j_Fighter;
00281     p_RedSpider.job = j_RedSpider;
00282     p_Spider_a.job = j_Spider;
00283     p_Spider_b.job = j_Spider;
00284     p_Spider_c.job = j_Spider;
00285     p_FallenHero.job = j_Hero;
00286     p_Mage_4a.job = j_Mage;
00287     p_Mage_4b.job = j_Mage;
00288     p_Mage_4c.job = j_Mage;
00289     p_TrueAssassin.job = j_Assassin_M;
00290     p_Zombie_5a.job = j_Zombie;
00291     p_Zombie_5b.job = j_Zombie;
00292     p_Zombie_5c.job = j_Zombie;
00293     p_General.job = j_General;
00294     p_Zombie_6a.job = j_Zombie;
00295     p_Zombie_6b.job = j_Zombie;
00296     p_Mage_6a.job = j_Mage;
00297     p_Mage_6b.job = j_Mage;
00298     p_Summoner.job = j_Summoner;
00299     p_Zombie_7a.job = j_Zombie;
00300     p_Zombie_7b.job = j_Zombie;
00301     p_Zombie_7c.job = j_Zombie;
00302     p_Zombie_7d.job = j_Zombie;
00303     
00304     
00305     
00306     
00307     person_t* temp_person_init;
00308     for( char i = 1; i<42; i++){
00309         temp_person_init = person_array[i];
00310         temp_person_init->v = i;
00311         temp_person_init->status = 1;
00312         Mc.remain_moves = Mc.job.movement;
00313         temp_person_init->remain_moves = temp_person_init->job.movement;
00314         temp_person_init->health = temp_person_init->job.maxHealth;
00315         if (i<=11)
00316             temp_person_init->FoF = USER;
00317         else
00318             temp_person_init->FoF = COMP;
00319     }
00320     Mc.job = Lord;
00321     Mc.v = 1;
00322     Mc.FoF = USER;
00323     Mc.xlast = 1;
00324     Mc.xloc = 1;
00325     Mc.ylast = 3;
00326     Mc.yloc = 3;
00327     Mc.remain_moves = Mc.job.movement;
00328     Mc.health = Mc.job.maxHealth;
00329     //uLCD.set_sector_address(Mc.job.x, Mc.job.y);
00330     //uLCD.display_image((Mc.xloc*16),(Mc.yloc*16));
00331     
00332     Ak.job = Armor;
00333     Ak.v = 2;
00334     Ak.FoF = USER;
00335     Ak.xlast = 1;
00336     Ak.xloc = 1;
00337     Ak.ylast = 4;
00338     Ak.yloc = 4;
00339     Ak.remain_moves = Ak.job.movement;
00340     Ak.health = Ak.job.maxHealth;
00341     
00342     //uLCD.set_sector_address(Ak.job.x, Ak.job.y);
00343     //uLCD.display_image((Ak.xloc*16),(Ak.yloc*16));
00344     
00345     Ap.job = j_Pirate;
00346     Ap.v = 3;
00347     Ap.FoF = USER;
00348     Ap.xlast = 2;
00349     Ap.xloc = 2;
00350     Ap.ylast = 3;
00351     Ap.yloc = 3;
00352     Ap.remain_moves = Ap.job.movement;
00353     Ap.health = Ap.job.maxHealth;
00354     
00355     //uLCD.set_sector_address(Ap.job.x, Ap.job.y);
00356     //uLCD.display_image((Ap.xloc*16),(Ap.yloc*16));
00357     
00358 //    Healer.job = j_Cleric;
00359 //    Healer.v = 4;
00360 //    Healer.FoF = USER;
00361 //    Healer.xlast = 0;
00362 //    Healer.xloc = 0;
00363 //    Healer.ylast = 3;
00364 //    Healer.yloc = 3;
00365 //    Healer.remain_moves = Healer.job.movement;
00366 //    Healer.health = Healer.job.maxHealth;
00367 //    
00368     //uLCD.set_sector_address(Healer.job.x, Healer.job.y);
00369     //uLCD.display_image((Healer.xloc*16),(Healer.yloc*16));
00370     
00371     Wolf.job = Beast;
00372     Wolf.v = 8;
00373     Wolf.FoF = COMP;
00374     Wolf.xlast = 7;
00375     Wolf.xloc = 7;
00376     Wolf.ylast = 4;
00377     Wolf.yloc = 4;
00378     Wolf.remain_moves = Wolf.job.movement;
00379     Wolf.health = Wolf.job.maxHealth;
00380     
00381     //uLCD.set_sector_address(Wolf.job.x, Wolf.job.y);
00382     //uLCD.display_image((Wolf.xloc*16),(Wolf.yloc*16));    
00383     //unitsMap1_1[Wolf.yloc][Wolf.xloc] = Wolf.v;
00384     
00385     Wolfa.job = Beast;
00386     Wolfa.v = 9;
00387     Wolfa.FoF = COMP;
00388     Wolfa.xlast = 7;
00389     Wolfa.xloc = 7;
00390     Wolfa.ylast = 5;
00391     Wolfa.yloc = 5;
00392     Wolfa.remain_moves = Wolfa.job.movement;
00393     Wolfa.health = Wolfa.job.maxHealth;
00394     
00395     //uLCD.set_sector_address(Wolfa.job.x, Wolfa.job.y);
00396     //uLCD.display_image((Wolfa.xloc*16),(Wolfa.yloc*16));    
00397     //unitsMap1_1[Wolfa.yloc][Wolfa.xloc] = Wolfa.v;
00398     
00399     Wolfb.job = Beast;
00400     Wolfb.v = 10;
00401     Wolfb.FoF = COMP;
00402     Wolfb.xlast = 6;
00403     Wolfb.xloc = 6;
00404     Wolfb.ylast = 4;
00405     Wolfb.yloc = 4;
00406     Wolfb.remain_moves = Wolfb.job.movement;
00407     Wolfb.health = Wolfb.job.maxHealth;
00408     
00409     //uLCD.set_sector_address(Wolfb.job.x, Wolfb.job.y);
00410     //uLCD.display_image((Wolfb.xloc*16),(Wolfb.yloc*16));    
00411     //unitsMap1_1[Wolfb.yloc][Wolfb.xloc] = Wolfb.v;
00412     
00413     
00414     Wolfc.job = Beast;
00415     Wolfc.v = 11;
00416     Wolfc.FoF = COMP;
00417     Wolfc.xlast = 7;
00418     Wolfc.xloc = 7;
00419     Wolfc.ylast = 3;
00420     Wolfc.yloc = 3;
00421     Wolfc.remain_moves = Wolfc.job.movement;
00422     Wolfc.health = Wolfc.job.maxHealth;
00423     
00424     //uLCD.set_sector_address(Wolfc.job.x, Wolfc.job.y);
00425     //uLCD.display_image((Wolfc.xloc*16),(Wolfc.yloc*16));    
00426     //unitsMap1_1[Wolfc.yloc][Wolfc.xloc] = Wolfc.v;
00427     
00428     //unitsMap1_1[Mc.yloc][Mc.xloc] = Mc.v;
00429     //unitsMap1_1[Ak.yloc][Ak.xloc] = Ak.v;
00430     //unitsMap1_1[Ap.yloc][Ap.xloc] = Ap.v;
00431     //unitsMap1_1[Healer.yloc][Healer.xloc] = Healer.v;
00432     
00433     //get map
00434     //get units
00435     //send julio the char[2] {255, map/track number}
00436     uLCD.printf("Load Map\r\n");
00437     //put read from gui map code here
00438     uLCD.printf("Load Units\r\n");
00439     //put read units from gui code here
00440     uLCD.printf("New Game\r\n");
00441     uLCD.printf("Or Continue\r\n");
00442     while(units_received == 0){
00443         waitForAck();}
00444     load_floor(stagenumber);
00445     load_units_map();
00446     return;
00447 }
00448 int user_selecting(void){    
00449     int xlast;
00450     int ylast;
00451     int action = 0;
00452     int person_index = 0;
00453     int tile_index = 0;
00454     int keypad = 5;
00455     person_t* temp_person;
00456     tiles tile_erased;
00457     int j = pb5;
00458     int y = pb8;
00459     while(action == 0){
00460         keypad = 5;
00461         xlast = cursor_x;
00462         ylast = cursor_y;
00463         while((keypad == 5)&&(j ==1)){
00464         wait(1);
00465         j = pb5;
00466         y = pb8;
00467         keypad = analog2Keypad(&joysttick);
00468                 if (y==0){
00469                     state = 3;
00470                     return 0;
00471                     }
00472         }
00473         switch (keypad)
00474     {
00475         case 1:
00476         {
00477             cursor_x = cursor_x -1;
00478             cursor_y = cursor_y +1;
00479         }
00480             break;
00481         case 2:
00482         {
00483             cursor_y = cursor_y +1;
00484         }
00485             break;
00486         case 3:
00487         {
00488             cursor_x = cursor_x +1;
00489             cursor_y = cursor_y +1;
00490         }
00491             break;
00492         case 4:
00493         {
00494             cursor_x = cursor_x -1;
00495         }
00496             break;
00497         case 5:
00498             break;
00499         case 6:
00500         {
00501             cursor_x = cursor_x +1;
00502         }
00503             break;
00504         case 7:
00505         {
00506             cursor_x = cursor_x -1;
00507             cursor_y = cursor_y -1;
00508         }
00509             break;
00510         case 8:
00511         {
00512             cursor_y = cursor_y -1;
00513         }
00514             break;
00515         case 9:
00516         {
00517             cursor_x = cursor_x + 1;
00518             cursor_y = cursor_y - 1;
00519         }
00520             break;
00521     }
00522         if (cursor_y <0)
00523             cursor_y = 0;
00524         if (cursor_x <0)
00525             cursor_x = 0;
00526         if (cursor_y >7)
00527             cursor_y = 7;
00528         if (cursor_x >7)
00529             cursor_x = 7;
00530         
00531         
00532         if((xlast != cursor_x) || (ylast != cursor_y)){
00533             person_index = unitsMap1_1[ylast][xlast];
00534             tile_index = Map1_1[ylast][xlast];
00535             if (person_index != 0){
00536                 temp_person = person_array[person_index];
00537                 uLCD.set_sector_address(temp_person->job.x, temp_person->job.y);
00538                 uLCD.display_image((xlast*16), (ylast*16));
00539                 }
00540             else{
00541                 tile_erased = tile_array[tile_index];
00542                 if (tile_index == 0){
00543                     uLCD.set_sector_address(tile_erased.x, tile_erased.y);
00544                     uLCD.display_image(0, 0);
00545                     }
00546                 else{
00547                     uLCD.set_sector_address(tile_erased.x, tile_erased.y);
00548                     uLCD.display_image(xlast*16, ylast*16);
00549                     }
00550             }    
00551             uLCD.rectangle((cursor_x*16), (cursor_y*16), ((cursor_x*16)+15), ((cursor_y*16)+15), 0x000000);
00552         }
00553         int a = pb5;
00554         //int b = pb6;
00555         int c = pb7;
00556         int d = pb8;
00557         if (pb5 == 0){
00558         state = 2;
00559         action = 1;
00560         }
00561         //else if (pb6 == 0){
00562         //state = 3;
00563         //action = 2;
00564         //}
00565         else if (pb7 == 0){
00566         action = 3;
00567         }
00568         else if (pb8 == 0){
00569         action = 4;
00570         }
00571     }
00572     if (pb5 == 0){
00573         state = 2;
00574         action = 1;
00575     }
00576     return action;
00577 }
00578 
00579 
00580 int user_movement(int selection){
00581     int Index_j;
00582     Index_j = unitsMap1_1[cursor_y][cursor_x];
00583     person_t* User_person;
00584     if (Index_j == 0){
00585     state = 4;
00586     return 69;   
00587     }
00588     User_person = person_array[Index_j];
00589     if ((User_person->remain_moves) == 0)
00590     {
00591         state = 0;
00592         return 0;
00593     }
00594         if ((User_person->FoF) != player_turn)
00595     {
00596         state = 0;
00597         return 0;
00598     }
00599     Julio_Mbed.putc(1);
00600     Julio_Mbed.putc(User_person->v);
00601     Julio_Mbed.putc(User_person->health);
00602     Julio_Mbed.putc(User_person->job.maxHealth);
00603     int j = pb7;
00604     int xloc_u = 1;
00605     int yloc_u = 1;
00606     int xlast = 1;
00607     int ylast = 1;
00608 while(j==1){
00609     j = pb7;
00610     xloc_u = (User_person->xloc);
00611     yloc_u = User_person->yloc;
00612     xlast = User_person->xlast;
00613     ylast = User_person->ylast;
00614     int keypad = 5;
00615         while((keypad == 5)&&(j==1)){
00616         wait(1);
00617         j = pb7;
00618         keypad = analog2Keypad(&joysttick);
00619         }
00620         switch (keypad)
00621         {
00622         case 1:
00623             xloc_u = xloc_u -1;
00624             yloc_u = yloc_u +1;
00625             break;
00626         case 2:
00627             yloc_u = yloc_u +1;
00628             break;
00629         case 3:
00630             xloc_u = xloc_u +1;
00631             yloc_u = yloc_u +1;
00632             break;
00633         case 4:
00634             xloc_u = xloc_u -1;
00635             break;
00636         case 5:
00637             break;
00638         case 6:
00639             xloc_u = xloc_u +1;
00640             break;
00641         case 7:
00642             xloc_u = xloc_u -1;
00643             yloc_u = yloc_u -1;
00644             break;
00645         case 8:
00646             yloc_u = yloc_u -1;
00647             break;
00648         case 9:
00649             xloc_u = xloc_u + 1;
00650             yloc_u = yloc_u - 1;
00651             break;
00652         }
00653         if (yloc_u <0)
00654             yloc_u = 0;
00655         if (xloc_u <0)
00656             xloc_u = 0;
00657         if (yloc_u >7)
00658             yloc_u = 7;
00659         if (xloc_u >7)
00660             xloc_u = 7;
00661         int x_change = 0;
00662         if (xloc_u != xlast)
00663             x_change = 1;
00664         int y_change = 0;
00665         if (yloc_u != ylast)
00666             y_change = 1;
00667         if (((User_person->remain_moves)-(y_change+x_change))<0){
00668             xloc_u = xlast;
00669             yloc_u = ylast;
00670             x_change = 0;
00671             y_change = 0;
00672             }
00673         
00674         
00675         int unit_index = unitsMap1_1[yloc_u][xloc_u];
00676         int unit_index_R = 0;  
00677         int alliance = 3;
00678         if ((User_person->job.rng == 2)&&((xloc_u!=xlast)||(yloc_u!=ylast))){
00679             int yloc_R = yloc_u + (yloc_u - ylast);
00680             int xloc_R = xloc_u + (xloc_u - xlast);
00681             if ((xloc_R <=7)&&(xloc_R>=0)&&(yloc_R>=0)&&(yloc_R <=7)){
00682                 unit_index_R = unitsMap1_1[yloc_u][xloc_u]; 
00683                 if (unit_index_R != 0){
00684                     person_t* target_R;
00685                     target_R = person_array[unit_index_R];
00686                     alliance = target_R->FoF;
00687                     if(((xloc_u!=xlast)||(yloc_u!=ylast))&& (alliance != (User_person-> FoF)))
00688                     {
00689                         combat(User_person, target_R);
00690                         User_person->remain_moves = 0;
00691                         state = 0;
00692                         return 0;
00693                     }
00694                 }
00695             }
00696         }
00697         if (unit_index != 0){
00698             person_t* target;
00699             target = person_array[unit_index];
00700             alliance = target->FoF;
00701             if (User_person->job.rng == 2){
00702                 m.speed(1.0);
00703                 xloc_u = xlast;
00704                 yloc_u = ylast;
00705                 x_change = 0;
00706                 y_change = 0;
00707                 wait(1.0);
00708                 m.speed(0.0);
00709                 }
00710             else if ((alliance ==player_turn)&&(User_person->job.attack != staff)){
00711                 m.speed(1.0);
00712                 xloc_u = xlast;
00713                 yloc_u = ylast;
00714                 x_change = 0;
00715                 y_change = 0;
00716                 wait(1.0);
00717                 m.speed(0.0);
00718                 }
00719             else if(((xloc_u!=xlast)||(yloc_u!=ylast))&& (alliance != (User_person-> FoF)))
00720             {
00721                 combat(User_person, target);
00722                 User_person->remain_moves = 0;
00723                 state = 0;
00724                 return 0;
00725             }
00726             else if(((xloc_u!=xlast)||(yloc_u!=ylast))&& ((User_person->job.attack == staff)&&(alliance == (User_person->FoF))))
00727             {
00728                 heal(User_person, target);
00729                 User_person->remain_moves = 0;
00730                 state = 0;
00731                 return 0;
00732             }
00733         }  
00734         int tile_index = Map1_1[yloc_u][xloc_u];
00735         tiles check_passable = tile_array[tile_index];
00736         if ((check_passable.passable) == 1){
00737             m.speed(1.0); 
00738             xloc_u = xlast;
00739             yloc_u = ylast;
00740             x_change = 0;
00741             y_change = 0;
00742             wait(1.0);
00743             m.speed(0.0);
00744         }      
00745         
00746         if((xlast != xloc_u) || (ylast != yloc_u)){
00747                 tile_index = Map1_1[ylast][xlast];
00748                 unitsMap1_1[ylast][xlast] = 0;
00749                 unitsMap1_1[yloc_u][xloc_u] = User_person->v;
00750                 tiles tile_erased = tile_array[tile_index];
00751                 if (tile_index == 0){
00752                     uLCD.set_sector_address(tile_erased.x, tile_erased.y);
00753                     uLCD.display_image(0, 0);
00754                     }
00755                 else{
00756                     uLCD.set_sector_address(tile_erased.x, tile_erased.y);
00757                     uLCD.display_image(xlast*16, ylast*16);
00758                     }
00759                 uLCD.set_sector_address(User_person->job.x,User_person->job.y);
00760                 User_person->xloc = xloc_u;
00761                 User_person->yloc = yloc_u;
00762                 User_person->xlast = xloc_u;
00763                 User_person->ylast = yloc_u;
00764                 User_person->remain_moves = (User_person->remain_moves) - x_change - y_change;
00765                 uLCD.display_image(((User_person->xloc)*16), ((User_person->yloc)*16));    
00766         }    
00767 }    
00768     state = 0;
00769     return 0;
00770 }
00771 
00772 int analog2Keypad(SparkfunAnalogJoystick *joystick) {
00773     if (joystick->distance() > .1) { //not in deadzone, accept input
00774         if ((joystick->angle() > 0.0 && joystick->angle() < 22.5) ||(joystick->angle() > 337.5 && joystick->angle() < 360.0)) {
00775             return 6;
00776         } else if (joystick->angle() >= 22.5 && joystick->angle() < 67.5) {
00777             return 9;
00778         } else if (joystick->angle() >= 67.5 && joystick->angle() < 112.5) {
00779             return 8;    
00780         } else if (joystick->angle() >= 112.5 && joystick->angle() < 157.5) {
00781             return 7;
00782         } else if (joystick->angle() >= 157.5 && joystick->angle() < 202.5) {
00783             return 4;
00784         } else if (joystick->angle() >= 202.5 && joystick->angle() < 247.5) {
00785             return 1;
00786         } else if (joystick->angle() >= 247.5 && joystick->angle() < 292.5) {
00787             return 2;
00788         } else if (joystick->angle() >= 292.5 && joystick->angle() < 337.5) {
00789             return 3;
00790         }
00791     } else {
00792         return 5;
00793     }
00794 }
00795 
00796 int combat(person_t* attacker, person_t* defender){
00797     int d_health;
00798     if (attacker->job.attack == magic){
00799         d_health = (defender->health)-((attacker->job.damage)-(defender->job.defence));
00800         }
00801     else if (attacker->job.attack == dagger){
00802         d_health = (defender->health)-((attacker->job.damage)-((defender->job.defence)/2));
00803         }
00804     else{
00805         d_health = (defender->health)-((attacker->job.damage)-(defender->job.res));
00806         }
00807     int a_health;
00808     if (d_health < (defender->health))
00809         defender->health = d_health;
00810     if (((defender->health)>0)&&(attacker->job.rng==1)){
00811         a_health = (defender->health)-(((attacker->job.damage)-(defender->job.defence))/2);
00812         attacker->health = a_health;
00813         }
00814     //pc.printf("attacker health %d \n",attacker->health);
00815     //pc.printf("defender health %d \n",defender->health);
00816     combatPhase(attacker->v, defender->v);
00817     if ((defender->health)<=0)
00818         kill_unit(defender);
00819     if ((attacker->health)<=0)
00820         kill_unit(attacker);
00821     
00822     
00823     return 0;
00824 }
00825     
00826     
00827 int heal(person_t* attacker, person_t* defender){
00828     int d_health = (defender->health)+(attacker->job.damage);
00829     if (d_health > defender->job.maxHealth)
00830         d_health = defender->job.maxHealth;
00831     if (d_health != (defender->health))
00832         defender->health = d_health;
00833     pc.printf("attacker health %d \n",attacker->health);
00834     pc.printf("defender health %d \n",defender->health);
00835     
00836     
00837     return 0;
00838 }    
00839 
00840 void kill_unit(person_t* dead_unit){
00841     int xloc = dead_unit->xloc;
00842     int yloc = dead_unit->yloc;
00843     unitsMap1_1[yloc][xloc] = 0;
00844     int tile_index = Map1_1[yloc][xloc];
00845     tiles tile_dead_spot;
00846     tile_dead_spot = tile_array[tile_index];
00847     uLCD.set_sector_address(tile_dead_spot.x,tile_dead_spot.y);
00848     uLCD.display_image(((dead_unit->xloc)*16), ((dead_unit->yloc)*16));    
00849     dead_unit->status = 0;
00850     return;
00851     }
00852     
00853     
00854 void ai_movement(){
00855     int ai_r;
00856     int ai_col;
00857     int notdead;
00858     notdead = 0;
00859     person_t* ai_unit;
00860     for( ai_r = 0; ai_r<8; ai_r++){
00861         for( ai_col = 0; ai_col<8; ai_col++){
00862             if (unitsMap1_1[ai_r][ai_col] > 10){
00863                 notdead = 1;
00864                 ai_unit = person_array[unitsMap1_1[ai_r][ai_col]];
00865                 //find enemy
00866                 //move to enemy
00867                 find_player(ai_unit);
00868                 
00869                 
00870             }
00871         }
00872     }
00873     if(notdead == 0)
00874         ask_nextstage();
00875 }
00876 
00877 void find_player(person_t* ai_pred){
00878     int xloc = ai_pred->xloc;
00879     int yloc = ai_pred->yloc;
00880     person_t* ai_target;
00881     int movement = ai_pred->remain_moves;
00882     int movement_vert;
00883     int movement_horiz;
00884     int y;
00885     int x;
00886     int negy;
00887     int negx;
00888     //parse unit array near me for enemy
00889     for(movement_vert = 0; movement_vert < movement; movement_vert++){
00890         for(movement_horiz = 0; movement_horiz < (movement -movement_vert); movement_horiz++){
00891             y = movement_vert + yloc;
00892             if(y<0)
00893                 y = 0;
00894             if(y>7)
00895                 y=7;
00896             x = movement_horiz + xloc;
00897             if(x<0)
00898                 x = 0;
00899             if(x>7)
00900                 x=7;
00901             negx = xloc - movement_horiz;
00902             if(negx<0)
00903                 negx = 0;
00904             if(negx>7)
00905                 negx=7;
00906             negy = yloc - movement_vert;
00907             if(negy<0)
00908                 negy = 0;
00909             if(negy>7)
00910                 negy=7;
00911             if((unitsMap1_1[y][x]<10) && (unitsMap1_1[y][x] !=0)){
00912                 ai_target = person_array[unitsMap1_1[y][x]];
00913                 hunt_player(ai_pred, ai_target, x, y, xloc, yloc);
00914             }
00915             else if((unitsMap1_1[y][negx]<10) && (unitsMap1_1[y][negx] !=0)){
00916                 ai_target = person_array[unitsMap1_1[y][negx]];
00917                 hunt_player(ai_pred, ai_target, negx, y, xloc, yloc);
00918             }
00919             else if((unitsMap1_1[negy][x]<10) && (unitsMap1_1[negy][x] !=0)){
00920                 ai_target = person_array[unitsMap1_1[negy][x]];
00921                 hunt_player(ai_pred, ai_target, x, negy, xloc, yloc);
00922             }
00923             else if((unitsMap1_1[negy][negx]<10) && (unitsMap1_1[negy][negx] !=0)){
00924                 ai_target = person_array[unitsMap1_1[negy][negx]];
00925                 hunt_player(ai_pred, ai_target, negx, negy, xloc, yloc);
00926             }
00927         }
00928     }
00929 }
00930 
00931 
00932 //move to prey
00933 void hunt_player(person_t* predator, person_t* prey, int prey_x, int prey_y, int predator_x, int predator_y){
00934     int keypad;
00935     int count = 0;
00936     while (count <12){
00937         count++;
00938         if ((predator_y - prey_y) >0)
00939             keypad = 2;
00940         else if ((predator_y - prey_y) <0)
00941             keypad = 8;
00942         if ((predator_x - prey_x) >0)
00943             keypad = 4;
00944         else if ((predator_x - prey_x) <0)
00945             keypad = 6;
00946         if ( count%2 !=0){
00947             if ((predator_y - prey_y) >0)
00948                 keypad = 2;
00949             else if ((predator_y - prey_y) <0)
00950                 keypad = 8;
00951         }
00952     person_t* User_person;
00953     User_person = predator;
00954     if ((User_person->remain_moves) == 0)
00955     {
00956         return;
00957     }
00958         if ((User_person->FoF) != player_turn)
00959     {
00960         return;
00961     }
00962     int xloc_u = 1;
00963     int yloc_u = 1;
00964     int xlast = 1;
00965     int ylast = 1;
00966     xloc_u = (User_person->xloc);
00967     yloc_u = User_person->yloc;
00968     xlast = User_person->xlast;
00969     ylast = User_person->ylast;
00970         switch (keypad)
00971         {
00972         case 1:
00973             xloc_u = xloc_u -1;
00974             yloc_u = yloc_u +1;
00975             break;
00976         case 2:
00977             yloc_u = yloc_u +1;
00978             break;
00979         case 3:
00980             xloc_u = xloc_u +1;
00981             yloc_u = yloc_u +1;
00982             break;
00983         case 4:
00984             xloc_u = xloc_u -1;
00985             break;
00986         case 5:
00987             break;
00988         case 6:
00989             xloc_u = xloc_u +1;
00990             break;
00991         case 7:
00992             xloc_u = xloc_u -1;
00993             yloc_u = yloc_u -1;
00994             break;
00995         case 8:
00996             yloc_u = yloc_u -1;
00997             break;
00998         case 9:
00999             xloc_u = xloc_u + 1;
01000             yloc_u = yloc_u - 1;
01001             break;
01002         }
01003         if (yloc_u <0)
01004             yloc_u = 0;
01005         if (xloc_u <0)
01006             xloc_u = 0;
01007         if (yloc_u >7)
01008             yloc_u = 7;
01009         if (xloc_u >7)
01010             xloc_u = 7;
01011         int x_change = 0;
01012         if (xloc_u != xlast)
01013             x_change = 1;
01014         int y_change = 0;
01015         if (yloc_u != ylast)
01016             y_change = 1;
01017         if (((User_person->remain_moves)-(y_change+x_change))<0){
01018             xloc_u = xlast;
01019             yloc_u = ylast;
01020             x_change = 0;
01021             y_change = 0;
01022             }
01023         
01024         
01025         int unit_index = unitsMap1_1[yloc_u][xloc_u];
01026         int unit_index_R = 0;  
01027         int alliance = 3;
01028         if ((User_person->job.rng == 2)&&((xloc_u!=xlast)||(yloc_u!=ylast))){
01029             int yloc_R = yloc_u + (yloc_u - ylast);
01030             int xloc_R = xloc_u + (xloc_u - xlast);
01031             if ((xloc_R <=7)&&(xloc_R>=0)&&(yloc_R>=0)&&(yloc_R <=7)){
01032                 unit_index_R = unitsMap1_1[yloc_u][xloc_u]; 
01033                 if (unit_index_R != 0){
01034                     person_t* target_R;
01035                     target_R = person_array[unit_index_R];
01036                     alliance = target_R->FoF;
01037                     if(((xloc_u!=xlast)||(yloc_u!=ylast))&& (alliance != (User_person-> FoF)))
01038                     {
01039                         combat(User_person, target_R);
01040                         User_person->remain_moves = 0;
01041                         return;
01042                     }
01043                 }
01044             }
01045         }
01046         if (unit_index != 0){
01047             person_t* target;
01048             target = person_array[unit_index];
01049             alliance = target->FoF;
01050             if (User_person->job.rng == 2){
01051                 m.speed(1.0);
01052                 xloc_u = xlast;
01053                 yloc_u = ylast;
01054                 x_change = 0;
01055                 y_change = 0;
01056                 wait(1.0);
01057                 m.speed(0.0);
01058                 }
01059             else if ((alliance ==player_turn)&&(User_person->job.attack != staff)){
01060                 m.speed(1.0);
01061                 xloc_u = xlast;
01062                 yloc_u = ylast;
01063                 x_change = 0;
01064                 y_change = 0;
01065                 wait(1.0);
01066                 m.speed(0.0);
01067                 }
01068             else if(((xloc_u!=xlast)||(yloc_u!=ylast))&& (alliance != (User_person-> FoF)))
01069             {
01070                 combat(User_person, target);
01071                 User_person->remain_moves = 0;
01072                 return;
01073             }
01074             else if(((xloc_u!=xlast)||(yloc_u!=ylast))&& ((User_person->job.attack == staff)&&(alliance == (User_person->FoF))))
01075             {
01076                 heal(User_person, target);
01077                 User_person->remain_moves = 0;
01078                 return;
01079             }
01080         }  
01081         int tile_index = Map1_1[yloc_u][xloc_u];
01082         if (tile_index == 0){
01083             xloc_u = xlast;
01084             yloc_u = ylast;
01085             x_change = 0;
01086             y_change = 0;
01087         }      
01088         
01089         if((xlast != xloc_u) || (ylast != yloc_u)){
01090                 tile_index = Map1_1[ylast][xlast];
01091                 unitsMap1_1[ylast][xlast] = 0;
01092                 unitsMap1_1[yloc_u][xloc_u] = User_person->v;
01093                 tiles tile_erased = tile_array[tile_index];
01094                 if (tile_index == 0){
01095                     uLCD.set_sector_address(tile_erased.x, tile_erased.y);
01096                     uLCD.display_image(0, 0);
01097                     }
01098                 else{
01099                     uLCD.set_sector_address(tile_erased.x, tile_erased.y);
01100                     uLCD.display_image(xlast*16, ylast*16);
01101                     }
01102                 uLCD.set_sector_address(User_person->job.x,User_person->job.y);
01103                 User_person->xloc = xloc_u;
01104                 User_person->yloc = yloc_u;
01105                 User_person->xlast = xloc_u;
01106                 User_person->ylast = yloc_u;
01107                 User_person->remain_moves = (User_person->remain_moves) - x_change - y_change;
01108                 uLCD.display_image(((User_person->xloc)*16), ((User_person->yloc)*16));    
01109         }    
01110     }    
01111     return;
01112 }
01113 
01114 
01115 
01116 
01117 void ask_nextstage(){
01118     pc.putc(0);
01119     pc.putc(12);
01120     pc.putc(1);
01121     wait(.2);
01122     pc.putc(stagenumber);
01123     person_t* save_send_person;
01124     for(int i = 1; i<12; i++)
01125     {
01126         save_send_person = person_array[i];
01127         pc.putc(save_send_person->status);
01128     }
01129     player_turn = 1;
01130     state = 0;
01131     //load next map
01132     //init new map
01133     while (units_received == 0){
01134         waitForAck();
01135     }
01136     load_floor(stagenumber);
01137     load_units_map();
01138     
01139     
01140     return;    
01141 }
01142 
01143 void load_units_map(){
01144     int y_num;
01145     int x_num;
01146     int castle_drawn;
01147     castle_drawn = 0;
01148     Julio_Mbed.putc(2);
01149     Julio_Mbed.putc(stagenumber);
01150     tiles tile_draw; 
01151     person_t* draw_unit;
01152     //draw map
01153     for( y_num = 0; y_num<8; y_num++){
01154         for( x_num = 0; x_num<8; x_num++){
01155             tile_draw = tile_array[Map1_1[y_num][x_num]];
01156             if(Map1_1[y_num][x_num]!=0){
01157             uLCD.set_sector_address(tile_draw.x,tile_draw.y);
01158             uLCD.display_image((16*x_num),(16*y_num));
01159             }
01160             else if((Map1_1[y_num][x_num]==0)&&(castle_drawn==0)){
01161             uLCD.set_sector_address(tile_draw.x,tile_draw.y);
01162             uLCD.display_image((16*x_num),(16*y_num));
01163             castle_drawn = 1;  
01164             }
01165         }
01166     }
01167     //load and draw units on the map
01168     for( y_num = 0; y_num<8; y_num++){
01169         for( x_num = 0; x_num<8; x_num++){
01170             if(unitsMap1_1[y_num][x_num] != 0){
01171                draw_unit = person_array[unitsMap1_1[y_num][x_num]];
01172                draw_unit->xloc = x_num;
01173                draw_unit->yloc = y_num;
01174                draw_unit->xlast = x_num;
01175                draw_unit->ylast = y_num;
01176                draw_unit->health = draw_unit->job.maxHealth;
01177                draw_unit->remain_moves = draw_unit->job.movement;
01178                draw_unit->status = 1;
01179                uLCD.set_sector_address(draw_unit->job.x,draw_unit->job.y);
01180                uLCD.display_image((16*x_num),(16*y_num));
01181             }
01182         }
01183     }
01184     
01185     
01186     
01187     units_received=0;
01188     return;    
01189 }
01190 
01191 
01192  
01193 void sendMap()
01194 {
01195     for(int i = 0; i < 8; i++) 
01196     {
01197         for(int j = 0; j < 8; j++)
01198         {
01199             pc.putc(Map1_1[i][j]);
01200         }
01201     }
01202 }
01203  
01204 void sendUnits()
01205 {
01206     for(int a = 0; a < 8; a++) 
01207     {
01208         for(int b = 0; b < 8; b++)
01209         {
01210             pc.putc(unitsMap1_1[a][b]);
01211         }
01212     }
01213 }
01214  
01215 void receiveMap()
01216 {
01217     for(int c = 0; c < 8; c++)
01218     {
01219         for(int d = 0; d < 8; d++)
01220         {
01221             while(!pc.readable()){}
01222             Map1_1[c][d] = pc.getc();
01223         }   
01224     }
01225 }
01226  
01227 void receiveUnits()
01228 {
01229     for(int c = 0; c < 8; c++)
01230     {
01231         for(int d = 0; d < 8; d++)
01232         {
01233             while(!pc.readable()){}
01234             unitsMap1_1[c][d] = pc.getc();
01235         }   
01236     }
01237     units_received = 1;
01238     for(int q = 0; q < 8; q++)
01239     {
01240         for(int w = 0; w < 8; w++)
01241         { 
01242             uLCD.printf("%d", Map1_1[q][w]);
01243         }   
01244     }
01245 }
01246  
01247 void waitForAck()
01248 {
01249     char command[2];
01250     command[0] = '0';
01251     led3=!led3;
01252     while(pc.readable())
01253     {
01254         led4=!led4;
01255         command[0] = pc.getc();
01256         if(command[0] == 'm')
01257         {
01258             led1 = !led1;
01259             sendMap();
01260         }
01261         if(command[0] == 'u')
01262         {
01263             sendUnits();
01264             led1 = !led1;
01265             
01266             //uLCD.printf("enter combat phase");
01267             //combatPhase();
01268         }
01269         if(command[0] == 's')
01270         {            
01271             receiveMap();
01272             led2 = !led2;
01273         }
01274         if(command[0] == 'z')
01275         {
01276             receiveUnits();
01277             led2 = !led2;
01278         }
01279         if(command[0] == 'f'){
01280             //loadFile();
01281             led3 = !led3;
01282         }
01283     }
01284 }
01285  
01286  
01287 void combatPhase(char a, char d)
01288 {
01289     pc.putc('1');
01290     pc.putc(a);
01291     pc.putc(d);
01292     led4 = 1;
01293     return;
01294 }