CPrivilegedInfoCallback.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * CPrivilegedInfoCallback.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 "CPrivilegedInfoCallback.h"
  12. #include "../CPlayerState.h"
  13. #include "../entities/artifact/EArtifactClass.h"
  14. #include "../entities/building/CBuilding.h"
  15. #include "../gameState/CGameState.h"
  16. #include "../mapping/CMap.h"
  17. #include "../spells/CSpellHandler.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. void CPrivilegedInfoCallback::getFreeTiles(std::vector<int3> & tiles) const
  20. {
  21. std::vector<int> floors;
  22. floors.reserve(gameState().getMap().levels());
  23. for(int b = 0; b < gameState().getMap().levels(); ++b)
  24. {
  25. floors.push_back(b);
  26. }
  27. const TerrainTile * tinfo = nullptr;
  28. for (auto zd : floors)
  29. {
  30. for (int xd = 0; xd < gameState().getMap().width; xd++)
  31. {
  32. for (int yd = 0; yd < gameState().getMap().height; yd++)
  33. {
  34. tinfo = getTile(int3 (xd,yd,zd));
  35. if (tinfo->isLand() && tinfo->getTerrain()->isPassable() && !tinfo->blocked()) //land and free
  36. tiles.emplace_back(xd, yd, zd);
  37. }
  38. }
  39. }
  40. }
  41. void CPrivilegedInfoCallback::getTilesInRange(std::unordered_set<int3> & tiles,
  42. const int3 & pos,
  43. int radious,
  44. ETileVisibility mode,
  45. std::optional<PlayerColor> player,
  46. int3::EDistanceFormula distanceFormula) const
  47. {
  48. if(!!player && !player->isValidPlayer())
  49. {
  50. logGlobal->error("Illegal call to getTilesInRange!");
  51. return;
  52. }
  53. if(radious == CBuilding::HEIGHT_SKYSHIP) //reveal entire map
  54. getAllTiles (tiles, player, -1, [](auto * tile){return true;});
  55. else
  56. {
  57. const TeamState * team = !player ? nullptr : gameState().getPlayerTeam(*player);
  58. for (int xd = std::max<int>(pos.x - radious , 0); xd <= std::min<int>(pos.x + radious, gameState().getMap().width - 1); xd++)
  59. {
  60. for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gameState().getMap().height - 1); yd++)
  61. {
  62. int3 tilePos(xd,yd,pos.z);
  63. int distance = pos.dist(tilePos, distanceFormula);
  64. if(distance <= radious)
  65. {
  66. if(!player
  67. || (mode == ETileVisibility::HIDDEN && team->fogOfWarMap[pos.z][xd][yd] == 0)
  68. || (mode == ETileVisibility::REVEALED && team->fogOfWarMap[pos.z][xd][yd] == 1)
  69. )
  70. tiles.insert(int3(xd,yd,pos.z));
  71. }
  72. }
  73. }
  74. }
  75. }
  76. void CPrivilegedInfoCallback::getAllTiles(std::unordered_set<int3> & tiles, std::optional<PlayerColor> Player, int level, std::function<bool(const TerrainTile *)> filter) const
  77. {
  78. if(!!Player && !Player->isValidPlayer())
  79. {
  80. logGlobal->error("Illegal call to getAllTiles !");
  81. return;
  82. }
  83. std::vector<int> floors;
  84. if(level == -1)
  85. {
  86. for(int b = 0; b < gameState().getMap().levels(); ++b)
  87. {
  88. floors.push_back(b);
  89. }
  90. }
  91. else
  92. floors.push_back(level);
  93. for(auto zd: floors)
  94. {
  95. for(int xd = 0; xd < gameState().getMap().width; xd++)
  96. {
  97. for(int yd = 0; yd < gameState().getMap().height; yd++)
  98. {
  99. int3 coordinates(xd, yd, zd);
  100. if (filter(getTile(coordinates)))
  101. tiles.insert(coordinates);
  102. }
  103. }
  104. }
  105. }
  106. void CPrivilegedInfoCallback::pickAllowedArtsSet(std::vector<ArtifactID> & out, vstd::RNG & rand)
  107. {
  108. for (int j = 0; j < 3 ; j++)
  109. out.push_back(gameState().pickRandomArtifact(rand, EArtifactClass::ART_TREASURE));
  110. for (int j = 0; j < 3 ; j++)
  111. out.push_back(gameState().pickRandomArtifact(rand, EArtifactClass::ART_MINOR));
  112. out.push_back(gameState().pickRandomArtifact(rand, EArtifactClass::ART_MAJOR));
  113. }
  114. void CPrivilegedInfoCallback::getAllowedSpells(std::vector<SpellID> & out, std::optional<ui16> level)
  115. {
  116. for (auto const & spellID : gameState().getMap().allowedSpells)
  117. {
  118. const auto * spell = spellID.toEntity(LIBRARY);
  119. if (!isAllowed(spellID))
  120. continue;
  121. if (level.has_value() && spell->getLevel() != level)
  122. continue;
  123. out.push_back(spellID);
  124. }
  125. }
  126. VCMI_LIB_NAMESPACE_END