Obstacle.cpp 8.8 KB

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