• 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!

TFS 1.X+ tfs 1.3 creature:setDirection(direction)

roriscrave

Advanced OT User
Joined
Dec 7, 2011
Messages
1,188
Solutions
34
Reaction score
200
Hi, how can i use creature:setDirection(direction), when player use a pick, he set direction to the hole pos??
Lua:
creature:setDirection(direction)

pick.lua
Lua:
local groundIds = {354, 355}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if toPosition.x == CONTAINER_POSITION then
        return false
    end

    local tile = Tile(toPosition)
    if not tile then
        return false
    end

    local ground = tile:getGround()
    if not ground then
        return false
    end

    if (ground.uid > 65535 or ground.actionid == 0) and not isInArray(groundIds, ground.itemid) then
        return false
    end

    ground:transform(392)
    ground:decay()

    toPosition.z = toPosition.z + 1
    tile:relocateTo(toPosition)
    return true
end
 
Solution
You can take the target and player position and do following calculations:

Code:
target position x - player position x

if result is larger than 0: player is on left side of target (change direction to EAST)
if result is smaller than 0: player is on right side of target (change direction to WEST)

target position y - player position y

if result is larger than 0: player is below target (change direction to NORTH)
if result is smaller than 0: player is above (change direction to SOUTH)

Visualization:

Przechwytywanie.PNG

Lua:
local groundIds = {354, 355}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if toPosition.x == CONTAINER_POSITION then
        return false
    end

    local tile = Tile(toPosition)
    if...
You can take the target and player position and do following calculations:

Code:
target position x - player position x

if result is larger than 0: player is on left side of target (change direction to EAST)
if result is smaller than 0: player is on right side of target (change direction to WEST)

target position y - player position y

if result is larger than 0: player is below target (change direction to NORTH)
if result is smaller than 0: player is above (change direction to SOUTH)

Visualization:

Przechwytywanie.PNG

Lua:
local groundIds = {354, 355}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if toPosition.x == CONTAINER_POSITION then
        return false
    end

    local tile = Tile(toPosition)
    if not tile then
        return false
    end

    local verticalDirection = toPosition.y - player:getPosition().y
    local horizontalDirection = toPosition.x - player:getPosition().x
   
    if verticalDirection > 0 then
        player:setDirection(SOUTH)
    elseif verticalDirection < 0 then
        player:setDirection(NORTH)
    end
   
    if horizontalDirection > 0 then
        player:setDirection(EAST)
    elseif horizontalDirection < 0 then
        player:setDirection(WEST)
    end

    local ground = tile:getGround()
    if not ground then
        return false
    end

    if (ground.uid > 65535 or ground.actionid == 0) and not isInArray(groundIds, ground.itemid) then
        return false
    end

    ground:transform(392)
    ground:decay()

    toPosition.z = toPosition.z + 1
    tile:relocateTo(toPosition)
    return true
end

Note that checking the horizontal direction is checked after vertical so if the player is standing diagonally to the target he will always face east or west. You can change conditional order so the player will always face north or south when standing diagonally.

I don't have a possibility to test this rn but I think everything should work fine without any big problems.
 
Solution
Back
Top