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.
Also, we can even make this pos table locked, since we don't need to alter these values, using this function:
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