IGameCallback.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 floor, int surface )
  109. {
  110. if(player >= PLAYER_LIMIT)
  111. {
  112. tlog1 << "Illegal call to getTilesInRange!\n";
  113. return;
  114. }
  115. bool water = false, land = false;
  116. if (surface == 0 || surface == 2)
  117. water = true;
  118. if (surface == 0 || surface == 1)
  119. land = true;
  120. std::set<int> floors;
  121. if (floor==1 || floor == 0) // ground
  122. floors.insert(0);
  123. if (floor==2 || floor == 0) // undergroundground
  124. floors.insert(1);
  125. for (std::set<int>::iterator i = floors.begin(); i!= floors.end(); i++)
  126. {
  127. for (int xd = 0; xd <= gs->map->width - 1; xd++)
  128. {
  129. for (int yd = 0; yd <= gs->map->height - 1; yd++)
  130. {
  131. if ((getTile (int3 (xd,yd,*i))->tertype == 8 && water == true)
  132. || (getTile (int3 (xd,yd,*i))->tertype != 8 && land == true))
  133. tiles.insert(int3(xd,yd,*i));
  134. }
  135. }
  136. }
  137. }
  138. bool IGameCallback::isAllowed( int type, int id )
  139. {
  140. switch(type)
  141. {
  142. case 0:
  143. return gs->map->allowedSpell[id];
  144. case 1:
  145. return gs->map->allowedArtifact[id];
  146. default:
  147. tlog1 << "Wrong call to IGameCallback::isAllowed!\n";
  148. return false;
  149. }
  150. }
  151. void IGameCallback::getAllowedArts(std::vector<CArtifact*> &out, std::vector<CArtifact*> CArtHandler::*arts)
  152. {
  153. for(int i = 0; i < (VLC->arth->*arts).size(); i++)
  154. {
  155. CArtifact *art = (VLC->arth->*arts)[i];
  156. if(isAllowed(1,art->id))
  157. {
  158. out.push_back(art);
  159. }
  160. }
  161. }
  162. void IGameCallback::getAllowed(std::vector<CArtifact*> &out, int flags)
  163. {
  164. if(flags & CArtifact::ART_TREASURE)
  165. getAllowedArts(out,&CArtHandler::treasures);
  166. if(flags & CArtifact::ART_MINOR)
  167. getAllowedArts(out,&CArtHandler::minors);
  168. if(flags & CArtifact::ART_MAJOR)
  169. getAllowedArts(out,&CArtHandler::majors);
  170. if(flags & CArtifact::ART_RELIC)
  171. getAllowedArts(out,&CArtHandler::relics);
  172. }
  173. TerrainTile * IGameCallback::getTile( int3 pos )
  174. {
  175. if(!gs->map->isInTheMap(pos))
  176. return NULL;
  177. return &gs->map->getTile(pos);
  178. }