top
factorio
warehouse supply

Circle


This will write every coordinate, with top-left at 0, 0, that falls inside of a circle with radius 'r', to a text file "in_circle.txt"
                
local r = 10

local d = r * 2
local rSq = r ^ 2
local count = 0
local inside = {}
local area = {top_left={x=0, y=0}, bottom_right={x=0+d, y=0+d}, center={x=0+r, y=0+r}}
local results = {}
for i = area.top_left.x, area.bottom_right.x, 1 do
    for j = area.top_left.y, area.bottom_right.y, 1 do
        -- This ( X^2 + Y^2 ) is used to calculate if something
        -- is inside a circle area. (A^2+B^2=C^2)
        local distVar = math.floor((area.center.x - i) ^ 2 + (area.center.y - j) ^ 2)
        if (distVar < rSq) then
            count = count + 1
            table.insert(inside, {index=count, position = {x=i, y=j}})
        end 
    end
end


file = io.open("in_circle.txt", "w")
io.output(file)
io.write("radius " .. r)
for index, node in ipairs(inside) do
    io.write("\n[" .. index .. "]\tx" .. node.position.x .. " y" .. node.position.y)
end
                
            

and for Factorio, you can feed it something to contruct on every x, y instead of writing to a file.
This will create a filled concrete circle with radius 'r', and also create a wall border to visualize the circumfrence
                
/sc
  local results = {}
  local r = 32
  local r_sq = r ^ 2
  local wall_space = r*math.pi
  local center = game.player.position
  local area = {top_left={x=center.x-r, y=center.y-r}, bottom_right={x=center.x+r, y=center.y+r}}

  game.player.print("\nr = " .. r .. "\nr_sq = " .. r_sq .. "\ncenter = x" .. center.x .. ", y" .. center.y .. "\narea = x" .. area.top_left.x .. ", y" .. area.top_left.y .. " to x" .. area.bottom_right.x .. ", y" .. area.bottom_right.y)

  for i = area.top_left.x, area.bottom_right.x, 1 do
    for j = area.top_left.y, area.bottom_right.y, 1 do
  
        local dist = math.floor((center.x - i) ^ 2 + (center.y - j) ^ 2)
  
        if (dist < r_sq) then
            table.insert(results, {name = "concrete", position ={i,j}})

            if ((dist < r_sq) and
            (dist > r_sq-wall_space)) then
                game.player.surface.create_entity({name="stone-wall", amount=1, position={i, j}})
            end
        end
    end
end
  
game.player.surface.set_tiles(results)
game.player.force.chart(game.player.surface, area)
                
            

this will dump all entities that belong to player 1s force and character-placed tiles (besides landfill) in a 20,000×20,000 square area centered on spawn (0, 0) to file 'layout.lua' in script-output
as WARNING: this checks 400 million tiles, and may lag the game while running. You should edit the area to a size that can be handled.
            
            /sc
        local player = game.players[1]
        local surface = game.players[1].surface
        game.write_file('layout.lua', '', false)

        local area = {
            left_top = {x = -10000, y = -10000},
            right_bottom = {x = 10000, y = 10000}
        }

        local entities = surface.find_entities_filtered {area = area, force = player.force}
        local tiles = surface.find_tiles_filtered {area = area, name = {"stone-path", "concrete", "refined-concrete", "hazard-concrete-left", "hazard-concrete-right"}}

        for _, e in pairs(entities) do
            local str = '{position = {x = ' .. e.position.x
            str = str .. ', y = '
            str = str .. e.position.y
            str = str .. '}, name = "'
            str = str .. e.name
            str = str .. '", direction = '
            str = str .. tostring(e.direction)
            str = str .. ', force = "'
            str = str .. e.force.name
            str = str .. '"},'
            if e.name ~= 'character' then
                game.write_file('layout.lua', str .. '\n', true)
            end
        end

        game.write_file('layout.lua', '\n', true)
        game.write_file('layout.lua', '\n', true)
        game.write_file('layout.lua', 'Tiles: \n', true)

        for _, t in pairs(tiles) do
            local str = '{position = {x = ' .. t.position.x
            str = str .. ', y = '
            str = str .. t.position.y
            str = str .. '}, name = "'
            str = str .. t.name
            str = str .. '"},'
            game.write_file('layout.lua', str .. '\n', true)
            player.print('Dumped layout as file: layout.lua')
        end