ObstacleHandler.cpp 2.9 KB

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