Summon.cpp 4.9 KB

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