Obstacle.cpp 8.9 KB

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