IGameCallback.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "BattleFieldHandler.h"
  17. #include "ObstacleHandler.h"
  18. #include "bonuses/Limiters.h"
  19. #include "bonuses/Propagators.h"
  20. #include "bonuses/Updaters.h"
  21. #include "networkPacks/ArtifactLocation.h"
  22. #include "serializer/CLoadFile.h"
  23. #include "serializer/CSaveFile.h"
  24. #include "rmg/CMapGenOptions.h"
  25. #include "mapObjectConstructors/AObjectTypeHandler.h"
  26. #include "mapObjectConstructors/CObjectClassesHandler.h"
  27. #include "mapObjects/CGMarket.h"
  28. #include "mapObjects/CGTownInstance.h"
  29. #include "mapObjects/CObjectHandler.h"
  30. #include "mapObjects/CQuest.h"
  31. #include "mapObjects/MiscObjects.h"
  32. #include "mapObjects/ObjectTemplate.h"
  33. #include "campaign/CampaignState.h"
  34. #include "StartInfo.h"
  35. #include "gameState/CGameState.h"
  36. #include "gameState/CGameStateCampaign.h"
  37. #include "gameState/TavernHeroesPool.h"
  38. #include "gameState/QuestInfo.h"
  39. #include "mapping/CMap.h"
  40. #include "modding/CModHandler.h"
  41. #include "modding/CModInfo.h"
  42. #include "modding/IdentifierStorage.h"
  43. #include "modding/CModVersion.h"
  44. #include "modding/ActiveModsInSaveList.h"
  45. #include "CPlayerState.h"
  46. #include "GameSettings.h"
  47. #include "ScriptHandler.h"
  48. #include "RoadHandler.h"
  49. #include "RiverHandler.h"
  50. #include "TerrainHandler.h"
  51. #include <vstd/RNG.h>
  52. VCMI_LIB_NAMESPACE_BEGIN
  53. void CPrivilegedInfoCallback::getFreeTiles(std::vector<int3> & tiles) const
  54. {
  55. std::vector<int> floors;
  56. floors.reserve(gs->map->levels());
  57. for(int b = 0; b < gs->map->levels(); ++b)
  58. {
  59. floors.push_back(b);
  60. }
  61. const TerrainTile * tinfo = nullptr;
  62. for (auto zd : floors)
  63. {
  64. for (int xd = 0; xd < gs->map->width; xd++)
  65. {
  66. for (int yd = 0; yd < gs->map->height; yd++)
  67. {
  68. tinfo = getTile(int3 (xd,yd,zd));
  69. if (tinfo->terType->isLand() && tinfo->terType->isPassable() && !tinfo->blocked) //land and free
  70. tiles.emplace_back(xd, yd, zd);
  71. }
  72. }
  73. }
  74. }
  75. void CPrivilegedInfoCallback::getTilesInRange(std::unordered_set<int3> & tiles,
  76. const int3 & pos,
  77. int radious,
  78. ETileVisibility mode,
  79. std::optional<PlayerColor> player,
  80. int3::EDistanceFormula distanceFormula) const
  81. {
  82. if(!!player && !player->isValidPlayer())
  83. {
  84. logGlobal->error("Illegal call to getTilesInRange!");
  85. return;
  86. }
  87. if(radious == CBuilding::HEIGHT_SKYSHIP) //reveal entire map
  88. getAllTiles (tiles, player, -1, [](auto * tile){return true;});
  89. else
  90. {
  91. const TeamState * team = !player ? nullptr : gs->getPlayerTeam(*player);
  92. for (int xd = std::max<int>(pos.x - radious , 0); xd <= std::min<int>(pos.x + radious, gs->map->width - 1); xd++)
  93. {
  94. for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gs->map->height - 1); yd++)
  95. {
  96. int3 tilePos(xd,yd,pos.z);
  97. int distance = pos.dist(tilePos, distanceFormula);
  98. if(distance <= radious)
  99. {
  100. if(!player
  101. || (mode == ETileVisibility::HIDDEN && team->fogOfWarMap[pos.z][xd][yd] == 0)
  102. || (mode == ETileVisibility::REVEALED && team->fogOfWarMap[pos.z][xd][yd] == 1)
  103. )
  104. tiles.insert(int3(xd,yd,pos.z));
  105. }
  106. }
  107. }
  108. }
  109. }
  110. void CPrivilegedInfoCallback::getAllTiles(std::unordered_set<int3> & tiles, std::optional<PlayerColor> Player, int level, std::function<bool(const TerrainTile *)> filter) const
  111. {
  112. if(!!Player && !Player->isValidPlayer())
  113. {
  114. logGlobal->error("Illegal call to getAllTiles !");
  115. return;
  116. }
  117. std::vector<int> floors;
  118. if(level == -1)
  119. {
  120. for(int b = 0; b < gs->map->levels(); ++b)
  121. {
  122. floors.push_back(b);
  123. }
  124. }
  125. else
  126. floors.push_back(level);
  127. for(auto zd: floors)
  128. {
  129. for(int xd = 0; xd < gs->map->width; xd++)
  130. {
  131. for(int yd = 0; yd < gs->map->height; yd++)
  132. {
  133. int3 coordinates(xd, yd, zd);
  134. if (filter(getTile(coordinates)))
  135. tiles.insert(coordinates);
  136. }
  137. }
  138. }
  139. }
  140. void CPrivilegedInfoCallback::pickAllowedArtsSet(std::vector<const CArtifact *> & out, vstd::RNG & rand)
  141. {
  142. for (int j = 0; j < 3 ; j++)
  143. out.push_back(gameState()->pickRandomArtifact(rand, CArtifact::ART_TREASURE).toArtifact());
  144. for (int j = 0; j < 3 ; j++)
  145. out.push_back(gameState()->pickRandomArtifact(rand, CArtifact::ART_MINOR).toArtifact());
  146. out.push_back(gameState()->pickRandomArtifact(rand, CArtifact::ART_MAJOR).toArtifact());
  147. }
  148. void CPrivilegedInfoCallback::getAllowedSpells(std::vector<SpellID> & out, std::optional<ui16> level)
  149. {
  150. for (auto const & spellID : gs->map->allowedSpells)
  151. {
  152. const auto * spell = spellID.toEntity(VLC);
  153. if (!isAllowed(spellID))
  154. continue;
  155. if (level.has_value() && spell->getLevel() != level)
  156. continue;
  157. out.push_back(spellID);
  158. }
  159. }
  160. CGameState * CPrivilegedInfoCallback::gameState()
  161. {
  162. return gs;
  163. }
  164. template<typename Loader>
  165. void CPrivilegedInfoCallback::loadCommonState(Loader & in)
  166. {
  167. logGlobal->info("Loading lib part of game...");
  168. in.checkMagicBytes(SAVEGAME_MAGIC);
  169. CMapHeader dum;
  170. StartInfo * si = nullptr;
  171. ActiveModsInSaveList activeMods;
  172. logGlobal->info("\tReading header");
  173. in.serializer & dum;
  174. logGlobal->info("\tReading options");
  175. in.serializer & si;
  176. logGlobal->info("\tReading mod list");
  177. in.serializer & activeMods;
  178. logGlobal->info("\tReading gamestate");
  179. in.serializer & gs;
  180. }
  181. template<typename Saver>
  182. void CPrivilegedInfoCallback::saveCommonState(Saver & out) const
  183. {
  184. ActiveModsInSaveList activeMods;
  185. logGlobal->info("Saving lib part of game...");
  186. out.putMagicBytes(SAVEGAME_MAGIC);
  187. logGlobal->info("\tSaving header");
  188. out.serializer & static_cast<CMapHeader&>(*gs->map);
  189. logGlobal->info("\tSaving options");
  190. out.serializer & gs->scenarioOps;
  191. logGlobal->info("\tSaving mod list");
  192. out.serializer & activeMods;
  193. logGlobal->info("\tSaving gamestate");
  194. out.serializer & gs;
  195. }
  196. // hardly memory usage for `-gdwarf-4` flag
  197. template DLL_LINKAGE void CPrivilegedInfoCallback::loadCommonState<CLoadFile>(CLoadFile &);
  198. template DLL_LINKAGE void CPrivilegedInfoCallback::saveCommonState<CSaveFile>(CSaveFile &) const;
  199. TerrainTile * CNonConstInfoCallback::getTile(const int3 & pos)
  200. {
  201. if(!gs->map->isInTheMap(pos))
  202. return nullptr;
  203. return &gs->map->getTile(pos);
  204. }
  205. CGHeroInstance * CNonConstInfoCallback::getHero(const ObjectInstanceID & objid)
  206. {
  207. return const_cast<CGHeroInstance*>(CGameInfoCallback::getHero(objid));
  208. }
  209. CGTownInstance * CNonConstInfoCallback::getTown(const ObjectInstanceID & objid)
  210. {
  211. return const_cast<CGTownInstance*>(CGameInfoCallback::getTown(objid));
  212. }
  213. TeamState * CNonConstInfoCallback::getTeam(const TeamID & teamID)
  214. {
  215. return const_cast<TeamState*>(CGameInfoCallback::getTeam(teamID));
  216. }
  217. TeamState * CNonConstInfoCallback::getPlayerTeam(const PlayerColor & color)
  218. {
  219. return const_cast<TeamState*>(CGameInfoCallback::getPlayerTeam(color));
  220. }
  221. PlayerState * CNonConstInfoCallback::getPlayerState(const PlayerColor & color, bool verbose)
  222. {
  223. return const_cast<PlayerState*>(CGameInfoCallback::getPlayerState(color, verbose));
  224. }
  225. CArtifactInstance * CNonConstInfoCallback::getArtInstance(const ArtifactInstanceID & aid)
  226. {
  227. return gs->map->artInstances.at(aid.num);
  228. }
  229. CGObjectInstance * CNonConstInfoCallback::getObjInstance(const ObjectInstanceID & oid)
  230. {
  231. return gs->map->objects.at(oid.num);
  232. }
  233. CArmedInstance * CNonConstInfoCallback::getArmyInstance(const ObjectInstanceID & oid)
  234. {
  235. return dynamic_cast<CArmedInstance *>(getObjInstance(oid));
  236. }
  237. CArtifactSet * CNonConstInfoCallback::getArtSet(const ArtifactLocation & loc)
  238. {
  239. if(auto hero = getHero(loc.artHolder))
  240. {
  241. if(loc.creature.has_value())
  242. {
  243. if(loc.creature.value() == SlotID::COMMANDER_SLOT_PLACEHOLDER)
  244. return hero->commander;
  245. else
  246. return hero->getStackPtr(loc.creature.value());
  247. }
  248. else
  249. {
  250. return hero;
  251. }
  252. }
  253. else if(auto army = getArmyInstance(loc.artHolder))
  254. {
  255. return army->getStackPtr(loc.creature.value());
  256. }
  257. else if(auto market = dynamic_cast<CGArtifactsAltar*>(getObjInstance(loc.artHolder)))
  258. {
  259. return market;
  260. }
  261. else
  262. {
  263. return nullptr;
  264. }
  265. }
  266. bool IGameCallback::isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero)
  267. {
  268. //only server knows
  269. logGlobal->error("isVisitCoveredByAnotherQuery call on client side");
  270. return false;
  271. }
  272. VCMI_LIB_NAMESPACE_END