CCallback.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. #include "stdafx.h"
  2. #include "CCallback.h"
  3. #include "CPathfinder.h"
  4. #include "hch/CHeroHandler.h"
  5. #include "hch/CTownHandler.h"
  6. #include "CGameInfo.h"
  7. #include "hch/CAmbarCendamo.h"
  8. #include "mapHandler.h"
  9. #include "CGameState.h"
  10. #include "CPlayerInterface.h"
  11. #include "hch/CGeneralTextHandler.h"
  12. #include "CAdvmapInterface.h"
  13. #include "CPlayerInterface.h"
  14. #include "hch/CBuildingHandler.h"
  15. #include "hch/CObjectHandler.h"
  16. #include "lib/Connection.h"
  17. #include "client/Client.h"
  18. #include <boost/thread.hpp>
  19. #include <boost/foreach.hpp>
  20. #include "lib/NetPacks.h"
  21. //LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
  22. extern CSharedCond<std::set<IPack*> > mess;
  23. HeroMoveDetails::HeroMoveDetails(int3 Src, int3 Dst, CGHeroInstance*Ho)
  24. :src(Src),dst(Dst),ho(Ho)
  25. {
  26. owner = ho->getOwner();
  27. };
  28. bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType)
  29. {
  30. CGHeroInstance * hero = NULL;
  31. if (idtype==0)
  32. {
  33. if (player==-1)
  34. hero=gs->players[player+1].heroes[ID];
  35. else
  36. hero=gs->players[player].heroes[ID];
  37. }
  38. else if (idtype==1 && player>=0) //looking for it in local area
  39. {
  40. for (int i=0; i<gs->players[player].heroes.size();i++)
  41. {
  42. if (gs->players[player].heroes[i]->type->ID == ID)
  43. hero = gs->players[player].heroes[i];
  44. }
  45. }
  46. else //idtype==1; player<0
  47. {
  48. for(std::map<ui8, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)
  49. {
  50. for (int i=0; i<(*j).second.heroes.size();i++)
  51. {
  52. if ((*j).second.heroes[i]->type->ID == ID)
  53. {
  54. hero = (*j).second.heroes[i];
  55. }
  56. }
  57. }
  58. }
  59. if (!hero)
  60. return false; //can't find hero
  61. if(!verifyPath(path,!hero->canWalkOnSea()))//TODO: not check sea, if hero has flying or walking on water
  62. return false; //invalid path
  63. //check path format
  64. if (pathType==0)
  65. CPathfinder::convertPath(path,pathType);
  66. if (pathType>1)
  67. throw std::exception("Unknown path format");
  68. CPath * ourPath = path;
  69. if(!ourPath)
  70. return false;
  71. for(int i=ourPath->nodes.size()-1; i>0; i--)
  72. {
  73. int3 stpos(ourPath->nodes[i].coord.x, ourPath->nodes[i].coord.y, hero->pos.z),
  74. endpos(ourPath->nodes[i-1].coord.x, ourPath->nodes[i-1].coord.y, hero->pos.z);
  75. HeroMoveDetails curd(stpos,endpos,hero);
  76. *cl->serv << ui16(501) << hero->id << stpos << endpos;
  77. {//wait till there is server answer
  78. boost::unique_lock<boost::mutex> lock(*mess.mx);
  79. while(std::find_if(mess.res->begin(),mess.res->end(),IPack::isType<501>) == mess.res->end())
  80. mess.cv->wait(lock);
  81. std::set<IPack*>::iterator itr = std::find_if(mess.res->begin(),mess.res->end(),IPack::isType<501>);
  82. TryMoveHero tmh = *static_cast<TryMoveHero*>(*itr);
  83. mess.res->erase(itr);
  84. if(!tmh.result)
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. void CCallback::selectionMade(int selection, int asker)
  91. {
  92. //todo - jak bedzie multiplayer po sieci, to moze wymagac przerobek zaleznych od obranego modelu
  93. //IChosen * ask = (IChosen *)asker;
  94. //ask->chosen(selection);
  95. }
  96. void CCallback::recruitCreatures(const CGObjectInstance *obj, int ID, int amount)
  97. {
  98. if(amount<=0) return;
  99. if(obj->ID==98) //recruiting from town
  100. {
  101. int ser=-1; //used dwelling level
  102. CGTownInstance *t = const_cast<CGTownInstance*>(static_cast<const CGTownInstance*>(obj));
  103. //verify
  104. bool found = false;
  105. typedef std::pair<const int,int> Parka;
  106. for(std::map<int,int>::iterator av=t->strInfo.creatures.begin();av!=t->strInfo.creatures.end();av++)
  107. {
  108. if( ( found = (ID == t->town->basicCreatures[av->first]) ) //creature is available among basic cretures
  109. || (found = (ID == t->town->upgradedCreatures[av->first])) )//creature is available among upgraded cretures
  110. {
  111. amount = min(amount,av->second); //reduce recruited amount up to available amount
  112. ser = av->first;
  113. break;
  114. }
  115. }
  116. if(!found) //no such creature
  117. return;
  118. if(amount > CGI->creh->creatures[ID].maxAmount(gs->players[player].resources))
  119. return; //not enough resources
  120. //for(int i=0;i<RESOURCE_QUANTITY;i++)
  121. // if (gs->players[player].resources[i] < (CGI->creh->creatures[ID].cost[i] * amount))
  122. // return; //not enough resources
  123. if(amount<=0) return;
  124. //recruit
  125. int slot = -1; //slot ID
  126. std::pair<int,std::pair<CCreature*,int> > parb;
  127. for(int i=0;i<7;i++) //TODO: if there is already stack of same creatures it should be used always
  128. {
  129. if((!t->army.slots[i].first) || (t->army.slots[i].first->idNumber == ID)) //slot is free or there is saem creature
  130. {
  131. slot = i;
  132. break;
  133. }
  134. }
  135. if(slot<0) //no free slot
  136. return;
  137. for(int i=0;i<RESOURCE_QUANTITY;i++)
  138. gs->players[player].resources[i] -= (CGI->creh->creatures[ID].cost[i] * amount);
  139. t->strInfo.creatures[ser] -= amount;
  140. if(t->army.slots[slot].first) //add new creatures to the existing stack
  141. {
  142. t->army.slots[slot].second += amount;
  143. }
  144. else //create new stack in the garrison
  145. {
  146. t->army.slots[slot].first = &CGI->creh->creatures[ID];
  147. t->army.slots[slot].second = amount;
  148. }
  149. cl->playerint[player]->garrisonChanged(obj);
  150. }
  151. //TODO: recruit from dwellings on the adventure map
  152. }
  153. bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
  154. {
  155. if((player>=0) && obj->tempOwner != player)
  156. return false;
  157. CArmedInstance *ob = const_cast<CArmedInstance*>(obj);
  158. ob->army.slots.erase(stackPos);
  159. cl->playerint[player]->garrisonChanged(obj);
  160. return true;
  161. }
  162. bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
  163. {
  164. //TODO: write
  165. return false;
  166. }
  167. void CCallback::endTurn()
  168. {
  169. std::cout << "Player "<<(unsigned)player<<" end his turn."<<std::endl;
  170. cl->serv->wmx->lock();
  171. *cl->serv << ui16(100); //report that we ended turn
  172. cl->serv->wmx->unlock();
  173. }
  174. UpgradeInfo CCallback::getUpgradeInfo(const CArmedInstance *obj, int stackPos)
  175. {
  176. UpgradeInfo ret;
  177. CCreature *base = ((CArmedInstance*)obj)->army.slots[stackPos].first;
  178. if((obj->ID == 98) || ((obj->ID == 34) && static_cast<const CGHeroInstance*>(obj)->visitedTown))
  179. {
  180. CGTownInstance * t;
  181. if(obj->ID == 98)
  182. t = static_cast<CGTownInstance *>(const_cast<CArmedInstance *>(obj));
  183. else
  184. t = static_cast<const CGHeroInstance*>(obj)->visitedTown;
  185. for(std::set<int>::iterator i=t->builtBuildings.begin(); i!=t->builtBuildings.end(); i++)
  186. {
  187. if( (*i) >= 37 && (*i) < 44 ) //upgraded creature dwelling
  188. {
  189. int nid = t->town->upgradedCreatures[(*i)-37]; //upgrade offered by that building
  190. if(base->upgrades.find(nid) != base->upgrades.end()) //possible upgrade
  191. {
  192. ret.newID.push_back(nid);
  193. ret.cost.push_back(std::set<std::pair<int,int> >());
  194. for(int j=0;j<RESOURCE_QUANTITY;j++)
  195. {
  196. int dif = CGI->creh->creatures[nid].cost[j] - base->cost[j];
  197. if(dif)
  198. ret.cost[ret.cost.size()-1].insert(std::make_pair(j,dif));
  199. }
  200. }
  201. }
  202. }//end for
  203. }
  204. //TODO: check if hero ability makes some upgrades possible
  205. if(ret.newID.size())
  206. ret.oldID = base->idNumber;
  207. return ret;
  208. }
  209. const StartInfo * CCallback::getStartInfo()
  210. {
  211. return gs->scenarioOps;
  212. }
  213. int CCallback::howManyTowns()
  214. {
  215. return gs->players[gs->currentPlayer].towns.size();
  216. }
  217. const CGTownInstance * CCallback::getTownInfo(int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  218. {
  219. if (!mode)
  220. return gs->players[gs->currentPlayer].towns[val];
  221. else
  222. {
  223. //TODO: add some smart ID to the CTownInstance
  224. //for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
  225. //{
  226. // if (gs->players[gs->currentPlayer].towns[i]->someID==val)
  227. // return gs->players[gs->currentPlayer].towns[i];
  228. //}
  229. return NULL;
  230. }
  231. return NULL;
  232. }
  233. int CCallback::howManyHeroes()
  234. {
  235. return gs->players[player].heroes.size();
  236. }
  237. const CGHeroInstance * CCallback::getHeroInfo(int player, int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  238. {
  239. if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  240. return NULL;
  241. if (!mode)
  242. if(val<gs->players[player].heroes.size())
  243. return gs->players[player].heroes[val];
  244. else return NULL;
  245. else
  246. {
  247. for (int i=0; i<gs->players[player].heroes.size();i++)
  248. {
  249. if (gs->players[player].heroes[i]->type->ID==val)
  250. return gs->players[player].heroes[i];
  251. }
  252. }
  253. return NULL;
  254. }
  255. int CCallback::getResourceAmount(int type)
  256. {
  257. return gs->players[player].resources[type];
  258. }
  259. std::vector<int> CCallback::getResourceAmount()
  260. {
  261. return gs->players[player].resources;
  262. }
  263. int CCallback::getDate(int mode)
  264. {
  265. return gs->getDate(mode);
  266. }
  267. std::vector < std::string > CCallback::getObjDescriptions(int3 pos)
  268. {
  269. std::vector<std::string> ret;
  270. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
  271. ret.push_back(obj->hoverName);
  272. return ret;
  273. }
  274. bool CCallback::verifyPath(CPath * path, bool blockSea)
  275. {
  276. for (int i=0;i<path->nodes.size();i++)
  277. {
  278. if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->blocked
  279. && (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->visitable)))
  280. return false; //path is wrong - one of the tiles is blocked
  281. if (blockSea)
  282. {
  283. if (i==0)
  284. continue;
  285. if (
  286. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype==EterrainType::water)
  287. &&
  288. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype!=EterrainType::water))
  289. ||
  290. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype!=EterrainType::water)
  291. &&
  292. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==EterrainType::water))
  293. ||
  294. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==EterrainType::rock)
  295. )
  296. return false;
  297. }
  298. }
  299. return true;
  300. }
  301. std::vector< std::vector< std::vector<unsigned char> > > & CCallback::getVisibilityMap()
  302. {
  303. return gs->players[player].fogOfWarMap;
  304. }
  305. bool CCallback::isVisible(int3 pos, int Player)
  306. {
  307. return gs->players[Player].fogOfWarMap[pos.x][pos.y][pos.z];
  308. }
  309. std::vector < const CGTownInstance *> CCallback::getTownsInfo(bool onlyOur)
  310. {
  311. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  312. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  313. {
  314. for (int j=0;j<(*i).second.towns.size();j++)
  315. {
  316. if ( ( isVisible((*i).second.towns[j]->pos,player) ) || (*i).first==player)
  317. {
  318. ret.push_back((*i).second.towns[j]);
  319. }
  320. }
  321. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  322. return ret;
  323. }
  324. std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur)
  325. {
  326. std::vector < const CGHeroInstance *> ret = std::vector < const CGHeroInstance *>();
  327. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  328. {
  329. for (int j=0;j<(*i).second.heroes.size();j++)
  330. {
  331. if ( ( isVisible((*i).second.heroes[j]->getPosition(false),player) ) || (*i).first==player)
  332. {
  333. ret.push_back((*i).second.heroes[j]);
  334. }
  335. }
  336. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  337. return ret;
  338. }
  339. bool CCallback::isVisible(int3 pos)
  340. {
  341. return isVisible(pos,player);
  342. }
  343. int CCallback::getMyColor()
  344. {
  345. return player;
  346. }
  347. int CCallback::getHeroSerial(const CGHeroInstance * hero)
  348. {
  349. for (int i=0; i<gs->players[player].heroes.size();i++)
  350. {
  351. if (gs->players[player].heroes[i]==hero)
  352. return i;
  353. }
  354. return -1;
  355. }
  356. const CCreatureSet* CCallback::getGarrison(const CGObjectInstance *obj)
  357. {
  358. if(!obj)
  359. return NULL;
  360. if(obj->ID == 34)
  361. return &(dynamic_cast<const CGHeroInstance*>(obj))->army;
  362. else if(obj->ID == 98)
  363. return &(dynamic_cast<const CGTownInstance*>(obj)->army);
  364. else return NULL;
  365. }
  366. int CCallback::swapCreatures(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
  367. {
  368. CCreatureSet *S1 = const_cast<CCreatureSet*>(getGarrison(s1)), *S2 = const_cast<CCreatureSet*>(getGarrison(s2));
  369. if (false)
  370. {
  371. //TODO: check if we are allowed to swap these creatures
  372. return -1;
  373. }
  374. CCreature * pom = S2->slots[p2].first;
  375. S2->slots[p2].first = S1->slots[p1].first;
  376. S1->slots[p1].first = pom;
  377. int pom2 = S2->slots[p2].second;
  378. S2->slots[p2].second = S1->slots[p1].second;
  379. S1->slots[p1].second = pom2;
  380. if(!S1->slots[p1].first)
  381. S1->slots.erase(p1);
  382. if(!S2->slots[p2].first)
  383. S2->slots.erase(p2);
  384. if(s1->tempOwner<PLAYER_LIMIT)
  385. cl->playerint[s1->tempOwner]->garrisonChanged(s1);
  386. if((s2->tempOwner<PLAYER_LIMIT) && (s2 != s1))
  387. cl->playerint[s2->tempOwner]->garrisonChanged(s2);
  388. return 0;
  389. }
  390. int CCallback::mergeStacks(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
  391. {
  392. CCreatureSet *S1 = const_cast<CCreatureSet*>(getGarrison(s1)), *S2 = const_cast<CCreatureSet*>(getGarrison(s2));
  393. if ((S1->slots[p1].first != S2->slots[p2].first) && (true /*we are allowed to*/))
  394. {
  395. return -1;
  396. }
  397. S2->slots[p2].second += S1->slots[p1].second;
  398. S1->slots[p1].first = NULL;
  399. S1->slots[p1].second = 0;
  400. S1->slots.erase(p1);
  401. if(s1->tempOwner<PLAYER_LIMIT)
  402. cl->playerint[s1->tempOwner]->garrisonChanged(s1);
  403. if((s2->tempOwner<PLAYER_LIMIT) && (s2 != s1))
  404. cl->playerint[s2->tempOwner]->garrisonChanged(s2);
  405. return 0;
  406. }
  407. int CCallback::splitStack(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2, int val)
  408. {
  409. if(!val)
  410. return -1;
  411. CCreatureSet *S1 = const_cast<CCreatureSet*>(getGarrison(s1)), *S2 = const_cast<CCreatureSet*>(getGarrison(s2));
  412. if ((S1->slots[p1].second<val) && (true /*we are allowed to*/))
  413. {
  414. return -1;
  415. }
  416. S2->slots[p2].first = S1->slots[p1].first;
  417. S2->slots[p2].second = val;
  418. S1->slots[p1].second -= val;
  419. if(!S1->slots[p1].second) //if we've moved all creatures
  420. S1->slots.erase(p1);
  421. if(s1->tempOwner<PLAYER_LIMIT)
  422. {
  423. cl->playerint[s1->tempOwner]->garrisonChanged(s1);
  424. }
  425. if((s2->tempOwner<PLAYER_LIMIT) && (s2 != s1))
  426. {
  427. cl->playerint[s2->tempOwner]->garrisonChanged(s2);
  428. }
  429. return 0;
  430. }
  431. bool CCallback::dismissHero(const CGHeroInstance *hero)
  432. {
  433. CGHeroInstance * Vhero = const_cast<CGHeroInstance *>(hero);
  434. CGI->mh->removeObject(Vhero);
  435. std::vector<CGHeroInstance*>::iterator nitr = find(CGI->state->players[player].heroes.begin(), CGI->state->players[player].heroes.end(), Vhero);
  436. CGI->state->players[player].heroes.erase(nitr);
  437. LOCPLINT->adventureInt->heroList.updateHList();
  438. return false;
  439. }
  440. int CCallback::getMySerial()
  441. {
  442. return gs->players[player].serial;
  443. }
  444. bool CCallback::swapArifacts(const CGHeroInstance * hero1, bool worn1, int pos1, const CGHeroInstance * hero2, bool worn2, int pos2)
  445. {
  446. if(!hero1 || !hero2) //incorrect data
  447. return false;
  448. CGHeroInstance * Uhero1 = const_cast<CGHeroInstance *>(hero1);
  449. CGHeroInstance * Uhero2 = const_cast<CGHeroInstance *>(hero2);
  450. if(worn1 && worn2)
  451. {
  452. std::swap(Uhero1->artifWorn[pos1], Uhero2->artifWorn[pos2]);
  453. }
  454. else if(worn1 && !worn2)
  455. {
  456. std::swap(Uhero1->artifWorn[pos1], Uhero2->artifacts[pos2]);
  457. }
  458. else if(!worn1 && worn2)
  459. {
  460. std::swap(Uhero1->artifacts[pos1], Uhero2->artifWorn[pos2]);
  461. }
  462. else
  463. {
  464. std::swap(Uhero1->artifacts[pos1], Uhero2->artifacts[pos2]);
  465. }
  466. return true;
  467. }
  468. bool CCallback::buildBuilding(const CGTownInstance *town, int buildingID)
  469. {
  470. CGTownInstance * t = const_cast<CGTownInstance *>(town);
  471. CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
  472. if(0/*not allowed*/)//TODO: check if we are allowed to build
  473. return false;
  474. if(buildingID>36) //upg dwelling
  475. {
  476. if(t->getHordeLevel(0) == (buildingID-37))
  477. t->builtBuildings.insert(19);
  478. else if(t->getHordeLevel(1) == (buildingID-37))
  479. t->builtBuildings.insert(25);
  480. }
  481. else if(buildingID >= 30) //bas. dwelling
  482. {
  483. t->strInfo.creatures[buildingID-30] = CGI->creh->creatures[t->town->basicCreatures[buildingID-30]].growth;
  484. }
  485. t->builtBuildings.insert(buildingID);
  486. for(int i=0;i<7;i++)
  487. gs->players[player].resources[i]-=b->resources[i];
  488. t->builded++;
  489. cl->playerint[player]->buildChanged(town,buildingID,1);
  490. return true;
  491. }
  492. int CCallback::battleGetBattlefieldType()
  493. {
  494. return CGI->mh->ttiles[CGI->state->curB->tile.x][CGI->state->curB->tile.y][CGI->state->curB->tile.z].tileInfo->tertype;
  495. }
  496. int CCallback::battleGetObstaclesAtTile(int tile) //returns bitfield
  497. {
  498. //TODO - write
  499. return -1;
  500. }
  501. int CCallback::battleGetStack(int pos)
  502. {
  503. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  504. {
  505. if(CGI->state->curB->stacks[g]->position == pos ||
  506. ( CGI->state->curB->stacks[g]->creature->isDoubleWide() &&
  507. ( (CGI->state->curB->stacks[g]->attackerOwned && CGI->state->curB->stacks[g]->position-1 == pos) ||
  508. (!CGI->state->curB->stacks[g]->attackerOwned && CGI->state->curB->stacks[g]->position+1 == pos)
  509. )
  510. )
  511. )
  512. return CGI->state->curB->stacks[g]->ID;
  513. }
  514. return -1;
  515. }
  516. CStack* CCallback::battleGetStackByID(int ID)
  517. {
  518. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  519. {
  520. if(CGI->state->curB->stacks[g]->ID == ID)
  521. return CGI->state->curB->stacks[g];
  522. }
  523. return NULL;
  524. }
  525. CStack* CCallback::battleGetStackByPos(int pos)
  526. {
  527. return battleGetStackByID(battleGetStack(pos));
  528. }
  529. int CCallback::battleGetPos(int stack)
  530. {
  531. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  532. {
  533. if(CGI->state->curB->stacks[g]->ID == stack)
  534. return CGI->state->curB->stacks[g]->position;
  535. }
  536. return -1;
  537. }
  538. std::map<int, CStack> CCallback::battleGetStacks()
  539. {
  540. std::map<int, CStack> ret;
  541. for(int g=0; g<CGI->state->curB->stacks.size(); ++g)
  542. {
  543. ret[CGI->state->curB->stacks[g]->ID] = *(CGI->state->curB->stacks[g]);
  544. }
  545. return ret;
  546. }
  547. CCreature CCallback::battleGetCreature(int number)
  548. {
  549. for(int h=0; h<CGI->state->curB->stacks.size(); ++h)
  550. {
  551. if(CGI->state->curB->stacks[h]->ID == number) //creature found
  552. return *(CGI->state->curB->stacks[h]->creature);
  553. }
  554. throw new std::exception("Cannot find the creature");
  555. }
  556. std::vector<int> CCallback::battleGetAvailableHexes(int ID)
  557. {
  558. return CGI->state->battleGetRange(ID);
  559. }
  560. bool CCallback::battleIsStackMine(int ID)
  561. {
  562. for(int h=0; h<CGI->state->curB->stacks.size(); ++h)
  563. {
  564. if(CGI->state->curB->stacks[h]->ID == ID) //creature found
  565. return CGI->state->curB->stacks[h]->owner == player;
  566. }
  567. return false;
  568. }