CGameState.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. //selecting moved stack
  203. CStack * curStack = NULL;
  204. for(int y=0; y<curB->stacks.size(); ++y)
  205. {
  206. if(curB->stacks[y]->ID == ID)
  207. {
  208. curStack = curB->stacks[y];
  209. break;
  210. }
  211. }
  212. if(!curStack)
  213. return false;
  214. //initing necessary tables
  215. bool accessibility[187]; //accesibility of hexes
  216. for(int k=0; k<187; k++)
  217. accessibility[k] = true;
  218. for(int g=0; g<curB->stacks.size(); ++g)
  219. {
  220. accessibility[curB->stacks[g]->position] = false;
  221. }
  222. int predecessor[187]; //for getting the Path
  223. for(int b=0; b<187; ++b)
  224. predecessor[b] = -1;
  225. //bfsing
  226. int dists[187]; //calculated distances
  227. std::queue<int> hexq; //bfs queue
  228. hexq.push(curStack->position);
  229. for(int g=0; g<187; ++g)
  230. dists[g] = 100000000;
  231. dists[hexq.front()] = 0;
  232. int curNext = -1; //for bfs loop only (helper var)
  233. while(!hexq.empty()) //bfs loop
  234. {
  235. int curHex = hexq.front();
  236. hexq.pop();
  237. curNext = curHex - ( (curHex/17)%2 ? 17 : 18 );
  238. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
  239. {
  240. hexq.push(curNext);
  241. dists[curNext] = dists[curHex] + 1;
  242. predecessor[curNext] = curHex;
  243. }
  244. curNext = curHex - ( (curHex/17)%2 ? 16 : 17 );
  245. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
  246. {
  247. hexq.push(curNext);
  248. dists[curNext] = dists[curHex] + 1;
  249. predecessor[curNext] = curHex;
  250. }
  251. curNext = curHex - 1;
  252. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
  253. {
  254. hexq.push(curNext);
  255. dists[curNext] = dists[curHex] + 1;
  256. predecessor[curNext] = curHex;
  257. }
  258. curNext = curHex + 1;
  259. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
  260. {
  261. hexq.push(curNext);
  262. dists[curNext] = dists[curHex] + 1;
  263. predecessor[curNext] = curHex;
  264. }
  265. curNext = curHex + ( (curHex/17)%2 ? 16 : 17 );
  266. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
  267. {
  268. hexq.push(curNext);
  269. dists[curNext] = dists[curHex] + 1;
  270. predecessor[curNext] = curHex;
  271. }
  272. curNext = curHex + ( (curHex/17)%2 ? 17 : 18 );
  273. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
  274. {
  275. hexq.push(curNext);
  276. dists[curNext] = dists[curHex] + 1;
  277. predecessor[curNext] = curHex;
  278. }
  279. }
  280. //following the Path
  281. if(dists[dest] > curStack->creature->speed)
  282. return false;
  283. std::vector<int> path;
  284. int curElem = dest;
  285. while(curElem!=curStack->position)
  286. {
  287. path.push_back(curElem);
  288. curElem = predecessor[curElem];
  289. }
  290. for(int v=path.size()-1; v>=0; --v)
  291. {
  292. LOCPLINT->battleStackMoved(ID, path[v]);
  293. }
  294. return true;
  295. }