function chain(creature, effect, minDamage, maxDamage, combatType)
local spectators = Game.getSpectators(creature:getPosition(), false, false, 9, 9, 6, 6)
local totalTarget = 0
local targets = {}
for _, spectador in pairs(spectators) do
if spectador:isPlayer() then
targets[#targets + 1] = spectador
end
end
local lastChain = creature
local lastChainPosition = creature:getPosition()
local closestTarget, closestTargetIndex, closestTargetPosition
local path, tempPosition, updateLastChain
while (totalTarget < 5 and #targets > 0) do
closestTarget = nil
for index, target in pairs(targets) do
tempPosition = target:getPosition()
if not closestTarget or getDiagonalDistance(lastChain:getPosition(), tempPosition) < getDiagonalDistance(lastChain:getPosition(), closestTargetPosition) then
closestTarget = target
closestTargetIndex = index
closestTargetPosition = tempPosition
end
end
table.remove(targets, closestTargetIndex)
updateLastChain = false
if lastChainPosition:getDistance(closestTargetPosition) == 1 then
updateLastChain = true
else
path = lastChainPosition:getPathTo(closestTargetPosition, 0, 1, true, true, 9)
if path and #path > 0 then
for i=1, #path do
lastChainPosition:getNextPosition(path, 1)
lastChainPosition:sendMagicEffect(effect)
end
updateLastChain = true
end
end
if updateLastChain then
doTargetCombatHealth(creature, closestTarget, combatType, -minDamage, -maxDamage, effect)
closestTargetPosition:sendMagicEffect(effect)
lastChain = closestTarget
lastChainPosition = closestTargetPosition
totalTarget = totalTarget + 1
end
end
return totalTarget
end
function onCastSpell(creature, variant)
local min = 500
local max = 1000
local total = chain(creature, CONST_ME_WHITE_ENERGY_SPARK, min, max, COMBAT_DEATHDAMAGE)
if total > 0 then
return true
end
end