Obstacle.cpp 9.0 KB

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