Summon.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "../../MetaString.h"
  15. #include "../../battle/CBattleInfoCallback.h"
  16. #include "../../battle/BattleInfo.h"
  17. #include "../../battle/Unit.h"
  18. #include "../../serializer/JsonSerializeFormat.h"
  19. #include "../../CCreatureHandler.h"
  20. #include "../../CHeroHandler.h"
  21. #include "../../mapObjects/CGHeroInstance.h"
  22. #include "../../networkPacks/PacksForClientBattle.h"
  23. VCMI_LIB_NAMESPACE_BEGIN
  24. namespace spells
  25. {
  26. namespace effects
  27. {
  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 (creature == CreatureID::NONE)
  39. {
  40. logMod->error("Attempt to summon non-existing creature!");
  41. return m->adaptGenericProblem(problem);
  42. }
  43. if (summonedCreatureAmount(m) == 0)
  44. {
  45. logMod->debug("Attempt to summon zero creatures!");
  46. return m->adaptGenericProblem(problem);
  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. const auto *elemental = otherSummoned.front();
  61. MetaString text;
  62. text.appendLocalString(EMetaText::GENERAL_TXT, 538);
  63. const auto *caster = dynamic_cast<const CGHeroInstance *>(m->caster);
  64. if(caster)
  65. {
  66. text.replaceRawString(caster->getNameTranslated());
  67. text.replaceNamePlural(elemental->creatureId());
  68. if(caster->type->gender == EHeroGender::FEMALE)
  69. text.replaceLocalString(EMetaText::GENERAL_TXT, 540);
  70. else
  71. text.replaceLocalString(EMetaText::GENERAL_TXT, 539);
  72. }
  73. problem.add(std::move(text), Problem::NORMAL);
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. int32_t Summon::summonedCreatureHealth(const Mechanics * m, const battle::Unit * unit) const
  80. {
  81. auto valueWithBonus = m->applySpecificSpellBonus(m->calculateRawEffectValue(0, m->getEffectPower()));
  82. if(summonByHealth)
  83. return valueWithBonus;
  84. else
  85. return valueWithBonus * unit->getMaxHealth();
  86. }
  87. int32_t Summon::summonedCreatureAmount(const Mechanics * m) const
  88. {
  89. auto valueWithBonus = m->applySpecificSpellBonus(m->calculateRawEffectValue(0, m->getEffectPower()));
  90. if(summonByHealth)
  91. {
  92. const auto *creatureType = creature.toEntity(m->creatures());
  93. auto creatureMaxHealth = creatureType->getMaxHealth();
  94. return valueWithBonus / creatureMaxHealth;
  95. }
  96. else
  97. {
  98. return valueWithBonus;
  99. }
  100. }
  101. void Summon::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  102. {
  103. using battle::HealInfo;
  104. BattleUnitsChanged pack;
  105. pack.battleID = m->battle()->getBattle()->getBattleID();
  106. for(const auto & dest : target)
  107. {
  108. if(dest.unitValue)
  109. {
  110. const battle::Unit * summoned = dest.unitValue;
  111. std::shared_ptr<battle::Unit> state = summoned->acquire();
  112. int64_t healthValue = summonedCreatureHealth(m, summoned);
  113. state->heal(healthValue, EHealLevel::OVERHEAL, (permanent ? EHealPower::PERMANENT : EHealPower::ONE_BATTLE));
  114. pack.changedStacks.emplace_back(summoned->unitId(), UnitChanges::EOperation::RESET_STATE);
  115. state->save(pack.changedStacks.back().data);
  116. }
  117. else
  118. {
  119. int32_t amount = summonedCreatureAmount(m);
  120. if(amount < 1)
  121. {
  122. server->complain("Summoning didn't summon any!");
  123. continue;
  124. }
  125. battle::UnitInfo info;
  126. info.id = m->battle()->battleNextUnitId();
  127. info.count = amount;
  128. info.type = creature;
  129. info.side = m->casterSide;
  130. info.position = dest.hexValue;
  131. info.summoned = !permanent;
  132. pack.changedStacks.emplace_back(info.id, UnitChanges::EOperation::ADD);
  133. info.save(pack.changedStacks.back().data);
  134. }
  135. }
  136. if(!pack.changedStacks.empty())
  137. server->apply(&pack);
  138. }
  139. EffectTarget Summon::filterTarget(const Mechanics * m, const EffectTarget & target) const
  140. {
  141. return target;
  142. }
  143. void Summon::serializeJsonEffect(JsonSerializeFormat & handler)
  144. {
  145. handler.serializeId("id", creature, CreatureID());
  146. handler.serializeBool("permanent", permanent, false);
  147. handler.serializeBool("exclusive", exclusive, true);
  148. handler.serializeBool("summonByHealth", summonByHealth, false);
  149. handler.serializeBool("summonSameUnit", summonSameUnit, false);
  150. }
  151. EffectTarget Summon::transformTarget(const Mechanics * m, const Target & aimPoint, const Target & spellTarget) const
  152. {
  153. auto sameSummoned = m->battle()->battleGetUnitsIf([m, this](const battle::Unit * unit)
  154. {
  155. return (unit->unitOwner() == m->getCasterColor())
  156. && (unit->unitSlot() == SlotID::SUMMONED_SLOT_PLACEHOLDER)
  157. && (!unit->isClone())
  158. && (unit->creatureId() == creature)
  159. && (unit->alive());
  160. });
  161. EffectTarget effectTarget;
  162. if(sameSummoned.empty() || !summonSameUnit)
  163. {
  164. BattleHex hex = m->battle()->getAvaliableHex(creature, m->casterSide);
  165. if(!hex.isValid())
  166. logGlobal->error("No free space to summon creature!");
  167. else
  168. effectTarget.emplace_back(hex);
  169. }
  170. else
  171. {
  172. effectTarget.emplace_back(sameSummoned.front());
  173. }
  174. return effectTarget;
  175. }
  176. }
  177. }
  178. VCMI_LIB_NAMESPACE_END