IGameCallback.cpp 7.2 KB

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