IGameCallback.cpp 31 KB

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