IGameCallback.cpp 30 KB

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