Summon.cpp 5.1 KB

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