Obstacle.cpp 8.5 KB

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