IGameCallback.cpp 8.7 KB

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