IGameCallback.cpp 5.2 KB

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