IGameCallback.cpp 28 KB

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