ObstacleHandler.cpp 2.6 KB

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