CGameState.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "CGameState.h"
  2. #include "CGameInterface.h"
  3. #include "CPlayerInterface.h"
  4. #include <algorithm>
  5. #include "SDL_Thread.h"
  6. #include "SDL_Extensions.h"
  7. class CMP_stack
  8. {
  9. public:
  10. bool operator ()(const CStack* a, const CStack* b)
  11. {
  12. return (a->creature->speed)<(b->creature->speed);
  13. }
  14. } cmpst ;
  15. void CGameState::battle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CArmedInstance *hero1, CArmedInstance *hero2)
  16. {
  17. curB = new BattleInfo();
  18. std::vector<CStack*> & stacks = (curB->stacks);
  19. curB->army1=army1;
  20. curB->army2=army2;
  21. curB->hero1=dynamic_cast<CGHeroInstance*>(hero1);
  22. curB->hero2=dynamic_cast<CGHeroInstance*>(hero2);
  23. curB->side1=(hero1)?(hero1->tempOwner):(-1);
  24. curB->side2=(hero2)?(hero2->tempOwner):(-1);
  25. curB->round = -2;
  26. for(std::map<int,std::pair<CCreature*,int> >::iterator i = army1->slots.begin(); i!=army1->slots.end(); i++)
  27. stacks.push_back(new CStack(i->second.first,i->second.second,0, stacks.size()));
  28. //initialization of positions
  29. switch(army1->slots.size()) //for attacker
  30. {
  31. case 0:
  32. break;
  33. case 1:
  34. stacks[0]->position = 86; //6
  35. break;
  36. case 2:
  37. stacks[0]->position = 35; //3
  38. stacks[1]->position = 137; //9
  39. break;
  40. case 3:
  41. stacks[0]->position = 35; //3
  42. stacks[1]->position = 86; //6
  43. stacks[2]->position = 137; //9
  44. break;
  45. case 4:
  46. stacks[0]->position = 1; //1
  47. stacks[1]->position = 69; //5
  48. stacks[2]->position = 103; //7
  49. stacks[3]->position = 171; //11
  50. break;
  51. case 5:
  52. stacks[0]->position = 1; //1
  53. stacks[1]->position = 35; //3
  54. stacks[2]->position = 86; //6
  55. stacks[3]->position = 137; //9
  56. stacks[4]->position = 171; //11
  57. break;
  58. case 6:
  59. stacks[0]->position = 1; //1
  60. stacks[1]->position = 35; //3
  61. stacks[2]->position = 69; //5
  62. stacks[3]->position = 103; //7
  63. stacks[4]->position = 137; //9
  64. stacks[5]->position = 171; //11
  65. break;
  66. case 7:
  67. stacks[0]->position = 1; //1
  68. stacks[1]->position = 35; //3
  69. stacks[2]->position = 69; //5
  70. stacks[3]->position = 86; //6
  71. stacks[4]->position = 103; //7
  72. stacks[5]->position = 137; //9
  73. stacks[6]->position = 171; //11
  74. break;
  75. default: //fault
  76. break;
  77. }
  78. for(std::map<int,std::pair<CCreature*,int> >::iterator i = army2->slots.begin(); i!=army2->slots.end(); i++)
  79. stacks.push_back(new CStack(i->second.first,i->second.second,1, stacks.size()));
  80. switch(army2->slots.size()) //for attacker
  81. {
  82. case 0:
  83. break;
  84. case 1:
  85. stacks[0+army1->slots.size()]->position = 100; //6
  86. break;
  87. case 2:
  88. stacks[0+army1->slots.size()]->position = 49; //3
  89. stacks[1+army1->slots.size()]->position = 151; //9
  90. break;
  91. case 3:
  92. stacks[0+army1->slots.size()]->position = 49; //3
  93. stacks[1+army1->slots.size()]->position = 100; //6
  94. stacks[2+army1->slots.size()]->position = 151; //9
  95. break;
  96. case 4:
  97. stacks[0+army1->slots.size()]->position = 15; //1
  98. stacks[1+army1->slots.size()]->position = 83; //5
  99. stacks[2+army1->slots.size()]->position = 117; //7
  100. stacks[3+army1->slots.size()]->position = 185; //11
  101. break;
  102. case 5:
  103. stacks[0+army1->slots.size()]->position = 15; //1
  104. stacks[1+army1->slots.size()]->position = 49; //3
  105. stacks[2+army1->slots.size()]->position = 100; //6
  106. stacks[3+army1->slots.size()]->position = 151; //9
  107. stacks[4+army1->slots.size()]->position = 185; //11
  108. break;
  109. case 6:
  110. stacks[0+army1->slots.size()]->position = 15; //1
  111. stacks[1+army1->slots.size()]->position = 49; //3
  112. stacks[2+army1->slots.size()]->position = 83; //5
  113. stacks[3+army1->slots.size()]->position = 117; //7
  114. stacks[4+army1->slots.size()]->position = 151; //9
  115. stacks[5+army1->slots.size()]->position = 185; //11
  116. break;
  117. case 7:
  118. stacks[0+army1->slots.size()]->position = 15; //1
  119. stacks[1+army1->slots.size()]->position = 49; //3
  120. stacks[2+army1->slots.size()]->position = 83; //5
  121. stacks[3+army1->slots.size()]->position = 100; //6
  122. stacks[4+army1->slots.size()]->position = 117; //7
  123. stacks[5+army1->slots.size()]->position = 151; //9
  124. stacks[6+army1->slots.size()]->position = 185; //11
  125. break;
  126. default: //fault
  127. break;
  128. }
  129. std::stable_sort(stacks.begin(),stacks.end(),cmpst);
  130. //for start inform players about battle
  131. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
  132. {
  133. if (j->first > PLAYER_LIMIT)
  134. break;
  135. if(j->second.fogOfWarMap[tile.x][tile.y][tile.z])
  136. { //player should be notified
  137. tribool side = tribool::indeterminate_value;
  138. if(j->first == curB->side1) //player is attacker
  139. side = false;
  140. else if(j->first == curB->side2) //player is defender
  141. side = true;
  142. else
  143. return; //no witnesses
  144. if(CGI->playerint[j->second.serial]->human)
  145. {
  146. ((CPlayerInterface*)( CGI->playerint[j->second.serial] ))->battleStart(army1, army2, tile, curB->hero1, curB->hero2, side, curB->stacks);
  147. }
  148. else
  149. {
  150. //CGI->playerint[j->second.serial]->battleStart(army1, army2, tile, curB->hero1, curB->hero2, side);
  151. }
  152. }
  153. }
  154. curB->round++;
  155. if( (curB->hero1 && curB->hero1->getSecSkillLevel(19)>=0) || ( curB->hero2 && curB->hero2->getSecSkillLevel(19)>=0) )//someone has tactics
  156. {
  157. //TODO: wywolania dla rundy -1, ograniczenie pola ruchu, etc
  158. }
  159. curB->round++;
  160. //SDL_Thread * eventh = SDL_CreateThread(battleEventThread, NULL);
  161. while(true) //do zwyciestwa jednej ze stron
  162. {
  163. for(int i=0;i<stacks.size();i++)
  164. {
  165. curB->activeStack = i;
  166. if(stacks[i]->alive) //niech interfejs ruszy oddzialem
  167. {
  168. unsigned char owner = (stacks[i]->owner)?(hero2->tempOwner):(hero1->tempOwner);
  169. unsigned char serialOwner = -1;
  170. for(int g=0; g<CGI->playerint.size(); ++g)
  171. {
  172. if(CGI->playerint[g]->playerID == owner)
  173. {
  174. serialOwner = g;
  175. break;
  176. }
  177. }
  178. if(CGI->playerint[serialOwner]->human)
  179. {
  180. ((CPlayerInterface*)CGI->playerint[serialOwner])->activeStack(stacks[i]->ID);
  181. }
  182. else
  183. {
  184. //CGI->playerint[serialOwner]->activeStack(stacks[i]->ID);
  185. }
  186. }
  187. //sprawdzic czy po tej akcji ktoras strona nie wygrala bitwy
  188. }
  189. SDL_Delay(50);
  190. }
  191. for(int i=0;i<stacks.size();i++)
  192. delete stacks[i];
  193. delete curB;
  194. curB = NULL;
  195. }