CGameState.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #include <queue>
  8. class CMP_stack
  9. {
  10. public:
  11. bool operator ()(const CStack* a, const CStack* b)
  12. {
  13. return (a->creature->speed)<(b->creature->speed);
  14. }
  15. } cmpst ;
  16. void CGameState::battle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CArmedInstance *hero1, CArmedInstance *hero2)
  17. {
  18. curB = new BattleInfo();
  19. std::vector<CStack*> & stacks = (curB->stacks);
  20. curB->army1=army1;
  21. curB->army2=army2;
  22. curB->hero1=dynamic_cast<CGHeroInstance*>(hero1);
  23. curB->hero2=dynamic_cast<CGHeroInstance*>(hero2);
  24. curB->side1=(hero1)?(hero1->tempOwner):(-1);
  25. curB->side2=(hero2)?(hero2->tempOwner):(-1);
  26. curB->round = -2;
  27. for(std::map<int,std::pair<CCreature*,int> >::iterator i = army1->slots.begin(); i!=army1->slots.end(); i++)
  28. {
  29. stacks.push_back(new CStack(i->second.first,i->second.second,0, stacks.size()));
  30. stacks[stacks.size()-1]->ID = stacks.size()-1;
  31. }
  32. //initialization of positions
  33. switch(army1->slots.size()) //for attacker
  34. {
  35. case 0:
  36. break;
  37. case 1:
  38. stacks[0]->position = 86; //6
  39. break;
  40. case 2:
  41. stacks[0]->position = 35; //3
  42. stacks[1]->position = 137; //9
  43. break;
  44. case 3:
  45. stacks[0]->position = 35; //3
  46. stacks[1]->position = 86; //6
  47. stacks[2]->position = 137; //9
  48. break;
  49. case 4:
  50. stacks[0]->position = 1; //1
  51. stacks[1]->position = 69; //5
  52. stacks[2]->position = 103; //7
  53. stacks[3]->position = 171; //11
  54. break;
  55. case 5:
  56. stacks[0]->position = 1; //1
  57. stacks[1]->position = 35; //3
  58. stacks[2]->position = 86; //6
  59. stacks[3]->position = 137; //9
  60. stacks[4]->position = 171; //11
  61. break;
  62. case 6:
  63. stacks[0]->position = 1; //1
  64. stacks[1]->position = 35; //3
  65. stacks[2]->position = 69; //5
  66. stacks[3]->position = 103; //7
  67. stacks[4]->position = 137; //9
  68. stacks[5]->position = 171; //11
  69. break;
  70. case 7:
  71. stacks[0]->position = 1; //1
  72. stacks[1]->position = 35; //3
  73. stacks[2]->position = 69; //5
  74. stacks[3]->position = 86; //6
  75. stacks[4]->position = 103; //7
  76. stacks[5]->position = 137; //9
  77. stacks[6]->position = 171; //11
  78. break;
  79. default: //fault
  80. break;
  81. }
  82. for(std::map<int,std::pair<CCreature*,int> >::iterator i = army2->slots.begin(); i!=army2->slots.end(); i++)
  83. stacks.push_back(new CStack(i->second.first,i->second.second,1, stacks.size()));
  84. switch(army2->slots.size()) //for attacker
  85. {
  86. case 0:
  87. break;
  88. case 1:
  89. stacks[0+army1->slots.size()]->position = 100; //6
  90. break;
  91. case 2:
  92. stacks[0+army1->slots.size()]->position = 49; //3
  93. stacks[1+army1->slots.size()]->position = 151; //9
  94. break;
  95. case 3:
  96. stacks[0+army1->slots.size()]->position = 49; //3
  97. stacks[1+army1->slots.size()]->position = 100; //6
  98. stacks[2+army1->slots.size()]->position = 151; //9
  99. break;
  100. case 4:
  101. stacks[0+army1->slots.size()]->position = 15; //1
  102. stacks[1+army1->slots.size()]->position = 83; //5
  103. stacks[2+army1->slots.size()]->position = 117; //7
  104. stacks[3+army1->slots.size()]->position = 185; //11
  105. break;
  106. case 5:
  107. stacks[0+army1->slots.size()]->position = 15; //1
  108. stacks[1+army1->slots.size()]->position = 49; //3
  109. stacks[2+army1->slots.size()]->position = 100; //6
  110. stacks[3+army1->slots.size()]->position = 151; //9
  111. stacks[4+army1->slots.size()]->position = 185; //11
  112. break;
  113. case 6:
  114. stacks[0+army1->slots.size()]->position = 15; //1
  115. stacks[1+army1->slots.size()]->position = 49; //3
  116. stacks[2+army1->slots.size()]->position = 83; //5
  117. stacks[3+army1->slots.size()]->position = 117; //7
  118. stacks[4+army1->slots.size()]->position = 151; //9
  119. stacks[5+army1->slots.size()]->position = 185; //11
  120. break;
  121. case 7:
  122. stacks[0+army1->slots.size()]->position = 15; //1
  123. stacks[1+army1->slots.size()]->position = 49; //3
  124. stacks[2+army1->slots.size()]->position = 83; //5
  125. stacks[3+army1->slots.size()]->position = 100; //6
  126. stacks[4+army1->slots.size()]->position = 117; //7
  127. stacks[5+army1->slots.size()]->position = 151; //9
  128. stacks[6+army1->slots.size()]->position = 185; //11
  129. break;
  130. default: //fault
  131. break;
  132. }
  133. std::stable_sort(stacks.begin(),stacks.end(),cmpst);
  134. //for start inform players about battle
  135. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
  136. {
  137. if (j->first > PLAYER_LIMIT)
  138. break;
  139. if(j->second.fogOfWarMap[tile.x][tile.y][tile.z])
  140. { //player should be notified
  141. tribool side = tribool::indeterminate_value;
  142. if(j->first == curB->side1) //player is attacker
  143. side = false;
  144. else if(j->first == curB->side2) //player is defender
  145. side = true;
  146. else
  147. return; //no witnesses
  148. if(CGI->playerint[j->second.serial]->human)
  149. {
  150. ((CPlayerInterface*)( CGI->playerint[j->second.serial] ))->battleStart(army1, army2, tile, curB->hero1, curB->hero2, side);
  151. }
  152. else
  153. {
  154. //CGI->playerint[j->second.serial]->battleStart(army1, army2, tile, curB->hero1, curB->hero2, side);
  155. }
  156. }
  157. }
  158. curB->round++;
  159. if( (curB->hero1 && curB->hero1->getSecSkillLevel(19)>=0) || ( curB->hero2 && curB->hero2->getSecSkillLevel(19)>=0) )//someone has tactics
  160. {
  161. //TODO: wywolania dla rundy -1, ograniczenie pola ruchu, etc
  162. }
  163. curB->round++;
  164. //SDL_Thread * eventh = SDL_CreateThread(battleEventThread, NULL);
  165. while(true) //do zwyciestwa jednej ze stron
  166. {
  167. for(int i=0;i<stacks.size();i++)
  168. {
  169. curB->activeStack = i;
  170. if(stacks[i]->alive) //niech interfejs ruszy oddzialem
  171. {
  172. unsigned char owner = (stacks[i]->owner)?(hero2->tempOwner):(hero1->tempOwner);
  173. unsigned char serialOwner = -1;
  174. for(int g=0; g<CGI->playerint.size(); ++g)
  175. {
  176. if(CGI->playerint[g]->playerID == owner)
  177. {
  178. serialOwner = g;
  179. break;
  180. }
  181. }
  182. if(CGI->playerint[serialOwner]->human)
  183. {
  184. ((CPlayerInterface*)CGI->playerint[serialOwner])->activeStack(stacks[i]->ID);
  185. }
  186. else
  187. {
  188. //CGI->playerint[serialOwner]->activeStack(stacks[i]->ID);
  189. }
  190. }
  191. //sprawdzic czy po tej akcji ktoras strona nie wygrala bitwy
  192. }
  193. SDL_Delay(50);
  194. }
  195. for(int i=0;i<stacks.size();i++)
  196. delete stacks[i];
  197. delete curB;
  198. curB = NULL;
  199. }
  200. bool CGameState::battleMoveCreatureStack(int ID, int dest)
  201. {
  202. //initing necessary tables
  203. bool accessibility[187]; //accesibility of hexes
  204. for(int k=0; k<187; k++)
  205. accessibility[k] = true;
  206. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  207. {
  208. accessibility[CGI->state->curB->stacks[g]->position] = false;
  209. }
  210. int predecessor[187]; //for getting the Path
  211. for(int b=0; b<187; ++b)
  212. predecessor[b] = -1;
  213. //bfsing
  214. int dists[187]; //calculated distances
  215. std::queue<int> hexq; //bfs queue
  216. hexq.push(CGI->state->curB->stacks[ID]->position);
  217. for(int g=0; g<187; ++g)
  218. dists[g] = 100000000;
  219. dists[hexq.front()] = 0;
  220. int curNext = -1; //for bfs loop only (helper var)
  221. while(!hexq.empty()) //bfs loop
  222. {
  223. int curHex = hexq.front();
  224. hexq.pop();
  225. curNext = curHex - ( (curHex/17)%2 ? 17 : 18 );
  226. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
  227. {
  228. hexq.push(curNext);
  229. dists[curNext] = dists[curHex] + 1;
  230. predecessor[curNext] = curHex;
  231. }
  232. curNext = curHex - ( (curHex/17)%2 ? 16 : 17 );
  233. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
  234. {
  235. hexq.push(curNext);
  236. dists[curNext] = dists[curHex] + 1;
  237. predecessor[curNext] = curHex;
  238. }
  239. curNext = curHex - 1;
  240. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
  241. {
  242. hexq.push(curNext);
  243. dists[curNext] = dists[curHex] + 1;
  244. predecessor[curNext] = curHex;
  245. }
  246. curNext = curHex + 1;
  247. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
  248. {
  249. hexq.push(curNext);
  250. dists[curNext] = dists[curHex] + 1;
  251. predecessor[curNext] = curHex;
  252. }
  253. curNext = curHex + ( (curHex/17)%2 ? 16 : 17 );
  254. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
  255. {
  256. hexq.push(curNext);
  257. dists[curNext] = dists[curHex] + 1;
  258. predecessor[curNext] = curHex;
  259. }
  260. curNext = curHex + ( (curHex/17)%2 ? 17 : 18 );
  261. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
  262. {
  263. hexq.push(curNext);
  264. dists[curNext] = dists[curHex] + 1;
  265. predecessor[curNext] = curHex;
  266. }
  267. }
  268. //following the Path
  269. std::vector<int> path;
  270. int curElem = dest;
  271. while(curElem!=CGI->state->curB->stacks[ID]->position)
  272. {
  273. path.push_back(curElem);
  274. curElem = predecessor[curElem];
  275. }
  276. for(int v=path.size()-1; v>=0; --v)
  277. {
  278. LOCPLINT->battleStackMoved(ID, path[v]);
  279. }
  280. return true;
  281. }