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

Grouping positions together

Nostradamus

Member
Joined
Jun 2, 2007
Messages
219
Reaction score
6
I don't know if this is new, since it's too obvious, but I had an idea: why don't we use positions on a unix timestamp format or something like that?

We can use some convention like:

X
Y = 60X
Z = (60^2)X

So comparations between positions can be much easy.

Here it is an easy example of doing that with the memoize technique, to reuse the same table for the same ps.
PHP:
local results = {}
setmetatable(results, {__mode = "v"}) -- make values weak
function mkPos(posX, posY, posZ)
    local key = posX + posY*60 + posZ*(60^2) 
    if (results[key]) then
        return results[key]
    else
        local newPos = {x = posX, y = posY, z = posZ}
        results[key] = newPos
        return newPos
    end
end

Also, we can even make this pos table locked, since we don't need to alter these values, using this function:

PHP:
function table.lock(t)
    local lock = {}
    local mt = {
        __index = t,
        __newindex = function (t, k, v)
           error("Attempt to update a locked table")
        end
    }
    setmetatable(lock, mt)
    return lock
end
 
Nice, but I don't see much use of it ^.-

You might give some examples of the usage :p
 
I would rather move bits for the position ->
posbit |= pos.z << 16;
posbit |= pos.y << 8;
posbit |= pos.x;

and to get real pos
pos.z = posbit >> 16;
pos.y = posbit >> 8 & 0xFF;
pos.x = posbit & 0xFF;

simplier, alot.
 
Found this some time ago.

(unknown author)
Code:
if(bit == nil) then
	error("Your server does not have bit libraries.")
end

function posToIndex(pos)
	if(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 & 0xFFFF) << 24 | (y & 0xFFFF) << 8 | (z & 0xFF))
	local index = bit.bor( bit.lshift( bit.band(pos.x, 65535), 24 ), bit.lshift( bit.band(pos.y, 65535), 8), bit.band(pos.z, 255) )
	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) >> 24) & 0xFFFF; pos.y = ((index) >> 8) & 0xFFFF; pos.z = ((index) & 0xFF);
	local x = bit.band(bit.rshift(index, 24), 65535)
	local y = bit.band(bit.rshift(index, 8), 65535)
	local z = bit.band(index, 255)
	return {x=x, y=y, z=z}
end

Usage is simple, fe. saving position in 1 storage:
Code:
local pos = getPlayerPosition(cid)
setPlayerStorageValue(cid, 666, posToIndex(pos))

and reusing it
Code:
local oldPos = indexToPos(getPlayerStorageValue(cid, 666))
doSendMagicEffect(oldPos, 40)
 
@Up
Author is Pedro B. but he only converted code from POT to Lua.
 

Similar threads

Back
Top