DemonSummon.cpp 4.1 KB

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