DemonSummon.cpp 3.6 KB

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