IGameCallback.cpp 30 KB

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