Obstacle.cpp 8.9 KB

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