2
0

ObstacleHandler.cpp 2.5 KB

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