CGameState.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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) //till the end of the battle ;]
  178. {
  179. bool battleEnd = false;
  180. //tell players about next round
  181. for(int v=0; v<CGI->playerint.size(); ++v)
  182. CGI->playerint[v]->battleNewRound(curB->round);
  183. //stack loop
  184. for(int i=0;i<stacks.size();i++)
  185. {
  186. curB->activeStack = i;
  187. curB->stackActionPerformed = false;
  188. if(stacks[i]->alive) //indicate posiibility of making action for this unit
  189. {
  190. unsigned char owner = (stacks[i]->owner)?(hero2 ? hero2->tempOwner : 255):(hero1->tempOwner);
  191. unsigned char serialOwner = -1;
  192. for(int g=0; g<CGI->playerint.size(); ++g)
  193. {
  194. if(CGI->playerint[g]->playerID == owner)
  195. {
  196. serialOwner = g;
  197. break;
  198. }
  199. }
  200. if(serialOwner==255) //neutral unit
  201. {
  202. }
  203. else if(CGI->playerint[serialOwner]->human)
  204. {
  205. BattleAction ba = ((CPlayerInterface*)CGI->playerint[serialOwner])->activeStack(stacks[i]->ID);
  206. switch(ba.actionType)
  207. {
  208. case 2: //walk
  209. {
  210. battleMoveCreatureStack(ba.stackNumber, ba.destinationTile);
  211. }
  212. case 3: //defend
  213. {
  214. break;
  215. }
  216. case 4: //retreat/flee
  217. {
  218. for(int v=0; v<CGI->playerint.size(); ++v) //tell about the end of this battle to interfaces
  219. CGI->playerint[v]->battleEnd(army1, army2, hero1, hero2, std::vector<int>(), 0, false);
  220. battleEnd = true;
  221. break;
  222. }
  223. case 6: //walk or attack
  224. {
  225. battleMoveCreatureStack(ba.stackNumber, ba.destinationTile);
  226. battleAttackCreatureStack(ba.stackNumber, ba.destinationTile);
  227. break;
  228. }
  229. }
  230. }
  231. else
  232. {
  233. //CGI->playerint[serialOwner]->activeStack(stacks[i]->ID);
  234. }
  235. }
  236. if(battleEnd)
  237. break;
  238. //sprawdzic czy po tej akcji ktoras strona nie wygrala bitwy
  239. }
  240. if(battleEnd)
  241. break;
  242. curB->round++;
  243. SDL_Delay(50);
  244. }
  245. for(int i=0;i<stacks.size();i++)
  246. delete stacks[i];
  247. delete curB;
  248. curB = NULL;
  249. }
  250. bool CGameState::battleMoveCreatureStack(int ID, int dest)
  251. {
  252. //first checks
  253. if(curB->stackActionPerformed) //because unit cannot be moved more than once
  254. return false;
  255. unsigned char owner = -1; //owner moved of unit
  256. for(int g=0; g<curB->stacks.size(); ++g)
  257. {
  258. if(curB->stacks[g]->ID == ID)
  259. {
  260. owner = curB->stacks[g]->owner;
  261. break;
  262. }
  263. }
  264. bool stackAtEnd = false; //true if there is a stack at the end of the path (we should attack it)
  265. int numberOfStackAtEnd = -1;
  266. for(int g=0; g<curB->stacks.size(); ++g)
  267. {
  268. if(curB->stacks[g]->position == dest
  269. || (curB->stacks[g]->creature->isDoubleWide() && curB->stacks[g]->attackerOwned && curB->stacks[g]->position-1 == dest)
  270. || (curB->stacks[g]->creature->isDoubleWide() && !curB->stacks[g]->attackerOwned && curB->stacks[g]->position+1 == dest))
  271. {
  272. stackAtEnd = true;
  273. numberOfStackAtEnd = g;
  274. break;
  275. }
  276. }
  277. //selecting moved stack
  278. CStack * curStack = NULL;
  279. for(int y=0; y<curB->stacks.size(); ++y)
  280. {
  281. if(curB->stacks[y]->ID == ID)
  282. {
  283. curStack = curB->stacks[y];
  284. break;
  285. }
  286. }
  287. if(!curStack)
  288. return false;
  289. //initing necessary tables
  290. bool accessibility[187]; //accesibility of hexes
  291. for(int k=0; k<187; k++)
  292. accessibility[k] = true;
  293. for(int g=0; g<curB->stacks.size(); ++g)
  294. {
  295. if(curB->stacks[g]->ID != ID) //we don't want to lock enemy's positions and this units' position
  296. {
  297. accessibility[curB->stacks[g]->position] = false;
  298. if(curB->stacks[g]->creature->isDoubleWide()) //if it's a double hex creature
  299. {
  300. if(curB->stacks[g]->attackerOwned)
  301. accessibility[curB->stacks[g]->position-1] = false;
  302. else
  303. accessibility[curB->stacks[g]->position+1] = false;
  304. }
  305. }
  306. }
  307. accessibility[dest] = true;
  308. if(curStack->creature->isDoubleWide()) //locking positions unreachable by two-hex creatures
  309. {
  310. bool mac[187];
  311. for(int b=0; b<187; ++b)
  312. {
  313. //
  314. // && ( ? (curStack->attackerOwned ? accessibility[curNext-1] : accessibility[curNext+1]) : true )
  315. mac[b] = accessibility[b];
  316. if( accessibility[b] && !(curStack->attackerOwned ? accessibility[b-1] : accessibility[b+1]))
  317. {
  318. mac[b] = false;
  319. }
  320. }
  321. mac[curStack->attackerOwned ? curStack->position+1 : curStack->position-1]=true;
  322. for(int v=0; v<187; ++v)
  323. accessibility[v] = mac[v];
  324. //removing accessibility for side hexes
  325. for(int v=0; v<187; ++v)
  326. if(curStack->attackerOwned ? (v%17)==1 : (v%17)==15)
  327. accessibility[v] = false;
  328. }
  329. if(!accessibility[dest])
  330. return false;
  331. int predecessor[187]; //for getting the Path
  332. for(int b=0; b<187; ++b)
  333. predecessor[b] = -1;
  334. //bfsing
  335. int dists[187]; //calculated distances
  336. std::queue<int> hexq; //bfs queue
  337. hexq.push(curStack->position);
  338. for(int g=0; g<187; ++g)
  339. dists[g] = 100000000;
  340. dists[hexq.front()] = 0;
  341. int curNext = -1; //for bfs loop only (helper var)
  342. while(!hexq.empty()) //bfs loop
  343. {
  344. int curHex = hexq.front();
  345. hexq.pop();
  346. curNext = curHex - ( (curHex/17)%2 ? 18 : 17 );
  347. if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
  348. {
  349. hexq.push(curNext);
  350. dists[curNext] = dists[curHex] + 1;
  351. predecessor[curNext] = curHex;
  352. }
  353. curNext = curHex - ( (curHex/17)%2 ? 17 : 16 );
  354. if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
  355. {
  356. hexq.push(curNext);
  357. dists[curNext] = dists[curHex] + 1;
  358. predecessor[curNext] = curHex;
  359. }
  360. curNext = curHex - 1;
  361. if((curNext > 0) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
  362. {
  363. hexq.push(curNext);
  364. dists[curNext] = dists[curHex] + 1;
  365. predecessor[curNext] = curHex;
  366. }
  367. curNext = curHex + 1;
  368. if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
  369. {
  370. hexq.push(curNext);
  371. dists[curNext] = dists[curHex] + 1;
  372. predecessor[curNext] = curHex;
  373. }
  374. curNext = curHex + ( (curHex/17)%2 ? 16 : 17 );
  375. if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
  376. {
  377. hexq.push(curNext);
  378. dists[curNext] = dists[curHex] + 1;
  379. predecessor[curNext] = curHex;
  380. }
  381. curNext = curHex + ( (curHex/17)%2 ? 17 : 18 );
  382. if((curNext < 187) && (accessibility[curNext] || curNext==dest) && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
  383. {
  384. hexq.push(curNext);
  385. dists[curNext] = dists[curHex] + 1;
  386. predecessor[curNext] = curHex;
  387. }
  388. }
  389. //following the Path
  390. if(dists[dest] > curStack->creature->speed)
  391. return false;
  392. std::vector<int> path;
  393. int curElem = dest;
  394. while(curElem!=curStack->position)
  395. {
  396. path.push_back(curElem);
  397. curElem = predecessor[curElem];
  398. }
  399. for(int v=path.size()-1; v>=0; --v)
  400. {
  401. if(v!=0 || !stackAtEnd) //it's not the last step
  402. {
  403. LOCPLINT->battleStackMoved(ID, path[v], v==path.size()-1, v==0 || (stackAtEnd && v==1) );
  404. curStack->position = path[v];
  405. }
  406. else //if it's last step and we should attack unit at the end
  407. {
  408. LOCPLINT->battleStackAttacking(ID, path[v]);
  409. //counting dealt damage
  410. int numberOfCres = curStack->amount; //number of attacking creatures
  411. int attackDefenseBonus = curStack->creature->attack - curB->stacks[numberOfStackAtEnd]->creature->defence;
  412. int damageBase = 0;
  413. if(curStack->creature->damageMax == curStack->creature->damageMin) //constant damage
  414. {
  415. damageBase = curStack->creature->damageMin;
  416. }
  417. else
  418. {
  419. damageBase = rand()%(curStack->creature->damageMax - curStack->creature->damageMin) + curStack->creature->damageMin + 1;
  420. }
  421. float dmgBonusMultiplier = 1.0;
  422. if(attackDefenseBonus < 0) //decreasing dmg
  423. {
  424. if(0.02f * (-attackDefenseBonus) > 0.3f)
  425. {
  426. dmgBonusMultiplier += -0.3f;
  427. }
  428. else
  429. {
  430. dmgBonusMultiplier += 0.02f * attackDefenseBonus;
  431. }
  432. }
  433. else //increasing dmg
  434. {
  435. if(0.05f * attackDefenseBonus > 4.0f)
  436. {
  437. dmgBonusMultiplier += 4.0f;
  438. }
  439. else
  440. {
  441. dmgBonusMultiplier += 0.05f * attackDefenseBonus;
  442. }
  443. }
  444. int finalDmg = (float)damageBase * (float)curStack->amount * dmgBonusMultiplier;
  445. //applying damages
  446. int cresKilled = finalDmg / curB->stacks[numberOfStackAtEnd]->creature->hitPoints;
  447. int damageFirst = finalDmg % curB->stacks[numberOfStackAtEnd]->creature->hitPoints;
  448. if( curB->stacks[numberOfStackAtEnd]->firstHPleft <= damageFirst )
  449. {
  450. curB->stacks[numberOfStackAtEnd]->amount -= 1;
  451. curB->stacks[numberOfStackAtEnd]->firstHPleft += curB->stacks[numberOfStackAtEnd]->creature->hitPoints - damageFirst;
  452. }
  453. else
  454. {
  455. curB->stacks[numberOfStackAtEnd]->firstHPleft -= damageFirst;
  456. }
  457. int cresInstackBefore = curB->stacks[numberOfStackAtEnd]->amount;
  458. curB->stacks[numberOfStackAtEnd]->amount -= cresKilled;
  459. if(curB->stacks[numberOfStackAtEnd]->amount<=0) //stack killed
  460. {
  461. curB->stacks[numberOfStackAtEnd]->amount = 0;
  462. LOCPLINT->battleStackKilled(curB->stacks[numberOfStackAtEnd]->ID, finalDmg, std::min(cresKilled, cresInstackBefore) , ID);
  463. curB->stacks[numberOfStackAtEnd]->alive = false;
  464. }
  465. else
  466. {
  467. LOCPLINT->battleStackIsAttacked(curB->stacks[numberOfStackAtEnd]->ID, finalDmg, std::min(cresKilled, cresInstackBefore), ID);
  468. }
  469. //damage applied
  470. }
  471. }
  472. curB->stackActionPerformed = true;
  473. LOCPLINT->actionFinished(BattleAction());
  474. return true;
  475. }
  476. bool CGameState::battleAttackCreatureStack(int ID, int dest)
  477. {
  478. int attackedCreaure = -1; //-1 - there is no attacked creature
  479. for(int b=0; b<curB->stacks.size(); ++b) //TODO: make upgrades for two-hex cres.
  480. {
  481. if(curB->stacks[b]->position == dest)
  482. {
  483. attackedCreaure = curB->stacks[b]->ID;
  484. break;
  485. }
  486. }
  487. if(attackedCreaure == -1)
  488. return false;
  489. //LOCPLINT->cb->
  490. return true;
  491. }
  492. std::vector<int> CGameState::battleGetRange(int ID)
  493. {
  494. int initialPlace=-1; //position of unit
  495. int radius=-1; //range of unit
  496. unsigned char owner = -1; //owner of unit
  497. //selecting stack
  498. CStack * curStack = NULL;
  499. for(int y=0; y<curB->stacks.size(); ++y)
  500. {
  501. if(curB->stacks[y]->ID == ID)
  502. {
  503. curStack = curB->stacks[y];
  504. break;
  505. }
  506. }
  507. for(int g=0; g<curB->stacks.size(); ++g)
  508. {
  509. if(curB->stacks[g]->ID == ID)
  510. {
  511. initialPlace = curB->stacks[g]->position;
  512. radius = curB->stacks[g]->creature->speed;
  513. owner = curB->stacks[g]->owner;
  514. break;
  515. }
  516. }
  517. bool accessibility[187]; //accesibility of hexes
  518. for(int k=0; k<187; k++)
  519. accessibility[k] = true;
  520. for(int g=0; g<curB->stacks.size(); ++g)
  521. {
  522. if(curB->stacks[g]->ID != ID) //we don't want to lock current unit's position
  523. {
  524. accessibility[curB->stacks[g]->position] = false;
  525. if(curB->stacks[g]->creature->isDoubleWide()) //if it's a double hex creature
  526. {
  527. if(curB->stacks[g]->attackerOwned)
  528. accessibility[curB->stacks[g]->position-1] = false;
  529. else
  530. accessibility[curB->stacks[g]->position+1] = false;
  531. }
  532. }
  533. }
  534. if(curStack->creature->isDoubleWide()) //locking positions unreachable by two-hex creatures
  535. {
  536. bool mac[187];
  537. for(int b=0; b<187; ++b)
  538. {
  539. //
  540. // && ( ? (curStack->attackerOwned ? accessibility[curNext-1] : accessibility[curNext+1]) : true )
  541. mac[b] = accessibility[b];
  542. if( accessibility[b] && !(curStack->attackerOwned ? accessibility[b-1] : accessibility[b+1]))
  543. {
  544. mac[b] = false;
  545. }
  546. }
  547. mac[curStack->attackerOwned ? curStack->position+1 : curStack->position-1]=true;
  548. for(int v=0; v<187; ++v)
  549. accessibility[v] = mac[v];
  550. //removing accessibility for side hexes
  551. for(int v=0; v<187; ++v)
  552. if(curStack->attackerOwned ? (v%17)==1 : (v%17)==15)
  553. accessibility[v] = false;
  554. }
  555. int dists[187]; //calculated distances
  556. std::queue<int> hexq; //bfs queue
  557. hexq.push(initialPlace);
  558. for(int g=0; g<187; ++g)
  559. dists[g] = 100000000;
  560. dists[initialPlace] = 0;
  561. int curNext = -1; //for bfs loop only (helper var)
  562. while(!hexq.empty()) //bfs loop
  563. {
  564. int curHex = hexq.front();
  565. hexq.pop();
  566. curNext = curHex - ( (curHex/17)%2 ? 18 : 17 );
  567. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top left
  568. {
  569. hexq.push(curNext);
  570. dists[curNext] = dists[curHex] + 1;
  571. }
  572. curNext = curHex - ( (curHex/17)%2 ? 17 : 16 );
  573. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //top right
  574. {
  575. hexq.push(curNext);
  576. dists[curNext] = dists[curHex] + 1;
  577. }
  578. curNext = curHex - 1;
  579. if((curNext > 0) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //left
  580. {
  581. hexq.push(curNext);
  582. dists[curNext] = dists[curHex] + 1;
  583. }
  584. curNext = curHex + 1;
  585. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //right
  586. {
  587. hexq.push(curNext);
  588. dists[curNext] = dists[curHex] + 1;
  589. }
  590. curNext = curHex + ( (curHex/17)%2 ? 16 : 17 );
  591. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom left
  592. {
  593. hexq.push(curNext);
  594. dists[curNext] = dists[curHex] + 1;
  595. }
  596. curNext = curHex + ( (curHex/17)%2 ? 17 : 18 );
  597. if((curNext < 187) && accessibility[curNext] && (dists[curHex] + 1 < dists[curNext]) && (curNext)%17!=0 && (curNext)%17!=16) //bottom right
  598. {
  599. hexq.push(curNext);
  600. dists[curNext] = dists[curHex] + 1;
  601. }
  602. }
  603. std::vector<int> ret;
  604. for(int i=0; i<187; ++i)
  605. {
  606. if(dists[i]<=radius)
  607. {
  608. ret.push_back(i);
  609. }
  610. }
  611. return ret;
  612. }