2
0

IGameCallback.cpp 8.6 KB

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