IGameCallback.cpp 7.7 KB

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