DemonSummon.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * DemonSummon.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 "DemonSummon.h"
  12. #include "Registry.h"
  13. #include "../ISpellMechanics.h"
  14. #include "../../NetPacks.h"
  15. #include "../../battle/CBattleInfoCallback.h"
  16. #include "../../battle/CUnitState.h"
  17. #include "../../serializer/JsonSerializeFormat.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. static const std::string EFFECT_NAME = "core:demonSummon";
  20. namespace spells
  21. {
  22. namespace effects
  23. {
  24. VCMI_REGISTER_SPELL_EFFECT(DemonSummon, EFFECT_NAME);
  25. DemonSummon::DemonSummon()
  26. : UnitEffect()
  27. , creature(0)
  28. , permanent(false)
  29. {
  30. }
  31. DemonSummon::~DemonSummon() = default;
  32. void DemonSummon::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  33. {
  34. BattleUnitsChanged pack;
  35. for(const Destination & dest : target)
  36. {
  37. const battle::Unit * targetStack = dest.unitValue;
  38. //we shall have all targets to be stacks
  39. if(!targetStack || targetStack->alive() || targetStack->isGhost())
  40. {
  41. server->complain("No corpse to demonize! Invalid effect target transformation.");
  42. continue;
  43. }
  44. auto hex = m->battle()->getAvaliableHex(targetStack->creatureId(), m->casterSide, targetStack->getPosition());
  45. if(!hex.isValid())
  46. {
  47. server->complain("No place to put new summon!");
  48. break;
  49. }
  50. auto creatureType = creature.toCreature(m->creatures());
  51. int32_t deadCount = targetStack->unitBaseAmount();
  52. int32_t deadTotalHealth = targetStack->getTotalHealth();
  53. int32_t raisedMaxHealth = creatureType->getMaxHealth();
  54. int32_t raisedTotalHealth = m->applySpellBonus(m->getEffectValue(), targetStack);
  55. // Can't raise stack with more HP than original stack
  56. int32_t maxAmountFromHealth = deadTotalHealth / raisedMaxHealth;
  57. // Can't raise stack with more creatures than original stack
  58. int32_t maxAmountFromAmount = deadCount;
  59. // Can't raise stack with more HP than our spellpower
  60. int32_t maxAmountFromSpellpower = raisedTotalHealth / raisedMaxHealth;
  61. int32_t finalAmount = std::min( { maxAmountFromHealth, maxAmountFromAmount, maxAmountFromSpellpower } );
  62. if(finalAmount < 1)
  63. {
  64. server->complain("Summoning didn't summon any!");
  65. continue;
  66. }
  67. battle::UnitInfo info;
  68. info.id = m->battle()->battleNextUnitId();
  69. info.count = finalAmount;
  70. info.type = creature;
  71. info.side = m->casterSide;
  72. info.position = dest.hexValue;
  73. info.summoned = !permanent;
  74. // add newly created creature
  75. pack.changedStacks.emplace_back(info.id, UnitChanges::EOperation::ADD);
  76. info.save(pack.changedStacks.back().data);
  77. // and remove corpse to prevent second raising or resurrection
  78. pack.changedStacks.emplace_back(targetStack->unitId(), UnitChanges::EOperation::REMOVE);
  79. }
  80. if(!pack.changedStacks.empty())
  81. server->apply(&pack);
  82. }
  83. bool DemonSummon::isValidTarget(const Mechanics * m, const battle::Unit * s) const
  84. {
  85. if(!s->isDead())
  86. return false;
  87. if (s->isGhost())
  88. return false;
  89. auto creatureType = creature.toCreature(m->creatures());
  90. if (s->getTotalHealth() < creatureType->getMaxHealth())
  91. return false;
  92. return m->isReceptive(s);
  93. }
  94. void DemonSummon::serializeJsonUnitEffect(JsonSerializeFormat & handler)
  95. {
  96. handler.serializeId("id", creature, CreatureID());
  97. handler.serializeBool("permanent", permanent, false);
  98. }
  99. }
  100. }
  101. VCMI_LIB_NAMESPACE_END