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.
[Done] on_chunk_generated - position
[Done] on_chunk_generated - position
Last edited by TheSAguy on Sat Jun 22, 2019 1:56 am, edited 1 time in total.
Re: on_chunk_generated - position
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)
Re: on_chunk_generated - position
Thanks Mat1k.
Re: [Done] on_chunk_generated - position
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.
To use as part of a larger script, simply call variables x and y for the resulting co-ords.
Re: [Done] on_chunk_generated - position
From https://mods.factorio.com/mod/SmogVisualPollution
Sorry, wrong code
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