ObstacleHandler.cpp 2.8 KB

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