IGameCallback.cpp 7.7 KB

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