Page 1 of 1

Unique chunk ID

Posted: Wed Dec 11, 2024 8:29 pm
by Natha
I hope this is the right place...

This is a helper method for all modders who store positional data. Instead of having a storage table

Code: Select all

{ [x] = { [y] = {..., ...} }
, which is bad programming and nil-checking, there is something better.
2D positions are a countable set which means you can assign each {x, y} element a natural number. I wrote a spiral function for that in LUA, based on https://web.archive.org/web/20141202041 ... miserably/:

Code: Select all

function spiral(pos)
	local x = math.abs( {table.unpack(pos)}[1])
	local y = math.abs( {table.unpack(pos)}[2])
	local a = math.max(x, y)
	local dist = 0
	
	local i = 0
	while y > x or y >= -x do -- rotate by 90° (multiply by -j) until values are in the top triangle
		p = {-y, x}
		dist = dist + 2*a
		i = i + 1
		if i == 5 then break end
	end
	dist = dist + a - x
	return dist + ((a*2)-1)^2
end
Just give a position (tile or chunk) and a unique ID you get!