Moat.cpp 3.7 KB

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