Summon.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Summon.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 "Summon.h"
  12. #include "Registry.h"
  13. #include "../ISpellMechanics.h"
  14. #include "../../battle/CBattleInfoCallback.h"
  15. #include "../../battle/Unit.h"
  16. #include "../../NetPacks.h"
  17. #include "../../serializer/JsonSerializeFormat.h"
  18. #include "../../CCreatureHandler.h"
  19. #include "../../CHeroHandler.h"
  20. #include "../../mapObjects/CGHeroInstance.h"
  21. static const std::string EFFECT_NAME = "core:summon";
  22. namespace spells
  23. {
  24. namespace effects
  25. {
  26. VCMI_REGISTER_SPELL_EFFECT(Summon, EFFECT_NAME);
  27. Summon::Summon()
  28. : Effect(),
  29. creature(),
  30. permanent(false),
  31. exclusive(true),
  32. summonByHealth(false),
  33. summonSameUnit(false)
  34. {
  35. }
  36. Summon::~Summon() = default;
  37. void Summon::adjustAffectedHexes(std::set<BattleHex> & hexes, const Mechanics * m, const Target & spellTarget) const
  38. {
  39. //no hexes affected
  40. }
  41. void Summon::adjustTargetTypes(std::vector<TargetType> & types) const
  42. {
  43. //any target type allowed
  44. }
  45. bool Summon::applicable(Problem & problem, const Mechanics * m) const
  46. {
  47. if(exclusive)
  48. {
  49. //check if there are summoned creatures of other type
  50. auto otherSummoned = m->cb->battleGetUnitsIf([m, this](const battle::Unit * unit)
  51. {
  52. return (unit->unitOwner() == m->getCasterColor())
  53. && (unit->unitSlot() == SlotID::SUMMONED_SLOT_PLACEHOLDER)
  54. && (!unit->isClone())
  55. && (unit->creatureId() != creature);
  56. });
  57. if(!otherSummoned.empty())
  58. {
  59. auto elemental = otherSummoned.front();
  60. MetaString text;
  61. text.addTxt(MetaString::GENERAL_TXT, 538);
  62. auto caster = dynamic_cast<const CGHeroInstance *>(m->caster);
  63. if(caster)
  64. {
  65. text.addReplacement(caster->name);
  66. text.addReplacement(MetaString::CRE_PL_NAMES, elemental->creatureIndex());
  67. if(caster->type->sex)
  68. text.addReplacement(MetaString::GENERAL_TXT, 540);
  69. else
  70. text.addReplacement(MetaString::GENERAL_TXT, 539);
  71. }
  72. problem.add(std::move(text), Problem::NORMAL);
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. void Summon::apply(BattleStateProxy * battleState, RNG & rng, const Mechanics * m, const EffectTarget & target) const
  79. {
  80. //new feature - percentage bonus
  81. auto valueWithBonus = m->applySpecificSpellBonus(m->calculateRawEffectValue(0, m->getEffectPower()));//TODO: consider use base power too
  82. BattleUnitsChanged pack;
  83. for(auto & dest : target)
  84. {
  85. if(dest.unitValue)
  86. {
  87. const battle::Unit * summoned = dest.unitValue;
  88. std::shared_ptr<battle::Unit> state = summoned->acquire();
  89. int64_t healthValue = (summonByHealth ? valueWithBonus : (valueWithBonus * summoned->MaxHealth()));
  90. state->heal(healthValue, EHealLevel::OVERHEAL, (permanent ? EHealPower::PERMANENT : EHealPower::ONE_BATTLE));
  91. pack.changedStacks.emplace_back(summoned->unitId(), UnitChanges::EOperation::RESET_STATE);
  92. state->save(pack.changedStacks.back().data);
  93. }
  94. else
  95. {
  96. int32_t amount = 0;
  97. if(summonByHealth)
  98. {
  99. auto creatureType = creature.toCreature();
  100. auto creatureMaxHealth = creatureType->MaxHealth();
  101. amount = static_cast<int32_t>(valueWithBonus / creatureMaxHealth);
  102. }
  103. else
  104. {
  105. amount = static_cast<int32_t>(valueWithBonus);
  106. }
  107. if(amount < 1)
  108. {
  109. battleState->complain("Summoning didn't summon any!");
  110. continue;
  111. }
  112. battle::UnitInfo info;
  113. info.id = m->cb->battleNextUnitId();
  114. info.count = amount;
  115. info.type = creature;
  116. info.side = m->casterSide;
  117. info.position = dest.hexValue;
  118. info.summoned = !permanent;
  119. pack.changedStacks.emplace_back(info.id, UnitChanges::EOperation::ADD);
  120. info.save(pack.changedStacks.back().data);
  121. }
  122. }
  123. if(!pack.changedStacks.empty())
  124. battleState->apply(&pack);
  125. }
  126. EffectTarget Summon::filterTarget(const Mechanics * m, const EffectTarget & target) const
  127. {
  128. return target;
  129. }
  130. void Summon::serializeJsonEffect(JsonSerializeFormat & handler)
  131. {
  132. handler.serializeId("id", creature, CreatureID());
  133. handler.serializeBool("permanent", permanent, false);
  134. handler.serializeBool("exclusive", exclusive, true);
  135. handler.serializeBool("summonByHealth", summonByHealth, false);
  136. handler.serializeBool("summonSameUnit", summonSameUnit, false);
  137. }
  138. EffectTarget Summon::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  139. {
  140. auto sameSummoned = m->cb->battleGetUnitsIf([m, this](const battle::Unit * unit)
  141. {
  142. return (unit->unitOwner() == m->getCasterColor())
  143. && (unit->unitSlot() == SlotID::SUMMONED_SLOT_PLACEHOLDER)
  144. && (!unit->isClone())
  145. && (unit->creatureId() == creature)
  146. && (unit->alive());
  147. });
  148. EffectTarget effectTarget;
  149. if(sameSummoned.empty() || !summonSameUnit)
  150. {
  151. BattleHex hex = m->cb->getAvaliableHex(creature, m->casterSide);
  152. if(!hex.isValid())
  153. logGlobal->error("No free space to summon creature!");
  154. else
  155. effectTarget.emplace_back(hex);
  156. }
  157. else
  158. {
  159. effectTarget.emplace_back(sameSummoned.front());
  160. }
  161. return effectTarget;
  162. }
  163. }
  164. }