CGameState.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. curB->stackActionPerformed = false;
  28. for(std::map<int,std::pair<CCreature*,int> >::iterator i = army1->slots.begin(); i!=army1->slots.end(); i++)
  29. {
  30. stacks.push_back(new CStack(i->second.first,i->second.second,0, stacks.size(), true));
  31. stacks[stacks.size()-1]->ID = stacks.size()-1;
  32. }
  33. //initialization of positions
  34. switch(army1->slots.size()) //for attacker
  35. {
  36. case 0:
  37. break;
  38. case 1:
  39. stacks[0]->position = 86; //6
  40. break;
  41. case 2:
  42. stacks[0]->position = 35; //3
  43. stacks[1]->position = 137; //9
  44. break;
  45. case 3:
  46. stacks[0]->position = 35; //3
  47. stacks[1]->position = 86; //6
  48. stacks[2]->position = 137; //9
  49. break;
  50. case 4:
  51. stacks[0]->position = 1; //1
  52. stacks[1]->position = 69; //5
  53. stacks[2]->position = 103; //7
  54. stacks[3]->position = 171; //11
  55. break;
  56. case 5:
  57. stacks[0]->position = 1; //1
  58. stacks[1]->position = 35; //3
  59. stacks[2]->position = 86; //6
  60. stacks[3]->position = 137; //9
  61. stacks[4]->position = 171; //11
  62. break;
  63. case 6:
  64. stacks[0]->position = 1; //1
  65. stacks[1]->position = 35; //3
  66. stacks[2]->position = 69; //5
  67. stacks[3]->position = 103; //7
  68. stacks[4]->position = 137; //9
  69. stacks[5]->position = 171; //11
  70. break;
  71. case 7:
  72. stacks[0]->position = 1; //1
  73. stacks[1]->position = 35; //3
  74. stacks[2]->position = 69; //5
  75. stacks[3]->position = 86; //6
  76. stacks[4]->position = 103; //7
  77. stacks[5]->position = 137; //9
  78. stacks[6]->position = 171; //11
  79. break;
  80. default: //fault
  81. break;
  82. }
  83. for(std::map<int,std::pair<CCreature*,int> >::iterator i = army2->slots.begin(); i!=army2->slots.end(); i++)
  84. stacks.push_back(new CStack(i->second.first,i->second.second,1, stacks.size(), false));
  85. switch(army2->slots.size()) //for defender
  86. {
  87. case 0:
  88. break;
  89. case 1:
  90. stacks[0+army1->slots.size()]->position = 100; //6
  91. break;
  92. case 2:
  93. stacks[0+army1->slots.size()]->position = 49; //3
  94. stacks[1+army1->slots.size()]->position = 151; //9
  95. break;
  96. case 3:
  97. stacks[0+army1->slots.size()]->position = 49; //3
  98. stacks[1+army1->slots.size()]->position = 100; //6
  99. stacks[2+army1->slots.size()]->position = 151; //9
  100. break;
  101. case 4:
  102. stacks[0+army1->slots.size()]->position = 15; //1
  103. stacks[1+army1->slots.size()]->position = 83; //5
  104. stacks[2+army1->slots.size()]->position = 117; //7
  105. stacks[3+army1->slots.size()]->position = 185; //11
  106. break;
  107. case 5:
  108. stacks[0+army1->slots.size()]->position = 15; //1
  109. stacks[1+army1->slots.size()]->position = 49; //3
  110. stacks[2+army1->slots.size()]->position = 100; //6
  111. stacks[3+army1->slots.size()]->position = 151; //9
  112. stacks[4+army1->slots.size()]->position = 185; //11
  113. break;
  114. case 6:
  115. stacks[0+army1->slots.size()]->position = 15; //1
  116. stacks[1+army1->slots.size()]->position = 49; //3
  117. stacks[2+army1->slots.size()]->position = 83; //5
  118. stacks[3+army1->slots.size()]->position = 117; //7
  119. stacks[4+army1->slots.size()]->position = 151; //9
  120. stacks[5+army1->slots.size()]->position = 185; //11
  121. break;
  122. case 7:
  123. stacks[0+army1->slots.size()]->position = 15; //1
  124. stacks[1+army1->slots.size()]->position = 49; //3
  125. stacks[2+army1->slots.size()]->position = 83; //5
  126. stacks[3+army1->slots.size()]->position = 100; //6
  127. stacks[4+army1->slots.size()]->position = 117; //7
  128. stacks[5+army1->slots.size()]->position = 151; //9
  129. stacks[6+army1->slots.size()]->position = 185; //11
  130. break;
  131. default: //fault
  132. break;
  133. }
  134. for(int g=0; g<stacks.size(); ++g) //shifting positions of two-hex creatures
  135. {
  136. if((stacks[g]->position%17)==1 && stacks[g]->creature->isDoubleWide())
  137. {
  138. stacks[g]->position += 1;
  139. }
  140. else if((stacks[g]->position%17)==15 && stacks[g]->creature->isDoubleWide())
  141. {
  142. stacks[g]->position -= 1;
  143. }
  144. }
  145. std::stable_sort(stacks.begin(),stacks.end(),cmpst);
  146. //for start inform players about battle
  147. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
  148. {
  149. if (j->first > PLAYER_LIMIT)
  150. break;
  151. if(j->second.fogOfWarMap[tile.x][tile.y][tile.z])
  152. { //player should be notified
  153. tribool side = tribool::indeterminate_value;
  154. if(j->first == curB->side1) //player is attacker
  155. side = false;
  156. else if(j->first == curB->side2) //player is defender
  157. side = true;
  158. else
  159. return; //no witnesses
  160. if(CGI->playerint[j->second.serial]->human)
  161. {
  162. ((CPlayerInterface*)( CGI->playerint[j->second.serial] ))->battleStart(army1, army2, tile, curB->hero1, curB->hero2, side);
  163. }
  164. else
  165. {
  166. //CGI->playerint[j->second.serial]->battleStart(army1, army2, tile, curB->hero1, curB->hero2, side);
  167. }
  168. }
  169. }
  170. curB->round++;
  171. if( (curB->hero1 && curB->hero1->getSecSkillLevel(19)>=0) || ( curB->hero2 && curB->hero2->getSecSkillLevel(19)>=0) )//someone has tactics
  172. {
  173. //TODO: wywolania dla rundy -1, ograniczenie pola ruchu, etc
  174. }
  175. curB->round++;
  176. //SDL_Thread * eventh = SDL_CreateThread(battleEventThread, NULL);
  177. while(true) //do zwyciestwa jednej ze stron
  178. {
  179. for(int i=0;i<stacks.size();i++)
  180. {
  181. curB->activeStack = i;
  182. curB->stackActionPerformed = false;
  183. if(stacks[i]->alive) //niech interfejs ruszy oddzialem
  184. {
  185. unsigned char owner = (stacks[i]->owner)?(hero2 ? hero2->tempOwner : 255):(hero1->tempOwner);
  186. unsigned char serialOwner = -1;
  187. for(int g=0; g<CGI->playerint.size(); ++g)
  188. {
  189. if(CGI->playerint[g]->playerID == owner)
  190. {
  191. serialOwner = g;
  192. break;
  193. }
  194. }
  195. if(serialOwner==255) //neutral unit
  196. {
  197. }
  198. else if(CGI->playerint[serialOwner]->human)
  199. {
  200. ((CPlayerInterface*)CGI->playerint[serialOwner])->activeStack(stacks[i]->ID);
  201. }
  202. else
  203. {
  204. //CGI->playerint[serialOwner]->activeStack(stacks[i]->ID);
  205. }
  206. }
  207. //sprawdzic czy po tej akcji ktoras strona nie wygrala bitwy
  208. }
  209. SDL_Delay(50);
  210. }
  211. for(int i=0;i<stacks.size();i++)
  212. delete stacks[i];
  213. delete curB;
  214. curB = NULL;
  215. }
  216. bool CGameState::battleMoveCreatureStack(int ID, int dest)
  217. {
  218. //first checks
  219. if(curB->stackActionPerformed) //because unit cannot be moved more than once
  220. return false;
  221. bool stackAtEnd = false; //true if there is a stack at the end of the path (we should attack it)
  222. unsigned char owner = -1; //owner moved of unit
  223. for(int g=0; g<curB->stacks.size(); ++g)
  224. {
  225. if(curB->stacks[g]->position == dest)
  226. {
  227. stackAtEnd = true;
  228. break;
  229. }
  230. }
  231. for(int g=0; g<curB->stacks.size(); ++g)
  232. {
  233. if(curB->stacks[g]->ID == ID)
  234. {
  235. owner = curB->stacks[g]->owner;
  236. break;
  237. }
  238. }
  239. //selecting moved stack
  240. CStack * curStack = NULL;
  241. for(int y=0; y<curB->stacks.size(); ++y)
  242. {
  243. if(curB->stacks[y]->ID == ID)
  244. {
  245. curStack = curB->stacks[y];
  246. break;
  247. }
  248. }
  249. if(!curStack)
  250. return false;
  251. //initing necessary tables
  252. bool accessibility[187]; //accesibility of hexes
  253. for(int k=0; k<187; k++)
  254. accessibility[k] = true;
  255. for(int g=0; g<curB->stacks.size(); ++g)
  256. {
  257. if(curB->stacks[g]->owner == owner) //we don't want to lock enemy's positions
  258. {
  259. accessibility[curB->stacks[g]->position] = false;
  260. if(curB->stacks[g]->creature->isDoubleWide()) //if it's a double hex creature
  261. {
  262. if(curB->stacks[g]->attackerOwned)
  263. accessibility[curB->stacks[g]->position-1] = false;
  264. else
  265. accessibility[curB->stacks[g]->position+1] = false;
  266. }
  267. }
  268. }
  269. int predecessor[187]; //for getting the Path
  270. for(int b=0; b<187; ++b)
  271. predecessor[b] = -1;
  272. //bfsing
  273. int dists[187]; //calculated distances
  274. std::queue<int> hexq; //bfs queue
  275. hexq.push(curStack->position);
  276. for(int g=0; g<187; ++g)
  277. dists[g] = 100000000;
  278. dists[hexq.front()] = 0;
  279. int curNext = -1; //for bfs loop only (helper var)
  280. while(!hexq.empty()) //bfs loop
  281. {
  282. int curHex = hexq.front();
  283. hexq.pop();
  284. curNext = curHex - ( (curHex/17)%2 ? 18 : 17 );
  285. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
  286. {
  287. hexq.push(curNext);
  288. dists[curNext] = dists[curHex] + 1;
  289. predecessor[curNext] = curHex;
  290. }
  291. curNext = curHex - ( (curHex/17)%2 ? 17 : 16 );
  292. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
  293. {
  294. hexq.push(curNext);
  295. dists[curNext] = dists[curHex] + 1;
  296. predecessor[curNext] = curHex;
  297. }
  298. curNext = curHex - 1;
  299. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
  300. {
  301. hexq.push(curNext);
  302. dists[curNext] = dists[curHex] + 1;
  303. predecessor[curNext] = curHex;
  304. }
  305. curNext = curHex + 1;
  306. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
  307. {
  308. hexq.push(curNext);
  309. dists[curNext] = dists[curHex] + 1;
  310. predecessor[curNext] = curHex;
  311. }
  312. curNext = curHex + ( (curHex/17)%2 ? 16 : 17 );
  313. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
  314. {
  315. hexq.push(curNext);
  316. dists[curNext] = dists[curHex] + 1;
  317. predecessor[curNext] = curHex;
  318. }
  319. curNext = curHex + ( (curHex/17)%2 ? 17 : 18 );
  320. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
  321. {
  322. hexq.push(curNext);
  323. dists[curNext] = dists[curHex] + 1;
  324. predecessor[curNext] = curHex;
  325. }
  326. }
  327. //following the Path
  328. if(dists[dest] > curStack->creature->speed)
  329. return false;
  330. std::vector<int> path;
  331. int curElem = dest;
  332. while(curElem!=curStack->position)
  333. {
  334. path.push_back(curElem);
  335. curElem = predecessor[curElem];
  336. }
  337. for(int v=path.size()-1; v>=0; --v)
  338. {
  339. if(v!=0 || !stackAtEnd) //it's not the last step
  340. {
  341. LOCPLINT->battleStackMoved(ID, path[v], v==path.size()-1, v==0);
  342. curStack->position = path[v];
  343. }
  344. else //if it's last step and we should attack unit at the end
  345. {
  346. LOCPLINT->battleStackAttacking(ID, path[v]);
  347. }
  348. }
  349. curB->stackActionPerformed = true;
  350. LOCPLINT->actionFinished(BattleAction());
  351. return true;
  352. }
  353. std::vector<int> CGameState::battleGetRange(int ID)
  354. {
  355. int initialPlace=-1; //position of unit
  356. int radius=-1; //range of unit
  357. unsigned char owner = -1; //owner of unit
  358. for(int g=0; g<curB->stacks.size(); ++g)
  359. {
  360. if(curB->stacks[g]->ID == ID)
  361. {
  362. initialPlace = curB->stacks[g]->position;
  363. radius = curB->stacks[g]->creature->speed;
  364. owner = curB->stacks[g]->owner;
  365. break;
  366. }
  367. }
  368. bool accessibility[187]; //accesibility of hexes
  369. for(int k=0; k<187; k++)
  370. accessibility[k] = true;
  371. for(int g=0; g<curB->stacks.size(); ++g)
  372. {
  373. if(curB->stacks[g]->owner == owner) //we don't want to lock enemy's positions
  374. {
  375. accessibility[curB->stacks[g]->position] = false;
  376. if(curB->stacks[g]->creature->isDoubleWide()) //if it's a double hex creature
  377. {
  378. if(curB->stacks[g]->attackerOwned)
  379. accessibility[curB->stacks[g]->position-1] = false;
  380. else
  381. accessibility[curB->stacks[g]->position+1] = false;
  382. }
  383. }
  384. }
  385. int dists[187]; //calculated distances
  386. std::queue<int> hexq; //bfs queue
  387. hexq.push(initialPlace);
  388. for(int g=0; g<187; ++g)
  389. dists[g] = 100000000;
  390. dists[initialPlace] = 0;
  391. int curNext = -1; //for bfs loop only (helper var)
  392. while(!hexq.empty()) //bfs loop
  393. {
  394. int curHex = hexq.front();
  395. hexq.pop();
  396. curNext = curHex - ( (curHex/17)%2 ? 18 : 17 );
  397. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
  398. {
  399. hexq.push(curNext);
  400. dists[curNext] = dists[curHex] + 1;
  401. }
  402. curNext = curHex - ( (curHex/17)%2 ? 17 : 16 );
  403. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
  404. {
  405. hexq.push(curNext);
  406. dists[curNext] = dists[curHex] + 1;
  407. }
  408. curNext = curHex - 1;
  409. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
  410. {
  411. hexq.push(curNext);
  412. dists[curNext] = dists[curHex] + 1;
  413. }
  414. curNext = curHex + 1;
  415. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
  416. {
  417. hexq.push(curNext);
  418. dists[curNext] = dists[curHex] + 1;
  419. }
  420. curNext = curHex + ( (curHex/17)%2 ? 16 : 17 );
  421. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
  422. {
  423. hexq.push(curNext);
  424. dists[curNext] = dists[curHex] + 1;
  425. }
  426. curNext = curHex + ( (curHex/17)%2 ? 17 : 18 );
  427. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
  428. {
  429. hexq.push(curNext);
  430. dists[curNext] = dists[curHex] + 1;
  431. }
  432. }
  433. std::vector<int> ret;
  434. for(int i=0; i<187; ++i)
  435. {
  436. if(dists[i]<=radius)
  437. {
  438. ret.push_back(i);
  439. }
  440. }
  441. return ret;
  442. }