Obstacle.cpp 8.7 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 "../../NetPacks.h"
  15. #include "../../battle/IBattleState.h"
  16. #include "../../battle/CBattleInfoCallback.h"
  17. #include "../../serializer/JsonSerializeFormat.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. static const std::string EFFECT_NAME = "core:obstacle";
  20. namespace spells
  21. {
  22. namespace effects
  23. {
  24. VCMI_REGISTER_SPELL_EFFECT(Obstacle, EFFECT_NAME);
  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.serializeString("appearSound", appearSound);
  74. handler.serializeString("appearAnimation", appearAnimation);
  75. handler.serializeString("triggerSound", triggerSound);
  76. handler.serializeString("triggerAnimation", triggerAnimation);
  77. handler.serializeString("animation", animation);
  78. handler.serializeInt("offsetY", offsetY);
  79. }
  80. void Obstacle::adjustAffectedHexes(std::set<BattleHex> & hexes, const Mechanics * m, const Target & spellTarget) const
  81. {
  82. EffectTarget effectTarget = transformTarget(m, spellTarget, spellTarget);
  83. const ObstacleSideOptions & options = sideOptions.at(m->casterSide);
  84. for(auto & destination : effectTarget)
  85. {
  86. for(const auto & trasformation : options.shape)
  87. {
  88. BattleHex hex = destination.hexValue;
  89. for(auto direction : trasformation)
  90. hex.moveInDirection(direction, false);
  91. if(hex.isValid())
  92. hexes.insert(hex);
  93. }
  94. }
  95. }
  96. bool Obstacle::applicable(Problem & problem, const Mechanics * m) const
  97. {
  98. if(hidden && m->battle()->battleHasNativeStack(m->battle()->otherSide(m->casterSide)))
  99. return m->adaptProblem(ESpellCastProblem::NO_APPROPRIATE_TARGET, problem);
  100. return LocationEffect::applicable(problem, m);
  101. }
  102. bool Obstacle::applicable(Problem & problem, const Mechanics * m, const EffectTarget & target) const
  103. {
  104. if(!m->isMassive())
  105. {
  106. const bool requiresClearTiles = m->requiresClearTiles();
  107. const ObstacleSideOptions & options = sideOptions.at(m->casterSide);
  108. if(target.empty())
  109. return noRoomToPlace(problem, m);
  110. for(const auto & destination : target)
  111. {
  112. for(const auto & trasformation : options.shape)
  113. {
  114. BattleHex hex = destination.hexValue;
  115. for(auto direction : trasformation)
  116. hex.moveInDirection(direction, false);
  117. if(!isHexAvailable(m->battle(), hex, requiresClearTiles))
  118. return noRoomToPlace(problem, m);
  119. }
  120. }
  121. }
  122. return LocationEffect::applicable(problem, m, target);
  123. }
  124. EffectTarget Obstacle::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  125. {
  126. const ObstacleSideOptions & options = sideOptions.at(m->casterSide);
  127. EffectTarget ret;
  128. if(!m->isMassive())
  129. {
  130. for(const auto & spellDestination : spellTarget)
  131. {
  132. for(const auto & rangeShape : options.range)
  133. {
  134. BattleHex hex = spellDestination.hexValue;
  135. for(auto direction : rangeShape)
  136. hex.moveInDirection(direction, false);
  137. ret.emplace_back(hex);
  138. }
  139. }
  140. }
  141. return ret;
  142. }
  143. void Obstacle::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  144. {
  145. if(m->isMassive())
  146. {
  147. std::vector<BattleHex> availableTiles;
  148. for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
  149. {
  150. BattleHex hex = i;
  151. if(isHexAvailable(m->battle(), hex, true))
  152. availableTiles.push_back(hex);
  153. }
  154. RandomGeneratorUtil::randomShuffle(availableTiles, *server->getRNG());
  155. const int patchesToPut = std::min(patchCount, static_cast<int>(availableTiles.size()));
  156. EffectTarget randomTarget;
  157. randomTarget.reserve(patchesToPut);
  158. for(int i = 0; i < patchesToPut; i++)
  159. randomTarget.emplace_back(availableTiles.at(i));
  160. placeObstacles(server, m, randomTarget);
  161. }
  162. else
  163. {
  164. placeObstacles(server, m, target);
  165. }
  166. }
  167. void Obstacle::serializeJsonEffect(JsonSerializeFormat & handler)
  168. {
  169. handler.serializeBool("hidden", hidden);
  170. handler.serializeBool("passable", passable);
  171. handler.serializeBool("trigger", trigger);
  172. handler.serializeBool("trap", trap);
  173. handler.serializeBool("removeOnTrigger", removeOnTrigger);
  174. handler.serializeInt("patchCount", patchCount);
  175. handler.serializeInt("turnsRemaining", turnsRemaining, -1);
  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.addTxt(MetaString::GENERAL_TXT, 181);//No room to place %s here
  211. text.addReplacement(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. auto all = m->battle()->battleGetAllObstacles(BattlePerspective::ALL_KNOWING);
  220. int obstacleIdToGive = 1;
  221. for(auto & one : all)
  222. if(one->uniqueID >= obstacleIdToGive)
  223. obstacleIdToGive = one->uniqueID + 1;
  224. for(const Destination & destination : target)
  225. {
  226. SpellCreatedObstacle obstacle;
  227. obstacle.uniqueID = obstacleIdToGive++;
  228. obstacle.pos = destination.hexValue;
  229. obstacle.obstacleType = CObstacleInstance::USUAL;
  230. obstacle.ID = m->getSpellIndex();
  231. obstacle.turnsRemaining = turnsRemaining;
  232. obstacle.casterSpellPower = m->getEffectPower();
  233. obstacle.spellLevel = m->getEffectLevel();//todo: level of indirect effect should be also configurable
  234. obstacle.casterSide = m->casterSide;
  235. obstacle.hidden = hidden;
  236. obstacle.passable = passable;
  237. obstacle.trigger = trigger;
  238. obstacle.trap = trap;
  239. obstacle.removeOnTrigger = removeOnTrigger;
  240. obstacle.appearSound = options.appearSound;
  241. obstacle.appearAnimation = options.appearAnimation;
  242. obstacle.triggerSound = options.triggerSound;
  243. obstacle.triggerAnimation = options.triggerAnimation;
  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