local config = {
fearStorage = 48462, -- free storage
fearTimes = 10, -- in miliseconds
fearWalkInterval = 500, -- in miliseconds
range = {2, 2}
}
function doCreatureFear(target, times, interval, storage)
if isCreature(target) and times > 0 then
local p, newPos = getThingPos(target), {}
local fearDirections = {
[0] = {{{x=p.x,y=p.y-1,z=p.z}}, {{x=p.x+1,y=p.y,z=p.z}, {x=p.x-1,y=p.y,z=p.z}, {x=p.x-1,y=p.y-1,z=p.z}, {x=p.x+1,y=p.y-1,z=p.z}}},
[1] = {{{x=p.x+1,y=p.y,z=p.z}}, {{x=p.x,y=p.y+1,z=p.z}, {x=p.x,y=p.y-1,z=p.z}, {x=p.x+1,y=p.y-1,z=p.z}, {x=p.x+1,y=p.y+1,z=p.z}}},
[2] = {{{x=p.x,y=p.y+1,z=p.z}}, {{x=p.x+1,y=p.y,z=p.z}, {x=p.x-1,y=p.y,z=p.z}, {x=p.x-1,y=p.y+1,z=p.z}, {x=p.x+1,y=p.y+1,z=p.z}}},
[3] = {{{x=p.x-1,y=p.y,z=p.z}}, {{x=p.x,y=p.y+1,z=p.z}, {x=p.x,y=p.y-1,z=p.z}, {x=p.x-1,y=p.y-1,z=p.z}, {x=p.x-1,y=p.y+1,z=p.z}}},
[4] = {{{x=p.x,y=p.y+1,z=p.z}, {x=p.x-1,y=p.y,z=p.z}}, {{x=p.x-1,y=p.y-1,z=p.z}, {x=p.x+1,y=p.y+1,z=p.z}, {x=p.x-1,y=p.y+1,z=p.z}}},
[5] = {{{x=p.x+1,y=p.y,z=p.z}, {x=p.x,y=p.y+1,z=p.z}}, {{x=p.x-1,y=p.y+1,z=p.z}, {x=p.x+1,y=p.y-1,z=p.z}, {x=p.x+1,y=p.y+1,z=p.z}}},
[6] = {{{x=p.x,y=p.y-1,z=p.z}, {x=p.x-1,y=p.y,z=p.z}}, {{x=p.x+1,y=p.y-1,z=p.z}, {x=p.x-1,y=p.y+1,z=p.z}, {x=p.x-1,y=p.y-1,z=p.z}}},
[7] = {{{x=p.x+1,y=p.y,z=p.z}, {x=p.x,y=p.y-1,z=p.z}}, {{x=p.x-1,y=p.y-1,z=p.z}, {x=p.x+1,y=p.y+1,z=p.z}, {x=p.x+1,y=p.y-1,z=p.z}}}
}
local fear = fearDirections[getCreatureStorage(target, storage)]
for _, pos in pairs(fear[1]) do
if doTileQueryAdd(target, pos, 0, false) == RETURNVALUE_NOERROR then
if getTilePzInfo(pos) ~= true then
table.insert(newPos, pos)
end
end
end
if #newPos == 0 then
for _, pos in pairs(fear[2]) do
if doTileQueryAdd(target, pos, 0, false) == RETURNVALUE_NOERROR then
if getTilePzInfo(pos) ~= true then
table.insert(newPos, pos)
end
end
end
end
if #newPos ~= 0 then
doTeleportThing(target, newPos[math.random(#newPos)])
end
doSendAnimatedText(getThingPos(target), 'Fear', TEXTCOLOR_GREY)
addEvent(doCreatureFear, interval, target, times - 1, interval, storage)
end
end
function doCreatureStopFear(target, storage)
if isCreature(target) then
doCreatureSetNoMove(target, false)
doCreatureSetStorage(target, storage, -1)
end
end
function onCastSpell(cid, var)
local list, p, targets = getSpectators(getThingPos(cid), config.range[1], config.range[2]), getThingPos(cid), {}
if #list > 0 then
for i = 1, #list do
if cid ~= list[i] then
table.insert(targets, list[i])
end
end
end
if #targets > 0 then
for i = 1, #targets do
if isPlayer(targets[i]) or isMonster(targets[i]) then
if getCreatureStorage(targets[i], config.fearStorage) > 0 then
return false
end
doAddCondition(targets[i], createConditionObject(CONDITION_INFIGHT, 1000 * 60))
doSendDistanceShoot(p, getThingPos(targets[i]), CONST_ANI_DEATH)
doCreatureSetStorage(targets[i], config.fearStorage, getDirectionTo(getThingPos(cid), getThingPos(targets[i])))
doCreatureSetNoMove(targets[i], true)
doCreatureFear(targets[i], config.fearTimes, config.fearWalkInterval, config.fearStorage)
addEvent(doCreatureStopFear, config.fearTimes * config.fearWalkInterval, targets[i], config.fearStorage)
end
end
end
return true
end