Summon.cpp 5.0 KB

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