Obstacle.cpp 8.9 KB

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