IGameCallback.cpp 29 KB

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