Moat.cpp 5.6 KB

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