Moat.cpp 3.8 KB

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