Hello guys!
Well, I almost never look at this forum, but since I was bored I decided to post here two simple functions. They are useful IMO, they are called indexToPos() and posToIndex(): they convert positions to numbers and numbers to positions!
yeye, I released it on OpenLua also, WHATEVER!
Hope you like it
Credits to Remere, since he found the right numbers to use here so they work fine with storage values.
How to use them? Example:
Isn't that FON?
It's a great use for storage values.
Cya!
PS: Meh, a "Function" prefix or a "Functions" subforum should be added u.u
Well, I almost never look at this forum, but since I was bored I decided to post here two simple functions. They are useful IMO, they are called indexToPos() and posToIndex(): they convert positions to numbers and numbers to positions!
yeye, I released it on OpenLua also, WHATEVER!
Hope you like it
Credits to Remere, since he found the right numbers to use here so they work fine with storage values.
Code:
if(bit == nil) then
error("Your server does not have bit libraries.")
end
function posToIndex(pos)
if(type(pos) ~= "table" or pos.x == nil or pos.y == nil or pos.z == nil) then
error("[posToIndex]: Argument is not a valid position!")
return LUA_ERROR
end
-- ((x & 0x3FFF) << 18 | (y & 0x3FFF) << 4 | (z & 0xF))
local index = bit.bor( bit.lshift( bit.band(pos.x,16383), 18), bit.lshift( bit.band(pos.y, 16383), 4), bit.band(pos.z, 15) )
return index
end
function indexToPos(index)
if(type(index) ~= "number") then
error("[indexToPos]: Argument is not a valid index!")
return LUA_ERROR
end
-- pos.x = ((index) >> 18) & 0x3FFF; pos.y = ((index) >> 4) & 0x3FFF; pos.z = ((index) & 0xF);
local x = bit.band(bit.rshift(index, 18), 16383)
local y = bit.band(bit.rshift(index, 4), 16383)
local z = bit.band(index, 15)
return {x=x, y=y, z=z}
end
How to use them? Example:
Code:
setPlayerStorageValue(cid, 6000, posToIndex(getThingPos(cid)))
Code:
local pos = indexToPos(getPlayerStorageValue(cid, 6000))
Isn't that FON?
It's a great use for storage values.
Cya!
PS: Meh, a "Function" prefix or a "Functions" subforum should be added u.u
Last edited: