Summon.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "../../NetPacks.h"
  18. #include "../../serializer/JsonSerializeFormat.h"
  19. #include "../../CCreatureHandler.h"
  20. #include "../../CHeroHandler.h"
  21. #include "../../mapObjects/CGHeroInstance.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(exclusive)
  38. {
  39. //check if there are summoned creatures of other type
  40. auto otherSummoned = m->battle()->battleGetUnitsIf([m, this](const battle::Unit * unit)
  41. {
  42. return (unit->unitOwner() == m->getCasterColor())
  43. && (unit->unitSlot() == SlotID::SUMMONED_SLOT_PLACEHOLDER)
  44. && (!unit->isClone())
  45. && (unit->creatureId() != creature);
  46. });
  47. if(!otherSummoned.empty())
  48. {
  49. const auto *elemental = otherSummoned.front();
  50. MetaString text;
  51. text.appendLocalString(EMetaText::GENERAL_TXT, 538);
  52. const auto *caster = dynamic_cast<const CGHeroInstance *>(m->caster);
  53. if(caster)
  54. {
  55. text.replaceRawString(caster->getNameTranslated());
  56. text.replaceLocalString(EMetaText::CRE_PL_NAMES, elemental->creatureIndex());
  57. if(caster->type->gender == EHeroGender::FEMALE)
  58. text.replaceLocalString(EMetaText::GENERAL_TXT, 540);
  59. else
  60. text.replaceLocalString(EMetaText::GENERAL_TXT, 539);
  61. }
  62. problem.add(std::move(text), Problem::NORMAL);
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. void Summon::apply(ServerCallback * server, const Mechanics * m, const EffectTarget & target) const
  69. {
  70. //new feature - percentage bonus
  71. auto valueWithBonus = m->applySpecificSpellBonus(m->calculateRawEffectValue(0, m->getEffectPower()));//TODO: consider use base power too
  72. BattleUnitsChanged pack;
  73. pack.battleID = m->battle()->getBattle()->getBattleID();
  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->getMaxHealth()));
  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