Obstacle.cpp 8.6 KB

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