IGameCallback.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 "CHeroHandler.h" // for CHeroHandler
  13. #include "spells/CSpellHandler.h"// for CSpell
  14. #include "CSkillHandler.h"// for CSkill
  15. #include "CBonusTypeHandler.h"
  16. #include "CRandomGenerator.h"
  17. #include "BattleFieldHandler.h"
  18. #include "ObstacleHandler.h"
  19. #include "bonuses/Limiters.h"
  20. #include "bonuses/Propagators.h"
  21. #include "bonuses/Updaters.h"
  22. #include "networkPacks/ArtifactLocation.h"
  23. #include "serializer/CLoadFile.h"
  24. #include "serializer/CSaveFile.h"
  25. #include "rmg/CMapGenOptions.h"
  26. #include "mapObjectConstructors/AObjectTypeHandler.h"
  27. #include "mapObjectConstructors/CObjectClassesHandler.h"
  28. #include "mapObjects/CGMarket.h"
  29. #include "mapObjects/CGTownInstance.h"
  30. #include "mapObjects/CObjectHandler.h"
  31. #include "mapObjects/CQuest.h"
  32. #include "mapObjects/MiscObjects.h"
  33. #include "mapObjects/ObjectTemplate.h"
  34. #include "campaign/CampaignState.h"
  35. #include "StartInfo.h"
  36. #include "gameState/CGameState.h"
  37. #include "gameState/CGameStateCampaign.h"
  38. #include "gameState/TavernHeroesPool.h"
  39. #include "gameState/QuestInfo.h"
  40. #include "mapping/CMap.h"
  41. #include "modding/CModHandler.h"
  42. #include "modding/CModInfo.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(gs->map->levels());
  58. for(int b = 0; b < gs->map->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 < gs->map->width; xd++)
  66. {
  67. for (int yd = 0; yd < gs->map->height; yd++)
  68. {
  69. tinfo = getTile(int3 (xd,yd,zd));
  70. if (tinfo->terType->isLand() && tinfo->terType->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 : gs->getPlayerTeam(*player);
  93. for (int xd = std::max<int>(pos.x - radious , 0); xd <= std::min<int>(pos.x + radious, gs->map->width - 1); xd++)
  94. {
  95. for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gs->map->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 < gs->map->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 < gs->map->width; xd++)
  131. {
  132. for(int yd = 0; yd < gs->map->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<const CArtifact *> & out, vstd::RNG & rand)
  142. {
  143. for (int j = 0; j < 3 ; j++)
  144. out.push_back(gameState()->pickRandomArtifact(rand, CArtifact::ART_TREASURE).toArtifact());
  145. for (int j = 0; j < 3 ; j++)
  146. out.push_back(gameState()->pickRandomArtifact(rand, CArtifact::ART_MINOR).toArtifact());
  147. out.push_back(gameState()->pickRandomArtifact(rand, CArtifact::ART_MAJOR).toArtifact());
  148. }
  149. void CPrivilegedInfoCallback::getAllowedSpells(std::vector<SpellID> & out, std::optional<ui16> level)
  150. {
  151. for (auto const & spellID : gs->map->allowedSpells)
  152. {
  153. const auto * spell = spellID.toEntity(VLC);
  154. if (!isAllowed(spellID))
  155. continue;
  156. if (level.has_value() && spell->getLevel() != level)
  157. continue;
  158. out.push_back(spellID);
  159. }
  160. }
  161. CGameState * CPrivilegedInfoCallback::gameState()
  162. {
  163. return gs;
  164. }
  165. template<typename Loader>
  166. void CPrivilegedInfoCallback::loadCommonState(Loader & in)
  167. {
  168. logGlobal->info("Loading lib part of game...");
  169. in.checkMagicBytes(SAVEGAME_MAGIC);
  170. CMapHeader dum;
  171. StartInfo * si = nullptr;
  172. ActiveModsInSaveList activeMods;
  173. logGlobal->info("\tReading header");
  174. in.serializer & dum;
  175. logGlobal->info("\tReading options");
  176. in.serializer & si;
  177. logGlobal->info("\tReading mod list");
  178. in.serializer & activeMods;
  179. logGlobal->info("\tReading gamestate");
  180. in.serializer & gs;
  181. }
  182. template<typename Saver>
  183. void CPrivilegedInfoCallback::saveCommonState(Saver & out) const
  184. {
  185. ActiveModsInSaveList activeMods;
  186. logGlobal->info("Saving lib part of game...");
  187. out.putMagicBytes(SAVEGAME_MAGIC);
  188. logGlobal->info("\tSaving header");
  189. out.serializer & static_cast<CMapHeader&>(*gs->map);
  190. logGlobal->info("\tSaving options");
  191. out.serializer & gs->scenarioOps;
  192. logGlobal->info("\tSaving mod list");
  193. out.serializer & activeMods;
  194. logGlobal->info("\tSaving gamestate");
  195. out.serializer & gs;
  196. }
  197. // hardly memory usage for `-gdwarf-4` flag
  198. template DLL_LINKAGE void CPrivilegedInfoCallback::loadCommonState<CLoadFile>(CLoadFile &);
  199. template DLL_LINKAGE void CPrivilegedInfoCallback::saveCommonState<CSaveFile>(CSaveFile &) const;
  200. TerrainTile * CNonConstInfoCallback::getTile(const int3 & pos)
  201. {
  202. if(!gs->map->isInTheMap(pos))
  203. return nullptr;
  204. return &gs->map->getTile(pos);
  205. }
  206. CGHeroInstance * CNonConstInfoCallback::getHero(const ObjectInstanceID & objid)
  207. {
  208. return const_cast<CGHeroInstance*>(CGameInfoCallback::getHero(objid));
  209. }
  210. CGTownInstance * CNonConstInfoCallback::getTown(const ObjectInstanceID & objid)
  211. {
  212. return const_cast<CGTownInstance*>(CGameInfoCallback::getTown(objid));
  213. }
  214. TeamState * CNonConstInfoCallback::getTeam(const TeamID & teamID)
  215. {
  216. return const_cast<TeamState*>(CGameInfoCallback::getTeam(teamID));
  217. }
  218. TeamState * CNonConstInfoCallback::getPlayerTeam(const PlayerColor & color)
  219. {
  220. return const_cast<TeamState*>(CGameInfoCallback::getPlayerTeam(color));
  221. }
  222. PlayerState * CNonConstInfoCallback::getPlayerState(const PlayerColor & color, bool verbose)
  223. {
  224. return const_cast<PlayerState*>(CGameInfoCallback::getPlayerState(color, verbose));
  225. }
  226. CArtifactInstance * CNonConstInfoCallback::getArtInstance(const ArtifactInstanceID & aid)
  227. {
  228. return gs->map->artInstances.at(aid.num);
  229. }
  230. CGObjectInstance * CNonConstInfoCallback::getObjInstance(const ObjectInstanceID & oid)
  231. {
  232. return gs->map->objects.at(oid.num);
  233. }
  234. CArmedInstance * CNonConstInfoCallback::getArmyInstance(const ObjectInstanceID & oid)
  235. {
  236. return dynamic_cast<CArmedInstance *>(getObjInstance(oid));
  237. }
  238. CArtifactSet * CNonConstInfoCallback::getArtSet(const ArtifactLocation & loc)
  239. {
  240. if(auto hero = getHero(loc.artHolder))
  241. {
  242. if(loc.creature.has_value())
  243. {
  244. if(loc.creature.value() == SlotID::COMMANDER_SLOT_PLACEHOLDER)
  245. return hero->commander;
  246. else
  247. return hero->getStackPtr(loc.creature.value());
  248. }
  249. else
  250. {
  251. return hero;
  252. }
  253. }
  254. else if(auto army = getArmyInstance(loc.artHolder))
  255. {
  256. return army->getStackPtr(loc.creature.value());
  257. }
  258. else if(auto market = dynamic_cast<CGArtifactsAltar*>(getObjInstance(loc.artHolder)))
  259. {
  260. return market;
  261. }
  262. else
  263. {
  264. return nullptr;
  265. }
  266. }
  267. bool IGameCallback::isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero)
  268. {
  269. //only server knows
  270. logGlobal->error("isVisitCoveredByAnotherQuery call on client side");
  271. return false;
  272. }
  273. VCMI_LIB_NAMESPACE_END