IGameCallback.cpp 7.2 KB

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