IGameCallback.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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)
  27. {
  28. if(objid < 0 || objid >= gs->map->objects.size())
  29. {
  30. tlog1 << "Cannot get object with id " << objid << std::endl;
  31. return NULL;
  32. }
  33. else if (!gs->map->objects[objid])
  34. {
  35. tlog1 << "Cannot get object with id " << objid << ". Object was removed.\n";
  36. return NULL;
  37. }
  38. return gs->map->objects[objid];
  39. }
  40. const CGHeroInstance* IGameCallback::getHero(int objid)
  41. {
  42. const CGObjectInstance *obj = getObj(objid);
  43. if(obj)
  44. return dynamic_cast<const CGHeroInstance*>(obj);
  45. else
  46. return NULL;
  47. }
  48. const CGTownInstance* IGameCallback::getTown(int objid)
  49. {
  50. const CGObjectInstance *obj = getObj(objid);
  51. if(obj)
  52. return dynamic_cast<const CGTownInstance*>(gs->map->objects[objid]);
  53. else
  54. return NULL;
  55. }
  56. int IGameCallback::getOwner(int heroID)
  57. {
  58. return gs->map->objects[heroID]->tempOwner;
  59. }
  60. int IGameCallback::getResource(int player, int which)
  61. {
  62. return gs->players.find(player)->second.resources[which];
  63. }
  64. int IGameCallback::getDate(int mode)
  65. {
  66. return gs->getDate(mode);
  67. }
  68. const CGHeroInstance* IGameCallback::getSelectedHero( int player )
  69. {
  70. if(gs->players.find(player)->second.currentSelection==-1)
  71. return NULL;
  72. return getHero(gs->players.find(player)->second.currentSelection);
  73. }
  74. const PlayerSettings * IGameCallback::getPlayerSettings( int color )
  75. {
  76. return &gs->scenarioOps->getIthPlayersSettings(color);
  77. }
  78. int IGameCallback::getHeroCount( int player, bool includeGarrisoned )
  79. {
  80. int ret = 0;
  81. if(includeGarrisoned)
  82. return gs->getPlayer(player)->heroes.size();
  83. else
  84. for(int i=0; i < gs->getPlayer(player)->heroes.size(); i++)
  85. if(!gs->getPlayer(player)->heroes[i]->inTownGarrison)
  86. ret++;
  87. return ret;
  88. }
  89. void IGameCallback::getTilesInRange( std::set<int3> &tiles, int3 pos, int radious, int player/*=-1*/, int mode/*=0*/ )
  90. {
  91. if(player >= PLAYER_LIMIT)
  92. {
  93. tlog1 << "Illegal call to getTilesInRange!\n";
  94. return;
  95. }
  96. if (radious == -1) //reveal entire map
  97. getAllTiles (tiles, player, -1, 0);
  98. else
  99. {
  100. PlayerState * plr = &gs->players.find(player)->second;
  101. for (int xd = std::max<int>(pos.x - radious , 0); xd <= std::min<int>(pos.x + radious, gs->map->width - 1); xd++)
  102. {
  103. for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gs->map->height - 1); yd++)
  104. {
  105. double distance = pos.dist2d(int3(xd,yd,pos.z)) - 0.5;
  106. if(distance <= radious)
  107. {
  108. if(player < 0
  109. || (mode == 1 && plr->fogOfWarMap[xd][yd][pos.z]==0)
  110. || (mode == -1 && plr->fogOfWarMap[xd][yd][pos.z]==1)
  111. )
  112. tiles.insert(int3(xd,yd,pos.z));
  113. }
  114. }
  115. }
  116. }
  117. }
  118. void IGameCallback::getAllTiles (std::set<int3> &tiles, int player/*=-1*/, int level, int surface )
  119. {
  120. if(player >= PLAYER_LIMIT)
  121. {
  122. tlog1 << "Illegal call to getAllTiles !\n";
  123. return;
  124. }
  125. bool water = surface == 0 || surface == 2,
  126. land = surface == 0 || surface == 1;
  127. std::vector<int> floors;
  128. if(level == -1)
  129. {
  130. for (int xd = 0; xd <= gs->map->width - 1; xd++)
  131. 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
  132. {
  133. floors.push_back(b);
  134. }
  135. }
  136. else
  137. floors.push_back(level);
  138. for (std::vector<int>::const_iterator i = floors.begin(); i!= floors.end(); i++)
  139. {
  140. register int zd = *i;
  141. for (int xd = 0; xd < gs->map->width; xd++)
  142. {
  143. for (int yd = 0; yd < gs->map->height; yd++)
  144. {
  145. if ((getTile (int3 (xd,yd,zd))->tertype == 8 && water)
  146. || (getTile (int3 (xd,yd,zd))->tertype != 8 && land))
  147. tiles.insert(int3(xd,yd,zd));
  148. }
  149. }
  150. }
  151. }
  152. bool IGameCallback::isAllowed( int type, int id )
  153. {
  154. switch(type)
  155. {
  156. case 0:
  157. return gs->map->allowedSpell[id];
  158. case 1:
  159. return gs->map->allowedArtifact[id];
  160. case 2:
  161. return gs->map->allowedAbilities[id];
  162. default:
  163. tlog1 << "Wrong call to IGameCallback::isAllowed!\n";
  164. return false;
  165. }
  166. }
  167. void IGameCallback::pickAllowedArtsSet(std::vector<const CArtifact*> &out)
  168. {
  169. for (int i = 0; i < 2; i++)
  170. {
  171. for (int j = 0; j < 3 ; j++)
  172. {
  173. out.push_back(VLC->arth->artifacts[getRandomArt(CArtifact::ART_TREASURE << i)]);
  174. }
  175. }
  176. out.push_back(VLC->arth->artifacts[getRandomArt(CArtifact::ART_MAJOR)]);
  177. }
  178. ui16 IGameCallback::getRandomArt (int flags)
  179. {
  180. return VLC->arth->getRandomArt(flags);
  181. }
  182. ui16 IGameCallback::getArtSync (ui32 rand, int flags)
  183. {
  184. return VLC->arth->getArtSync (rand, flags);
  185. }
  186. void IGameCallback::erasePickedArt (si32 id)
  187. {
  188. VLC->arth->erasePickedArt(id);
  189. }
  190. void IGameCallback::getAllowedSpells(std::vector<ui16> &out, ui16 level)
  191. {
  192. CSpell *spell;
  193. for (int i = 0; i < gs->map->allowedSpell.size(); i++) //spellh size appears to be greater (?)
  194. {
  195. spell = &(VLC->spellh->spells[i]);
  196. if (isAllowed (0, spell->id) && spell->level == level)
  197. {
  198. out.push_back(spell->id);
  199. }
  200. }
  201. }
  202. int3 IGameCallback::getMapSize()
  203. {
  204. return int3(gs->map->width, gs->map->height, gs->map->twoLevel + 1);
  205. }
  206. inline TerrainTile * IGameCallback::getTile( int3 pos )
  207. {
  208. if(!gs->map->isInTheMap(pos))
  209. return NULL;
  210. return &gs->map->getTile(pos);
  211. }
  212. const PlayerState * IGameCallback::getPlayerState( int color )
  213. {
  214. return gs->getPlayer(color, false);
  215. }
  216. const CTown * IGameCallback::getNativeTown(int color)
  217. {
  218. return &VLC->townh->towns[gs->scenarioOps->getIthPlayersSettings(color).castle];
  219. }