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