IGameCallback.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. #include "StdInc.h"
  2. #include "IGameCallback.h"
  3. #include "../lib/CGameState.h"
  4. #include "../lib/map.h"
  5. #include "CObjectHandler.h"
  6. #include "CHeroHandler.h"
  7. #include "StartInfo.h"
  8. #include "CArtHandler.h"
  9. #include "CSpellHandler.h"
  10. #include "../lib/VCMI_Lib.h"
  11. #include <boost/random/linear_congruential.hpp>
  12. #include "CTownHandler.h"
  13. #include "BattleState.h"
  14. #include "NetPacks.h"
  15. #include "CBuildingHandler.h"
  16. #include "GameConstants.h"
  17. /*
  18. * IGameCallback.cpp, part of VCMI engine
  19. *
  20. * Authors: listed in file AUTHORS in main folder
  21. *
  22. * License: GNU General Public License v2.0 or later
  23. * Full text of license available in license.txt file, in main folder
  24. *
  25. */
  26. //TODO make clean
  27. #define ERROR_SILENT_RET_VAL_IF(cond, txt, retVal) do {if(cond){return retVal;}} while(0)
  28. #define ERROR_VERBOSE_OR_NOT_RET_VAL_IF(cond, verbose, txt, retVal) do {if(cond){if(verbose)tlog1 << BOOST_CURRENT_FUNCTION << ": " << txt << std::endl; return retVal;}} while(0)
  29. #define ERROR_RET_IF(cond, txt) do {if(cond){tlog1 << BOOST_CURRENT_FUNCTION << ": " << txt << std::endl; return;}} while(0)
  30. #define ERROR_RET_VAL_IF(cond, txt, retVal) do {if(cond){tlog1 << BOOST_CURRENT_FUNCTION << ": " << txt << std::endl; return retVal;}} while(0)
  31. extern boost::rand48 ran;
  32. CGameState * CPrivilagedInfoCallback::gameState ()
  33. {
  34. return gs;
  35. }
  36. int CGameInfoCallback::getOwner(int heroID) const
  37. {
  38. const CGObjectInstance *obj = getObj(heroID);
  39. ERROR_RET_VAL_IF(!obj, "No such object!", -1);
  40. return gs->map->objects[heroID]->tempOwner;
  41. }
  42. int CGameInfoCallback::getResource(int Player, int which) const
  43. {
  44. const PlayerState *p = getPlayer(Player);
  45. ERROR_RET_VAL_IF(!p, "No player info!", -1);
  46. ERROR_RET_VAL_IF(p->resources.size() <= which || which < 0, "No such resource!", -1);
  47. return p->resources[which];
  48. }
  49. const CGHeroInstance* CGameInfoCallback::getSelectedHero( int Player ) const
  50. {
  51. const PlayerState *p = getPlayer(Player);
  52. ERROR_RET_VAL_IF(!p, "No player info!", NULL);
  53. return getHero(p->currentSelection);
  54. }
  55. const CGHeroInstance* CGameInfoCallback::getSelectedHero() const
  56. {
  57. return getSelectedHero(gs->currentPlayer);
  58. }
  59. const PlayerSettings * CGameInfoCallback::getPlayerSettings(int color) const
  60. {
  61. return &gs->scenarioOps->getIthPlayersSettings(color);
  62. }
  63. void CPrivilagedInfoCallback::getTilesInRange( boost::unordered_set<int3, ShashInt3> &tiles, int3 pos, int radious, int player/*=-1*/, int mode/*=0*/ ) const
  64. {
  65. if(player >= GameConstants::PLAYER_LIMIT)
  66. {
  67. tlog1 << "Illegal call to getTilesInRange!\n";
  68. return;
  69. }
  70. if (radious == -1) //reveal entire map
  71. getAllTiles (tiles, player, -1, 0);
  72. else
  73. {
  74. const TeamState * team = gs->getPlayerTeam(player);
  75. for (int xd = std::max<int>(pos.x - radious , 0); xd <= std::min<int>(pos.x + radious, gs->map->width - 1); xd++)
  76. {
  77. for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gs->map->height - 1); yd++)
  78. {
  79. double distance = pos.dist2d(int3(xd,yd,pos.z)) - 0.5;
  80. if(distance <= radious)
  81. {
  82. if(player < 0
  83. || (mode == 1 && team->fogOfWarMap[xd][yd][pos.z]==0)
  84. || (mode == -1 && team->fogOfWarMap[xd][yd][pos.z]==1)
  85. )
  86. tiles.insert(int3(xd,yd,pos.z));
  87. }
  88. }
  89. }
  90. }
  91. }
  92. void CPrivilagedInfoCallback::getAllTiles (boost::unordered_set<int3, ShashInt3> &tiles, int Player/*=-1*/, int level, int surface ) const
  93. {
  94. if(Player >= GameConstants::PLAYER_LIMIT)
  95. {
  96. tlog1 << "Illegal call to getAllTiles !\n";
  97. return;
  98. }
  99. bool water = surface == 0 || surface == 2,
  100. land = surface == 0 || surface == 1;
  101. std::vector<int> floors;
  102. if(level == -1)
  103. {
  104. for (int xd = 0; xd <= gs->map->width - 1; xd++)
  105. for(int b=0; b<gs->map->twoLevel + 1; ++b) //if gs->map->twoLevel is false then false (0) + 1 is 1, if it's true (1) then we have 2
  106. {
  107. floors.push_back(b);
  108. }
  109. }
  110. else
  111. floors.push_back(level);
  112. for (std::vector<int>::const_iterator i = floors.begin(); i!= floors.end(); i++)
  113. {
  114. register int zd = *i;
  115. for (int xd = 0; xd < gs->map->width; xd++)
  116. {
  117. for (int yd = 0; yd < gs->map->height; yd++)
  118. {
  119. if ((getTile (int3 (xd,yd,zd))->tertype == 8 && water)
  120. || (getTile (int3 (xd,yd,zd))->tertype != 8 && land))
  121. tiles.insert(int3(xd,yd,zd));
  122. }
  123. }
  124. }
  125. }
  126. void CPrivilagedInfoCallback::getFreeTiles (std::vector<int3> &tiles) const
  127. {
  128. std::vector<int> floors;
  129. for (int b=0; b<gs->map->twoLevel + 1; ++b) //if gs->map->twoLevel is false then false (0) + 1 is 1, if it's true (1) then we have 2
  130. {
  131. floors.push_back(b);
  132. }
  133. const TerrainTile *tinfo;
  134. for (std::vector<int>::const_iterator i = floors.begin(); i!= floors.end(); i++)
  135. {
  136. register int zd = *i;
  137. for (int xd = 0; xd < gs->map->width; xd++)
  138. {
  139. for (int yd = 0; yd < gs->map->height; yd++)
  140. {
  141. tinfo = getTile(int3 (xd,yd,zd));
  142. if (tinfo->tertype != 8 && !tinfo->blocked) //land and free
  143. tiles.push_back (int3 (xd,yd,zd));
  144. }
  145. }
  146. }
  147. }
  148. bool CGameInfoCallback::isAllowed( int type, int id )
  149. {
  150. switch(type)
  151. {
  152. case 0:
  153. return gs->map->allowedSpell[id];
  154. case 1:
  155. return gs->map->allowedArtifact[id];
  156. case 2:
  157. return gs->map->allowedAbilities[id];
  158. default:
  159. ERROR_RET_VAL_IF(1, "Wrong type!", false);
  160. }
  161. }
  162. void CPrivilagedInfoCallback::pickAllowedArtsSet(std::vector<const CArtifact*> &out)
  163. {
  164. for (int i = 0; i < 2; i++)
  165. {
  166. for (int j = 0; j < 3 ; j++)
  167. {
  168. out.push_back(VLC->arth->artifacts[getRandomArt(CArtifact::ART_TREASURE << i)]);
  169. }
  170. }
  171. out.push_back(VLC->arth->artifacts[getRandomArt(CArtifact::ART_MAJOR)]);
  172. }
  173. ui16 CPrivilagedInfoCallback::getRandomArt (int flags)
  174. {
  175. return VLC->arth->getRandomArt(flags);
  176. }
  177. ui16 CPrivilagedInfoCallback::getArtSync (ui32 rand, int flags)
  178. {
  179. return VLC->arth->getArtSync (rand, flags);
  180. }
  181. void CPrivilagedInfoCallback::erasePickedArt (si32 id)
  182. {
  183. VLC->arth->erasePickedArt(id);
  184. }
  185. void CPrivilagedInfoCallback::getAllowedSpells(std::vector<ui16> &out, ui16 level)
  186. {
  187. CSpell *spell;
  188. for (ui32 i = 0; i < gs->map->allowedSpell.size(); i++) //spellh size appears to be greater (?)
  189. {
  190. spell = VLC->spellh->spells[i];
  191. if (isAllowed (0, spell->id) && spell->level == level)
  192. {
  193. out.push_back(spell->id);
  194. }
  195. }
  196. }
  197. inline TerrainTile * CNonConstInfoCallback::getTile( int3 pos )
  198. {
  199. if(!gs->map->isInTheMap(pos))
  200. return NULL;
  201. return &gs->map->getTile(pos);
  202. }
  203. const PlayerState * CGameInfoCallback::getPlayer(int color, bool verbose) const
  204. {
  205. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!hasAccess(color), verbose, "Cannot access player " << color << "info!", NULL);
  206. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!vstd::contains(gs->players,color), verbose, "Cannot find player " << color << "info!", NULL);
  207. return &gs->players[color];
  208. }
  209. const CTown * CGameInfoCallback::getNativeTown(int color) const
  210. {
  211. const PlayerSettings *ps = getPlayerSettings(color);
  212. ERROR_RET_VAL_IF(!ps, "There is no such player!", NULL);
  213. return &VLC->townh->towns[ps->castle];
  214. }
  215. const CGObjectInstance * CGameInfoCallback::getObjByQuestIdentifier(int identifier) const
  216. {
  217. ERROR_RET_VAL_IF(!vstd::contains(gs->map->questIdentifierToId, identifier), "There is no object with such quest identifier!", NULL);
  218. return getObj(gs->map->questIdentifierToId[identifier]);
  219. }
  220. /************************************************************************/
  221. /* */
  222. /************************************************************************/
  223. const CGObjectInstance* CGameInfoCallback::getObj(int objid, bool verbose) const
  224. {
  225. if(objid < 0 || objid >= gs->map->objects.size())
  226. {
  227. if(verbose)
  228. tlog1 << "Cannot get object with id " << objid << std::endl;
  229. return NULL;
  230. }
  231. const CGObjectInstance *ret = gs->map->objects[objid];
  232. if(!ret)
  233. {
  234. if(verbose)
  235. tlog1 << "Cannot get object with id " << objid << ". Object was removed.\n";
  236. return NULL;
  237. }
  238. if(!isVisible(ret, player))
  239. {
  240. if(verbose)
  241. tlog1 << "Cannot get object with id " << objid << ". Object is not visible.\n";
  242. return NULL;
  243. }
  244. return ret;
  245. }
  246. const CGHeroInstance* CGameInfoCallback::getHero(int objid) const
  247. {
  248. const CGObjectInstance *obj = getObj(objid, false);
  249. if(obj)
  250. return dynamic_cast<const CGHeroInstance*>(obj);
  251. else
  252. return NULL;
  253. }
  254. const CGTownInstance* CGameInfoCallback::getTown(int objid) const
  255. {
  256. const CGObjectInstance *obj = getObj(objid, false);
  257. if(obj)
  258. return dynamic_cast<const CGTownInstance*>(gs->map->objects[objid].get());
  259. else
  260. return NULL;
  261. }
  262. void CGameInfoCallback::getUpgradeInfo(const CArmedInstance *obj, int stackPos, UpgradeInfo &out) const
  263. {
  264. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  265. ERROR_RET_IF(!canGetFullInfo(obj), "Cannot get info about not owned object!");
  266. ERROR_RET_IF(!obj->hasStackAtSlot(stackPos), "There is no such stack!");
  267. out = gs->getUpgradeInfo(obj->getStack(stackPos));
  268. //return gs->getUpgradeInfo(obj->getStack(stackPos));
  269. }
  270. const StartInfo * CGameInfoCallback::getStartInfo(bool beforeRandomization /*= false*/) const
  271. {
  272. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  273. if(beforeRandomization)
  274. return gs->initialOpts;
  275. else
  276. return gs->scenarioOps;
  277. }
  278. int CGameInfoCallback::getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const
  279. {
  280. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  281. ERROR_RET_VAL_IF(!canGetFullInfo(caster), "Cannot get info about caster!", -1);
  282. //if there is a battle
  283. if(gs->curB)
  284. return gs->curB->battleGetSpellCost(sp, caster);
  285. //if there is no battle
  286. return caster->getSpellCost(sp);
  287. }
  288. int CGameInfoCallback::estimateSpellDamage(const CSpell * sp, const CGHeroInstance * hero) const
  289. {
  290. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  291. ERROR_RET_VAL_IF(hero && !canGetFullInfo(hero), "Cannot get info about caster!", -1);
  292. if(!gs->curB) //no battle
  293. {
  294. if (hero) //but we see hero's spellbook
  295. return gs->curB->calculateSpellDmg(sp, hero, NULL, hero->getSpellSchoolLevel(sp), hero->getPrimSkillLevel(2));
  296. else
  297. return 0; //mage guild
  298. }
  299. //gs->getHero(gs->currentPlayer)
  300. //const CGHeroInstance * ourHero = gs->curB->heroes[0]->tempOwner == player ? gs->curB->heroes[0] : gs->curB->heroes[1];
  301. const CGHeroInstance * ourHero = hero;
  302. return gs->curB->calculateSpellDmg(sp, ourHero, NULL, ourHero->getSpellSchoolLevel(sp), ourHero->getPrimSkillLevel(2));
  303. }
  304. void CGameInfoCallback::getThievesGuildInfo(SThievesGuildInfo & thi, const CGObjectInstance * obj)
  305. {
  306. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  307. ERROR_RET_IF(!obj, "No guild object!");
  308. ERROR_RET_IF(obj->ID == GameConstants::TOWNI_TYPE && !canGetFullInfo(obj), "Cannot get info about town guild object!");
  309. //TODO: advmap object -> check if they're visited by our hero
  310. if(obj->ID == GameConstants::TOWNI_TYPE || obj->ID == 95) //it is a town or adv map tavern
  311. {
  312. gs->obtainPlayersStats(thi, gs->players[obj->tempOwner].towns.size());
  313. }
  314. else if(obj->ID == 97) //Den of Thieves
  315. {
  316. gs->obtainPlayersStats(thi, 20);
  317. }
  318. }
  319. int CGameInfoCallback::howManyTowns(int Player) const
  320. {
  321. ERROR_RET_VAL_IF(!hasAccess(Player), "Access forbidden!", -1);
  322. return gs->players[Player].towns.size();
  323. }
  324. bool CGameInfoCallback::getTownInfo( const CGObjectInstance *town, InfoAboutTown &dest ) const
  325. {
  326. ERROR_RET_VAL_IF(!isVisible(town, player), "Town is not visible!", false); //it's not a town or it's not visible for layer
  327. bool detailed = hasAccess(town->tempOwner);
  328. //TODO vision support
  329. if(town->ID == GameConstants::TOWNI_TYPE)
  330. dest.initFromTown(static_cast<const CGTownInstance *>(town), detailed);
  331. else if(town->ID == Obj::GARRISON || town->ID == Obj::GARRISON2)
  332. dest.initFromArmy(static_cast<const CArmedInstance *>(town), detailed);
  333. else
  334. return false;
  335. return true;
  336. }
  337. int3 CGameInfoCallback::guardingCreaturePosition (int3 pos) const
  338. {
  339. ERROR_RET_VAL_IF(!isVisible(pos), "Tile is not visible!", int3(-1,-1,-1));
  340. return gs->guardingCreaturePosition(pos);
  341. }
  342. bool CGameInfoCallback::getHeroInfo( const CGObjectInstance *hero, InfoAboutHero &dest ) const
  343. {
  344. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(hero);
  345. ERROR_RET_VAL_IF(!h, "That's not a hero!", false);
  346. ERROR_RET_VAL_IF(!isVisible(h->getPosition(false)), "That hero is not visible!", false);
  347. //TODO vision support
  348. dest.initFromHero(h, hasAccess(h->tempOwner));
  349. return true;
  350. }
  351. int CGameInfoCallback::getDate(int mode) const
  352. {
  353. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  354. return gs->getDate(mode);
  355. }
  356. std::vector < std::string > CGameInfoCallback::getObjDescriptions(int3 pos) const
  357. {
  358. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  359. std::vector<std::string> ret;
  360. const TerrainTile *t = getTile(pos);
  361. ERROR_RET_VAL_IF(!t, "Not a valid tile given!", ret);
  362. BOOST_FOREACH(const CGObjectInstance * obj, t->blockingObjects)
  363. ret.push_back(obj->getHoverText());
  364. return ret;
  365. }
  366. bool CGameInfoCallback::verifyPath(CPath * path, bool blockSea) const
  367. {
  368. for (size_t i=0; i < path->nodes.size(); ++i)
  369. {
  370. const TerrainTile *t = getTile(path->nodes[i].coord); //current tile
  371. ERROR_RET_VAL_IF(!t, "Path contains not visible tile: " << path->nodes[i].coord << "!", false);
  372. if (t->blocked && !t->visitable)
  373. return false; //path is wrong - one of the tiles is blocked
  374. if (blockSea)
  375. {
  376. if (i==0)
  377. continue;
  378. const TerrainTile *prev = getTile(path->nodes[i-1].coord); //tile of previous node on the path
  379. if (( t->tertype == TerrainTile::water && prev->tertype != TerrainTile::water)
  380. || (t->tertype != TerrainTile::water && prev->tertype == TerrainTile::water)
  381. || prev->tertype == TerrainTile::rock
  382. )
  383. return false;
  384. }
  385. }
  386. return true;
  387. }
  388. bool CGameInfoCallback::isVisible(int3 pos, int Player) const
  389. {
  390. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  391. return gs->map->isInTheMap(pos) && (Player == -1 || gs->isVisible(pos, Player));
  392. }
  393. bool CGameInfoCallback::isVisible(int3 pos) const
  394. {
  395. return isVisible(pos,player);
  396. }
  397. bool CGameInfoCallback::isVisible( const CGObjectInstance *obj, int Player ) const
  398. {
  399. return gs->isVisible(obj, Player);
  400. }
  401. bool CGameInfoCallback::isVisible(const CGObjectInstance *obj) const
  402. {
  403. return isVisible(obj, player);
  404. }
  405. // const CCreatureSet* CInfoCallback::getGarrison(const CGObjectInstance *obj) const
  406. // {
  407. // //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  408. // if()
  409. // const CArmedInstance *armi = dynamic_cast<const CArmedInstance*>(obj);
  410. // if(!armi)
  411. // return NULL;
  412. // else
  413. // return armi;
  414. // }
  415. std::vector < const CGObjectInstance * > CGameInfoCallback::getBlockingObjs( int3 pos ) const
  416. {
  417. std::vector<const CGObjectInstance *> ret;
  418. const TerrainTile *t = getTile(pos);
  419. ERROR_RET_VAL_IF(!t, "Not a valid tile requested!", ret);
  420. BOOST_FOREACH(const CGObjectInstance * obj, t->blockingObjects)
  421. ret.push_back(obj);
  422. return ret;
  423. }
  424. std::vector <const CGObjectInstance * > CGameInfoCallback::getVisitableObjs(int3 pos, bool verbose /*= true*/) const
  425. {
  426. std::vector<const CGObjectInstance *> ret;
  427. const TerrainTile *t = getTile(pos, verbose);
  428. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!t, verbose, pos << " is not visible!", ret);
  429. BOOST_FOREACH(const CGObjectInstance * obj, t->visitableObjects)
  430. {
  431. if(player < 0 || obj->ID != GameConstants::EVENTI_TYPE) //hide events from players
  432. ret.push_back(obj);
  433. }
  434. return ret;
  435. }
  436. std::vector < const CGObjectInstance * > CGameInfoCallback::getFlaggableObjects(int3 pos) const
  437. {
  438. std::vector<const CGObjectInstance *> ret;
  439. const TerrainTile *t = getTile(pos);
  440. ERROR_RET_VAL_IF(!t, "Not a valid tile requested!", ret);
  441. BOOST_FOREACH(const CGObjectInstance *obj, t->blockingObjects)
  442. if(obj->tempOwner != 254)
  443. ret.push_back(obj);
  444. // const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
  445. // for(size_t b=0; b<objs.size(); ++b)
  446. // {
  447. // if(objs[b].first->tempOwner!=254 && !((objs[b].first->defInfo->blockMap[pos.y - objs[b].first->pos.y + 5] >> (objs[b].first->pos.x - pos.x)) & 1))
  448. // ret.push_back(CGI->mh->ttiles[pos.x][pos.y][pos.z].objects[b].first);
  449. // }
  450. return ret;
  451. }
  452. int3 CGameInfoCallback::getMapSize() const
  453. {
  454. return int3(gs->map->width, gs->map->height, gs->map->twoLevel+1);
  455. }
  456. std::vector<const CGHeroInstance *> CGameInfoCallback::getAvailableHeroes(const CGObjectInstance * townOrTavern) const
  457. {
  458. std::vector<const CGHeroInstance *> ret;
  459. //ERROR_RET_VAL_IF(!isOwnedOrVisited(townOrTavern), "Town or tavern must be owned or visited!", ret);
  460. //TODO: town needs to be owned, advmap tavern needs to be visited; to be reimplemented when visit tracking is done
  461. ret.resize(gs->players[player].availableHeroes.size());
  462. std::copy(gs->players[player].availableHeroes.begin(),gs->players[player].availableHeroes.end(),ret.begin());
  463. return ret;
  464. }
  465. const TerrainTile * CGameInfoCallback::getTile( int3 tile, bool verbose) const
  466. {
  467. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!isVisible(tile), verbose, tile << " is not visible!", NULL);
  468. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  469. return &gs->map->getTile(tile);
  470. }
  471. int CGameInfoCallback::canBuildStructure( const CGTownInstance *t, int ID )
  472. {
  473. ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", -1);
  474. int ret = EBuildingState::ALLOWED;
  475. if(t->builded >= GameConstants::MAX_BUILDING_PER_TURN)
  476. ret = EBuildingState::CANT_BUILD_TODAY; //building limit
  477. CBuilding * pom = t->town->buildings[ID];
  478. if(!pom)
  479. return EBuildingState::BUILDING_ERROR;
  480. //checking resources
  481. if(!pom->resources.canBeAfforded(getPlayer(t->tempOwner)->resources))
  482. ret = EBuildingState::NO_RESOURCES; //lack of res
  483. //checking for requirements
  484. std::set<int> reqs = getBuildingRequiments(t, ID);//getting all requirements
  485. for( std::set<int>::iterator ri = reqs.begin(); ri != reqs.end(); ri++ )
  486. {
  487. if(t->builtBuildings.find(*ri)==t->builtBuildings.end())
  488. ret = EBuildingState::PREREQUIRES; //lack of requirements - cannot build
  489. }
  490. //can we build it?
  491. if(t->forbiddenBuildings.find(ID)!=t->forbiddenBuildings.end())
  492. ret = EBuildingState::FORBIDDEN; //forbidden
  493. if(ID == 13) //capitol
  494. {
  495. const PlayerState *ps = getPlayer(t->tempOwner);
  496. if(ps)
  497. {
  498. BOOST_FOREACH(const CGTownInstance *t, ps->towns)
  499. {
  500. if(vstd::contains(t->builtBuildings, 13))
  501. {
  502. ret = EBuildingState::HAVE_CAPITAL; //no more than one capitol
  503. break;
  504. }
  505. }
  506. }
  507. }
  508. else if(ID == 6) //shipyard
  509. {
  510. const TerrainTile *tile = getTile(t->bestLocation(), false);
  511. if(!tile || tile->tertype != TerrainTile::water )
  512. ret = EBuildingState::NO_WATER; //lack of water
  513. }
  514. if(t->builtBuildings.find(ID)!=t->builtBuildings.end()) //already built
  515. ret = EBuildingState::ALREADY_PRESENT;
  516. return ret;
  517. }
  518. std::set<int> CGameInfoCallback::getBuildingRequiments( const CGTownInstance *t, int ID )
  519. {
  520. ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", std::set<int>());
  521. std::set<int> used;
  522. used.insert(ID);
  523. std::set<int> reqs = t->town->buildings[ID]->requirements;
  524. while(true)
  525. {
  526. size_t noloop=0;
  527. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  528. {
  529. if(used.find(*i)==used.end()) //we haven't added requirements for this building
  530. {
  531. used.insert(*i);
  532. for(
  533. std::set<int>::iterator j= t->town->buildings[*i]->requirements.begin();
  534. j!= t->town->buildings[*i]->requirements.end();
  535. j++)
  536. {
  537. reqs.insert(*j);//creating full list of requirements
  538. }
  539. }
  540. else
  541. {
  542. noloop++;
  543. }
  544. }
  545. if(noloop==reqs.size())
  546. break;
  547. }
  548. return reqs;
  549. }
  550. const CMapHeader * CGameInfoCallback::getMapHeader() const
  551. {
  552. return gs->map;
  553. }
  554. bool CGameInfoCallback::hasAccess(int playerId) const
  555. {
  556. return player < 0 || gs->getPlayerRelations( playerId, player );
  557. }
  558. int CGameInfoCallback::getPlayerStatus(int player) const
  559. {
  560. const PlayerState *ps = gs->getPlayer(player, false);
  561. if(!ps)
  562. return -1;
  563. return ps->status;
  564. }
  565. std::string CGameInfoCallback::getTavernGossip(const CGObjectInstance * townOrTavern) const
  566. {
  567. return "GOSSIP TEST";
  568. }
  569. int CGameInfoCallback::getPlayerRelations( ui8 color1, ui8 color2 ) const
  570. {
  571. return gs->getPlayerRelations(color1, color2);
  572. }
  573. bool CGameInfoCallback::canGetFullInfo(const CGObjectInstance *obj) const
  574. {
  575. return !obj || hasAccess(obj->tempOwner);
  576. }
  577. int CGameInfoCallback::getHeroCount( int player, bool includeGarrisoned ) const
  578. {
  579. int ret = 0;
  580. const PlayerState *p = gs->getPlayer(player);
  581. ERROR_RET_VAL_IF(!p, "No such player!", -1);
  582. if(includeGarrisoned)
  583. return p->heroes.size();
  584. else
  585. for(ui32 i = 0; i < p->heroes.size(); i++)
  586. if(!p->heroes[i]->inTownGarrison)
  587. ret++;
  588. return ret;
  589. }
  590. bool CGameInfoCallback::isOwnedOrVisited(const CGObjectInstance *obj) const
  591. {
  592. if(canGetFullInfo(obj))
  593. return true;
  594. const TerrainTile *t = getTile(obj->visitablePos()); //get entrance tile
  595. const CGObjectInstance *visitor = t->visitableObjects.back(); //visitong hero if present or the obejct itself at last
  596. return visitor->ID == GameConstants::HEROI_TYPE && canGetFullInfo(visitor); //owned or allied hero is a visitor
  597. }
  598. int CGameInfoCallback::getCurrentPlayer() const
  599. {
  600. return gs->currentPlayer;
  601. }
  602. CGameInfoCallback::CGameInfoCallback()
  603. {
  604. }
  605. CGameInfoCallback::CGameInfoCallback(CGameState *GS, int Player)
  606. {
  607. gs = GS;
  608. player = Player;
  609. }
  610. const std::vector< std::vector< std::vector<ui8> > > & CPlayerSpecificInfoCallback::getVisibilityMap() const
  611. {
  612. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  613. return gs->getPlayerTeam(player)->fogOfWarMap;
  614. }
  615. int CPlayerSpecificInfoCallback::howManyTowns() const
  616. {
  617. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  618. ERROR_RET_VAL_IF(player == -1, "Applicable only for player callbacks", -1);
  619. return CGameInfoCallback::howManyTowns(player);
  620. }
  621. std::vector < const CGTownInstance *> CPlayerSpecificInfoCallback::getTownsInfo(bool onlyOur) const
  622. {
  623. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  624. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  625. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  626. {
  627. for (size_t j = 0; j < (*i).second.towns.size(); ++j)
  628. {
  629. if ((*i).first==player
  630. || (isVisible((*i).second.towns[j],player) && !onlyOur))
  631. {
  632. ret.push_back((*i).second.towns[j]);
  633. }
  634. }
  635. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  636. return ret;
  637. }
  638. std::vector < const CGHeroInstance *> CPlayerSpecificInfoCallback::getHeroesInfo(bool onlyOur) const
  639. {
  640. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  641. std::vector < const CGHeroInstance *> ret;
  642. for(size_t i=0;i<gs->map->heroes.size();i++)
  643. {
  644. if( (gs->map->heroes[i]->tempOwner==player) ||
  645. (isVisible(gs->map->heroes[i]->getPosition(false),player) && !onlyOur) )
  646. {
  647. ret.push_back(gs->map->heroes[i]);
  648. }
  649. }
  650. return ret;
  651. }
  652. int CPlayerSpecificInfoCallback::getMyColor() const
  653. {
  654. return player;
  655. }
  656. int CPlayerSpecificInfoCallback::getHeroSerial(const CGHeroInstance * hero, bool includeGarrisoned) const
  657. {
  658. if (hero->inTownGarrison && !includeGarrisoned)
  659. return -1;
  660. size_t index = 0;
  661. auto & heroes = gs->players[player].heroes;
  662. for (auto curHero= heroes.begin(); curHero!=heroes.end(); curHero++)
  663. {
  664. if (includeGarrisoned || !(*curHero)->inTownGarrison)
  665. index++;
  666. if (*curHero == hero)
  667. return index;
  668. }
  669. return -1;
  670. }
  671. int3 CPlayerSpecificInfoCallback::getGrailPos( double &outKnownRatio )
  672. {
  673. if (CGObelisk::obeliskCount == 0)
  674. {
  675. outKnownRatio = 0.0;
  676. }
  677. else
  678. {
  679. outKnownRatio = static_cast<double>(CGObelisk::visited[gs->getPlayerTeam(player)->id]) / CGObelisk::obeliskCount;
  680. }
  681. return gs->map->grailPos;
  682. }
  683. std::vector < const CGObjectInstance * > CPlayerSpecificInfoCallback::getMyObjects() const
  684. {
  685. std::vector < const CGObjectInstance * > ret;
  686. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->objects)
  687. {
  688. if(obj && obj->tempOwner == player)
  689. ret.push_back(obj);
  690. }
  691. return ret;
  692. }
  693. std::vector < const CGDwelling * > CPlayerSpecificInfoCallback::getMyDwellings() const
  694. {
  695. std::vector < const CGDwelling * > ret;
  696. BOOST_FOREACH(CGDwelling * dw, gs->getPlayer(player)->dwellings)
  697. {
  698. ret.push_back(dw);
  699. }
  700. return ret;
  701. }
  702. std::vector <QuestInfo> CPlayerSpecificInfoCallback::getMyQuests() const
  703. {
  704. std::vector <QuestInfo> ret;
  705. BOOST_FOREACH (auto quest, gs->getPlayer(player)->quests)
  706. {
  707. ret.push_back (quest);
  708. }
  709. return ret;
  710. }
  711. int CPlayerSpecificInfoCallback::howManyHeroes(bool includeGarrisoned) const
  712. {
  713. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  714. ERROR_RET_VAL_IF(player == -1, "Applicable only for player callbacks", -1);
  715. return getHeroCount(player,includeGarrisoned);
  716. }
  717. const CGHeroInstance* CPlayerSpecificInfoCallback::getHeroBySerial(int serialId, bool includeGarrisoned) const
  718. {
  719. const PlayerState *p = getPlayer(player);
  720. ERROR_RET_VAL_IF(!p, "No player info", NULL);
  721. if (!includeGarrisoned)
  722. {
  723. for(ui32 i = 0; i < p->heroes.size() && i<=serialId; i++)
  724. if(p->heroes[i]->inTownGarrison)
  725. serialId++;
  726. }
  727. ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->heroes.size(), "No player info", NULL);
  728. return p->heroes[serialId];
  729. }
  730. const CGTownInstance* CPlayerSpecificInfoCallback::getTownBySerial(int serialId) const
  731. {
  732. const PlayerState *p = getPlayer(player);
  733. ERROR_RET_VAL_IF(!p, "No player info", NULL);
  734. ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->towns.size(), "No player info", NULL);
  735. return p->towns[serialId];
  736. }
  737. int CPlayerSpecificInfoCallback::getResourceAmount(int type) const
  738. {
  739. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  740. ERROR_RET_VAL_IF(player == -1, "Applicable only for player callbacks", -1);
  741. return getResource(player, type);
  742. }
  743. TResources CPlayerSpecificInfoCallback::getResourceAmount() const
  744. {
  745. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  746. ERROR_RET_VAL_IF(player == -1, "Applicable only for player callbacks", TResources());
  747. return gs->players[player].resources;
  748. }
  749. CGHeroInstance *CNonConstInfoCallback::getHero(int objid)
  750. {
  751. return const_cast<CGHeroInstance*>(CGameInfoCallback::getHero(objid));
  752. }
  753. CGTownInstance *CNonConstInfoCallback::getTown(int objid)
  754. {
  755. return const_cast<CGTownInstance*>(CGameInfoCallback::getTown(objid));
  756. }
  757. TeamState *CNonConstInfoCallback::getTeam(ui8 teamID)
  758. {
  759. return const_cast<TeamState*>(CGameInfoCallback::getTeam(teamID));
  760. }
  761. TeamState *CNonConstInfoCallback::getPlayerTeam(ui8 color)
  762. {
  763. return const_cast<TeamState*>(CGameInfoCallback::getPlayerTeam(color));
  764. }
  765. PlayerState * CNonConstInfoCallback::getPlayer( ui8 color, bool verbose )
  766. {
  767. return const_cast<PlayerState*>(CGameInfoCallback::getPlayer(color, verbose));
  768. }
  769. const TeamState * CGameInfoCallback::getTeam( ui8 teamID ) const
  770. {
  771. ERROR_RET_VAL_IF(!vstd::contains(gs->teams, teamID), "Cannot find info for team " << int(teamID), NULL);
  772. const TeamState *ret = &gs->teams[teamID];
  773. ERROR_RET_VAL_IF(player != -1 && !vstd::contains(ret->players, player), "Illegal attempt to access team data!", NULL);
  774. return ret;
  775. }
  776. const TeamState * CGameInfoCallback::getPlayerTeam( ui8 teamID ) const
  777. {
  778. const PlayerState * ps = getPlayer(teamID);
  779. if (ps)
  780. return getTeam(ps->team);
  781. return NULL;
  782. }
  783. const CGHeroInstance* CGameInfoCallback::getHeroWithSubid( int subid ) const
  784. {
  785. BOOST_FOREACH(const CGHeroInstance *h, gs->map->heroes)
  786. if(h->subID == subid)
  787. return h;
  788. return NULL;
  789. }
  790. int CGameInfoCallback::getLocalPlayer() const
  791. {
  792. return getCurrentPlayer();
  793. }
  794. bool CGameInfoCallback::isInTheMap(const int3 &pos) const
  795. {
  796. return gs->map->isInTheMap(pos);
  797. }
  798. void IGameEventRealizer::showInfoDialog( InfoWindow *iw )
  799. {
  800. commitPackage(iw);
  801. }
  802. void IGameEventRealizer::showInfoDialog(const std::string &msg, int player)
  803. {
  804. InfoWindow iw;
  805. iw.player = player;
  806. iw.text << msg;
  807. showInfoDialog(&iw);
  808. }
  809. void IGameEventRealizer::setObjProperty(int objid, int prop, si64 val)
  810. {
  811. SetObjectProperty sob;
  812. sob.id = objid;
  813. sob.what = prop;
  814. sob.val = static_cast<ui32>(val);
  815. commitPackage(&sob);
  816. }
  817. const CGObjectInstance * IGameCallback::putNewObject(int ID, int subID, int3 pos)
  818. {
  819. NewObject no;
  820. no.ID = ID; //creature
  821. no.subID= subID;
  822. no.pos = pos;
  823. commitPackage(&no);
  824. return getObj(no.id); //id field will be filled during applying on gs
  825. }
  826. const CGCreature * IGameCallback::putNewMonster(int creID, int count, int3 pos)
  827. {
  828. const CGObjectInstance *m = putNewObject(54, creID, pos);
  829. setObjProperty(m->id, ObjProperty::MONSTER_COUNT, count);
  830. setObjProperty(m->id, ObjProperty::MONSTER_POWER, (si64)1000*count);
  831. return dynamic_cast<const CGCreature*>(m);
  832. }