ObstacleHandler.cpp 2.7 KB

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