CGameState.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 "CBattleInterface.h" //for CBattleHex
  8. #include <queue>
  9. class CMP_stack
  10. {
  11. public:
  12. bool operator ()(const CStack* a, const CStack* b)
  13. {
  14. return (a->creature->speed)>(b->creature->speed);
  15. }
  16. } cmpst ;
  17. void CGameState::battle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CArmedInstance *hero1, CArmedInstance *hero2)
  18. {
  19. curB = new BattleInfo();
  20. std::vector<CStack*> & stacks = (curB->stacks);
  21. curB->army1=army1;
  22. curB->army2=army2;
  23. curB->hero1=dynamic_cast<CGHeroInstance*>(hero1);
  24. curB->hero2=dynamic_cast<CGHeroInstance*>(hero2);
  25. curB->side1=(hero1)?(hero1->tempOwner):(-1);
  26. curB->side2=(hero2)?(hero2->tempOwner):(-1);
  27. curB->round = -2;
  28. curB->stackActionPerformed = false;
  29. for(std::map<int,std::pair<CCreature*,int> >::iterator i = army1->slots.begin(); i!=army1->slots.end(); i++)
  30. {
  31. stacks.push_back(new CStack(i->second.first,i->second.second,0, stacks.size(), true));
  32. stacks[stacks.size()-1]->ID = stacks.size()-1;
  33. }
  34. //initialization of positions
  35. switch(army1->slots.size()) //for attacker
  36. {
  37. case 0:
  38. break;
  39. case 1:
  40. stacks[0]->position = 86; //6
  41. break;
  42. case 2:
  43. stacks[0]->position = 35; //3
  44. stacks[1]->position = 137; //9
  45. break;
  46. case 3:
  47. stacks[0]->position = 35; //3
  48. stacks[1]->position = 86; //6
  49. stacks[2]->position = 137; //9
  50. break;
  51. case 4:
  52. stacks[0]->position = 1; //1
  53. stacks[1]->position = 69; //5
  54. stacks[2]->position = 103; //7
  55. stacks[3]->position = 171; //11
  56. break;
  57. case 5:
  58. stacks[0]->position = 1; //1
  59. stacks[1]->position = 35; //3
  60. stacks[2]->position = 86; //6
  61. stacks[3]->position = 137; //9
  62. stacks[4]->position = 171; //11
  63. break;
  64. case 6:
  65. stacks[0]->position = 1; //1
  66. stacks[1]->position = 35; //3
  67. stacks[2]->position = 69; //5
  68. stacks[3]->position = 103; //7
  69. stacks[4]->position = 137; //9
  70. stacks[5]->position = 171; //11
  71. break;
  72. case 7:
  73. stacks[0]->position = 1; //1
  74. stacks[1]->position = 35; //3
  75. stacks[2]->position = 69; //5
  76. stacks[3]->position = 86; //6
  77. stacks[4]->position = 103; //7
  78. stacks[5]->position = 137; //9
  79. stacks[6]->position = 171; //11
  80. break;
  81. default: //fault
  82. break;
  83. }
  84. for(std::map<int,std::pair<CCreature*,int> >::iterator i = army2->slots.begin(); i!=army2->slots.end(); i++)
  85. stacks.push_back(new CStack(i->second.first,i->second.second,1, stacks.size(), false));
  86. switch(army2->slots.size()) //for defender
  87. {
  88. case 0:
  89. break;
  90. case 1:
  91. stacks[0+army1->slots.size()]->position = 100; //6
  92. break;
  93. case 2:
  94. stacks[0+army1->slots.size()]->position = 49; //3
  95. stacks[1+army1->slots.size()]->position = 151; //9
  96. break;
  97. case 3:
  98. stacks[0+army1->slots.size()]->position = 49; //3
  99. stacks[1+army1->slots.size()]->position = 100; //6
  100. stacks[2+army1->slots.size()]->position = 151; //9
  101. break;
  102. case 4:
  103. stacks[0+army1->slots.size()]->position = 15; //1
  104. stacks[1+army1->slots.size()]->position = 83; //5
  105. stacks[2+army1->slots.size()]->position = 117; //7
  106. stacks[3+army1->slots.size()]->position = 185; //11
  107. break;
  108. case 5:
  109. stacks[0+army1->slots.size()]->position = 15; //1
  110. stacks[1+army1->slots.size()]->position = 49; //3
  111. stacks[2+army1->slots.size()]->position = 100; //6
  112. stacks[3+army1->slots.size()]->position = 151; //9
  113. stacks[4+army1->slots.size()]->position = 185; //11
  114. break;
  115. case 6:
  116. stacks[0+army1->slots.size()]->position = 15; //1
  117. stacks[1+army1->slots.size()]->position = 49; //3
  118. stacks[2+army1->slots.size()]->position = 83; //5
  119. stacks[3+army1->slots.size()]->position = 117; //7
  120. stacks[4+army1->slots.size()]->position = 151; //9
  121. stacks[5+army1->slots.size()]->position = 185; //11
  122. break;
  123. case 7:
  124. stacks[0+army1->slots.size()]->position = 15; //1
  125. stacks[1+army1->slots.size()]->position = 49; //3
  126. stacks[2+army1->slots.size()]->position = 83; //5
  127. stacks[3+army1->slots.size()]->position = 100; //6
  128. stacks[4+army1->slots.size()]->position = 117; //7
  129. stacks[5+army1->slots.size()]->position = 151; //9
  130. stacks[6+army1->slots.size()]->position = 185; //11
  131. break;
  132. default: //fault
  133. break;
  134. }
  135. for(int g=0; g<stacks.size(); ++g) //shifting positions of two-hex creatures
  136. {
  137. if((stacks[g]->position%17)==1 && stacks[g]->creature->isDoubleWide())
  138. {
  139. stacks[g]->position += 1;
  140. }
  141. else if((stacks[g]->position%17)==15 && stacks[g]->creature->isDoubleWide())
  142. {
  143. stacks[g]->position -= 1;
  144. }
  145. }
  146. std::stable_sort(stacks.begin(),stacks.end(),cmpst);
  147. //for start inform players about battle
  148. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
  149. {
  150. if (j->first > PLAYER_LIMIT)
  151. break;
  152. if(j->second.fogOfWarMap[tile.x][tile.y][tile.z])
  153. { //player should be notified
  154. tribool side = tribool::indeterminate_value;
  155. if(j->first == curB->side1) //player is attacker
  156. side = false;
  157. else if(j->first == curB->side2) //player is defender
  158. side = true;
  159. else
  160. continue; //no witnesses
  161. if(CGI->playerint[j->second.serial]->human)
  162. {
  163. ((CPlayerInterface*)( CGI->playerint[j->second.serial] ))->battleStart(army1, army2, tile, curB->hero1, curB->hero2, side);
  164. }
  165. else
  166. {
  167. //CGI->playerint[j->second.serial]->battleStart(army1, army2, tile, curB->hero1, curB->hero2, side);
  168. }
  169. }
  170. }
  171. curB->round++;
  172. if( (curB->hero1 && curB->hero1->getSecSkillLevel(19)>=0) || ( curB->hero2 && curB->hero2->getSecSkillLevel(19)>=0) )//someone has tactics
  173. {
  174. //TODO: wywolania dla rundy -1, ograniczenie pola ruchu, etc
  175. }
  176. curB->round++;
  177. //SDL_Thread * eventh = SDL_CreateThread(battleEventThread, NULL);
  178. while(true) //till the end of the battle ;]
  179. {
  180. bool battleEnd = false;
  181. //tell players about next round
  182. for(int v=0; v<CGI->playerint.size(); ++v)
  183. CGI->playerint[v]->battleNewRound(curB->round);
  184. //stack loop
  185. for(int i=0;i<stacks.size();i++)
  186. {
  187. curB->activeStack = i;
  188. curB->stackActionPerformed = false;
  189. if(stacks[i]->alive) //indicate posiibility of making action for this unit
  190. {
  191. unsigned char owner = (stacks[i]->owner)?(hero2 ? hero2->tempOwner : 255):(hero1->tempOwner);
  192. unsigned char serialOwner = -1;
  193. for(int g=0; g<CGI->playerint.size(); ++g)
  194. {
  195. if(CGI->playerint[g]->playerID == owner)
  196. {
  197. serialOwner = g;
  198. break;
  199. }
  200. }
  201. if(serialOwner==255) //neutral unit
  202. {
  203. }
  204. else if(CGI->playerint[serialOwner]->human)
  205. {
  206. BattleAction ba = ((CPlayerInterface*)CGI->playerint[serialOwner])->activeStack(stacks[i]->ID);
  207. switch(ba.actionType)
  208. {
  209. case 2: //walk
  210. {
  211. battleMoveCreatureStack(ba.stackNumber, ba.destinationTile);
  212. }
  213. case 3: //defend
  214. {
  215. break;
  216. }
  217. case 4: //retreat/flee
  218. {
  219. for(int v=0; v<CGI->playerint.size(); ++v) //tell about the end of this battle to interfaces
  220. CGI->playerint[v]->battleEnd(army1, army2, hero1, hero2, std::vector<int>(), 0, false);
  221. battleEnd = true;
  222. break;
  223. }
  224. case 6: //walk or attack
  225. {
  226. battleMoveCreatureStack(ba.stackNumber, ba.destinationTile);
  227. battleAttackCreatureStack(ba.stackNumber, ba.destinationTile);
  228. break;
  229. }
  230. case 7: //shoot
  231. {
  232. break;
  233. }
  234. }
  235. }
  236. else
  237. {
  238. //CGI->playerint[serialOwner]->activeStack(stacks[i]->ID);
  239. }
  240. }
  241. if(battleEnd)
  242. break;
  243. //sprawdzic czy po tej akcji ktoras strona nie wygrala bitwy
  244. }
  245. if(battleEnd)
  246. break;
  247. curB->round++;
  248. SDL_Delay(50);
  249. }
  250. for(int i=0;i<stacks.size();i++)
  251. delete stacks[i];
  252. delete curB;
  253. curB = NULL;
  254. }
  255. bool CGameState::battleMoveCreatureStack(int ID, int dest)
  256. {
  257. //first checks
  258. if(curB->stackActionPerformed) //because unit cannot be moved more than once
  259. return false;
  260. unsigned char owner = -1; //owner moved of unit
  261. for(int g=0; g<curB->stacks.size(); ++g)
  262. {
  263. if(curB->stacks[g]->ID == ID)
  264. {
  265. owner = curB->stacks[g]->owner;
  266. break;
  267. }
  268. }
  269. bool stackAtEnd = false; //true if there is a stack at the end of the path (we should attack it)
  270. int numberOfStackAtEnd = -1;
  271. for(int g=0; g<curB->stacks.size(); ++g)
  272. {
  273. if(curB->stacks[g]->position == dest
  274. || (curB->stacks[g]->creature->isDoubleWide() && curB->stacks[g]->attackerOwned && curB->stacks[g]->position-1 == dest)
  275. || (curB->stacks[g]->creature->isDoubleWide() && !curB->stacks[g]->attackerOwned && curB->stacks[g]->position+1 == dest))
  276. {
  277. if(curB->stacks[g]->alive)
  278. {
  279. stackAtEnd = true;
  280. numberOfStackAtEnd = g;
  281. break;
  282. }
  283. }
  284. }
  285. //selecting moved stack
  286. CStack * curStack = NULL;
  287. for(int y=0; y<curB->stacks.size(); ++y)
  288. {
  289. if(curB->stacks[y]->ID == ID)
  290. {
  291. curStack = curB->stacks[y];
  292. break;
  293. }
  294. }
  295. if(!curStack)
  296. return false;
  297. //initing necessary tables
  298. bool accessibility[187]; //accesibility of hexes
  299. for(int k=0; k<187; k++)
  300. accessibility[k] = true;
  301. for(int g=0; g<curB->stacks.size(); ++g)
  302. {
  303. if(curB->stacks[g]->ID != ID && curB->stacks[g]->alive) //we don't want to lock enemy's positions and this units' position
  304. {
  305. accessibility[curB->stacks[g]->position] = false;
  306. if(curB->stacks[g]->creature->isDoubleWide()) //if it's a double hex creature
  307. {
  308. if(curB->stacks[g]->attackerOwned)
  309. accessibility[curB->stacks[g]->position-1] = false;
  310. else
  311. accessibility[curB->stacks[g]->position+1] = false;
  312. }
  313. }
  314. }
  315. accessibility[dest] = true;
  316. if(curStack->creature->isDoubleWide()) //locking positions unreachable by two-hex creatures
  317. {
  318. bool mac[187];
  319. for(int b=0; b<187; ++b)
  320. {
  321. //
  322. // && ( ? (curStack->attackerOwned ? accessibility[curNext-1] : accessibility[curNext+1]) : true )
  323. mac[b] = accessibility[b];
  324. if( accessibility[b] && !(curStack->attackerOwned ? accessibility[b-1] : accessibility[b+1]))
  325. {
  326. mac[b] = false;
  327. }
  328. }
  329. mac[curStack->attackerOwned ? curStack->position+1 : curStack->position-1]=true;
  330. for(int v=0; v<187; ++v)
  331. accessibility[v] = mac[v];
  332. //removing accessibility for side hexes
  333. for(int v=0; v<187; ++v)
  334. if(curStack->attackerOwned ? (v%17)==1 : (v%17)==15)
  335. accessibility[v] = false;
  336. }
  337. if(!stackAtEnd && !accessibility[dest])
  338. return false;
  339. int predecessor[187]; //for getting the Path
  340. for(int b=0; b<187; ++b)
  341. predecessor[b] = -1;
  342. //bfsing
  343. int dists[187]; //calculated distances
  344. std::queue<int> hexq; //bfs queue
  345. hexq.push(curStack->position);
  346. for(int g=0; g<187; ++g)
  347. dists[g] = 100000000;
  348. dists[hexq.front()] = 0;
  349. int curNext = -1; //for bfs loop only (helper var)
  350. while(!hexq.empty()) //bfs loop
  351. {
  352. int curHex = hexq.front();
  353. hexq.pop();
  354. curNext = curHex - ( (curHex/17)%2 ? 18 : 17 );
  355. if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
  356. {
  357. hexq.push(curNext);
  358. dists[curNext] = dists[curHex] + 1;
  359. predecessor[curNext] = curHex;
  360. }
  361. curNext = curHex - ( (curHex/17)%2 ? 17 : 16 );
  362. if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
  363. {
  364. hexq.push(curNext);
  365. dists[curNext] = dists[curHex] + 1;
  366. predecessor[curNext] = curHex;
  367. }
  368. curNext = curHex - 1;
  369. if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
  370. {
  371. hexq.push(curNext);
  372. dists[curNext] = dists[curHex] + 1;
  373. predecessor[curNext] = curHex;
  374. }
  375. curNext = curHex + 1;
  376. if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
  377. {
  378. hexq.push(curNext);
  379. dists[curNext] = dists[curHex] + 1;
  380. predecessor[curNext] = curHex;
  381. }
  382. curNext = curHex + ( (curHex/17)%2 ? 16 : 17 );
  383. if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
  384. {
  385. hexq.push(curNext);
  386. dists[curNext] = dists[curHex] + 1;
  387. predecessor[curNext] = curHex;
  388. }
  389. curNext = curHex + ( (curHex/17)%2 ? 17 : 18 );
  390. if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
  391. {
  392. hexq.push(curNext);
  393. dists[curNext] = dists[curHex] + 1;
  394. predecessor[curNext] = curHex;
  395. }
  396. }
  397. //following the Path
  398. if(dists[dest] > curStack->creature->speed && !(stackAtEnd && dists[dest] == curStack->creature->speed+1)) //we can attack a stack if we can go to adjacent hex
  399. return false;
  400. std::vector<int> path;
  401. int curElem = dest;
  402. while(curElem!=curStack->position)
  403. {
  404. path.push_back(curElem);
  405. curElem = predecessor[curElem];
  406. }
  407. for(int v=path.size()-1; v>=0; --v)
  408. {
  409. if(v!=0 || !stackAtEnd) //it's not the last step
  410. {
  411. LOCPLINT->battleStackMoved(ID, path[v], v==path.size()-1, v==0 || (stackAtEnd && v==1) );
  412. curStack->position = path[v];
  413. }
  414. else //if it's last step and we should attack unit at the end
  415. {
  416. LOCPLINT->battleStackAttacking(ID, path[v]);
  417. //counting dealt damage
  418. int numberOfCres = curStack->amount; //number of attacking creatures
  419. int attackDefenseBonus = curStack->creature->attack - curB->stacks[numberOfStackAtEnd]->creature->defence;
  420. int damageBase = 0;
  421. if(curStack->creature->damageMax == curStack->creature->damageMin) //constant damage
  422. {
  423. damageBase = curStack->creature->damageMin;
  424. }
  425. else
  426. {
  427. damageBase = rand()%(curStack->creature->damageMax - curStack->creature->damageMin) + curStack->creature->damageMin + 1;
  428. }
  429. float dmgBonusMultiplier = 1.0;
  430. if(attackDefenseBonus < 0) //decreasing dmg
  431. {
  432. if(0.02f * (-attackDefenseBonus) > 0.3f)
  433. {
  434. dmgBonusMultiplier += -0.3f;
  435. }
  436. else
  437. {
  438. dmgBonusMultiplier += 0.02f * attackDefenseBonus;
  439. }
  440. }
  441. else //increasing dmg
  442. {
  443. if(0.05f * attackDefenseBonus > 4.0f)
  444. {
  445. dmgBonusMultiplier += 4.0f;
  446. }
  447. else
  448. {
  449. dmgBonusMultiplier += 0.05f * attackDefenseBonus;
  450. }
  451. }
  452. int finalDmg = (float)damageBase * (float)curStack->amount * dmgBonusMultiplier;
  453. //applying damages
  454. int cresKilled = finalDmg / curB->stacks[numberOfStackAtEnd]->creature->hitPoints;
  455. int damageFirst = finalDmg % curB->stacks[numberOfStackAtEnd]->creature->hitPoints;
  456. if( curB->stacks[numberOfStackAtEnd]->firstHPleft <= damageFirst )
  457. {
  458. curB->stacks[numberOfStackAtEnd]->amount -= 1;
  459. curB->stacks[numberOfStackAtEnd]->firstHPleft += curB->stacks[numberOfStackAtEnd]->creature->hitPoints - damageFirst;
  460. }
  461. else
  462. {
  463. curB->stacks[numberOfStackAtEnd]->firstHPleft -= damageFirst;
  464. }
  465. int cresInstackBefore = curB->stacks[numberOfStackAtEnd]->amount;
  466. curB->stacks[numberOfStackAtEnd]->amount -= cresKilled;
  467. if(curB->stacks[numberOfStackAtEnd]->amount<=0) //stack killed
  468. {
  469. curB->stacks[numberOfStackAtEnd]->amount = 0;
  470. LOCPLINT->battleStackKilled(curB->stacks[numberOfStackAtEnd]->ID, finalDmg, std::min(cresKilled, cresInstackBefore) , ID);
  471. curB->stacks[numberOfStackAtEnd]->alive = false;
  472. }
  473. else
  474. {
  475. LOCPLINT->battleStackIsAttacked(curB->stacks[numberOfStackAtEnd]->ID, finalDmg, std::min(cresKilled, cresInstackBefore), ID);
  476. }
  477. //damage applied
  478. }
  479. }
  480. curB->stackActionPerformed = true;
  481. LOCPLINT->actionFinished(BattleAction());
  482. return true;
  483. }
  484. bool CGameState::battleAttackCreatureStack(int ID, int dest)
  485. {
  486. int attackedCreaure = -1; //-1 - there is no attacked creature
  487. for(int b=0; b<curB->stacks.size(); ++b) //TODO: make upgrades for two-hex cres.
  488. {
  489. if(curB->stacks[b]->position == dest)
  490. {
  491. attackedCreaure = curB->stacks[b]->ID;
  492. break;
  493. }
  494. }
  495. if(attackedCreaure == -1)
  496. return false;
  497. //LOCPLINT->cb->
  498. return true;
  499. }
  500. std::vector<int> CGameState::battleGetRange(int ID)
  501. {
  502. int initialPlace=-1; //position of unit
  503. int radius=-1; //range of unit
  504. unsigned char owner = -1; //owner of unit
  505. //selecting stack
  506. CStack * curStack = NULL;
  507. for(int y=0; y<curB->stacks.size(); ++y)
  508. {
  509. if(curB->stacks[y]->ID == ID)
  510. {
  511. curStack = curB->stacks[y];
  512. break;
  513. }
  514. }
  515. for(int g=0; g<curB->stacks.size(); ++g)
  516. {
  517. if(curB->stacks[g]->ID == ID)
  518. {
  519. initialPlace = curB->stacks[g]->position;
  520. radius = curB->stacks[g]->creature->speed;
  521. owner = curB->stacks[g]->owner;
  522. break;
  523. }
  524. }
  525. bool accessibility[187]; //accesibility of hexes
  526. for(int k=0; k<187; k++)
  527. accessibility[k] = true;
  528. for(int g=0; g<curB->stacks.size(); ++g)
  529. {
  530. if(curB->stacks[g]->ID != ID && curB->stacks[g]->alive) //we don't want to lock current unit's position
  531. {
  532. accessibility[curB->stacks[g]->position] = false;
  533. if(curB->stacks[g]->creature->isDoubleWide()) //if it's a double hex creature
  534. {
  535. if(curB->stacks[g]->attackerOwned)
  536. accessibility[curB->stacks[g]->position-1] = false;
  537. else
  538. accessibility[curB->stacks[g]->position+1] = false;
  539. }
  540. }
  541. }
  542. if(curStack->creature->isDoubleWide()) //locking positions unreachable by two-hex creatures
  543. {
  544. bool mac[187];
  545. for(int b=0; b<187; ++b)
  546. {
  547. //
  548. // && ( ? (curStack->attackerOwned ? accessibility[curNext-1] : accessibility[curNext+1]) : true )
  549. mac[b] = accessibility[b];
  550. if( accessibility[b] && !(curStack->attackerOwned ? accessibility[b-1] : accessibility[b+1]))
  551. {
  552. mac[b] = false;
  553. }
  554. }
  555. mac[curStack->attackerOwned ? curStack->position+1 : curStack->position-1]=true;
  556. for(int v=0; v<187; ++v)
  557. accessibility[v] = mac[v];
  558. //removing accessibility for side hexes
  559. for(int v=0; v<187; ++v)
  560. if(curStack->attackerOwned ? (v%17)==1 : (v%17)==15)
  561. accessibility[v] = false;
  562. }
  563. int dists[187]; //calculated distances
  564. std::queue<int> hexq; //bfs queue
  565. hexq.push(initialPlace);
  566. for(int g=0; g<187; ++g)
  567. dists[g] = 100000000;
  568. dists[initialPlace] = 0;
  569. int curNext = -1; //for bfs loop only (helper var)
  570. while(!hexq.empty()) //bfs loop
  571. {
  572. int curHex = hexq.front();
  573. hexq.pop();
  574. curNext = curHex - ( (curHex/17)%2 ? 18 : 17 );
  575. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
  576. {
  577. hexq.push(curNext);
  578. dists[curNext] = dists[curHex] + 1;
  579. }
  580. curNext = curHex - ( (curHex/17)%2 ? 17 : 16 );
  581. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
  582. {
  583. hexq.push(curNext);
  584. dists[curNext] = dists[curHex] + 1;
  585. }
  586. curNext = curHex - 1;
  587. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
  588. {
  589. hexq.push(curNext);
  590. dists[curNext] = dists[curHex] + 1;
  591. }
  592. curNext = curHex + 1;
  593. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
  594. {
  595. hexq.push(curNext);
  596. dists[curNext] = dists[curHex] + 1;
  597. }
  598. curNext = curHex + ( (curHex/17)%2 ? 16 : 17 );
  599. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
  600. {
  601. hexq.push(curNext);
  602. dists[curNext] = dists[curHex] + 1;
  603. }
  604. curNext = curHex + ( (curHex/17)%2 ? 17 : 18 );
  605. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
  606. {
  607. hexq.push(curNext);
  608. dists[curNext] = dists[curHex] + 1;
  609. }
  610. }
  611. std::vector<int> ret;
  612. for(int i=0; i<187; ++i)
  613. {
  614. if(dists[i]<=radius)
  615. {
  616. ret.push_back(i);
  617. }
  618. }
  619. std::vector<int> additionals;
  620. //adding enemies' positions
  621. for(int c=0; c<curB->stacks.size(); ++c)
  622. {
  623. if(curB->stacks[c]->alive && curB->stacks[c]->owner != owner)
  624. {
  625. for(int g=0; g<ret.size(); ++g)
  626. {
  627. if(CBattleHex::mutualPosition(ret[g], curB->stacks[c]->position) != -1)
  628. {
  629. additionals.push_back(curB->stacks[c]->position);
  630. }
  631. if(curB->stacks[c]->creature->isDoubleWide() && curB->stacks[c]->attackerOwned && CBattleHex::mutualPosition(ret[g], curB->stacks[c]->position-1) != -1)
  632. {
  633. additionals.push_back(curB->stacks[c]->position-1);
  634. }
  635. if(curB->stacks[c]->creature->isDoubleWide() && !curB->stacks[c]->attackerOwned && CBattleHex::mutualPosition(ret[g], curB->stacks[c]->position+1) != -1)
  636. {
  637. additionals.push_back(curB->stacks[c]->position+1);
  638. }
  639. }
  640. }
  641. }
  642. for(int g=0; g<additionals.size(); ++g)
  643. {
  644. ret.push_back(additionals[g]);
  645. }
  646. std::sort(ret.begin(), ret.end());
  647. std::vector<int>::iterator nend = std::unique(ret.begin(), ret.end());
  648. std::vector<int> ret2;
  649. for(std::vector<int>::iterator it = ret.begin(); it != nend; ++it)
  650. {
  651. ret2.push_back(*it);
  652. }
  653. return ret2;
  654. }