IGameCallback.cpp 8.3 KB

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