• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

turnCreatureTowardPosition [TFS 1.3] Lua Function

Xikini

I whore myself out for likes
Support Team
Joined
Nov 17, 2010
Messages
5,558
Solutions
386
Reaction score
3,682
bandicam-2021-05-23-18-54-01-842.gif

Nice and simple function.
Turns a creature toward a position.

Easily editable, if you don't like the default cutoff's I decided on.
Lua:
function turnCreatureTowardPosition(creature, position)
    local creaturePosition = creature:getPosition()
    local x, y = creaturePosition.x - position.x, creaturePosition.y - position.y
    local angle = math.deg(math.atan2(y, x)) + 180
    if angle > 315 or angle <= 45 then
        creature:setDirection(1)
    elseif angle <= 135 then
        creature:setDirection(2)
    elseif angle <= 225 then
        creature:setDirection(3)
    else
        creature:setDirection(0)
    end
end

Lua:
local function turnCreatureTowardPosition(creature, position)
    local creaturePosition = creature:getPosition()
    local x, y = creaturePosition.x - position.x, creaturePosition.y - position.y
    local angle = math.deg(math.atan2(y, x)) + 180
    if angle > 315 or angle <= 45 then
        creature:setDirection(1)
    elseif angle <= 135 then
        creature:setDirection(2)
    elseif angle <= 225 then
        creature:setDirection(3)
    else
        creature:setDirection(0)
    end
end

local combat = Combat()
combat:setParameter(COMBAT_PARAM_TYPE, COMBAT_ENERGYDAMAGE)
combat:setParameter(COMBAT_PARAM_EFFECT, CONST_ME_ENERGYAREA)
combat:setParameter(COMBAT_PARAM_DISTANCEEFFECT, CONST_ANI_ENERGY)

function onGetFormulaValues(player, level, magicLevel)
    local min = (level / 5) + (magicLevel * 1.4) + 8
    local max = (level / 5) + (magicLevel * 2.2) + 14
    return -min, -max
end

combat:setCallback(CALLBACK_PARAM_LEVELMAGICVALUE, "onGetFormulaValues")

function onCastSpell(creature, variant)
    turnCreatureTowardPosition(creature, creature:getTarget():getPosition())
    return combat:execute(creature, variant)
end
 

Alpha

Relentless
Premium User
Joined
Apr 3, 2011
Messages
637
Solutions
35
Reaction score
438
Location
South Korea
Should probably use constants for the directions

Lua:
function turnCreatureTowardPosition(creature, position)
    local creaturePosition = creature:getPosition()
    local x = creaturePosition.x - position.x
    local y = creaturePosition.y - position.y
    local angle = math.deg(math.atan2(y, x)) + 180
    
    if angle > 315 or angle <= 45 then
        creature:setDirection(DIRECTION_EAST)
    elseif angle <= 135 then
        creature:setDirection(DIRECTION_SOUTH)
    elseif angle <= 225 then
        creature:setDirection(DIRECTION_WEST)
    else
        creature:setDirection(DIRECTION_NORTH)
    end
end
 

Alpha

Relentless
Premium User
Joined
Apr 3, 2011
Messages
637
Solutions
35
Reaction score
438
Location
South Korea
Sye3Skv.png
 
Top