Summon.cpp 4.8 KB

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