IGameCallback.cpp 7.3 KB

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