IGameCallback.cpp 8.5 KB

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