IGameCallback.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /*
  11. * IGameCallback.cpp, part of VCMI engine
  12. *
  13. * Authors: listed in file AUTHORS in main folder
  14. *
  15. * License: GNU General Public License v2.0 or later
  16. * Full text of license available in license.txt file, in main folder
  17. *
  18. */
  19. CGameState *const IGameCallback::gameState ()
  20. {
  21. return gs;
  22. }
  23. const CGObjectInstance* IGameCallback::getObj(int objid)
  24. {
  25. if(objid < 0 || objid >= gs->map->objects.size())
  26. {
  27. tlog1 << "Cannot get object with id " << objid << std::endl;
  28. return NULL;
  29. }
  30. else if (!gs->map->objects[objid])
  31. {
  32. tlog1 << "Cannot get object with id " << objid << ". Object was removed.\n";
  33. return NULL;
  34. }
  35. return gs->map->objects[objid];
  36. }
  37. const CGHeroInstance* IGameCallback::getHero(int objid)
  38. {
  39. const CGObjectInstance *obj = getObj(objid);
  40. if(obj)
  41. return dynamic_cast<const CGHeroInstance*>(obj);
  42. else
  43. return NULL;
  44. }
  45. const CGTownInstance* IGameCallback::getTown(int objid)
  46. {
  47. const CGObjectInstance *obj = getObj(objid);
  48. if(obj)
  49. return dynamic_cast<const CGTownInstance*>(gs->map->objects[objid]);
  50. else
  51. return NULL;
  52. }
  53. int IGameCallback::getOwner(int heroID)
  54. {
  55. return gs->map->objects[heroID]->tempOwner;
  56. }
  57. int IGameCallback::getResource(int player, int which)
  58. {
  59. return gs->players.find(player)->second.resources[which];
  60. }
  61. int IGameCallback::getDate(int mode)
  62. {
  63. return gs->getDate(mode);
  64. }
  65. const CGHeroInstance* IGameCallback::getSelectedHero( int player )
  66. {
  67. if(gs->players.find(player)->second.currentSelection==-1)
  68. return NULL;
  69. return getHero(gs->players.find(player)->second.currentSelection);
  70. }
  71. const PlayerSettings * IGameCallback::getPlayerSettings( int color )
  72. {
  73. return &gs->scenarioOps->getIthPlayersSettings(color);
  74. }
  75. int IGameCallback::getHeroCount( int player, bool includeGarrisoned )
  76. {
  77. int ret = 0;
  78. if(includeGarrisoned)
  79. return gs->getPlayer(player)->heroes.size();
  80. else
  81. for(int i=0; i < gs->getPlayer(player)->heroes.size(); i++)
  82. if(!gs->getPlayer(player)->heroes[i]->inTownGarrison)
  83. ret++;
  84. return ret;
  85. }
  86. void IGameCallback::getTilesInRange( std::set<int3> &tiles, int3 pos, int radious, int player/*=-1*/, int mode/*=0*/ )
  87. {
  88. if(player >= PLAYER_LIMIT)
  89. {
  90. tlog1 << "Illegal call to getTilesInRange!\n";
  91. return;
  92. }
  93. for (int xd = std::max<int>(pos.x - radious , 0); xd <= std::min<int>(pos.x + radious, gs->map->width - 1); xd++)
  94. {
  95. for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gs->map->height - 1); yd++)
  96. {
  97. double distance = pos.dist2d(int3(xd,yd,pos.z)) - 0.5;
  98. if(distance <= radious)
  99. {
  100. if(player < 0
  101. || (mode == 1 && gs->players.find(player)->second.fogOfWarMap[xd][yd][pos.z]==0)
  102. || (mode == -1 && gs->players.find(player)->second.fogOfWarMap[xd][yd][pos.z]==1)
  103. )
  104. tiles.insert(int3(xd,yd,pos.z));
  105. }
  106. }
  107. }
  108. }
  109. void IGameCallback::getAllTiles (std::set<int3> &tiles, int player/*=-1*/, int level, int surface )
  110. {
  111. if(player >= PLAYER_LIMIT)
  112. {
  113. tlog1 << "Illegal call to getTilesInRange!\n";
  114. return;
  115. }
  116. bool water = surface == 0 || surface == 2,
  117. land = surface == 0 || surface == 1;
  118. std::vector<int> floors;
  119. if(level == -1)
  120. {
  121. for (int xd = 0; xd <= gs->map->width - 1; xd++)
  122. 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
  123. {
  124. floors.push_back(b);
  125. }
  126. }
  127. else
  128. floors.push_back(level);
  129. for (std::vector<int>::const_iterator i = floors.begin(); i!= floors.end(); i++)
  130. {
  131. register int zd = *i;
  132. for (int xd = 0; xd < gs->map->width; xd++)
  133. {
  134. for (int yd = 0; yd < gs->map->height; yd++)
  135. {
  136. if ((getTile (int3 (xd,yd,zd))->tertype == 8 && water)
  137. || (getTile (int3 (xd,yd,zd))->tertype != 8 && land))
  138. tiles.insert(int3(xd,yd,zd));
  139. }
  140. }
  141. }
  142. }
  143. bool IGameCallback::isAllowed( int type, int id )
  144. {
  145. switch(type)
  146. {
  147. case 0:
  148. return gs->map->allowedSpell[id];
  149. case 1:
  150. return gs->map->allowedArtifact[id];
  151. default:
  152. tlog1 << "Wrong call to IGameCallback::isAllowed!\n";
  153. return false;
  154. }
  155. }
  156. void IGameCallback::getAllowedArts(std::vector<CArtifact*> &out, std::vector<CArtifact*> CArtHandler::*arts)
  157. {
  158. for(int i = 0; i < (VLC->arth->*arts).size(); i++)
  159. {
  160. CArtifact *art = (VLC->arth->*arts)[i];
  161. if(isAllowed(1,art->id))
  162. {
  163. out.push_back(art);
  164. }
  165. }
  166. }
  167. void IGameCallback::getAllowed(std::vector<CArtifact*> &out, int flags)
  168. {
  169. if(flags & CArtifact::ART_TREASURE)
  170. getAllowedArts(out,&CArtHandler::treasures);
  171. if(flags & CArtifact::ART_MINOR)
  172. getAllowedArts(out,&CArtHandler::minors);
  173. if(flags & CArtifact::ART_MAJOR)
  174. getAllowedArts(out,&CArtHandler::majors);
  175. if(flags & CArtifact::ART_RELIC)
  176. getAllowedArts(out,&CArtHandler::relics);
  177. }
  178. void IGameCallback::getAllowedSpells(std::vector<ui16> &out, ui16 level)
  179. {
  180. CSpell *spell;
  181. for (int i = 0; i < gs->map->allowedSpell.size(); i++) //spellh size appears to be greater (?)
  182. {
  183. spell = &(VLC->spellh->spells[i]);
  184. if (isAllowed (0, spell->id) && spell->level == level)
  185. {
  186. out.push_back(i);
  187. }
  188. }
  189. }
  190. int3 IGameCallback::getMapSize()
  191. {
  192. return int3(gs->map->width, gs->map->height, gs->map->twoLevel + 1);
  193. }
  194. inline TerrainTile * IGameCallback::getTile( int3 pos )
  195. {
  196. if(!gs->map->isInTheMap(pos))
  197. return NULL;
  198. return &gs->map->getTile(pos);
  199. }