CPlayerSpecificInfoCallback.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * CPlayerSpecificInfoCallback.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CPlayerSpecificInfoCallback.h"
  12. #include "../gameState/CGameState.h"
  13. #include "../gameState/QuestInfo.h"
  14. #include "../CPlayerState.h"
  15. #include "../mapping/CMap.h"
  16. #include "../mapObjects/CGHeroInstance.h"
  17. #include "../mapObjects/CGTownInstance.h"
  18. //TODO make clean
  19. #define ASSERT_IF_CALLED_WITH_PLAYER if(!getPlayerID()) {logGlobal->error(BOOST_CURRENT_FUNCTION); assert(0);}
  20. #define ERROR_VERBOSE_OR_NOT_RET_VAL_IF(cond, verbose, txt, retVal) do {if(cond){if(verbose)logGlobal->error("%s: %s",BOOST_CURRENT_FUNCTION, txt); return retVal;}} while(0)
  21. #define ERROR_RET_IF(cond, txt) do {if(cond){logGlobal->error("%s: %s", BOOST_CURRENT_FUNCTION, txt); return;}} while(0)
  22. #define ERROR_RET_VAL_IF(cond, txt, retVal) do {if(cond){logGlobal->error("%s: %s", BOOST_CURRENT_FUNCTION, txt); return retVal;}} while(0)
  23. VCMI_LIB_NAMESPACE_BEGIN
  24. int CPlayerSpecificInfoCallback::howManyTowns() const
  25. {
  26. ERROR_RET_VAL_IF(!getPlayerID(), "Applicable only for player callbacks", -1);
  27. return CGameInfoCallback::howManyTowns(*getPlayerID());
  28. }
  29. std::vector < const CGTownInstance *> CPlayerSpecificInfoCallback::getTownsInfo(bool onlyOur) const
  30. {
  31. auto ret = std::vector < const CGTownInstance *>();
  32. for(const auto & i : gameState().players)
  33. {
  34. for(const auto & town : i.second.getTowns())
  35. {
  36. if(i.first == getPlayerID() || (!onlyOur && isVisibleFor(town, *getPlayerID())))
  37. {
  38. ret.push_back(town);
  39. }
  40. }
  41. } // for ( std::map<int, PlayerState>::iterator i=gameState().players.begin() ; i!=gameState().players.end();i++)
  42. return ret;
  43. }
  44. std::vector < const CGHeroInstance *> CPlayerSpecificInfoCallback::getHeroesInfo() const
  45. {
  46. const auto * playerState = gameState().getPlayerState(*getPlayerID());
  47. return playerState->getHeroes();
  48. }
  49. int CPlayerSpecificInfoCallback::getHeroSerial(const CGHeroInstance * hero, bool includeGarrisoned) const
  50. {
  51. if (hero->isGarrisoned() && !includeGarrisoned)
  52. return -1;
  53. size_t index = 0;
  54. const auto & heroes = gameState().players.at(*getPlayerID()).getHeroes();
  55. for (auto & possibleHero : heroes)
  56. {
  57. if (includeGarrisoned || !possibleHero->isGarrisoned())
  58. index++;
  59. if (possibleHero == hero)
  60. return static_cast<int>(index);
  61. }
  62. return -1;
  63. }
  64. int3 CPlayerSpecificInfoCallback::getGrailPos( double *outKnownRatio )
  65. {
  66. if (!getPlayerID() || gameState().getMap().obeliskCount == 0)
  67. {
  68. *outKnownRatio = 0.0;
  69. }
  70. else
  71. {
  72. TeamID t = gameState().getPlayerTeam(*getPlayerID())->id;
  73. double visited = 0.0;
  74. if(gameState().getMap().obelisksVisited.count(t))
  75. visited = static_cast<double>(gameState().getMap().obelisksVisited[t]);
  76. *outKnownRatio = visited / gameState().getMap().obeliskCount;
  77. }
  78. return gameState().getMap().grailPos;
  79. }
  80. std::vector < const CGObjectInstance * > CPlayerSpecificInfoCallback::getMyObjects() const
  81. {
  82. return gameState().getPlayerState(*getPlayerID())->getOwnedObjects();
  83. }
  84. std::vector <QuestInfo> CPlayerSpecificInfoCallback::getMyQuests() const
  85. {
  86. return gameState().getPlayerState(*getPlayerID())->quests;
  87. }
  88. int CPlayerSpecificInfoCallback::howManyHeroes(bool includeGarrisoned) const
  89. {
  90. ERROR_RET_VAL_IF(!getPlayerID(), "Applicable only for player callbacks", -1);
  91. return getHeroCount(*getPlayerID(), includeGarrisoned);
  92. }
  93. const CGHeroInstance* CPlayerSpecificInfoCallback::getHeroBySerial(int serialId, bool includeGarrisoned) const
  94. {
  95. ASSERT_IF_CALLED_WITH_PLAYER
  96. const PlayerState *p = getPlayerState(*getPlayerID());
  97. ERROR_RET_VAL_IF(!p, "No player info", nullptr);
  98. if (!includeGarrisoned)
  99. {
  100. for(ui32 i = 0; i < p->getHeroes().size() && static_cast<int>(i) <= serialId; i++)
  101. if(p->getHeroes()[i]->isGarrisoned())
  102. serialId++;
  103. }
  104. ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->getHeroes().size(), "No player info", nullptr);
  105. return p->getHeroes()[serialId];
  106. }
  107. const CGTownInstance* CPlayerSpecificInfoCallback::getTownBySerial(int serialId) const
  108. {
  109. ASSERT_IF_CALLED_WITH_PLAYER
  110. const PlayerState *p = getPlayerState(*getPlayerID());
  111. ERROR_RET_VAL_IF(!p, "No player info", nullptr);
  112. ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->getTowns().size(), "No player info", nullptr);
  113. return p->getTowns()[serialId];
  114. }
  115. int CPlayerSpecificInfoCallback::getResourceAmount(GameResID type) const
  116. {
  117. ERROR_RET_VAL_IF(!getPlayerID(), "Applicable only for player callbacks", -1);
  118. return getResource(*getPlayerID(), type);
  119. }
  120. TResources CPlayerSpecificInfoCallback::getResourceAmount() const
  121. {
  122. ERROR_RET_VAL_IF(!getPlayerID(), "Applicable only for player callbacks", TResources());
  123. return gameState().players.at(*getPlayerID()).resources;
  124. }
  125. VCMI_LIB_NAMESPACE_END