ObstacleHandler.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * ObstacleHandler.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 "ObstacleHandler.h"
  12. #include "BattleFieldHandler.h"
  13. #include "json/JsonNode.h"
  14. #include "modding/IdentifierStorage.h"
  15. #include "VCMI_Lib.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. int32_t ObstacleInfo::getIndex() const
  18. {
  19. return obstacle.getNum();
  20. }
  21. int32_t ObstacleInfo::getIconIndex() const
  22. {
  23. return iconIndex;
  24. }
  25. std::string ObstacleInfo::getJsonKey() const
  26. {
  27. return identifier;
  28. }
  29. std::string ObstacleInfo::getNameTranslated() const
  30. {
  31. return identifier;
  32. }
  33. std::string ObstacleInfo::getNameTextID() const
  34. {
  35. return identifier; // TODO?
  36. }
  37. void ObstacleInfo::registerIcons(const IconRegistar & cb) const
  38. {
  39. }
  40. Obstacle ObstacleInfo::getId() const
  41. {
  42. return obstacle;
  43. }
  44. std::vector<BattleHex> ObstacleInfo::getBlocked(BattleHex hex) const
  45. {
  46. std::vector<BattleHex> ret;
  47. if(isAbsoluteObstacle)
  48. {
  49. assert(!hex.isValid());
  50. range::copy(blockedTiles, std::back_inserter(ret));
  51. return ret;
  52. }
  53. for(int offset : blockedTiles)
  54. {
  55. BattleHex toBlock = hex + offset;
  56. if((hex.getY() & 1) && !(toBlock.getY() & 1))
  57. toBlock += BattleHex::LEFT;
  58. if(!toBlock.isValid())
  59. logGlobal->error("Misplaced obstacle!");
  60. else
  61. ret.push_back(toBlock);
  62. }
  63. return ret;
  64. }
  65. bool ObstacleInfo::isAppropriate(const TerrainId terrainType, const BattleField & battlefield) const
  66. {
  67. const auto * bgInfo = battlefield.getInfo();
  68. if(bgInfo->isSpecial)
  69. return vstd::contains(allowedSpecialBfields, bgInfo->identifier);
  70. return vstd::contains(allowedTerrains, terrainType);
  71. }
  72. std::shared_ptr<ObstacleInfo> ObstacleHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index)
  73. {
  74. assert(identifier.find(':') == std::string::npos);
  75. auto info = std::make_shared<ObstacleInfo>(Obstacle(index), identifier);
  76. info->animation = AnimationPath::fromJson(json["animation"]);
  77. info->width = json["width"].Integer();
  78. info->height = json["height"].Integer();
  79. for(const auto & t : json["allowedTerrains"].Vector())
  80. {
  81. VLC->identifiers()->requestIdentifier("terrain", t, [info](int32_t identifier){
  82. info->allowedTerrains.emplace_back(identifier);
  83. });
  84. }
  85. for(const auto & t : json["specialBattlefields"].Vector())
  86. info->allowedSpecialBfields.emplace_back(t.String());
  87. info->blockedTiles = json["blockedTiles"].convertTo<std::vector<si16>>();
  88. info->isAbsoluteObstacle = json["absolute"].Bool();
  89. info->isForegroundObstacle = json["foreground"].Bool();
  90. return info;
  91. }
  92. std::vector<JsonNode> ObstacleHandler::loadLegacyData()
  93. {
  94. return {};
  95. }
  96. const std::vector<std::string> & ObstacleHandler::getTypeNames() const
  97. {
  98. static const std::vector<std::string> types = { "obstacle" };
  99. return types;
  100. }
  101. VCMI_LIB_NAMESPACE_END