IGameCallback.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 "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/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->getMap().levels());
  58. for(int b = 0; b < gs->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 < gs->getMap().width; xd++)
  66. {
  67. for (int yd = 0; yd < gs->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 : gs->getPlayerTeam(*player);
  93. for (int xd = std::max<int>(pos.x - radious , 0); xd <= std::min<int>(pos.x + radious, gs->getMap().width - 1); xd++)
  94. {
  95. for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gs->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 < gs->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 < gs->getMap().width; xd++)
  131. {
  132. for(int yd = 0; yd < gs->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 : gs->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. CGameState * CPrivilegedInfoCallback::gameState()
  162. {
  163. return gs;
  164. }
  165. void CPrivilegedInfoCallback::loadCommonState(CLoadFile & 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. void CPrivilegedInfoCallback::saveCommonState(CSaveFile & 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->getMap());
  188. logGlobal->info("\tSaving options");
  189. out.serializer & gs->getStartInfo();
  190. logGlobal->info("\tSaving mod list");
  191. out.serializer & activeMods;
  192. logGlobal->info("\tSaving gamestate");
  193. out.serializer & gs;
  194. }
  195. TerrainTile * CNonConstInfoCallback::getTile(const int3 & pos)
  196. {
  197. if(!gs->getMap().isInTheMap(pos))
  198. return nullptr;
  199. return &gs->getMap().getTile(pos);
  200. }
  201. CGHeroInstance * CNonConstInfoCallback::getHero(const ObjectInstanceID & objid)
  202. {
  203. return const_cast<CGHeroInstance*>(CGameInfoCallback::getHero(objid));
  204. }
  205. CGTownInstance * CNonConstInfoCallback::getTown(const ObjectInstanceID & objid)
  206. {
  207. return const_cast<CGTownInstance*>(CGameInfoCallback::getTown(objid));
  208. }
  209. TeamState * CNonConstInfoCallback::getTeam(const TeamID & teamID)
  210. {
  211. return const_cast<TeamState*>(CGameInfoCallback::getTeam(teamID));
  212. }
  213. TeamState * CNonConstInfoCallback::getPlayerTeam(const PlayerColor & color)
  214. {
  215. return const_cast<TeamState*>(CGameInfoCallback::getPlayerTeam(color));
  216. }
  217. PlayerState * CNonConstInfoCallback::getPlayerState(const PlayerColor & color, bool verbose)
  218. {
  219. return const_cast<PlayerState*>(CGameInfoCallback::getPlayerState(color, verbose));
  220. }
  221. CArtifactInstance * CNonConstInfoCallback::getArtInstance(const ArtifactInstanceID & aid)
  222. {
  223. return gs->getMap().getArtifactInstance(aid);
  224. }
  225. CGObjectInstance * CNonConstInfoCallback::getObjInstance(const ObjectInstanceID & oid)
  226. {
  227. return gs->getMap().objects.at(oid.num);
  228. }
  229. CArmedInstance * CNonConstInfoCallback::getArmyInstance(const ObjectInstanceID & oid)
  230. {
  231. return dynamic_cast<CArmedInstance *>(getObjInstance(oid));
  232. }
  233. CArtifactSet * CNonConstInfoCallback::getArtSet(const ArtifactLocation & loc)
  234. {
  235. if(auto hero = getHero(loc.artHolder))
  236. {
  237. if(loc.creature.has_value())
  238. {
  239. if(loc.creature.value() == SlotID::COMMANDER_SLOT_PLACEHOLDER)
  240. return hero->getCommander();
  241. else
  242. return hero->getStackPtr(loc.creature.value());
  243. }
  244. else
  245. {
  246. return hero;
  247. }
  248. }
  249. else if(auto market = getMarket(loc.artHolder))
  250. {
  251. if(auto artSet = market->getArtifactsStorage())
  252. return artSet;
  253. }
  254. else if(auto army = getArmyInstance(loc.artHolder))
  255. {
  256. return army->getStackPtr(loc.creature.value());
  257. }
  258. return nullptr;
  259. }
  260. bool IGameCallback::isVisitCoveredByAnotherQuery(const CGObjectInstance *obj, const CGHeroInstance *hero)
  261. {
  262. //only server knows
  263. logGlobal->error("isVisitCoveredByAnotherQuery call on client side");
  264. return false;
  265. }
  266. VCMI_LIB_NAMESPACE_END