IGameCallback.cpp 8.2 KB

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