IGameCallback.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #define VCMI_DLL
  2. #include "IGameCallback.h"
  3. #include "../lib/CGameState.h"
  4. #include "../lib/map.h"
  5. #include "../hch/CObjectHandler.h"
  6. #include "../StartInfo.h"
  7. #include "../hch/CArtHandler.h"
  8. #include "../hch/CSpellHandler.h"
  9. #include "../lib/VCMI_Lib.h"
  10. #include <boost/random/linear_congruential.hpp>
  11. #include "../hch/CTownHandler.h"
  12. /*
  13. * IGameCallback.cpp, part of VCMI engine
  14. *
  15. * Authors: listed in file AUTHORS in main folder
  16. *
  17. * License: GNU General Public License v2.0 or later
  18. * Full text of license available in license.txt file, in main folder
  19. *
  20. */
  21. extern boost::rand48 ran;
  22. CGameState *const IGameCallback::gameState ()
  23. {
  24. return gs;
  25. }
  26. const CGObjectInstance* IGameCallback::getObj(int objid, bool verbose)
  27. {
  28. if(objid < 0 || objid >= gs->map->objects.size())
  29. {
  30. if(verbose)
  31. tlog1 << "Cannot get object with id " << objid << std::endl;
  32. return NULL;
  33. }
  34. else if (!gs->map->objects[objid])
  35. {
  36. if(verbose)
  37. tlog1 << "Cannot get object with id " << objid << ". Object was removed.\n";
  38. return NULL;
  39. }
  40. return gs->map->objects[objid];
  41. }
  42. const CGHeroInstance* IGameCallback::getHero(int objid)
  43. {
  44. const CGObjectInstance *obj = getObj(objid, false);
  45. if(obj)
  46. return dynamic_cast<const CGHeroInstance*>(obj);
  47. else
  48. return NULL;
  49. }
  50. const CGTownInstance* IGameCallback::getTown(int objid)
  51. {
  52. const CGObjectInstance *obj = getObj(objid, false);
  53. if(obj)
  54. return dynamic_cast<const CGTownInstance*>(gs->map->objects[objid]);
  55. else
  56. return NULL;
  57. }
  58. int IGameCallback::getOwner(int heroID)
  59. {
  60. return gs->map->objects[heroID]->tempOwner;
  61. }
  62. int IGameCallback::getResource(int player, int which)
  63. {
  64. return gs->players.find(player)->second.resources[which];
  65. }
  66. int IGameCallback::getDate(int mode)
  67. {
  68. return gs->getDate(mode);
  69. }
  70. const CGHeroInstance* IGameCallback::getSelectedHero( int player )
  71. {
  72. if(gs->players.find(player)->second.currentSelection==-1)
  73. return NULL;
  74. return getHero(gs->players.find(player)->second.currentSelection);
  75. }
  76. const PlayerSettings * IGameCallback::getPlayerSettings( int color )
  77. {
  78. return &gs->scenarioOps->getIthPlayersSettings(color);
  79. }
  80. int IGameCallback::getHeroCount( int player, bool includeGarrisoned )
  81. {
  82. int ret = 0;
  83. if(includeGarrisoned)
  84. return gs->getPlayer(player)->heroes.size();
  85. else
  86. for(int i=0; i < gs->getPlayer(player)->heroes.size(); i++)
  87. if(!gs->getPlayer(player)->heroes[i]->inTownGarrison)
  88. ret++;
  89. return ret;
  90. }
  91. void IGameCallback::getTilesInRange( std::set<int3> &tiles, int3 pos, int radious, int player/*=-1*/, int mode/*=0*/ )
  92. {
  93. if(player >= PLAYER_LIMIT)
  94. {
  95. tlog1 << "Illegal call to getTilesInRange!\n";
  96. return;
  97. }
  98. if (radious == -1) //reveal entire map
  99. getAllTiles (tiles, player, -1, 0);
  100. else
  101. {
  102. const TeamState * team = gs->getPlayerTeam(player);
  103. for (int xd = std::max<int>(pos.x - radious , 0); xd <= std::min<int>(pos.x + radious, gs->map->width - 1); xd++)
  104. {
  105. for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gs->map->height - 1); yd++)
  106. {
  107. double distance = pos.dist2d(int3(xd,yd,pos.z)) - 0.5;
  108. if(distance <= radious)
  109. {
  110. if(player < 0
  111. || (mode == 1 && team->fogOfWarMap[xd][yd][pos.z]==0)
  112. || (mode == -1 && team->fogOfWarMap[xd][yd][pos.z]==1)
  113. )
  114. tiles.insert(int3(xd,yd,pos.z));
  115. }
  116. }
  117. }
  118. }
  119. }
  120. void IGameCallback::getAllTiles (std::set<int3> &tiles, int player/*=-1*/, int level, int surface )
  121. {
  122. if(player >= PLAYER_LIMIT)
  123. {
  124. tlog1 << "Illegal call to getAllTiles !\n";
  125. return;
  126. }
  127. bool water = surface == 0 || surface == 2,
  128. land = surface == 0 || surface == 1;
  129. std::vector<int> floors;
  130. if(level == -1)
  131. {
  132. for (int xd = 0; xd <= gs->map->width - 1; xd++)
  133. for(int b=0; b<gs->map->twoLevel + 1; ++b) //if gs->map->twoLevel is false then false (0) + 1 is 1, if it's true (1) then we have 2
  134. {
  135. floors.push_back(b);
  136. }
  137. }
  138. else
  139. floors.push_back(level);
  140. for (std::vector<int>::const_iterator i = floors.begin(); i!= floors.end(); i++)
  141. {
  142. register int zd = *i;
  143. for (int xd = 0; xd < gs->map->width; xd++)
  144. {
  145. for (int yd = 0; yd < gs->map->height; yd++)
  146. {
  147. if ((getTile (int3 (xd,yd,zd))->tertype == 8 && water)
  148. || (getTile (int3 (xd,yd,zd))->tertype != 8 && land))
  149. tiles.insert(int3(xd,yd,zd));
  150. }
  151. }
  152. }
  153. }
  154. void IGameCallback::getFreeTiles (std::vector<int3> &tiles)
  155. {
  156. std::vector<int> floors;
  157. for (int b=0; b<gs->map->twoLevel + 1; ++b) //if gs->map->twoLevel is false then false (0) + 1 is 1, if it's true (1) then we have 2
  158. {
  159. floors.push_back(b);
  160. }
  161. TerrainTile *tinfo;
  162. for (std::vector<int>::const_iterator i = floors.begin(); i!= floors.end(); i++)
  163. {
  164. register int zd = *i;
  165. for (int xd = 0; xd < gs->map->width; xd++)
  166. {
  167. for (int yd = 0; yd < gs->map->height; yd++)
  168. {
  169. tinfo = getTile (int3 (xd,yd,zd));
  170. if (tinfo->tertype != 8 && !tinfo->blocked) //land and free
  171. tiles.push_back (int3 (xd,yd,zd));
  172. }
  173. }
  174. }
  175. }
  176. bool IGameCallback::isAllowed( int type, int id )
  177. {
  178. switch(type)
  179. {
  180. case 0:
  181. return gs->map->allowedSpell[id];
  182. case 1:
  183. return gs->map->allowedArtifact[id];
  184. case 2:
  185. return gs->map->allowedAbilities[id];
  186. default:
  187. tlog1 << "Wrong call to IGameCallback::isAllowed!\n";
  188. return false;
  189. }
  190. }
  191. void IGameCallback::pickAllowedArtsSet(std::vector<const CArtifact*> &out)
  192. {
  193. for (int i = 0; i < 2; i++)
  194. {
  195. for (int j = 0; j < 3 ; j++)
  196. {
  197. out.push_back(VLC->arth->artifacts[getRandomArt(CArtifact::ART_TREASURE << i)]);
  198. }
  199. }
  200. out.push_back(VLC->arth->artifacts[getRandomArt(CArtifact::ART_MAJOR)]);
  201. }
  202. ui16 IGameCallback::getRandomArt (int flags)
  203. {
  204. return VLC->arth->getRandomArt(flags);
  205. }
  206. ui16 IGameCallback::getArtSync (ui32 rand, int flags)
  207. {
  208. return VLC->arth->getArtSync (rand, flags);
  209. }
  210. void IGameCallback::erasePickedArt (si32 id)
  211. {
  212. VLC->arth->erasePickedArt(id);
  213. }
  214. void IGameCallback::getAllowedSpells(std::vector<ui16> &out, ui16 level)
  215. {
  216. CSpell *spell;
  217. for (int i = 0; i < gs->map->allowedSpell.size(); i++) //spellh size appears to be greater (?)
  218. {
  219. spell = &(VLC->spellh->spells[i]);
  220. if (isAllowed (0, spell->id) && spell->level == level)
  221. {
  222. out.push_back(spell->id);
  223. }
  224. }
  225. }
  226. int3 IGameCallback::getMapSize()
  227. {
  228. return int3(gs->map->width, gs->map->height, gs->map->twoLevel + 1);
  229. }
  230. inline TerrainTile * IGameCallback::getTile( int3 pos )
  231. {
  232. if(!gs->map->isInTheMap(pos))
  233. return NULL;
  234. return &gs->map->getTile(pos);
  235. }
  236. const PlayerState * IGameCallback::getPlayerState( int color )
  237. {
  238. return gs->getPlayer(color, false);
  239. }
  240. const CTown * IGameCallback::getNativeTown(int color)
  241. {
  242. return &VLC->townh->towns[gs->scenarioOps->getIthPlayersSettings(color).castle];
  243. }