IGameCallback.cpp 7.1 KB

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