1
0
mirror of https://github.com/vcmi/vcmi.git synced 2026-05-22 09:55:17 +02:00
Files
vcmi/scripts/SpellEffectSummon.lua
T

155 lines
3.9 KiB
Lua

local BattleUnitsChanged = require("netpacks.BattleUnitsChanged")
local MetaString = require("texts.MetaString")
-- expected globals:
-- parameters:
-- - creature
-- - permanent
-- - exclusive
-- - summonByHealth
-- - summonSameUnit
local function summonedEffectValue(parameters, mechanics)
effectPower = mechanics:getEffectPower()
rawEffectPower = mechanics:calculateRawEffectValue(0, effectPower)
finaleEffectPower = mechanics:applySpecificSpellBonus(rawEffectPower)
return finaleEffectPower
end
local function summonedCreatureHealth(parameters, mechanics)
local valueWithBonus = summonedEffectValue(parameters, mechanics)
if parameters.summonByHealth then
return valueWithBonus
else
return valueWithBonus * parameters.creature:getMaxHealth()
end
end
local function summonedCreatureAmount(parameters, mechanics)
local valueWithBonus = summonedEffectValue(parameters, mechanics)
if parameters.summonByHealth then
return math.floor(valueWithBonus / parameters.creature:getMaxHealth())
else
return valueWithBonus
end
end
applicable = function(parameters, mechanics, problem)
if parameters.creature == "nil" then
return false -- mechanics:adaptGenericProblem(problem)
end
if summonedCreatureAmount(parameters, mechanics) == 0 then
return false -- mechanics:adaptGenericProblem(problem)
end
-- check if there are summoned creatures of other type
if parameters.exclusive then
local battle = mechanics:getBattle();
local otherSummoned = battle:getAnyUnitIf(function(unit)
return (unit:getOwner() == mechanics:getCasterColor())
and (unit:isSummoned())
and (not unit:isClone())
and (unit:getCreature() ~= parameters.creature)
end)
if otherSummoned ~= nil then
local elemental = otherSummoned[1]
local text = MetaString.new()
text:appendTextID("core.genrltxt.538")
local hero = mechanics:caster():getHeroCaster()
if caster then
text:replaceRawString(caster:getNameTranslated())
text:replaceNamePlural(elemental:creatureId())
if caster.gender == EHeroGender.FEMALE then
text:replaceTextID("core.genrltxt.540")
else
text:replaceTextID("core.genrltxt.539")
end
end
problem:add(text, Problem.NORMAL)
return false
end
end
return true
end
apply = function(parameters, mechanics, server, target)
local pack = BattleUnitsChanged.new()
pack.battleID = mechanics:getBattleID()
for _, dest in ipairs(target) do
if dest.unitValue then
local summoned = dest.unitValue
local state = summoned:acquire()
local healthValue = summonedCreatureHealth(parameters, mechanics)
state:heal(
healthValue,
EHealLevel.OVERHEAL,
(parameters.permanent and EHealPower.PERMANENT or EHealPower.ONE_BATTLE)
)
server:battleUnitChanged(
summoned:unitId(),
UnitChanges.EOperation.UPDATE,
state:save()
)
else
server:battleUnitChanged(
mechanics:getBattle():getNextUnitId(),
UnitChanges.EOperation.ADD,
{
count = summonedCreatureAmount(parameters, mechanics),
type = parameters.creature,
side = mechanics:casterSide(),
position = dest.hexValue,
summoned = not parameters.permanent
}
)
end
end
if #pack.changedStacks > 0 then
server:apply(pack)
end
end
transformTarget = function(parameters, mechanics, aimPoint, spellTarget)
local sameSummoned = mechanics:getBattle():getAnyUnitIf(function(unit)
return (unit:getOwner() == mechanics:getCasterColor())
and (unit:isSummoned())
and (not unit:isClone())
and (unit:getCreature() == parameters.creature)
and (unit:isAlive())
end)
local effectTarget = {}
if sameSummoned == nil or not parameters.summonSameUnit then
local hex = mechanics:getBattle():getAvailableHex(parameters.creature, mechanics:casterSide())
if not hex:isValid() then
return {} -- no free space. FIXME: should be in isApplicable
else
return {
{ hex }
}
end
else
return {
{ sameSummoned[1]:getPosition(), sameSummoned[1] }
}
end
end