IGameCallback.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. const CGObjectInstance* IGameCallback::getObj(int objid)
  19. {
  20. if(objid < 0 || objid >= gs->map->objects.size())
  21. {
  22. tlog1 << "Cannot get object with id " << objid << std::endl;
  23. return NULL;
  24. }
  25. else if (!gs->map->objects[objid])
  26. {
  27. tlog1 << "Cannot get object with id " << objid << ". Object was removed.\n";
  28. return NULL;
  29. }
  30. return gs->map->objects[objid];
  31. }
  32. const CGHeroInstance* IGameCallback::getHero(int objid)
  33. {
  34. const CGObjectInstance *obj = getObj(objid);
  35. if(obj)
  36. return dynamic_cast<const CGHeroInstance*>(obj);
  37. else
  38. return NULL;
  39. }
  40. const CGTownInstance* IGameCallback::getTown(int objid)
  41. {
  42. const CGObjectInstance *obj = getObj(objid);
  43. if(obj)
  44. return dynamic_cast<const CGTownInstance*>(gs->map->objects[objid]);
  45. else
  46. return NULL;
  47. }
  48. int IGameCallback::getOwner(int heroID)
  49. {
  50. return gs->map->objects[heroID]->tempOwner;
  51. }
  52. int IGameCallback::getResource(int player, int which)
  53. {
  54. return gs->players.find(player)->second.resources[which];
  55. }
  56. int IGameCallback::getDate(int mode)
  57. {
  58. return gs->getDate(mode);
  59. }
  60. const CGHeroInstance* IGameCallback::getSelectedHero( int player )
  61. {
  62. if(gs->players.find(player)->second.currentSelection==-1)
  63. return NULL;
  64. return getHero(gs->players.find(player)->second.currentSelection);
  65. }
  66. const PlayerSettings * IGameCallback::getPlayerSettings( int color )
  67. {
  68. return &gs->scenarioOps->getIthPlayersSettings(color);
  69. }
  70. int IGameCallback::getHeroCount( int player, bool includeGarrisoned )
  71. {
  72. int ret = 0;
  73. if(includeGarrisoned)
  74. return gs->getPlayer(player)->heroes.size();
  75. else
  76. for(int i=0; i < gs->getPlayer(player)->heroes.size(); i++)
  77. if(!gs->getPlayer(player)->heroes[i]->inTownGarrison)
  78. ret++;
  79. return ret;
  80. }
  81. void IGameCallback::getTilesInRange( std::set<int3> &tiles, int3 pos, int radious, int player/*=-1*/, int mode/*=0*/ )
  82. {
  83. if(player >= PLAYER_LIMIT)
  84. {
  85. tlog1 << "Illegal call to getTilesInRange!\n";
  86. return;
  87. }
  88. for (int xd = std::max<int>(pos.x - radious , 0); xd <= std::min<int>(pos.x + radious, gs->map->width - 1); xd++)
  89. {
  90. for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gs->map->height - 1); yd++)
  91. {
  92. double distance = pos.dist2d(int3(xd,yd,pos.z)) - 0.5;
  93. if(distance <= radious)
  94. {
  95. if(player < 0
  96. || (mode == 1 && gs->players.find(player)->second.fogOfWarMap[xd][yd][pos.z]==0)
  97. || (mode == -1 && gs->players.find(player)->second.fogOfWarMap[xd][yd][pos.z]==1)
  98. )
  99. tiles.insert(int3(xd,yd,pos.z));
  100. }
  101. }
  102. }
  103. }
  104. bool IGameCallback::isAllowed( int type, int id )
  105. {
  106. switch(type)
  107. {
  108. case 0:
  109. return gs->map->allowedSpell[id];
  110. case 1:
  111. return gs->map->allowedArtifact[id];
  112. default:
  113. tlog1 << "Wrong call to IGameCallback::isAllowed!\n";
  114. return false;
  115. }
  116. }
  117. void IGameCallback::getAllowedArts(std::vector<CArtifact*> &out, std::vector<CArtifact*> CArtHandler::*arts)
  118. {
  119. for(int i = 0; i < (VLC->arth->*arts).size(); i++)
  120. {
  121. CArtifact *art = (VLC->arth->*arts)[i];
  122. if(isAllowed(1,art->id))
  123. {
  124. out.push_back(art);
  125. }
  126. }
  127. }
  128. void IGameCallback::getAllowed(std::vector<CArtifact*> &out, int flags)
  129. {
  130. if(flags & CArtifact::ART_TREASURE)
  131. getAllowedArts(out,&CArtHandler::treasures);
  132. if(flags & CArtifact::ART_MINOR)
  133. getAllowedArts(out,&CArtHandler::minors);
  134. if(flags & CArtifact::ART_MAJOR)
  135. getAllowedArts(out,&CArtHandler::majors);
  136. if(flags & CArtifact::ART_RELIC)
  137. getAllowedArts(out,&CArtHandler::relics);
  138. }
  139. TerrainTile * IGameCallback::getTile( int3 pos )
  140. {
  141. if(!gs->map->isInTheMap(pos))
  142. return NULL;
  143. return &gs->map->getTile(pos);
  144. }