Moat.cpp 5.7 KB

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