IGameCallback.cpp 7.4 KB

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