ObstacleHandler.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. std::vector<BattleHex> ObstacleInfo::getBlocked(BattleHex hex) const
  49. {
  50. std::vector<BattleHex> ret;
  51. if(isAbsoluteObstacle)
  52. {
  53. assert(!hex.isValid());
  54. range::copy(blockedTiles, std::back_inserter(ret));
  55. return ret;
  56. }
  57. for(int offset : blockedTiles)
  58. {
  59. BattleHex toBlock = hex + offset;
  60. if((hex.getY() & 1) && !(toBlock.getY() & 1))
  61. toBlock += BattleHex::LEFT;
  62. if(!toBlock.isValid())
  63. logGlobal->error("Misplaced obstacle!");
  64. else
  65. ret.push_back(toBlock);
  66. }
  67. return ret;
  68. }
  69. bool ObstacleInfo::isAppropriate(const TerrainId terrainType, const BattleField & battlefield) const
  70. {
  71. const auto * bgInfo = battlefield.getInfo();
  72. if(bgInfo->isSpecial)
  73. return vstd::contains(allowedSpecialBfields, bgInfo->identifier);
  74. return vstd::contains(allowedTerrains, terrainType);
  75. }
  76. std::shared_ptr<ObstacleInfo> ObstacleHandler::loadFromJson(const std::string & scope, const JsonNode & json, const std::string & identifier, size_t index)
  77. {
  78. assert(identifier.find(':') == std::string::npos);
  79. auto info = std::make_shared<ObstacleInfo>(Obstacle(index), identifier);
  80. info->modScope = scope;
  81. info->animation = AnimationPath::fromJson(json["animation"]);
  82. info->width = json["width"].Integer();
  83. info->height = json["height"].Integer();
  84. for(const auto & t : json["allowedTerrains"].Vector())
  85. {
  86. VLC->identifiers()->requestIdentifier("terrain", t, [info](int32_t identifier){
  87. info->allowedTerrains.emplace_back(identifier);
  88. });
  89. }
  90. for(const auto & t : json["specialBattlefields"].Vector())
  91. info->allowedSpecialBfields.emplace_back(t.String());
  92. info->blockedTiles = json["blockedTiles"].convertTo<std::vector<si16>>();
  93. info->isAbsoluteObstacle = json["absolute"].Bool();
  94. info->isForegroundObstacle = json["foreground"].Bool();
  95. return info;
  96. }
  97. std::vector<JsonNode> ObstacleHandler::loadLegacyData()
  98. {
  99. return {};
  100. }
  101. const std::vector<std::string> & ObstacleHandler::getTypeNames() const
  102. {
  103. static const std::vector<std::string> types = { "obstacle" };
  104. return types;
  105. }
  106. VCMI_LIB_NAMESPACE_END