Summon.cpp 5.4 KB

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