2
0

IGameCallback.cpp 30 KB

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