IGameCallback.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #define VCMI_DLL
  2. #include "IGameCallback.h"
  3. #include "../CGameState.h"
  4. #include "../map.h"
  5. #include "../hch/CObjectHandler.h"
  6. #include "../StartInfo.h"
  7. const CGObjectInstance* IGameCallback::getObj(int objid)
  8. {
  9. if(objid < 0 || objid >= gs->map->objects.size())
  10. {
  11. tlog1 << "Cannot get object with id " << objid << std::endl;
  12. return NULL;
  13. }
  14. else if (!gs->map->objects[objid])
  15. {
  16. tlog1 << "Cannot get object with id " << objid << ". Object was removed.\n";
  17. return NULL;
  18. }
  19. return gs->map->objects[objid];
  20. }
  21. const CGHeroInstance* IGameCallback::getHero(int objid)
  22. {
  23. const CGObjectInstance *obj = getObj(objid);
  24. if(obj)
  25. return dynamic_cast<const CGHeroInstance*>(obj);
  26. else
  27. return NULL;
  28. }
  29. const CGTownInstance* IGameCallback::getTown(int objid)
  30. {
  31. const CGObjectInstance *obj = getObj(objid);
  32. if(obj)
  33. return dynamic_cast<const CGTownInstance*>(gs->map->objects[objid]);
  34. else
  35. return NULL;
  36. }
  37. int IGameCallback::getOwner(int heroID)
  38. {
  39. return gs->map->objects[heroID]->tempOwner;
  40. }
  41. int IGameCallback::getResource(int player, int which)
  42. {
  43. return gs->players.find(player)->second.resources[which];
  44. }
  45. int IGameCallback::getDate(int mode)
  46. {
  47. return gs->getDate(mode);
  48. }
  49. const CGHeroInstance* IGameCallback::getSelectedHero( int player )
  50. {
  51. return getHero(gs->players.find(player)->second.currentSelection);
  52. }
  53. const PlayerSettings * IGameCallback::getPlayerSettings( int color )
  54. {
  55. return &gs->scenarioOps->getIthPlayersSettings(color);
  56. }
  57. int IGameCallback::getHeroCount( int player, bool includeGarrisoned )
  58. {
  59. int ret = 0;
  60. if(includeGarrisoned)
  61. return gs->getPlayer(player)->heroes.size();
  62. else
  63. for(int i=0; i < gs->getPlayer(player)->heroes.size(); i++)
  64. if(!gs->getPlayer(player)->heroes[i]->inTownGarrison)
  65. ret++;
  66. return ret;
  67. }
  68. void IGameCallback::getTilesInRange( std::set<int3> &tiles, int3 pos, int radious, int player/*=-1*/, int mode/*=0*/ )
  69. {
  70. if(player >= PLAYER_LIMIT)
  71. {
  72. tlog1 << "Illegal call to getTilesInRange!\n";
  73. return;
  74. }
  75. for (int xd = std::max<int>(pos.x - radious , 0); xd <= std::min<int>(pos.x + radious, gs->map->width - 1); xd++)
  76. {
  77. for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gs->map->height - 1); yd++)
  78. {
  79. double distance = pos.dist2d(int3(xd,yd,pos.z)) - 0.5;
  80. if(distance <= radious)
  81. {
  82. if(player < 0
  83. || (mode == 1 && gs->players.find(player)->second.fogOfWarMap[xd][yd][pos.z]==0)
  84. || (mode == -1 && gs->players.find(player)->second.fogOfWarMap[xd][yd][pos.z]==1)
  85. )
  86. tiles.insert(int3(xd,yd,pos.z));
  87. }
  88. }
  89. }
  90. }
  91. bool IGameCallback::isAllowed( int type, int id )
  92. {
  93. switch(type)
  94. {
  95. case 0:
  96. return gs->map->allowedSpell[id];
  97. default:
  98. tlog1 << "Wrong call to IGameCallback::isAllowed!\n";
  99. }
  100. }