2
0

Moat.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "../../battle/IBattleState.h"
  17. #include "../../battle/CBattleInfoCallback.h"
  18. #include "../../serializer/JsonSerializeFormat.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. static const std::string EFFECT_NAME = "core:moat";
  21. namespace spells
  22. {
  23. namespace effects
  24. {
  25. VCMI_REGISTER_SPELL_EFFECT(Moat, EFFECT_NAME);
  26. static void serializeMoatHexes(JsonSerializeFormat & handler, const std::string & fieldName, std::vector<std::vector<BattleHex>> & moatHexes)
  27. {
  28. {
  29. JsonArraySerializer outer = handler.enterArray(fieldName);
  30. outer.syncSize(moatHexes, JsonNode::JsonType::DATA_VECTOR);
  31. for(size_t outerIndex = 0; outerIndex < outer.size(); outerIndex++)
  32. {
  33. JsonArraySerializer inner = outer.enterArray(outerIndex);
  34. inner.syncSize(moatHexes.at(outerIndex), JsonNode::JsonType::DATA_INTEGER);
  35. for(size_t innerIndex = 0; innerIndex < inner.size(); innerIndex++)
  36. inner.serializeInt(innerIndex, moatHexes.at(outerIndex).at(innerIndex));
  37. }
  38. }
  39. }
  40. void Moat::serializeJsonEffect(JsonSerializeFormat & handler)
  41. {
  42. handler.serializeBool("hidden", hidden);
  43. handler.serializeBool("trigger", trigger);
  44. handler.serializeBool("trap", trap);
  45. handler.serializeBool("removeOnTrigger", removeOnTrigger);
  46. handler.serializeBool("dispellable", dispellable);
  47. handler.serializeInt("moatDamage", moatDamage);
  48. serializeMoatHexes(handler, "moatHexes", moatHexes);
  49. handler.serializeId("triggerAbility", triggerAbility, SpellID::NONE);
  50. handler.serializeStruct("defender", sideOptions); //Moats are defender only
  51. }
  52. void Moat::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  53. {
  54. assert(m->isMassive());
  55. assert(m->battle()->battleGetDefendedTown());
  56. if(m->isMassive() && m->battle()->battleGetSiegeLevel() >= CGTownInstance::CITADEL)
  57. {
  58. EffectTarget moat;
  59. placeObstacles(server, m, moat);
  60. }
  61. }
  62. void Moat::placeObstacles(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  63. {
  64. assert(m->battle()->battleGetDefendedTown());
  65. assert(m->casterSide == BattleSide::DEFENDER); // Moats are always cast by defender
  66. assert(turnsRemaining < 0); // Moats should lasts infininte number of turns
  67. BattleObstaclesChanged pack;
  68. auto all = m->battle()->battleGetAllObstacles(BattlePerspective::ALL_KNOWING);
  69. int obstacleIdToGive = 1;
  70. for(auto & one : all)
  71. if(one->uniqueID >= obstacleIdToGive)
  72. obstacleIdToGive = one->uniqueID + 1;
  73. for(const auto & destination : moatHexes) //Moat hexes can be different obstacles
  74. {
  75. SpellCreatedObstacle obstacle;
  76. obstacle.uniqueID = obstacleIdToGive++;
  77. obstacle.pos = destination.at(0);
  78. obstacle.obstacleType = dispellable ? CObstacleInstance::SPELL_CREATED : CObstacleInstance::MOAT;
  79. obstacle.ID = triggerAbility;
  80. obstacle.turnsRemaining = -1; //Moat cannot be expired
  81. obstacle.casterSpellPower = m->getEffectPower();
  82. obstacle.spellLevel = m->getEffectLevel(); //todo: level of indirect effect should be also configurable
  83. obstacle.casterSide = BattleSide::DEFENDER; // Moats are always cast by defender
  84. obstacle.minimalDamage = moatDamage; // Minimal moat damage
  85. obstacle.hidden = hidden;
  86. obstacle.passable = true; //Moats always passable
  87. obstacle.trigger = trigger;
  88. obstacle.trap = trap;
  89. obstacle.removeOnTrigger = removeOnTrigger;
  90. obstacle.nativeVisible = false; //Moats is invisible for native terrain
  91. obstacle.appearSound = sideOptions.appearSound; //For dispellable moats
  92. obstacle.appearAnimation = sideOptions.appearAnimation; //For dispellable moats
  93. obstacle.animation = sideOptions.animation;
  94. obstacle.customSize.insert(obstacle.customSize.end(),destination.cbegin(), destination.cend());
  95. obstacle.animationYOffset = sideOptions.offsetY;
  96. pack.changes.emplace_back();
  97. obstacle.toInfo(pack.changes.back());
  98. }
  99. if(!pack.changes.empty())
  100. server->apply(&pack);
  101. }
  102. }
  103. }
  104. VCMI_LIB_NAMESPACE_END