Desync-safe generated coordinates list?

Place to get help with not working mods / modding interface.
Post Reply
User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Desync-safe generated coordinates list?

Post 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?
Image

Bilka
Factorio Staff
Factorio Staff
Posts: 3135
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Desync-safe generated coordinates list?

Post 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.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: Desync-safe generated coordinates list?

Post 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
Image

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Desync-safe generated coordinates list?

Post by Klonan »

Deadlock989 wrote:
Fri Feb 21, 2020 10:43 am
What would you do?
#1

pleegwat
Filter Inserter
Filter Inserter
Posts: 258
Joined: Fri May 19, 2017 7:31 pm
Contact:

Re: Desync-safe generated coordinates list?

Post 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.

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: Desync-safe generated coordinates list?

Post 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.
Image

Post Reply

Return to “Modding help”