IGameCallback.cpp 7.5 KB

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