[Done] on_chunk_generated - position

Place to get help with not working mods / modding interface.
Post Reply
TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

[Done] on_chunk_generated - position

Post by TheSAguy »

Hi,

How would I get the center position of a newly generated chunk?
I know the chuck has an area. the are is a boundingBox and that has a left_top and right_bottom positions.

So if a boundingBox is, {left_top = {-2, -3}, right_bottom = {5, 8}} what would the center position be?

Thanks.
Last edited by TheSAguy on Sat Jun 22, 2019 1:56 am, edited 1 time in total.

User avatar
mat1k
Long Handed Inserter
Long Handed Inserter
Posts: 55
Joined: Sat Dec 22, 2018 8:48 am
Contact:

Re: on_chunk_generated - position

Post by mat1k »

The following script in control.lua will output bounding box and the CENTER to both the console and a file called chunks.log in the %appdata%\Factorio\script-output folder. It uses basic maths to calculate the middle of the x and y rows.

Code: Select all

local g = 0
script.on_event(defines.events.on_chunk_generated, function(event)
	g = g+1
	
	local lt = event.area.left_top
	local rb = event.area.right_bottom
	
	local tx = lt.x
	local ty = lt.y
	local rx = rb.x
	local ry = rb.y
	local x = 0
	local y =0
	
	
	if tx <= 0 and rx <= 0 then x = (tx+rx)/2 end
	if tx >= 0 and rx >= 0 then x = (tx+rx)/2 end
	if tx < 0 and rx > 0 then x = (tx-rx+1)/2 end
	
	if ty <= 0 and ry <= 0 then y = (ty+ry)/2 end
	if ty >= 0 and ry >= 0 then y = (ty+ry)/2 end
	if ty < 0 and ry > 0 then y = (ty-ry+1)/2 end
	
	game.print("\nNew Chunk "  .. g) -- To make it easier to track each chunk.
	game.print(lt) -- Left Top
	game.print(rb) -- Right Bottom
	game.print("X=" .. x .. " Y=" .. y) -- Center
	
	game.write_file("chunks.log", "New Chunk "  .. g .. "\n" , true)
	game.write_file("chunks.log", lt , true) -- Left Top
	game.write_file("chunks.log", "\n" , true)
	game.write_file("chunks.log", rb , true) -- Right Bottom
	game.write_file("chunks.log", "\n" , true)
	game.write_file("chunks.log", "X=" .. x .. " Y=" .. y .. "\n\n" , true) -- Center
end)

TheSAguy
Smart Inserter
Smart Inserter
Posts: 1449
Joined: Mon Jan 13, 2014 6:17 pm
Contact:

Re: on_chunk_generated - position

Post by TheSAguy »

Thanks Mat1k.

User avatar
mat1k
Long Handed Inserter
Long Handed Inserter
Posts: 55
Joined: Sat Dec 22, 2018 8:48 am
Contact:

Re: [Done] on_chunk_generated - position

Post by mat1k »

You can safely comment out any of the game.print() or game.writefile() lines.

To use as part of a larger script, simply call variables x and y for the resulting co-ords.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: [Done] on_chunk_generated - position

Post by darkfrei »

From https://mods.factorio.com/mod/SmogVisualPollution

Code: Select all

for chunk in surface.get_chunks() do
      max_id = max_id + 1
      local left_top = {x = chunk.x*32, y = chunk.y*32}
      local right_bottom = {x=left_top.x+32, y=left_top.y+32}
      local area = { left_top=left_top, right_bottom=right_bottom}
    end
Sorry, wrong code

Post Reply

Return to “Modding help”