1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-18 03:21:47 +02:00

changes to builder signatures

This commit is contained in:
grilledham 2018-05-08 17:47:34 +01:00
parent 094bfe8928
commit abc221427f
6 changed files with 257 additions and 239 deletions

View File

@ -5,16 +5,16 @@ map_gen_rows_per_tick = 8 -- Inclusive integer between 1 and 32. Used for map_ge
--require "map_gen.shared.generate_not_threaded" --require "map_gen.shared.generate_not_threaded"
require "map_gen.shared.generate" require "map_gen.shared.generate"
local function no_resources(x, y, world_x, world_y, tile, surface) local function no_resources(x, y, world, tile)
for _, e in ipairs(surface.find_entities_filtered({type = "resource", area = {{world_x, world_y}, {world_x + 1, world_y + 1}}})) do for _, e in ipairs(world.surface.find_entities_filtered({type = "resource", area = {{world.x, world.y}, {world.x + 1, world.y + 1}}})) do
e.destroy() e.destroy()
end end
return tile return tile
end end
local function less_resources(x, y, world_x, world_y, tile, surface) local function less_resources(x, y, world, tile)
for _, e in ipairs(surface.find_entities_filtered({type = "resource", area = {{world_x, world_y}, {world_x + 1, world_y + 1}}})) do for _, e in ipairs(world.surface.find_entities_filtered({type = "resource", area = {{world.x, world.y}, {world.x + 1, world.y + 1}}})) do
if e.name == "crude-oil" then if e.name == "crude-oil" then
-- e.amount = .995 * e.amount -- e.amount = .995 * e.amount
else else
@ -25,8 +25,8 @@ local function less_resources(x, y, world_x, world_y, tile, surface)
return tile return tile
end end
local function no_enemies(x, y, world_x, world_y, tile, surface) local function no_enemies(x, y, world, tile)
for _, e in ipairs(surface.find_entities_filtered({force = "enemy", position = {world_x, world_y}})) do for _, e in ipairs(world.surface.find_entities_filtered({force = "enemy", position = {world.x, world.y}})) do
e.destroy() e.destroy()
end end

View File

@ -8,8 +8,8 @@ local function value(base, mult)
end end
end end
local function no_resources(x, y, world_x, world_y, tile, surface) local function no_resources(x, y, world, tile)
for _, e in ipairs(surface.find_entities_filtered({ type = "resource", area = {{world_x, world_y }, {world_x + 1, world_y + 1 } } })) do for _, e in ipairs(world.surface.find_entities_filtered({ type = "resource", area = {{world.x, world.y }, {world.x + 1, world.y + 1 } } })) do
e.destroy() e.destroy()
end end

View File

@ -54,19 +54,19 @@ end
local init = false local init = false
local safe_distance = 480 local safe_distance = 480
local function effect(x, y, world_x, world_y, tile, surface) local function effect(x, y, world, tile)
if not init then if not init then
init = true init = true
game.forces["player"].chart(surface, {{-32, -32}, {31, 31}}) game.forces["player"].chart(world.surface, {{-32, -32}, {31, 31}})
end end
if world_x == 0 and world_y == 0 then if world.x == 0 and world.y == 0 then
for _, e in ipairs(surface.find_entities({{-5, -5}, {5, 5}})) do for _, e in ipairs(world.surface.find_entities({{-5, -5}, {5, 5}})) do
e.destroy() e.destroy()
end end
local e = surface.create_entity({name = "rocket-silo", position = {0, 0}, force = "player"}) local e = world.surface.create_entity({name = "rocket-silo", position = {0, 0}, force = "player"})
e.destructible = false e.destructible = false
e.minable = false e.minable = false
end end

View File

@ -196,74 +196,76 @@ function picture_builder(pic)
if y2 > 0 and y2 <= height and x2 > 0 and x2 <= width then if y2 > 0 and y2 <= height and x2 > 0 and x2 <= width then
return data[y2][x2] return data[y2][x2]
else
return false
end end
end end
end end
-- transforms and shape helpers -- transforms and shape helpers
function translate(builder, x_offset, y_offset) function translate(builder, x_offset, y_offset)
return function(x, y, world_x, world_y, surface) return function(x, y, world)
return builder(x - x_offset, y - y_offset, world_x, world_y, surface) return builder(x - x_offset, y - y_offset, world)
end end
end end
function scale(builder, x_scale, y_scale) function scale(builder, x_scale, y_scale)
x_scale = 1 / x_scale x_scale = 1 / x_scale
y_scale = 1 / y_scale y_scale = 1 / y_scale
return function(x, y, world_x, world_y, surface) return function(x, y, world)
return builder(x * x_scale, y * y_scale, world_x, world_y, surface) return builder(x * x_scale, y * y_scale, world)
end end
end end
function rotate(builder, angle) function rotate(builder, angle)
local qx = math.cos(angle) local qx = math.cos(angle)
local qy = math.sin(angle) local qy = math.sin(angle)
return function(x, y, world_x, world_y, surface) return function(x, y, world)
local rot_x = qx * x - qy * y local rot_x = qx * x - qy * y
local rot_y = qy * x + qx * y local rot_y = qy * x + qx * y
return builder(rot_x, rot_y, world_x, world_y, surface) return builder(rot_x, rot_y, world)
end end
end end
function flip_x(builder) function flip_x(builder)
return function(x, y, world_x, world_y, surface) return function(x, y, world)
return builder(-x, y, world_x, world_y, surface) return builder(-x, y, world)
end end
end end
function flip_y(builder) function flip_y(builder)
return function(x, y, world_x, world_y, surface) return function(x, y, world)
return builder(x, -y, world_x, world_y, surface) return builder(x, -y, world)
end end
end end
function flip_xy(builder) function flip_xy(builder)
return function(x, y, world_x, world_y, surface) return function(x, y, world)
return builder(-x, -y, world_x, world_y, surface) return builder(-x, -y, world)
end end
end end
-- For resource_module_builder it will return the first success. -- For resource_module_builder it will return the first success.
function compound_or(builders) function compound_or(builders)
return function(x, y, world_x, world_y, surface) return function(x, y, world)
for _, v in ipairs(builders) do for _, v in ipairs(builders) do
local tile = v(x, y, world_x, world_y, surface) local tile = v(x, y, world)
if tile then if tile then
return tile return tile
end end
end end
return nil return false
end end
end end
-- Wont work correctly with resource_module_builder becasues I don't know which one to return. -- Wont work correctly with resource_module_builder becasues I don't know which one to return.
function compound_and(builders) function compound_and(builders)
return function(x, y, world_x, world_y, surface) return function(x, y, world)
local tile local tile
for _, v in ipairs(builders) do for _, v in ipairs(builders) do
tile = v(x, y, world_x, world_y, surface) tile = v(x, y, world)
if not tile then if not tile then
return nil return false
end end
end end
return tile return tile
@ -271,110 +273,109 @@ function compound_and(builders)
end end
function invert(builder) function invert(builder)
return function(x, y, world_x, world_y, surface) return function(x, y, world)
local tile = builder(x, y, world_x, world_y, surface) local tile = builder(x, y, world)
return not tile return not tile
end end
end end
function throttle_x(builder, x_in, x_size) function throttle_x(builder, x_in, x_size)
return function(x, y, world_x, world_y, surface) return function(x, y, world)
if x % x_size < x_in then if x % x_size < x_in then
return builder(x, y, world_x, world_y, surface) return builder(x, y, world)
else else
return nil return false
end end
end end
end end
function throttle_y(builder, y_in, y_size) function throttle_y(builder, y_in, y_size)
return function(x, y, world_x, world_y, surface) return function(x, y, world)
if y % y_size < y_in then if y % y_size < y_in then
return builder(x, y, world_x, world_y, surface) return builder(x, y, world)
else else
return nil return false
end end
end end
end end
function throttle_xy(builder, x_in, x_size, y_in, y_size) function throttle_xy(builder, x_in, x_size, y_in, y_size)
return function(x, y, world_x, world_y, surface) return function(x, y, world)
if x % x_size < x_in and y % y_size < y_in then if x % x_size < x_in and y % y_size < y_in then
return builder(x, y, world_x, world_y, surface) return builder(x, y, world)
else else
return nil return false
end end
end end
end end
function throttle_xy(builder, x_in, x_size, y_in, y_size) function throttle_xy(builder, x_in, x_size, y_in, y_size)
return function(x, y, world_x, world_y, surface) return function(x, y, world)
if x % x_size < x_in and y % y_size < y_in then if x % x_size < x_in and y % y_size < y_in then
return builder(x, y, world_x, world_y, surface) return builder(x, y, world)
else else
return nil return false
end end
end end
end end
function throttle_world_xy(builder, x_in, x_size, y_in, y_size) function throttle_world_xy(builder, x_in, x_size, y_in, y_size)
return function(x, y, world_x, world_y, surface) return function(x, y, world)
if world_x % x_size < x_in and world_y % y_size < y_in then if world.x % x_size < x_in and world.y % y_size < y_in then
return builder(x, y, world_x, world_y, surface) return builder(x, y, world)
else else
return nil return false
end end
end end
end end
function choose(condition, true_shape, false_shape) function choose(condition, true_shape, false_shape)
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
if condition(local_x, local_y, world_x, world_y, surface) then if condition(x, y, world) then
return true_shape(local_x, local_y, world_x, world_y, surface) return true_shape(x, y, world)
else else
return false_shape(local_x, local_y, world_x, world_y, surface) return false_shape(x, y, world)
end end
end end
end end
function shape_or_else(shape, else_shape) function shape_or_else(shape, else_shape)
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
return shape(local_x, local_y, world_x, world_y, surface) or return shape(x, y, world) or else_shape(x, y, world)
else_shape(local_x, local_y, world_x, world_y, surface)
end end
end end
function linear_grow(shape, size) function linear_grow(shape, size)
local half_size = size / 2 local half_size = size / 2
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local t = math.ceil((local_y / size) + 0.5) local t = math.ceil((y / size) + 0.5)
local n = math.ceil((math.sqrt(8 * t + 1) - 1) / 2) local n = math.ceil((math.sqrt(8 * t + 1) - 1) / 2)
local t_upper = n * (n + 1) * 0.5 local t_upper = n * (n + 1) * 0.5
local t_lower = t_upper - n local t_lower = t_upper - n
local y = (local_y - size * (t_lower + n / 2 - 0.5)) / n local y = (y - size * (t_lower + n / 2 - 0.5)) / n
local x = local_x / n local x = x / n
return shape(x, y, world_x, world_y, surface) return shape(x, y, world)
end end
end end
function grow(in_shape, out_shape, size, offset) function grow(in_shape, out_shape, size, offset)
local half_size = size / 2 local half_size = size / 2
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local tx = math.ceil(math.abs(local_x) / half_size) local tx = math.ceil(math.abs(x) / half_size)
local ty = math.ceil(math.abs(local_y) / half_size) local ty = math.ceil(math.abs(y) / half_size)
local t = math.max(tx, ty) local t = math.max(tx, ty)
for i = t, 2.5 * t, 1 do for i = t, 2.5 * t, 1 do
local out_t = 1 / (i - offset) local out_t = 1 / (i - offset)
local in_t = 1 / i local in_t = 1 / i
if out_shape(out_t * local_x, out_t * local_y, world_x, world_y, surface) then if out_shape(out_t * x, out_t * y, world) then
return nil return nil
end end
local tile = in_shape(in_t * local_x, in_t * local_y, world_x, world_y, surface) local tile = in_shape(in_t * x, in_t * y, world)
if tile then if tile then
return tile return tile
end end
@ -389,9 +390,9 @@ function project(shape, size, r)
local r2 = 1 / (r - 1) local r2 = 1 / (r - 1)
local a = 1 / size local a = 1 / size
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local offset = 0.5 * size local offset = 0.5 * size
local sn = math.ceil(local_y + offset) local sn = math.ceil(y + offset)
local n = math.ceil(math.log((r - 1) * sn * a + 1) / ln_r - 1) local n = math.ceil(math.log((r - 1) * sn * a + 1) / ln_r - 1)
local rn = r ^ n local rn = r ^ n
@ -399,10 +400,10 @@ function project(shape, size, r)
local c = size * rn local c = size * rn
local sn_upper = size * (r ^ (n + 1) - 1) * r2 local sn_upper = size * (r ^ (n + 1) - 1) * r2
local x = local_x * rn2 local x = x * rn2
local y = (local_y - (sn_upper - 0.5 * c) + offset) * rn2 local y = (y - (sn_upper - 0.5 * c) + offset) * rn2
return shape(x, y, world_x, world_y, surface) return shape(x, y, world)
end end
end end
@ -412,8 +413,8 @@ function project_overlap(shape, size, r)
local a = 1 / size local a = 1 / size
local offset = 0.5 * size local offset = 0.5 * size
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local sn = math.ceil(local_y + offset) local sn = math.ceil(y + offset)
local n = math.ceil(math.log((r - 1) * sn * a + 1) / ln_r - 1) local n = math.ceil(math.log((r - 1) * sn * a + 1) / ln_r - 1)
local rn = r ^ n local rn = r ^ n
@ -421,12 +422,12 @@ function project_overlap(shape, size, r)
local c = size * rn local c = size * rn
local sn_upper = size * (r ^ (n + 1) - 1) * r2 local sn_upper = size * (r ^ (n + 1) - 1) * r2
local x = local_x * rn2 local x = x * rn2
local y = (local_y - (sn_upper - 0.5 * c) + offset) * rn2 local y = (y - (sn_upper - 0.5 * c) + offset) * rn2
local tile local tile
tile = shape(x, y, world_x, world_y, surface) tile = shape(x, y, world)
if tile then if tile then
return tile return tile
end end
@ -437,10 +438,10 @@ function project_overlap(shape, size, r)
local c_above = size * rn_above local c_above = size * rn_above
local sn_upper_above = sn_upper - c local sn_upper_above = sn_upper - c
local x_above = local_x * rn2_above local x_above = x * rn2_above
local y_above = (local_y - (sn_upper_above - 0.5 * c_above) + offset) * rn2_above local y_above = (y - (sn_upper_above - 0.5 * c_above) + offset) * rn2_above
tile = shape(x_above, y_above, world_x, world_y, surface) tile = shape(x_above, y_above, world)
if tile then if tile then
return tile return tile
end end
@ -451,10 +452,10 @@ function project_overlap(shape, size, r)
local c_below = size * rn_below local c_below = size * rn_below
local sn_upper_below = sn_upper + c_below local sn_upper_below = sn_upper + c_below
local x_below = local_x * rn2_below local x_below = x * rn2_below
local y_below = (local_y - (sn_upper_below - 0.5 * c_below) + offset) * rn2_below local y_below = (y - (sn_upper_below - 0.5 * c_below) + offset) * rn2_below
return shape(x_below, y_below, world_x, world_y, surface) return shape(x_below, y_below, world)
end end
end end
@ -464,34 +465,38 @@ function resource_module_builder(builder, resource_type, amount_function)
amount_function = amount_function or function(a, b) amount_function = amount_function or function(a, b)
return 404 return 404
end end
return function(x, y, world_x, world_y, surface) return function(x, y, world)
if builder(x, y, world_x, world_y, surface) then if builder(x, y, world) then
return { return {
name = resource_type, name = resource_type,
position = {world_x, world_y}, position = {world.x, world.y},
amount = amount_function(world_x, world_y) amount = amount_function(world.x, world.y)
} }
else
return nil
end end
end end
end end
function builder_with_resource(land_builder, resource_module) function builder_with_resource(land_builder, resource_module)
return function(x, y, world_x, world_y, surface) return function(x, y, world)
local tile = land_builder(x, y, world_x, world_y, surface) local tile = land_builder(x, y, world)
if tile then if tile then
local entity = resource_module(x, y, world_x, world_y, surface) local entity = resource_module(x, y, world)
return { if entity then
if type(tile) == "table" then
add_entity(tile, entity)
else
tile = {
tile = tile, tile = tile,
entities = {entity} entities = {entity}
} }
else
return nil
end end
end end
end end
return tile
end
end
-- pattern builders. -- pattern builders.
function single_pattern_builder(shape, width, height) function single_pattern_builder(shape, width, height)
shape = shape or empty_builder shape = shape or empty_builder
@ -503,11 +508,11 @@ function single_pattern_builder(shape, width, height)
half_height = half_width half_height = half_width
end end
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local_y = ((local_y + half_height) % height) - half_height y = ((y + half_height) % height) - half_height
local_x = ((local_x + half_width) % width) - half_width x = ((x + half_width) % width) - half_width
return shape(local_x, local_y, world_x, world_y, surface) return shape(x, y, world)
end end
end end
@ -521,15 +526,13 @@ function single_pattern_overlap_builder(shape, width, height)
half_height = half_width half_height = half_width
end end
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local_y = ((local_y + half_height) % height) - half_height y = ((y + half_height) % height) - half_height
local_x = ((local_x + half_width) % width) - half_width x = ((x + half_width) % width) - half_width
return shape(local_x, local_y, world_x, world_y, surface) or return shape(x, y, world) or shape(x + width, y, world) or shape(x - width, y, world) or
shape(local_x + width, local_y, world_x, world_y, surface) or shape(x, y + height, world) or
shape(local_x - width, local_y, world_x, world_y, surface) or shape(x, y - height, world)
shape(local_x, local_y + height, world_x, world_y, surface) or
shape(local_x, local_y - height, world_x, world_y, surface)
end end
end end
@ -537,10 +540,10 @@ function single_x_pattern_builder(shape, width)
shape = shape or empty_builder shape = shape or empty_builder
local half_width = width / 2 local half_width = width / 2
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local_x = ((local_x + half_width) % width) - half_width x = ((x + half_width) % width) - half_width
return shape(local_x, local_y, world_x, world_y, surface) return shape(x, y, world)
end end
end end
@ -548,10 +551,10 @@ function single_y_pattern_builder(shape, height)
shape = shape or empty_builder shape = shape or empty_builder
local half_height = height / 2 local half_height = height / 2
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local_y = ((local_y + half_height) % height) - half_height y = ((y + half_height) % height) - half_height
return shape(local_x, local_y, world_x, world_y, surface) return shape(x, y, world)
end end
end end
@ -559,18 +562,18 @@ function grid_pattern_builder(pattern, columns, rows, width, height)
local half_width = width / 2 local half_width = width / 2
local half_height = height / 2 local half_height = height / 2
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local local_y2 = ((local_y + half_height) % height) - half_height local y2 = ((y + half_height) % height) - half_height
local row_pos = math.floor(local_y / height + 0.5) local row_pos = math.floor(y / height + 0.5)
local row_i = row_pos % rows + 1 local row_i = row_pos % rows + 1
local row = pattern[row_i] or {} local row = pattern[row_i] or {}
local local_x2 = ((local_x + half_width) % width) - half_width local x2 = ((x + half_width) % width) - half_width
local col_pos = math.floor(local_x / width + 0.5) local col_pos = math.floor(x / width + 0.5)
local col_i = col_pos % columns + 1 local col_i = col_pos % columns + 1
local shape = row[col_i] or empty_builder local shape = row[col_i] or empty_builder
return shape(local_x2, local_y2, world_x, world_y, surface) return shape(x2, y2, world)
end end
end end
@ -578,19 +581,19 @@ function grid_pattern_overlap_builder(pattern, columns, rows, width, height)
local half_width = width / 2 local half_width = width / 2
local half_height = height / 2 local half_height = height / 2
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local local_y2 = ((local_y + half_height) % height) - half_height local y2 = ((y + half_height) % height) - half_height
local row_pos = math.floor(local_y / height + 0.5) local row_pos = math.floor(y / height + 0.5)
local row_i = row_pos % rows + 1 local row_i = row_pos % rows + 1
local row = pattern[row_i] or {} local row = pattern[row_i] or {}
local local_x2 = ((local_x + half_width) % width) - half_width local x2 = ((x + half_width) % width) - half_width
local col_pos = math.floor(local_x / width + 0.5) local col_pos = math.floor(x / width + 0.5)
local col_i = col_pos % columns + 1 local col_i = col_pos % columns + 1
local shape = row[col_i] or empty_builder local shape = row[col_i] or empty_builder
local tile = shape(local_x2, local_y2, world_x, world_y, surface) local tile = shape(x2, y2, world)
if tile then if tile then
return tile return tile
end end
@ -598,14 +601,14 @@ function grid_pattern_overlap_builder(pattern, columns, rows, width, height)
-- edges -- edges
local col_i_left = (col_pos - 1) % columns + 1 local col_i_left = (col_pos - 1) % columns + 1
shape = row[col_i_left] or empty_builder shape = row[col_i_left] or empty_builder
tile = shape(local_x2 + width, local_y2, world_x, world_y, surface) tile = shape(x2 + width, y2, world)
if tile then if tile then
return tile return tile
end end
local col_i_right = (col_pos + 1) % columns + 1 local col_i_right = (col_pos + 1) % columns + 1
shape = row[col_i_right] or empty_builder shape = row[col_i_right] or empty_builder
tile = shape(local_x2 - width, local_y2, world_x, world_y, surface) tile = shape(x2 - width, y2, world)
if tile then if tile then
return tile return tile
end end
@ -613,7 +616,7 @@ function grid_pattern_overlap_builder(pattern, columns, rows, width, height)
local row_i_up = (row_pos - 1) % rows + 1 local row_i_up = (row_pos - 1) % rows + 1
local row_up = pattern[row_i_up] or {} local row_up = pattern[row_i_up] or {}
shape = row_up[col_i] or empty_builder shape = row_up[col_i] or empty_builder
tile = shape(local_x2, local_y2 + height, world_x, world_y, surface) tile = shape(x2, y2 + height, world)
if tile then if tile then
return tile return tile
end end
@ -621,7 +624,7 @@ function grid_pattern_overlap_builder(pattern, columns, rows, width, height)
local row_i_down = (row_pos + 1) % rows + 1 local row_i_down = (row_pos + 1) % rows + 1
local row_down = pattern[row_i_down] or {} local row_down = pattern[row_i_down] or {}
shape = row_down[col_i] or empty_builder shape = row_down[col_i] or empty_builder
return shape(local_x2, local_y2 - height, world_x, world_y, surface) return shape(x2, y2 - height, world)
end end
end end
@ -629,14 +632,14 @@ function grid_pattern_full_overlap_builder(pattern, columns, rows, width, height
local half_width = width / 2 local half_width = width / 2
local half_height = height / 2 local half_height = height / 2
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local local_y2 = ((local_y + half_height) % height) - half_height local y2 = ((y + half_height) % height) - half_height
local row_pos = math.floor(local_y / height + 0.5) local row_pos = math.floor(y / height + 0.5)
local row_i = row_pos % rows + 1 local row_i = row_pos % rows + 1
local row = pattern[row_i] or {} local row = pattern[row_i] or {}
local local_x2 = ((local_x + half_width) % width) - half_width local x2 = ((x + half_width) % width) - half_width
local col_pos = math.floor(local_x / width + 0.5) local col_pos = math.floor(x / width + 0.5)
local col_i = col_pos % columns + 1 local col_i = col_pos % columns + 1
local row_i_up = (row_pos - 1) % rows + 1 local row_i_up = (row_pos - 1) % rows + 1
@ -649,66 +652,66 @@ function grid_pattern_full_overlap_builder(pattern, columns, rows, width, height
-- start from top left, move left to right then down -- start from top left, move left to right then down
local shape = row_up[col_i_left] or empty_builder local shape = row_up[col_i_left] or empty_builder
local tile = shape(local_x2 + width, local_y2 + height, world_x, world_y, surface) local tile = shape(x2 + width, y2 + height, world)
if tile then if tile then
return tile return tile
end end
shape = row_up[col_i] or empty_builder shape = row_up[col_i] or empty_builder
tile = shape(local_x2, local_y2 + height, world_x, world_y, surface) tile = shape(x2, y2 + height, world)
if tile then if tile then
return tile return tile
end end
shape = row_up[col_i_right] or empty_builder shape = row_up[col_i_right] or empty_builder
tile = shape(local_x2 - width, local_y2 + height, world_x, world_y, surface) tile = shape(x2 - width, y2 + height, world)
if tile then if tile then
return tile return tile
end end
shape = row[col_i_left] or empty_builder shape = row[col_i_left] or empty_builder
tile = shape(local_x2 + width, local_y2, world_x, world_y, surface) tile = shape(x2 + width, y2, world)
if tile then if tile then
return tile return tile
end end
local shape = row[col_i] or empty_builder local shape = row[col_i] or empty_builder
tile = shape(local_x2, local_y2, world_x, world_y, surface) tile = shape(x2, y2, world)
if tile then if tile then
return tile return tile
end end
shape = row[col_i_right] or empty_builder shape = row[col_i_right] or empty_builder
tile = shape(local_x2 - width, local_y2, world_x, world_y, surface) tile = shape(x2 - width, y2, world)
if tile then if tile then
return tile return tile
end end
shape = row_down[col_i_left] or empty_builder shape = row_down[col_i_left] or empty_builder
tile = shape(local_x2 + width, local_y2 - height, world_x, world_y, surface) tile = shape(x2 + width, y2 - height, world)
if tile then if tile then
return tile return tile
end end
shape = row_down[col_i] or empty_builder shape = row_down[col_i] or empty_builder
tile = shape(local_x2, local_y2 - height, world_x, world_y, surface) tile = shape(x2, y2 - height, world)
if tile then if tile then
return tile return tile
end end
shape = row_down[col_i_right] or empty_builder shape = row_down[col_i_right] or empty_builder
return shape(local_x2 - width, local_y2 - height, world_x, world_y, surface) return shape(x2 - width, y2 - height, world)
end end
end end
function segment_pattern_builder(pattern) function segment_pattern_builder(pattern)
local count = #pattern local count = #pattern
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local angle = math.atan2(-local_y, local_x) local angle = math.atan2(-y, x)
local index = math.floor(angle / tau * count) % count + 1 local index = math.floor(angle / tau * count) % count + 1
local shape = pattern[index] or empty_builder local shape = pattern[index] or empty_builder
return shape(local_x, local_y, world_x, world_y, surface) return shape(x, y, world)
end end
end end
@ -716,18 +719,18 @@ function pyramid_builder(pattern, columns, rows, width, height)
local half_width = width / 2 local half_width = width / 2
local half_height = height / 2 local half_height = height / 2
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local local_y2 = ((local_y + half_height) % height) - half_height local y2 = ((y + half_height) % height) - half_height
local row_pos = math.floor(local_y / height + 0.5) local row_pos = math.floor(y / height + 0.5)
local row_i = row_pos % rows + 1 local row_i = row_pos % rows + 1
local row = pattern[row_i] or {} local row = pattern[row_i] or {}
if row_pos % 2 ~= 0 then if row_pos % 2 ~= 0 then
local_x = local_x - half_width x = x - half_width
end end
local local_x2 = ((local_x + half_width) % width) - half_width local x2 = ((x + half_width) % width) - half_width
local col_pos = math.floor(local_x / width + 0.5) local col_pos = math.floor(x / width + 0.5)
local col_i = col_pos % columns + 1 local col_i = col_pos % columns + 1
if col_pos > row_pos / 2 or -col_pos > (row_pos + 1) / 2 then if col_pos > row_pos / 2 or -col_pos > (row_pos + 1) / 2 then
@ -735,7 +738,7 @@ function pyramid_builder(pattern, columns, rows, width, height)
end end
local shape = row[col_i] or empty_builder local shape = row[col_i] or empty_builder
return shape(local_x2, local_y2, world_x, world_y, surface) return shape(x2, y2, world)
end end
end end
@ -743,27 +746,27 @@ function pyramid_inner_overlap_builder(pattern, columns, rows, width, height)
local half_width = width / 2 local half_width = width / 2
local half_height = height / 2 local half_height = height / 2
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local local_y2 = ((local_y + half_height) % height) - half_height local y2 = ((y + half_height) % height) - half_height
local row_pos = math.floor(local_y / height + 0.5) local row_pos = math.floor(y / height + 0.5)
local row_i = row_pos % rows + 1 local row_i = row_pos % rows + 1
local row = pattern[row_i] or {} local row = pattern[row_i] or {}
local x_odd local x_odd
local x_even local x_even
if row_pos % 2 == 0 then if row_pos % 2 == 0 then
x_even = local_x x_even = x
x_odd = local_x - half_width x_odd = x - half_width
else else
x_even = local_x - half_width x_even = x - half_width
x_odd = local_x x_odd = x
local_x = local_x - half_width x = x - half_width
end end
x_even = ((x_even + half_width) % width) - half_width x_even = ((x_even + half_width) % width) - half_width
x_odd = ((x_odd + half_width) % width) - half_width x_odd = ((x_odd + half_width) % width) - half_width
local col_pos = math.floor(local_x / width + 0.5) local col_pos = math.floor(x / width + 0.5)
local offset = 1 local offset = 1
local offset_odd = 0 local offset_odd = 0
@ -792,55 +795,55 @@ function pyramid_inner_overlap_builder(pattern, columns, rows, width, height)
-- start from top left, move left to right then down -- start from top left, move left to right then down
local shape = row_up[col_i_left] or empty_builder local shape = row_up[col_i_left] or empty_builder
local tile = shape(x_even + width, local_y2 + height, world_x, world_y, surface) local tile = shape(x_even + width, y2 + height, world)
if tile then if tile then
return tile return tile
end end
shape = row_up[col_i_odd] or empty_builder shape = row_up[col_i_odd] or empty_builder
tile = shape(x_odd, local_y2 + height, world_x, world_y, surface) tile = shape(x_odd, y2 + height, world)
if tile then if tile then
return tile return tile
end end
shape = row_up[col_i_right] or empty_builder shape = row_up[col_i_right] or empty_builder
tile = shape(x_even - width, local_y2 + height, world_x, world_y, surface) tile = shape(x_even - width, y2 + height, world)
if tile then if tile then
return tile return tile
end end
shape = row[col_i_left] or empty_builder shape = row[col_i_left] or empty_builder
tile = shape(x_even + width, local_y2, world_x, world_y, surface) tile = shape(x_even + width, y2, world)
if tile then if tile then
return tile return tile
end end
local shape = row[col_i] or empty_builder local shape = row[col_i] or empty_builder
tile = shape(x_even, local_y2, world_x, world_y, surface) tile = shape(x_even, y2, world)
if tile then if tile then
return tile return tile
end end
shape = row[col_i_right] or empty_builder shape = row[col_i_right] or empty_builder
tile = shape(x_even - width, local_y2, world_x, world_y, surface) tile = shape(x_even - width, y2, world)
if tile then if tile then
return tile return tile
end end
shape = row_down[col_i_left] or empty_builder shape = row_down[col_i_left] or empty_builder
tile = shape(x_even + width, local_y2 - height, world_x, world_y, surface) tile = shape(x_even + width, y2 - height, world)
if tile then if tile then
return tile return tile
end end
shape = row_down[col_i_odd] or empty_builder shape = row_down[col_i_odd] or empty_builder
tile = shape(x_odd, local_y2 - height, world_x, world_y, surface) tile = shape(x_odd, y2 - height, world)
if tile then if tile then
return tile return tile
end end
shape = row_down[col_i_right] or empty_builder shape = row_down[col_i_right] or empty_builder
return shape(x_even - width, local_y2 - height, world_x, world_y, surface) return shape(x_even - width, y2 - height, world)
end end
end end
@ -848,45 +851,46 @@ function grid_pattern_offset_builder(pattern, columns, rows, width, height)
local half_width = width / 2 local half_width = width / 2
local half_height = height / 2 local half_height = height / 2
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local local_y2 = ((local_y + half_height) % height) - half_height local y2 = ((y + half_height) % height) - half_height
local row_pos = math.floor(local_y / height + 0.5) local row_pos = math.floor(y / height + 0.5)
local row_i = row_pos % rows + 1 local row_i = row_pos % rows + 1
local row = pattern[row_i] or {} local row = pattern[row_i] or {}
local local_x2 = ((local_x + half_width) % width) - half_width local x2 = ((x + half_width) % width) - half_width
local col_pos = math.floor(local_x / width + 0.5) local col_pos = math.floor(x / width + 0.5)
local col_i = col_pos % columns + 1 local col_i = col_pos % columns + 1
local local_y2 = local_y2 + height * math.floor((row_pos + 1) / rows) local y2 = y2 + height * math.floor((row_pos + 1) / rows)
local local_x2 = local_x2 + width * math.floor((col_pos + 1) / columns) local x2 = x2 + width * math.floor((col_pos + 1) / columns)
local shape = row[col_i] or empty_builder local shape = row[col_i] or empty_builder
return shape(local_x2, local_y2, world_x, world_y, surface) return shape(x2, y2, world)
end end
end end
-- tile converters -- tile converters
function change_tile(builder, old_tile, new_tile) function change_tile(builder, old_tile, new_tile)
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local tile = builder(local_x, local_y, world_x, world_y, surface) local tile = builder(x, y, world)
if type(tile) == "table" then if type(tile) == "table" then
if tile.tile == old_tile then if tile.tile == old_tile then
tile.tile = new_tile tile.tile = new_tile
return tile
end end
else else
if tile == old_tile then if tile == old_tile then
return new_tile tile = new_tile
end end
end end
return tile
end end
end end
function change_collision_tile(builder, collides, new_tile) function change_collision_tile(builder, collides, new_tile)
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local tile = builder(local_x, local_y, world_x, world_y, surface) local tile = builder(x, y, world)
if type(tile) == "table" then if type(tile) == "table" then
if tile.tile.collides_with(collides) then if tile.tile.collides_with(collides) then
@ -898,41 +902,42 @@ function change_collision_tile(builder, collides, new_tile)
return new_tile return new_tile
end end
end end
return tile
end end
end end
-- only changes tiles made by the factorio map generator. -- only changes tiles made by the factorio map generator.
function change_map_gen_tile(builder, old_tile, new_tile) function change_map_gen_tile(builder, old_tile, new_tile)
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local tile = builder(local_x, local_y, world_x, world_y, surface) local function handle_tile(tile)
if type(tile) == "table" then
if type(tile.tile) == "boolean" and tile.tile then
local gen_tile = surface.get_tile(world_x, world_y).name
if old_tile == gen_tile then
tile.tile = new_tile
return tile
end
end
else
if type(tile) == "boolean" and tile then if type(tile) == "boolean" and tile then
local gen_tile = surface.get_tile(world_x, world_y).name local gen_tile = world.surface.get_tile(world.x, world.y).name
if old_tile == gen_tile then if gen_tile == old_tile then
return new_tile return new_tile
end end
end end
return tile
end end
local tile = builder(x, y, world)
if type(tile) == "table" then
tile.tile = handle_tile(tile.tile)
else
tile = handle_tile(tile)
end
return tile
end end
end end
-- only changes tiles made by the factorio map generator. -- only changes tiles made by the factorio map generator.
function change_map_gen_collision_tile(builder, collides, new_tile) function change_map_gen_collision_tile(builder, collides, new_tile)
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local tile = builder(local_x, local_y, world_x, world_y, surface)
local function handle_tile(tile) local function handle_tile(tile)
if type(tile) == "boolean" and tile then if type(tile) == "boolean" and tile then
local gen_tile = surface.get_tile(world_x, world_y) local gen_tile = world.surface.get_tile(world.x, world.y)
if gen_tile.collides_with(collides) then if gen_tile.collides_with(collides) then
return new_tile return new_tile
end end
@ -940,6 +945,8 @@ function change_map_gen_collision_tile(builder, collides, new_tile)
return tile return tile
end end
local tile = builder(x, y, world)
if type(tile) == "table" then if type(tile) == "table" then
tile.tile = handle_tile(tile.tile) tile.tile = handle_tile(tile.tile)
else else
@ -958,22 +965,20 @@ local water_tiles = {
} }
function spawn_fish(builder, spawn_rate) function spawn_fish(builder, spawn_rate)
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local function handle_tile(tile) local function handle_tile(tile)
if type(tile) == "string" then if type(tile) == "string" then
if water_tiles[tile] and spawn_rate >= math.random() then if water_tiles[tile] and spawn_rate >= math.random() then
return {name = "fish", position = {world_x, world_y}} return {name = "fish", position = {world.x, world.y}}
end end
elseif tile then elseif tile then
if surface.get_tile(world_x, world_y).collides_with("water-tile") and spawn_rate >= math.random() then if world.surface.get_tile(world.x, world.y).collides_with("water-tile") and spawn_rate >= math.random() then
return {name = "fish", position = {world_x, world_y}} return {name = "fish", position = {world.x, world.y}}
end
end end
end end
return nil local tile = builder(x, y, world)
end
local tile = builder(local_x, local_y, world_x, world_y, surface)
if type(tile) == "table" then if type(tile) == "table" then
local entity = handle_tile(tile.tile) local entity = handle_tile(tile.tile)
@ -994,20 +999,19 @@ function spawn_fish(builder, spawn_rate)
end end
end end
--todo change to entiy_module_builder
function spawn_entity(builder, name) function spawn_entity(builder, name)
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
if builder(local_x, local_y, world_x, world_y, surface) then if builder(x, y, world) then
return {name = name, position = {world_x, world_y}} return {name = name, position = {world.x, world.y}}
else
return nil
end end
end end
end end
function apply_effect(builder, func) function apply_effect(builder, func)
return function(local_x, local_y, world_x, world_y, surface) return function(x, y, world)
local tile = builder(local_x, local_y, world_x, world_y, surface) local tile = builder(x, y, world)
return func(local_x, local_y, world_x, world_y, tile, surface) return func(x, y, world, tile)
end end
end end

View File

@ -18,9 +18,13 @@ local function do_row(row, data)
local y = data.top_y + row local y = data.top_y + row
local top_x = data.top_x local top_x = data.top_x
data.y = y
for x = top_x, top_x + 31 do for x = top_x, top_x + 31 do
data.x = x
-- local coords need to be 'centered' to allow for correct rotation and scaling. -- local coords need to be 'centered' to allow for correct rotation and scaling.
local tile = MAP_GEN(x + 0.5, y + 0.5, x, y, data.surface) local tile = MAP_GEN(x + 0.5, y + 0.5, data)
if type(tile) == "table" then if type(tile) == "table" then
do_tile(tile.tile, x, y) do_tile(tile.tile, x, y)

View File

@ -2,33 +2,43 @@ require("map_gen.shared.builders")
require("utils.poisson_rng") require("utils.poisson_rng")
local function do_row(row, data) local function do_row(row, data)
local y = data.top_y + row local function do_tile(tile, x, y)
local top_x = data.top_x
for x = top_x, top_x + 31 do
-- local coords need to be 'centered' to allow for correct rotation and scaling.
local tile, entity = MAP_GEN(x + 0.5, y + 0.5, x, y, data.surface)
if not tile then if not tile then
table.insert(data.tiles, {name = "out-of-map", position = {x, y}}) table.insert(data.tiles, {name = "out-of-map", position = {x, y}})
elseif type(tile) == "string" then elseif type(tile) == "string" then
table.insert(data.tiles, {name = tile, position = {x, y}}) table.insert(data.tiles, {name = tile, position = {x, y}})
end end
if map_gen_decoratives then
tile_decoratives = check_decorative(tile, x, y)
for _, tbl in ipairs(tile_decoratives) do
table.insert(data.decoratives, tbl)
end end
tile_entities = check_entities(tile, x, y) local y = data.top_y + row
for _, entity in ipairs(tile_entities) do local top_x = data.top_x
data.y = y
for x = top_x, top_x + 31 do
data.x = x
-- local coords need to be 'centered' to allow for correct rotation and scaling.
local tile, entity = MAP_GEN(x + 0.5, y + 0.5, data)
if type(tile) == "table" then
do_tile(tile.tile, x, y)
local entities = tile.entities
if entities then
for _, entity in ipairs(entities) do
table.insert(data.entities, entity) table.insert(data.entities, entity)
end end
end end
if entity then local decoratives = tile.decoratives
table.insert(data.entities, entity) if decoratives then
for _, decorative in ipairs(decoratives) do
table.insert(data.decoratives, decorative)
end
end
else
do_tile(tile, x, y)
end end
end end
end end