IGameCallback.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * IGameCallback.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "IGameCallback.h"
  12. #include "spells/CSpellHandler.h"// for CSpell
  13. #include "CSkillHandler.h"// for CSkill
  14. #include "CBonusTypeHandler.h"
  15. #include "BattleFieldHandler.h"
  16. #include "ObstacleHandler.h"
  17. #include "bonuses/Limiters.h"
  18. #include "bonuses/Propagators.h"
  19. #include "bonuses/Updaters.h"
  20. #include "entities/artifact/CArtifact.h"
  21. #include "entities/building/CBuilding.h"
  22. #include "entities/hero/CHero.h"
  23. #include "networkPacks/ArtifactLocation.h"
  24. #include "serializer/CLoadFile.h"
  25. #include "rmg/CMapGenOptions.h"
  26. #include "mapObjectConstructors/AObjectTypeHandler.h"
  27. #include "mapObjectConstructors/CObjectClassesHandler.h"
  28. #include "mapObjects/CGMarket.h"
  29. #include "mapObjects/TownBuildingInstance.h"
  30. #include "mapObjects/CGHeroInstance.h"
  31. #include "mapObjects/CGTownInstance.h"
  32. #include "mapObjects/CObjectHandler.h"
  33. #include "mapObjects/CQuest.h"
  34. #include "mapObjects/MiscObjects.h"
  35. #include "mapObjects/ObjectTemplate.h"
  36. #include "campaign/CampaignState.h"
  37. #include "StartInfo.h"
  38. #include "gameState/CGameState.h"
  39. #include "gameState/CGameStateCampaign.h"
  40. #include "gameState/TavernHeroesPool.h"
  41. #include "gameState/QuestInfo.h"
  42. #include "mapping/CMap.h"
  43. #include "modding/CModHandler.h"
  44. #include "modding/IdentifierStorage.h"
  45. #include "modding/CModVersion.h"
  46. #include "modding/ActiveModsInSaveList.h"
  47. #include "CPlayerState.h"
  48. #include "GameSettings.h"
  49. #include "ScriptHandler.h"
  50. #include "RoadHandler.h"
  51. #include "RiverHandler.h"
  52. #include "TerrainHandler.h"
  53. #include <vstd/RNG.h>
  54. VCMI_LIB_NAMESPACE_BEGIN
  55. void CPrivilegedInfoCallback::getFreeTiles(std::vector<int3> & tiles) const
  56. {
  57. std::vector<int> floors;
  58. floors.reserve(gameState().getMap().levels());
  59. for(int b = 0; b < gameState().getMap().levels(); ++b)
  60. {
  61. floors.push_back(b);
  62. }
  63. const TerrainTile * tinfo = nullptr;
  64. for (auto zd : floors)
  65. {
  66. for (int xd = 0; xd < gameState().getMap().width; xd++)
  67. {
  68. for (int yd = 0; yd < gameState().getMap().height; yd++)
  69. {
  70. tinfo = getTile(int3 (xd,yd,zd));
  71. if (tinfo->isLand() && tinfo->getTerrain()->isPassable() && !tinfo->blocked()) //land and free
  72. tiles.emplace_back(xd, yd, zd);
  73. }
  74. }
  75. }
  76. }
  77. void CPrivilegedInfoCallback::getTilesInRange(std::unordered_set<int3> & tiles,
  78. const int3 & pos,
  79. int radious,
  80. ETileVisibility mode,
  81. std::optional<PlayerColor> player,
  82. int3::EDistanceFormula distanceFormula) const
  83. {
  84. if(!!player && !player->isValidPlayer())
  85. {
  86. logGlobal->error("Illegal call to getTilesInRange!");
  87. return;
  88. }
  89. if(radious == CBuilding::HEIGHT_SKYSHIP) //reveal entire map
  90. getAllTiles (tiles, player, -1, [](auto * tile){return true;});
  91. else
  92. {
  93. const TeamState * team = !player ? nullptr : gameState().getPlayerTeam(*player);
  94. for (int xd = std::max<int>(pos.x - radious , 0); xd <= std::min<int>(pos.x + radious, gameState().getMap().width - 1); xd++)
  95. {
  96. for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gameState().getMap().height - 1); yd++)
  97. {
  98. int3 tilePos(xd,yd,pos.z);
  99. int distance = pos.dist(tilePos, distanceFormula);
  100. if(distance <= radious)
  101. {
  102. if(!player
  103. || (mode == ETileVisibility::HIDDEN && team->fogOfWarMap[pos.z][xd][yd] == 0)
  104. || (mode == ETileVisibility::REVEALED && team->fogOfWarMap[pos.z][xd][yd] == 1)
  105. )
  106. tiles.insert(int3(xd,yd,pos.z));
  107. }
  108. }
  109. }
  110. }
  111. }
  112. void CPrivilegedInfoCallback::getAllTiles(std::unordered_set<int3> & tiles, std::optional<PlayerColor> Player, int level, std::function<bool(const TerrainTile *)> filter) const
  113. {
  114. if(!!Player && !Player->isValidPlayer())
  115. {
  116. logGlobal->error("Illegal call to getAllTiles !");
  117. return;
  118. }
  119. std::vector<int> floors;
  120. if(level == -1)
  121. {
  122. for(int b = 0; b < gameState().getMap().levels(); ++b)
  123. {
  124. floors.push_back(b);
  125. }
  126. }
  127. else
  128. floors.push_back(level);
  129. for(auto zd: floors)
  130. {
  131. for(int xd = 0; xd < gameState().getMap().width; xd++)
  132. {
  133. for(int yd = 0; yd < gameState().getMap().height; yd++)
  134. {
  135. int3 coordinates(xd, yd, zd);
  136. if (filter(getTile(coordinates)))
  137. tiles.insert(coordinates);
  138. }
  139. }
  140. }
  141. }
  142. void CPrivilegedInfoCallback::pickAllowedArtsSet(std::vector<ArtifactID> & out, vstd::RNG & rand)
  143. {
  144. for (int j = 0; j < 3 ; j++)
  145. out.push_back(gameState().pickRandomArtifact(rand, EArtifactClass::ART_TREASURE));
  146. for (int j = 0; j < 3 ; j++)
  147. out.push_back(gameState().pickRandomArtifact(rand, EArtifactClass::ART_MINOR));
  148. out.push_back(gameState().pickRandomArtifact(rand, EArtifactClass::ART_MAJOR));
  149. }
  150. void CPrivilegedInfoCallback::getAllowedSpells(std::vector<SpellID> & out, std::optional<ui16> level)
  151. {
  152. for (auto const & spellID : gameState().getMap().allowedSpells)
  153. {
  154. const auto * spell = spellID.toEntity(LIBRARY);
  155. if (!isAllowed(spellID))
  156. continue;
  157. if (level.has_value() && spell->getLevel() != level)
  158. continue;
  159. out.push_back(spellID);
  160. }
  161. }
  162. TerrainTile * CNonConstInfoCallback::getTile(const int3 & pos)
  163. {
  164. if(!gameState().getMap().isInTheMap(pos))
  165. return nullptr;
  166. return &gameState().getMap().getTile(pos);
  167. }
  168. CGHeroInstance * CNonConstInfoCallback::getHero(const ObjectInstanceID & objid)
  169. {
  170. return const_cast<CGHeroInstance*>(CGameInfoCallback::getHero(objid));
  171. }
  172. CGTownInstance * CNonConstInfoCallback::getTown(const ObjectInstanceID & objid)
  173. {
  174. return const_cast<CGTownInstance*>(CGameInfoCallback::getTown(objid));
  175. }
  176. TeamState * CNonConstInfoCallback::getTeam(const TeamID & teamID)
  177. {
  178. return const_cast<TeamState*>(CGameInfoCallback::getTeam(teamID));
  179. }
  180. TeamState * CNonConstInfoCallback::getPlayerTeam(const PlayerColor & color)
  181. {
  182. return const_cast<TeamState*>(CGameInfoCallback::getPlayerTeam(color));
  183. }
  184. PlayerState * CNonConstInfoCallback::getPlayerState(const PlayerColor & color, bool verbose)
  185. {
  186. return const_cast<PlayerState*>(CGameInfoCallback::getPlayerState(color, verbose));
  187. }
  188. CArtifactInstance * CNonConstInfoCallback::getArtInstance(const ArtifactInstanceID & aid)
  189. {
  190. return gameState().getMap().getArtifactInstance(aid);
  191. }
  192. CGObjectInstance * CNonConstInfoCallback::getObjInstance(const ObjectInstanceID & oid)
  193. {
  194. return gameState().getMap().getObject(oid);
  195. }
  196. CArmedInstance * CNonConstInfoCallback::getArmyInstance(const ObjectInstanceID & oid)
  197. {
  198. return dynamic_cast<CArmedInstance *>(getObjInstance(oid));
  199. }
  200. CArtifactSet * CNonConstInfoCallback::getArtSet(const ArtifactLocation & loc)
  201. {
  202. if(auto hero = getHero(loc.artHolder))
  203. {
  204. if(loc.creature.has_value())
  205. {
  206. if(loc.creature.value() == SlotID::COMMANDER_SLOT_PLACEHOLDER)
  207. return hero->getCommander();
  208. else
  209. return hero->getStackPtr(loc.creature.value());
  210. }
  211. else
  212. {
  213. return hero;
  214. }
  215. }
  216. else if(auto market = getMarket(loc.artHolder))
  217. {
  218. if(auto artSet = market->getArtifactsStorage())
  219. return artSet;
  220. }
  221. else if(auto army = getArmyInstance(loc.artHolder))
  222. {
  223. return army->getStackPtr(loc.creature.value());
  224. }
  225. return nullptr;
  226. }
  227. bool IGameCallback::isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero)
  228. {
  229. //only server knows
  230. logGlobal->error("isVisitCoveredByAnotherQuery call on client side");
  231. return false;
  232. }
  233. VCMI_LIB_NAMESPACE_END