IGameCallback.cpp 7.9 KB

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