IGameCallback.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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. std::vector < const CGObjectInstance * > CGameInfoCallback::getFlaggableObjects(int3 pos) const
  453. {
  454. std::vector<const CGObjectInstance *> ret;
  455. const TerrainTile *t = getTile(pos);
  456. ERROR_RET_VAL_IF(!t, "Not a valid tile requested!", ret);
  457. for(const CGObjectInstance *obj : t->blockingObjects)
  458. if(obj->tempOwner != PlayerColor::UNFLAGGABLE)
  459. ret.push_back(obj);
  460. // const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
  461. // for(size_t b=0; b<objs.size(); ++b)
  462. // {
  463. // 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))
  464. // ret.push_back(CGI->mh->ttiles[pos.x][pos.y][pos.z].objects[b].first);
  465. // }
  466. return ret;
  467. }
  468. int3 CGameInfoCallback::getMapSize() const
  469. {
  470. return int3(gs->map->width, gs->map->height, gs->map->twoLevel ? 2 : 1);
  471. }
  472. std::vector<const CGHeroInstance *> CGameInfoCallback::getAvailableHeroes(const CGObjectInstance * townOrTavern) const
  473. {
  474. ASSERT_IF_CALLED_WITH_PLAYER
  475. std::vector<const CGHeroInstance *> ret;
  476. //ERROR_RET_VAL_IF(!isOwnedOrVisited(townOrTavern), "Town or tavern must be owned or visited!", ret);
  477. //TODO: town needs to be owned, advmap tavern needs to be visited; to be reimplemented when visit tracking is done
  478. ret.resize(gs->players[*player].availableHeroes.size());
  479. std::copy(gs->players[*player].availableHeroes.begin(),gs->players[*player].availableHeroes.end(),ret.begin());
  480. return ret;
  481. }
  482. const TerrainTile * CGameInfoCallback::getTile( int3 tile, bool verbose) const
  483. {
  484. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!isVisible(tile), verbose, tile << " is not visible!", nullptr);
  485. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  486. return &gs->map->getTile(tile);
  487. }
  488. EBuildingState::EBuildingState CGameInfoCallback::canBuildStructure( const CGTownInstance *t, BuildingID ID )
  489. {
  490. ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", EBuildingState::TOWN_NOT_OWNED);
  491. if(!t->town->buildings.count(ID))
  492. return EBuildingState::BUILDING_ERROR;
  493. const CBuilding * building = t->town->buildings.at(ID);
  494. if(t->hasBuilt(ID)) //already built
  495. return EBuildingState::ALREADY_PRESENT;
  496. //can we build it?
  497. if(vstd::contains(t->forbiddenBuildings, ID))
  498. return EBuildingState::FORBIDDEN; //forbidden
  499. auto buildTest = [&](const BuildingID & id)
  500. {
  501. return t->hasBuilt(id);
  502. };
  503. if(t->builded >= VLC->modh->settings.MAX_BUILDING_PER_TURN)
  504. return EBuildingState::CANT_BUILD_TODAY; //building limit
  505. if (!building->requirements.test(buildTest))
  506. return EBuildingState::PREREQUIRES;
  507. if (building->upgrade != BuildingID::NONE && !t->hasBuilt(building->upgrade))
  508. return EBuildingState::MISSING_BASE;
  509. if(ID == BuildingID::CAPITOL)
  510. {
  511. const PlayerState *ps = getPlayer(t->tempOwner);
  512. if(ps)
  513. {
  514. for(const CGTownInstance *t : ps->towns)
  515. {
  516. if(t->hasBuilt(BuildingID::CAPITOL))
  517. {
  518. return EBuildingState::HAVE_CAPITAL; //no more than one capitol
  519. }
  520. }
  521. }
  522. }
  523. else if(ID == BuildingID::SHIPYARD)
  524. {
  525. const TerrainTile *tile = getTile(t->bestLocation(), false);
  526. if(!tile || tile->terType != ETerrainType::WATER)
  527. return EBuildingState::NO_WATER; //lack of water
  528. }
  529. //checking resources
  530. if(!building->resources.canBeAfforded(getPlayer(t->tempOwner)->resources))
  531. return EBuildingState::NO_RESOURCES; //lack of res
  532. return EBuildingState::ALLOWED;
  533. }
  534. const CMapHeader * CGameInfoCallback::getMapHeader() const
  535. {
  536. return gs->map;
  537. }
  538. bool CGameInfoCallback::hasAccess(boost::optional<PlayerColor> playerId) const
  539. {
  540. return !player || gs->getPlayerRelations( *playerId, *player ) != PlayerRelations::ENEMIES;
  541. }
  542. EPlayerStatus::EStatus CGameInfoCallback::getPlayerStatus(PlayerColor player) const
  543. {
  544. const PlayerState *ps = gs->getPlayer(player, false);
  545. ERROR_RET_VAL_IF(!ps, "No such player!", EPlayerStatus::WRONG);
  546. return ps->status;
  547. }
  548. std::string CGameInfoCallback::getTavernGossip(const CGObjectInstance * townOrTavern) const
  549. {
  550. return "GOSSIP TEST";
  551. }
  552. PlayerRelations::PlayerRelations CGameInfoCallback::getPlayerRelations( PlayerColor color1, PlayerColor color2 ) const
  553. {
  554. return gs->getPlayerRelations(color1, color2);
  555. }
  556. bool CGameInfoCallback::canGetFullInfo(const CGObjectInstance *obj) const
  557. {
  558. return !obj || hasAccess(obj->tempOwner);
  559. }
  560. int CGameInfoCallback::getHeroCount( PlayerColor player, bool includeGarrisoned ) const
  561. {
  562. int ret = 0;
  563. const PlayerState *p = gs->getPlayer(player);
  564. ERROR_RET_VAL_IF(!p, "No such player!", -1);
  565. if(includeGarrisoned)
  566. return p->heroes.size();
  567. else
  568. for(auto & elem : p->heroes)
  569. if(!elem->inTownGarrison)
  570. ret++;
  571. return ret;
  572. }
  573. bool CGameInfoCallback::isOwnedOrVisited(const CGObjectInstance *obj) const
  574. {
  575. if(canGetFullInfo(obj))
  576. return true;
  577. const TerrainTile *t = getTile(obj->visitablePos()); //get entrance tile
  578. const CGObjectInstance *visitor = t->visitableObjects.back(); //visitong hero if present or the obejct itself at last
  579. return visitor->ID == Obj::HERO && canGetFullInfo(visitor); //owned or allied hero is a visitor
  580. }
  581. PlayerColor CGameInfoCallback::getCurrentPlayer() const
  582. {
  583. return gs->currentPlayer;
  584. }
  585. CGameInfoCallback::CGameInfoCallback()
  586. {
  587. }
  588. CGameInfoCallback::CGameInfoCallback(CGameState *GS, boost::optional<PlayerColor> Player)
  589. {
  590. gs = GS;
  591. player = Player;
  592. }
  593. const std::vector< std::vector< std::vector<ui8> > > & CPlayerSpecificInfoCallback::getVisibilityMap() const
  594. {
  595. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  596. return gs->getPlayerTeam(*player)->fogOfWarMap;
  597. }
  598. int CPlayerSpecificInfoCallback::howManyTowns() const
  599. {
  600. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  601. ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", -1);
  602. return CGameInfoCallback::howManyTowns(*player);
  603. }
  604. std::vector < const CGTownInstance *> CPlayerSpecificInfoCallback::getTownsInfo(bool onlyOur) const
  605. {
  606. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  607. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  608. for(const auto & i : gs->players)
  609. {
  610. for(const auto & town : i.second.towns)
  611. {
  612. if (i.first==player || (isVisible(town, player) && !onlyOur))
  613. {
  614. ret.push_back(town);
  615. }
  616. }
  617. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  618. return ret;
  619. }
  620. std::vector < const CGHeroInstance *> CPlayerSpecificInfoCallback::getHeroesInfo(bool onlyOur) const
  621. {
  622. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  623. std::vector < const CGHeroInstance *> ret;
  624. for(auto hero : gs->map->heroesOnMap)
  625. {
  626. if( !player || (hero->tempOwner == *player) ||
  627. (isVisible(hero->getPosition(false), player) && !onlyOur) )
  628. {
  629. ret.push_back(hero);
  630. }
  631. }
  632. return ret;
  633. }
  634. boost::optional<PlayerColor> CPlayerSpecificInfoCallback::getMyColor() const
  635. {
  636. return player;
  637. }
  638. int CPlayerSpecificInfoCallback::getHeroSerial(const CGHeroInstance * hero, bool includeGarrisoned) const
  639. {
  640. if (hero->inTownGarrison && !includeGarrisoned)
  641. return -1;
  642. size_t index = 0;
  643. auto & heroes = gs->players[*player].heroes;
  644. for (auto & heroe : heroes)
  645. {
  646. if (includeGarrisoned || !(heroe)->inTownGarrison)
  647. index++;
  648. if (heroe == hero)
  649. return index;
  650. }
  651. return -1;
  652. }
  653. int3 CPlayerSpecificInfoCallback::getGrailPos( double &outKnownRatio )
  654. {
  655. if (!player || CGObelisk::obeliskCount == 0)
  656. {
  657. outKnownRatio = 0.0;
  658. }
  659. else
  660. {
  661. outKnownRatio = static_cast<double>(CGObelisk::visited[gs->getPlayerTeam(*player)->id]) / CGObelisk::obeliskCount;
  662. }
  663. return gs->map->grailPos;
  664. }
  665. std::vector < const CGObjectInstance * > CPlayerSpecificInfoCallback::getMyObjects() const
  666. {
  667. std::vector < const CGObjectInstance * > ret;
  668. for(const CGObjectInstance * obj : gs->map->objects)
  669. {
  670. if(obj && obj->tempOwner == player)
  671. ret.push_back(obj);
  672. }
  673. return ret;
  674. }
  675. std::vector < const CGDwelling * > CPlayerSpecificInfoCallback::getMyDwellings() const
  676. {
  677. ASSERT_IF_CALLED_WITH_PLAYER
  678. std::vector < const CGDwelling * > ret;
  679. for(CGDwelling * dw : gs->getPlayer(*player)->dwellings)
  680. {
  681. ret.push_back(dw);
  682. }
  683. return ret;
  684. }
  685. std::vector <QuestInfo> CPlayerSpecificInfoCallback::getMyQuests() const
  686. {
  687. std::vector <QuestInfo> ret;
  688. for (auto quest : gs->getPlayer(*player)->quests)
  689. {
  690. ret.push_back (quest);
  691. }
  692. return ret;
  693. }
  694. int CPlayerSpecificInfoCallback::howManyHeroes(bool includeGarrisoned) const
  695. {
  696. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  697. ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", -1);
  698. return getHeroCount(*player,includeGarrisoned);
  699. }
  700. const CGHeroInstance* CPlayerSpecificInfoCallback::getHeroBySerial(int serialId, bool includeGarrisoned) const
  701. {
  702. ASSERT_IF_CALLED_WITH_PLAYER
  703. const PlayerState *p = getPlayer(*player);
  704. ERROR_RET_VAL_IF(!p, "No player info", nullptr);
  705. if (!includeGarrisoned)
  706. {
  707. for(ui32 i = 0; i < p->heroes.size() && i<=serialId; i++)
  708. if(p->heroes[i]->inTownGarrison)
  709. serialId++;
  710. }
  711. ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->heroes.size(), "No player info", nullptr);
  712. return p->heroes[serialId];
  713. }
  714. const CGTownInstance* CPlayerSpecificInfoCallback::getTownBySerial(int serialId) const
  715. {
  716. ASSERT_IF_CALLED_WITH_PLAYER
  717. const PlayerState *p = getPlayer(*player);
  718. ERROR_RET_VAL_IF(!p, "No player info", nullptr);
  719. ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->towns.size(), "No player info", nullptr);
  720. return p->towns[serialId];
  721. }
  722. int CPlayerSpecificInfoCallback::getResourceAmount(Res::ERes type) const
  723. {
  724. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  725. ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", -1);
  726. return getResource(*player, type);
  727. }
  728. TResources CPlayerSpecificInfoCallback::getResourceAmount() const
  729. {
  730. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  731. ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", TResources());
  732. return gs->players[*player].resources;
  733. }
  734. CGHeroInstance *CNonConstInfoCallback::getHero(ObjectInstanceID objid)
  735. {
  736. return const_cast<CGHeroInstance*>(CGameInfoCallback::getHero(objid));
  737. }
  738. CGTownInstance *CNonConstInfoCallback::getTown(ObjectInstanceID objid)
  739. {
  740. return const_cast<CGTownInstance*>(CGameInfoCallback::getTown(objid));
  741. }
  742. TeamState *CNonConstInfoCallback::getTeam(TeamID teamID)
  743. {
  744. return const_cast<TeamState*>(CGameInfoCallback::getTeam(teamID));
  745. }
  746. TeamState *CNonConstInfoCallback::getPlayerTeam(PlayerColor color)
  747. {
  748. return const_cast<TeamState*>(CGameInfoCallback::getPlayerTeam(color));
  749. }
  750. PlayerState * CNonConstInfoCallback::getPlayer( PlayerColor color, bool verbose )
  751. {
  752. return const_cast<PlayerState*>(CGameInfoCallback::getPlayer(color, verbose));
  753. }
  754. CArtifactInstance * CNonConstInfoCallback::getArtInstance( ArtifactInstanceID aid )
  755. {
  756. return gs->map->artInstances[aid.num];
  757. }
  758. CGObjectInstance * CNonConstInfoCallback::getObjInstance( ObjectInstanceID oid )
  759. {
  760. return gs->map->objects[oid.num];
  761. }
  762. const TeamState * CGameInfoCallback::getTeam( TeamID teamID ) const
  763. {
  764. ERROR_RET_VAL_IF(!vstd::contains(gs->teams, teamID), "Cannot find info for team " << teamID, nullptr);
  765. const TeamState *ret = &gs->teams[teamID];
  766. ERROR_RET_VAL_IF(!!player && !vstd::contains(ret->players, *player), "Illegal attempt to access team data!", nullptr);
  767. return ret;
  768. }
  769. const TeamState * CGameInfoCallback::getPlayerTeam( PlayerColor color ) const
  770. {
  771. const PlayerState * ps = getPlayer(color);
  772. if (ps)
  773. return getTeam(ps->team);
  774. return nullptr;
  775. }
  776. const CGHeroInstance* CGameInfoCallback::getHeroWithSubid( int subid ) const
  777. {
  778. for(const CGHeroInstance *h : gs->map->heroesOnMap)
  779. if(h->subID == subid)
  780. return h;
  781. return nullptr;
  782. }
  783. PlayerColor CGameInfoCallback::getLocalPlayer() const
  784. {
  785. return getCurrentPlayer();
  786. }
  787. bool CGameInfoCallback::isInTheMap(const int3 &pos) const
  788. {
  789. return gs->map->isInTheMap(pos);
  790. }
  791. const CArtifactInstance * CGameInfoCallback::getArtInstance( ArtifactInstanceID aid ) const
  792. {
  793. return gs->map->artInstances[aid.num];
  794. }
  795. const CGObjectInstance * CGameInfoCallback::getObjInstance( ObjectInstanceID oid ) const
  796. {
  797. return gs->map->objects[oid.num];
  798. }
  799. void IGameEventRealizer::showInfoDialog( InfoWindow *iw )
  800. {
  801. commitPackage(iw);
  802. }
  803. void IGameEventRealizer::showInfoDialog(const std::string &msg, PlayerColor player)
  804. {
  805. InfoWindow iw;
  806. iw.player = player;
  807. iw.text << msg;
  808. showInfoDialog(&iw);
  809. }
  810. void IGameEventRealizer::setObjProperty(ObjectInstanceID objid, int prop, si64 val)
  811. {
  812. SetObjectProperty sob;
  813. sob.id = objid;
  814. sob.what = prop;
  815. sob.val = static_cast<ui32>(val);
  816. commitPackage(&sob);
  817. }
  818. const CGObjectInstance * IGameCallback::putNewObject(Obj ID, int subID, int3 pos)
  819. {
  820. NewObject no;
  821. no.ID = ID; //creature
  822. no.subID= subID;
  823. no.pos = pos;
  824. commitPackage(&no);
  825. return getObj(no.id); //id field will be filled during applying on gs
  826. }
  827. const CGCreature * IGameCallback::putNewMonster(CreatureID creID, int count, int3 pos)
  828. {
  829. const CGObjectInstance *m = putNewObject(Obj::MONSTER, creID, pos);
  830. setObjProperty(m->id, ObjProperty::MONSTER_COUNT, count);
  831. setObjProperty(m->id, ObjProperty::MONSTER_POWER, (si64)1000*count);
  832. return dynamic_cast<const CGCreature*>(m);
  833. }
  834. bool IGameCallback::isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero)
  835. {
  836. //only server knows
  837. assert(0);
  838. return false;
  839. }