Obstacle.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Obstacle.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 "Obstacle.h"
  12. #include "Registry.h"
  13. #include "../ISpellMechanics.h"
  14. #include "../../NetPacks.h"
  15. #include "../../battle/IBattleState.h"
  16. #include "../../battle/CBattleInfoCallback.h"
  17. #include "../../serializer/JsonSerializeFormat.h"
  18. static const std::string EFFECT_NAME = "core:obstacle";
  19. namespace spells
  20. {
  21. namespace effects
  22. {
  23. VCMI_REGISTER_SPELL_EFFECT(Obstacle, EFFECT_NAME);
  24. ObstacleSideOptions::ObstacleSideOptions()
  25. : shape(),
  26. range()
  27. {
  28. }
  29. void ObstacleSideOptions::serializeJson(JsonSerializeFormat & handler)
  30. {
  31. serializeRelativeShape(handler, "shape", shape);
  32. serializeRelativeShape(handler, "range", range);
  33. handler.serializeString("appearAnimation", appearAnimation);
  34. handler.serializeString("animation", animation);
  35. handler.serializeInt("offsetY", offsetY);
  36. }
  37. void ObstacleSideOptions::serializeRelativeShape(JsonSerializeFormat & handler, const std::string & fieldName, RelativeShape & value)
  38. {
  39. static const std::vector<std::string> EDirMap =
  40. {
  41. "TL",
  42. "TR",
  43. "R",
  44. "BR",
  45. "BL",
  46. "L",
  47. ""
  48. };
  49. {
  50. JsonArraySerializer outer = handler.enterArray(fieldName);
  51. outer.syncSize(value, JsonNode::JsonType::DATA_VECTOR);
  52. for(size_t outerIndex = 0; outerIndex < outer.size(); outerIndex++)
  53. {
  54. JsonArraySerializer inner = outer.enterArray(outerIndex);
  55. inner.syncSize(value.at(outerIndex), JsonNode::JsonType::DATA_STRING);
  56. for(size_t innerIndex = 0; innerIndex < inner.size(); innerIndex++)
  57. {
  58. std::string temp;
  59. if(handler.saving)
  60. {
  61. temp = EDirMap.at(value.at(outerIndex).at(innerIndex));
  62. }
  63. inner.serializeString(innerIndex, temp);
  64. if(!handler.saving)
  65. {
  66. value.at(outerIndex).at(innerIndex) = (BattleHex::EDir) vstd::find_pos(EDirMap, temp);
  67. }
  68. }
  69. }
  70. }
  71. if(!handler.saving)
  72. {
  73. if(value.empty())
  74. value.emplace_back();
  75. if(value.back().empty())
  76. value.back().emplace_back(BattleHex::EDir::NONE);
  77. }
  78. }
  79. Obstacle::Obstacle()
  80. : LocationEffect(),
  81. hidden(false),
  82. passable(false),
  83. trigger(false),
  84. trap(false),
  85. removeOnTrigger(false),
  86. patchCount(1),
  87. turnsRemaining(-1)
  88. {
  89. }
  90. Obstacle::~Obstacle() = default;
  91. void Obstacle::adjustAffectedHexes(std::set<BattleHex> & hexes, const Mechanics * m, const Target & spellTarget) const
  92. {
  93. EffectTarget effectTarget = transformTarget(m, spellTarget, spellTarget);
  94. const ObstacleSideOptions & options = sideOptions.at(m->casterSide);
  95. for(auto & destination : effectTarget)
  96. {
  97. for(auto & trasformation : options.shape)
  98. {
  99. BattleHex hex = destination.hexValue;
  100. for(auto direction : trasformation)
  101. hex.moveInDirection(direction, false);
  102. if(hex.isValid())
  103. hexes.insert(hex);
  104. }
  105. }
  106. }
  107. bool Obstacle::applicable(Problem & problem, const Mechanics * m) const
  108. {
  109. return LocationEffect::applicable(problem, m);
  110. }
  111. bool Obstacle::applicable(Problem & problem, const Mechanics * m, const EffectTarget & target) const
  112. {
  113. if(!m->isMassive())
  114. {
  115. const bool requiresClearTiles = m->requiresClearTiles();
  116. const ObstacleSideOptions & options = sideOptions.at(m->casterSide);
  117. if(target.empty())
  118. return noRoomToPlace(problem, m);
  119. for(const auto & destination : target)
  120. {
  121. for(auto & trasformation : options.shape)
  122. {
  123. BattleHex hex = destination.hexValue;
  124. for(auto direction : trasformation)
  125. hex.moveInDirection(direction, false);
  126. if(!isHexAvailable(m->cb, hex, requiresClearTiles))
  127. return noRoomToPlace(problem, m);
  128. }
  129. }
  130. }
  131. return LocationEffect::applicable(problem, m, target);
  132. }
  133. EffectTarget Obstacle::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  134. {
  135. const ObstacleSideOptions & options = sideOptions.at(m->casterSide);
  136. EffectTarget ret;
  137. if(!m->isMassive())
  138. {
  139. for(auto & spellDestination : spellTarget)
  140. {
  141. for(auto & rangeShape : options.range)
  142. {
  143. BattleHex hex = spellDestination.hexValue;
  144. for(auto direction : rangeShape)
  145. hex.moveInDirection(direction, false);
  146. ret.emplace_back(hex);
  147. }
  148. }
  149. }
  150. return ret;
  151. }
  152. void Obstacle::apply(BattleStateProxy * battleState, RNG & rng, const Mechanics * m, const EffectTarget & target) const
  153. {
  154. if(m->isMassive())
  155. {
  156. std::vector<BattleHex> availableTiles;
  157. for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
  158. {
  159. BattleHex hex = i;
  160. if(isHexAvailable(m->cb, hex, true))
  161. availableTiles.push_back(hex);
  162. }
  163. RandomGeneratorUtil::randomShuffle(availableTiles, rng);
  164. const int patchesToPut = std::min<int>(patchCount, availableTiles.size());
  165. EffectTarget randomTarget;
  166. randomTarget.reserve(patchesToPut);
  167. for(int i = 0; i < patchesToPut; i++)
  168. randomTarget.emplace_back(availableTiles.at(i));
  169. placeObstacles(battleState, m, randomTarget);
  170. }
  171. else
  172. {
  173. placeObstacles(battleState, m, target);
  174. }
  175. }
  176. void Obstacle::serializeJsonEffect(JsonSerializeFormat & handler)
  177. {
  178. handler.serializeBool("hidden", hidden);
  179. handler.serializeBool("passable", passable);
  180. handler.serializeBool("trigger", trigger);
  181. handler.serializeBool("trap", trap);
  182. handler.serializeBool("removeOnTrigger", removeOnTrigger);
  183. handler.serializeInt("patchCount", patchCount);
  184. handler.serializeInt("turnsRemaining", turnsRemaining, -1);
  185. handler.serializeStruct("attacker", sideOptions.at(BattleSide::ATTACKER));
  186. handler.serializeStruct("defender", sideOptions.at(BattleSide::DEFENDER));
  187. }
  188. bool Obstacle::isHexAvailable(const CBattleInfoCallback * cb, const BattleHex & hex, const bool mustBeClear)
  189. {
  190. if(!hex.isAvailable())
  191. return false;
  192. if(!mustBeClear)
  193. return true;
  194. if(cb->battleGetUnitByPos(hex, true))
  195. return false;
  196. auto obst = cb->battleGetAllObstaclesOnPos(hex, false);
  197. for(auto & i : obst)
  198. if(i->obstacleType != CObstacleInstance::MOAT)
  199. return false;
  200. if(cb->battleGetSiegeLevel() != 0)
  201. {
  202. EWallPart::EWallPart part = cb->battleHexToWallPart(hex);
  203. if(part == EWallPart::INVALID || part == EWallPart::INDESTRUCTIBLE_PART_OF_GATE)
  204. return true;//no fortification here
  205. else if(static_cast<int>(part) < 0)
  206. return false;//indestructible part (cant be checked by battleGetWallState)
  207. else if(part == EWallPart::BOTTOM_TOWER || part == EWallPart::UPPER_TOWER)
  208. return false;//destructible, but should not be available
  209. else if(cb->battleGetWallState(part) != EWallState::DESTROYED && cb->battleGetWallState(part) != EWallState::NONE)
  210. return false;
  211. }
  212. return true;
  213. }
  214. bool Obstacle::noRoomToPlace(Problem & problem, const Mechanics * m)
  215. {
  216. MetaString text;
  217. text.addTxt(MetaString::GENERAL_TXT, 181);//No room to place %s here
  218. text.addReplacement(m->getSpellName());
  219. problem.add(std::move(text));
  220. return false;
  221. }
  222. void Obstacle::placeObstacles(BattleStateProxy * battleState, const Mechanics * m, const EffectTarget & target) const
  223. {
  224. const ObstacleSideOptions & options = sideOptions.at(m->casterSide);
  225. BattleObstaclesChanged pack;
  226. boost::optional<BattlePerspective::BattlePerspective> perspective;
  227. if(!m->cb->getPlayerID())
  228. perspective = boost::make_optional(BattlePerspective::ALL_KNOWING);
  229. auto all = m->cb->battleGetAllObstacles(perspective);
  230. int obstacleIdToGive = 1;
  231. for(auto & one : all)
  232. if(one->uniqueID >= obstacleIdToGive)
  233. obstacleIdToGive = one->uniqueID + 1;
  234. for(const Destination & destination : target)
  235. {
  236. SpellCreatedObstacle obstacle;
  237. obstacle.uniqueID = obstacleIdToGive++;
  238. obstacle.pos = destination.hexValue;
  239. obstacle.obstacleType = CObstacleInstance::USUAL;
  240. obstacle.ID = m->getSpellIndex();
  241. obstacle.turnsRemaining = turnsRemaining;
  242. obstacle.casterSpellPower = m->getEffectPower();
  243. obstacle.spellLevel = m->getEffectLevel();//todo: level of indirect effect should be also configurable
  244. obstacle.casterSide = m->casterSide;
  245. obstacle.hidden = hidden;
  246. obstacle.passable = passable;
  247. obstacle.trigger = trigger;
  248. obstacle.trap = trap;
  249. obstacle.removeOnTrigger = removeOnTrigger;
  250. obstacle.appearAnimation = options.appearAnimation;
  251. obstacle.animation = options.animation;
  252. obstacle.animationYOffset = options.offsetY;
  253. obstacle.customSize.clear();
  254. obstacle.customSize.reserve(options.shape.size());
  255. for(auto & shape : options.shape)
  256. {
  257. BattleHex hex = destination.hexValue;
  258. for(auto direction : shape)
  259. hex.moveInDirection(direction, false);
  260. obstacle.customSize.emplace_back(hex);
  261. }
  262. pack.changes.emplace_back();
  263. obstacle.toInfo(pack.changes.back());
  264. }
  265. if(!pack.changes.empty())
  266. battleState->apply(&pack);
  267. }
  268. }
  269. }