IGameCallback.cpp 8.2 KB

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