Moat.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Moat.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 "Moat.h"
  12. #include "Registry.h"
  13. #include "../ISpellMechanics.h"
  14. #include "../../NetPacks.h"
  15. #include "../../mapObjects/CGTownInstance.h"
  16. #include "../../bonuses/Limiters.h"
  17. #include "../../battle/IBattleState.h"
  18. #include "../../battle/CBattleInfoCallback.h"
  19. #include "../../serializer/JsonSerializeFormat.h"
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. namespace spells
  22. {
  23. namespace effects
  24. {
  25. static void serializeMoatHexes(JsonSerializeFormat & handler, const std::string & fieldName, std::vector<std::vector<BattleHex>> & moatHexes)
  26. {
  27. {
  28. JsonArraySerializer outer = handler.enterArray(fieldName);
  29. outer.syncSize(moatHexes, JsonNode::JsonType::DATA_VECTOR);
  30. for(size_t outerIndex = 0; outerIndex < outer.size(); outerIndex++)
  31. {
  32. JsonArraySerializer inner = outer.enterArray(outerIndex);
  33. inner.syncSize(moatHexes.at(outerIndex), JsonNode::JsonType::DATA_INTEGER);
  34. for(size_t innerIndex = 0; innerIndex < inner.size(); innerIndex++)
  35. inner.serializeInt(innerIndex, moatHexes.at(outerIndex).at(innerIndex));
  36. }
  37. }
  38. }
  39. void Moat::serializeJsonEffect(JsonSerializeFormat & handler)
  40. {
  41. handler.serializeBool("hidden", hidden);
  42. handler.serializeBool("trap", trap);
  43. handler.serializeBool("removeOnTrigger", removeOnTrigger);
  44. handler.serializeBool("dispellable", dispellable);
  45. handler.serializeInt("moatDamage", moatDamage);
  46. serializeMoatHexes(handler, "moatHexes", moatHexes);
  47. handler.serializeId("triggerAbility", triggerAbility, SpellID::NONE);
  48. handler.serializeStruct("defender", sideOptions); //Moats are defender only
  49. assert(!handler.saving);
  50. {
  51. auto guard = handler.enterStruct("bonus");
  52. const JsonNode & data = handler.getCurrent();
  53. for(const auto & p : data.Struct())
  54. {
  55. //TODO: support JsonSerializeFormat in Bonus
  56. auto guard = handler.enterStruct(p.first);
  57. const JsonNode & bonusNode = handler.getCurrent();
  58. auto b = JsonUtils::parseBonus(bonusNode);
  59. bonus.push_back(b);
  60. }
  61. }
  62. }
  63. void Moat::convertBonus(const Mechanics * m, std::vector<Bonus> & converted) const
  64. {
  65. for(const auto & b : bonus)
  66. {
  67. Bonus nb(*b);
  68. //Moat battlefield effect is always permanent
  69. nb.duration = Bonus::ONE_BATTLE;
  70. if(m->battle()->battleGetDefendedTown() && m->battle()->battleGetSiegeLevel() >= CGTownInstance::CITADEL)
  71. {
  72. nb.sid = Bonus::getSid32(m->battle()->battleGetDefendedTown()->getFaction(), BuildingID::CITADEL);
  73. nb.source = Bonus::TOWN_STRUCTURE;
  74. }
  75. else
  76. {
  77. nb.sid = m->getSpellIndex(); //for all
  78. nb.source = Bonus::SPELL_EFFECT;//for all
  79. }
  80. std::set<BattleHex> flatMoatHexes;
  81. for(const auto & moatPatch : moatHexes)
  82. flatMoatHexes.insert(moatPatch.begin(), moatPatch.end());
  83. nb.limiter = std::make_shared<UnitOnHexLimiter>(std::move(flatMoatHexes));
  84. converted.push_back(nb);
  85. }
  86. }
  87. void Moat::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  88. {
  89. assert(m->isMassive());
  90. assert(m->battle()->battleGetDefendedTown());
  91. if(m->isMassive() && m->battle()->battleGetSiegeLevel() >= CGTownInstance::CITADEL)
  92. {
  93. EffectTarget moat;
  94. placeObstacles(server, m, moat);
  95. std::vector<Bonus> converted;
  96. convertBonus(m, converted);
  97. for(auto & b : converted)
  98. {
  99. GiveBonus gb(GiveBonus::ETarget::BATTLE);
  100. gb.bonus = b;
  101. server->apply(&gb);
  102. }
  103. }
  104. }
  105. void Moat::placeObstacles(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  106. {
  107. assert(m->battle()->battleGetDefendedTown());
  108. assert(m->casterSide == BattleSide::DEFENDER); // Moats are always cast by defender
  109. BattleObstaclesChanged pack;
  110. auto all = m->battle()->battleGetAllObstacles(BattlePerspective::ALL_KNOWING);
  111. int obstacleIdToGive = 1;
  112. for(auto & one : all)
  113. if(one->uniqueID >= obstacleIdToGive)
  114. obstacleIdToGive = one->uniqueID + 1;
  115. for(const auto & destination : moatHexes) //Moat hexes can be different obstacles
  116. {
  117. SpellCreatedObstacle obstacle;
  118. obstacle.uniqueID = obstacleIdToGive++;
  119. obstacle.pos = destination.at(0);
  120. obstacle.obstacleType = dispellable ? CObstacleInstance::SPELL_CREATED : CObstacleInstance::MOAT;
  121. obstacle.ID = m->getSpellIndex();
  122. obstacle.turnsRemaining = -1; //Moat cannot be expired
  123. obstacle.casterSpellPower = m->getEffectPower();
  124. obstacle.spellLevel = m->getEffectLevel(); //todo: level of indirect effect should be also configurable
  125. obstacle.casterSide = BattleSide::DEFENDER; // Moats are always cast by defender
  126. obstacle.minimalDamage = moatDamage; // Minimal moat damage
  127. obstacle.hidden = hidden;
  128. obstacle.passable = true; //Moats always passable
  129. obstacle.trigger = triggerAbility;
  130. obstacle.trap = trap;
  131. obstacle.removeOnTrigger = removeOnTrigger;
  132. obstacle.nativeVisible = false; //Moats is invisible for native terrain
  133. obstacle.appearSound = sideOptions.appearSound; //For dispellable moats
  134. obstacle.appearAnimation = sideOptions.appearAnimation; //For dispellable moats
  135. obstacle.animation = sideOptions.animation;
  136. obstacle.customSize.insert(obstacle.customSize.end(),destination.cbegin(), destination.cend());
  137. obstacle.animationYOffset = sideOptions.offsetY;
  138. pack.changes.emplace_back();
  139. obstacle.toInfo(pack.changes.back());
  140. }
  141. if(!pack.changes.empty())
  142. server->apply(&pack);
  143. }
  144. }
  145. }
  146. VCMI_LIB_NAMESPACE_END