Page 1 of 1

Desync-safe generated coordinates list?

Posted: Fri Feb 21, 2020 10:43 am
by Deadlock989
The following code produces an array of grid coordinates and then sorts them into order of distance from (0,0):

Code: Select all

local circle_list = {}
for x = -5,5 do
	for y = -5,5 do
		table.insert(circle_list, {x=x,y=y})
	end
end		
table.sort(circle_list, function (k1,k2) return math.sqrt((k1.x^2)+(k1.y^2)) < math.sqrt((k2.x^2)+(k2.y^2)) end )
I want this list to be available for a function in control.lua, for plonking down item spills in a circular pattern from the origin, rather than running the maths every time the function is called. It's basically a look-up table for the nth nearest grid position from the centre.

It's deterministic, right?

... Isn't it?

I could do the following:

1. Believe it's deterministic and will always have the same result on any machine, so it's desync-safe, so I can just keep it in a local variable and treat it as a constant.

2. Keep it in the global table to be safe, but it will be sent around the network a fair bit even though it never changes during a game session.

3. Dump it to a text file, turn that output into a Lua table, and require it as fixed data. And then have to re-do all of that if I ever need it to change.

I want 1 because I'm lazy. But paranoia reigns. What would you do?

Re: Desync-safe generated coordinates list?

Posted: Fri Feb 21, 2020 10:46 am
by Bilka
Deadlock989 wrote: Fri Feb 21, 2020 10:43 am I want 1 because I'm lazy. But paranoia reigns. What would you do?
I'd use https://lua-api.factorio.com/latest/Lua ... item_stack, it uses the base game way of making a circular pattern and it's easy to use. If you want multiple spills on top of each other, just spill on top of your original spill.

Re: Desync-safe generated coordinates list?

Posted: Fri Feb 21, 2020 10:48 am
by Deadlock989
Bilka wrote: Fri Feb 21, 2020 10:46 am
Deadlock989 wrote: Fri Feb 21, 2020 10:43 am I want 1 because I'm lazy. But paranoia reigns. What would you do?
I'd use https://lua-api.factorio.com/latest/Lua ... item_stack, it uses the base game way of making a circular pattern and it's easy to use.
(a) I want stacks of items instead of individual items because there could be a lot of items, like, hundreds. If they're in stacks it reduces the number of entities by a factor of 50 or 100

(b) I don't like the pattern it makes

Re: Desync-safe generated coordinates list?

Posted: Fri Feb 21, 2020 12:13 pm
by Klonan
Deadlock989 wrote: Fri Feb 21, 2020 10:43 amWhat would you do?
#1

Re: Desync-safe generated coordinates list?

Posted: Fri Feb 21, 2020 3:46 pm
by pleegwat
You can make it slightly faster by eliminating the math.sqrt() (sorting on the square of the distance instead) since that's equivalent.

Re: Desync-safe generated coordinates list?

Posted: Fri Feb 21, 2020 6:19 pm
by Deadlock989
pleegwat wrote: Fri Feb 21, 2020 3:46 pm You can make it slightly faster by eliminating the math.sqrt() (sorting on the square of the distance instead) since that's equivalent.
I know, but since it runs exactly once, it doesn't matter.