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