Summon.cpp 5.4 KB

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