Obstacle.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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.serializeString("appearSound", appearSound);
  72. handler.serializeString("appearAnimation", appearAnimation);
  73. handler.serializeString("triggerSound", triggerSound);
  74. handler.serializeString("triggerAnimation", triggerAnimation);
  75. handler.serializeString("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 & trasformation : options.shape)
  85. {
  86. BattleHex hex = destination.hexValue;
  87. for(auto direction : trasformation)
  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 && 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 & trasformation : options.shape)
  111. {
  112. BattleHex hex = destination.hexValue;
  113. for(auto direction : trasformation)
  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(m->isMassive())
  144. {
  145. std::vector<BattleHex> availableTiles;
  146. for(int i = 0; i < GameConstants::BFIELD_SIZE; i++)
  147. {
  148. BattleHex hex = i;
  149. if(isHexAvailable(m->battle(), hex, true))
  150. availableTiles.push_back(hex);
  151. }
  152. RandomGeneratorUtil::randomShuffle(availableTiles, *server->getRNG());
  153. const int patchesToPut = std::min(patchCount, static_cast<int>(availableTiles.size()));
  154. EffectTarget randomTarget;
  155. randomTarget.reserve(patchesToPut);
  156. for(int i = 0; i < patchesToPut; i++)
  157. randomTarget.emplace_back(availableTiles.at(i));
  158. placeObstacles(server, m, randomTarget);
  159. }
  160. else
  161. {
  162. placeObstacles(server, m, target);
  163. }
  164. }
  165. void Obstacle::serializeJsonEffect(JsonSerializeFormat & handler)
  166. {
  167. handler.serializeBool("hidden", hidden);
  168. handler.serializeBool("passable", passable);
  169. handler.serializeBool("trigger", trigger);
  170. handler.serializeBool("trap", trap);
  171. handler.serializeBool("removeOnTrigger", removeOnTrigger);
  172. handler.serializeInt("patchCount", patchCount);
  173. handler.serializeInt("turnsRemaining", turnsRemaining, -1);
  174. handler.serializeStruct("attacker", sideOptions.at(BattleSide::ATTACKER));
  175. handler.serializeStruct("defender", sideOptions.at(BattleSide::DEFENDER));
  176. }
  177. bool Obstacle::isHexAvailable(const CBattleInfoCallback * cb, const BattleHex & hex, const bool mustBeClear)
  178. {
  179. if(!hex.isAvailable())
  180. return false;
  181. if(!mustBeClear)
  182. return true;
  183. if(cb->battleGetUnitByPos(hex, true))
  184. return false;
  185. auto obst = cb->battleGetAllObstaclesOnPos(hex, false);
  186. for(auto & i : obst)
  187. if(i->obstacleType != CObstacleInstance::MOAT)
  188. return false;
  189. if(cb->battleGetSiegeLevel() != 0)
  190. {
  191. EWallPart part = cb->battleHexToWallPart(hex);
  192. if(part == EWallPart::INVALID)
  193. return true;//no fortification here
  194. else if(part == EWallPart::INDESTRUCTIBLE_PART_OF_GATE)
  195. return true; // location accessible
  196. else if(part == EWallPart::INDESTRUCTIBLE_PART)
  197. return false;//indestructible part (cant be checked by battleGetWallState)
  198. else if(part == EWallPart::BOTTOM_TOWER || part == EWallPart::UPPER_TOWER)
  199. return false;//destructible, but should not be available
  200. else if(cb->battleGetWallState(part) != EWallState::DESTROYED && cb->battleGetWallState(part) != EWallState::NONE)
  201. return false;
  202. }
  203. return true;
  204. }
  205. bool Obstacle::noRoomToPlace(Problem & problem, const Mechanics * m)
  206. {
  207. MetaString text;
  208. text.addTxt(MetaString::GENERAL_TXT, 181);//No room to place %s here
  209. text.addReplacement(m->getSpellName());
  210. problem.add(std::move(text));
  211. return false;
  212. }
  213. void Obstacle::placeObstacles(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  214. {
  215. const ObstacleSideOptions & options = sideOptions.at(m->casterSide);
  216. BattleObstaclesChanged pack;
  217. auto all = m->battle()->battleGetAllObstacles(BattlePerspective::ALL_KNOWING);
  218. int obstacleIdToGive = 1;
  219. for(auto & one : all)
  220. if(one->uniqueID >= obstacleIdToGive)
  221. obstacleIdToGive = one->uniqueID + 1;
  222. for(const Destination & destination : target)
  223. {
  224. SpellCreatedObstacle obstacle;
  225. obstacle.uniqueID = obstacleIdToGive++;
  226. obstacle.pos = destination.hexValue;
  227. obstacle.obstacleType = CObstacleInstance::USUAL;
  228. obstacle.ID = m->getSpellIndex();
  229. obstacle.turnsRemaining = turnsRemaining;
  230. obstacle.casterSpellPower = m->getEffectPower();
  231. obstacle.spellLevel = m->getEffectLevel();//todo: level of indirect effect should be also configurable
  232. obstacle.casterSide = m->casterSide;
  233. obstacle.hidden = hidden;
  234. obstacle.passable = passable;
  235. obstacle.trigger = trigger;
  236. obstacle.trap = trap;
  237. obstacle.removeOnTrigger = removeOnTrigger;
  238. obstacle.appearSound = options.appearSound;
  239. obstacle.appearAnimation = options.appearAnimation;
  240. obstacle.triggerSound = options.triggerSound;
  241. obstacle.triggerAnimation = options.triggerAnimation;
  242. obstacle.animation = options.animation;
  243. obstacle.animationYOffset = options.offsetY;
  244. obstacle.customSize.clear();
  245. obstacle.customSize.reserve(options.shape.size());
  246. for(const auto & shape : options.shape)
  247. {
  248. BattleHex hex = destination.hexValue;
  249. for(auto direction : shape)
  250. hex.moveInDirection(direction, false);
  251. obstacle.customSize.emplace_back(hex);
  252. }
  253. pack.changes.emplace_back();
  254. obstacle.toInfo(pack.changes.back());
  255. }
  256. if(!pack.changes.empty())
  257. server->apply(&pack);
  258. }
  259. }
  260. }
  261. VCMI_LIB_NAMESPACE_END