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

Lua [TFS 0.3.6] What is the best way to check if player stand 1 square to you?

waqmaz

Member
Joined
Jun 17, 2015
Messages
203
Reaction score
11
What is the best way to check if player stand next to you? It doesn't need to be head to head, just one square to you. I need it, because I've done a new spell in lua and that spell works in every range, and it is for a knight, so it should be in 0 range.
There are some functions:
Code:
getCreaturePosition(target)
getClosestFreeTile(cid, targetpos[, extended = false[, ignoreHouse = true]])
I am trying to check if cid x is +1 of target x using "getCreaturePosition(target)", but this way there is so much to do, lol.
Code:
    local myPos = getCreaturePosition(cid)
    local targetPos = getCreaturePosition(target)
        if
            myPos.x == targetPos.x+1 or
            myPos.x == targetPos.x-1 or
            myPos.y == targetPos.y+1 or
            myPos.y == targetPos.y-1
        then
            print('player 1 square to you')
        else
            print('not 1 suare to you!')
        end
 
Last edited:
For 0.3.6 you have getDistanceBetween(firstPosition, secondPosition)
Same square returns 0.
Edit: Sorry you want to make something for people who are 1 sqm from you without already have a target id?
 
For 0.3.6 you have getDistanceBetween(firstPosition, secondPosition)
Same square returns 0.
Edit: Sorry you want to make something for people who are 1 sqm from you without already have a target id?
No, I own target ID. I've edited my post, look on example.
So I need to do:
Code:
if getDistanceBetween(getCreaturePosition(cid), getCreaturePosition(target)) == 1 then
???

cool, it works, thank, men
 
Code:
local myPos = getCreaturePosition(cid)
  local targetPos = getCreaturePosition(target)
   if getDistanceBetween(myPos, targetPos) == 1 then
               print('player 1 square to you')
        else
            print('not 1 square to you!')
        end
 
target in front of player, unless player changes look direction
Code:
if doComparePositions(getCreaturePosition(target), getCreatureLookPosition(cid)) then
 
These 2 versions are the same, which is the fastest (I wonder):
1.
Code:
local myPos = getCreaturePosition(cid)
    local targetPos = getCreaturePosition(target)
        if
            myPos.x == targetPos.x+1 or
            myPos.x == targetPos.x-1 or
            myPos.y == targetPos.y+1 or
            myPos.y == targetPos.y-1
        then
            print('player 1 square to you')
        else
            print('not 1 suare to you!')
        end
2.
Code:
local myPos = getCreaturePosition(cid)
local targetPos = getCreaturePosition(target)
if getDistanceBetween(myPos, targetPos) == 1 then
     print('player 1 square to you')
else
     print('not 1 square to you!')
end
target in front of player, unless player changes look direction
Code:
if doComparePositions(getCreaturePosition(target), getCreatureLookPosition(cid)) then
There is not a "doComparePositions" function in 03.6. probably.
 
Don't worry you can add it if you deleted it
Code:
function doComparePositions(position, positionEx)
    return position.x == positionEx.x and position.y == positionEx.y and position.z == positionEx.z
end
 
Don't worry you can add it if you deleted it
Code:
function doComparePositions(position, positionEx)
    return position.x == positionEx.x and position.y == positionEx.y and position.z == positionEx.z
end
So sorry, it is in data/lib/032-position.lua.
I haven't noticed that before, thank, but R. Tavares is the one who has just really helped me. Do you maybe know how to check the execution time job ? I want to print seconds which were needed to execute these functions.
 
Back
Top