ObstacleHandler.cpp 2.8 KB

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