mirror of
https://github.com/Refactorio/RedMew.git
synced 2024-12-14 10:13:13 +02:00
new builder functions
This commit is contained in:
parent
9a63f3a4f5
commit
40761191ee
@ -1,5 +1,4 @@
|
||||
-- helpers
|
||||
|
||||
tau = 2 * math.pi
|
||||
deg_to_rad = tau / 360
|
||||
function degrees(angle)
|
||||
@ -7,7 +6,6 @@ function degrees(angle)
|
||||
end
|
||||
|
||||
-- shape builders
|
||||
|
||||
function empty_builder(x, y)
|
||||
return false
|
||||
end
|
||||
@ -170,7 +168,6 @@ function picture_builder(pic)
|
||||
end
|
||||
|
||||
-- transforms and shape helpers
|
||||
|
||||
function translate(builder, x_offset, y_offset)
|
||||
return function(x, y, world_x, world_y, surface)
|
||||
return builder(x - x_offset, y - y_offset, world_x, world_y, surface)
|
||||
@ -312,6 +309,17 @@ function choose(condition, true_shape, false_shape)
|
||||
end
|
||||
end
|
||||
|
||||
function shape_or_else(shape, else_shape)
|
||||
return function(local_x, local_y, world_x, world_y, surface)
|
||||
local tile, entity = shape(local_x, local_y, world_x, world_y, surface)
|
||||
if (tile) then
|
||||
return tile, entity
|
||||
else
|
||||
return else_shape(local_x, local_y, world_x, world_y, surface)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function linear_grow(shape, size)
|
||||
local half_size = size / 2
|
||||
return function(local_x, local_y, world_x, world_y, surface)
|
||||
@ -431,7 +439,6 @@ function project_overlap(shape, size, r)
|
||||
end
|
||||
|
||||
-- ore generation.
|
||||
|
||||
-- builder is the shape of the ore patch.
|
||||
function resource_module_builder(builder, resource_type, amount_function)
|
||||
amount_function = amount_function or function(a, b)
|
||||
@ -463,7 +470,6 @@ function builder_with_resource(land_builder, resource_module)
|
||||
end
|
||||
|
||||
-- pattern builders.
|
||||
|
||||
function single_pattern_builder(shape, width, height)
|
||||
shape = shape or empty_builder
|
||||
local half_width = width / 2
|
||||
@ -545,6 +551,49 @@ function grid_pattern_builder(pattern, columns, rows, width, height)
|
||||
end
|
||||
end
|
||||
|
||||
function grid_pattern_overlap_builder(pattern, columns, rows, width, height)
|
||||
local half_width = width / 2
|
||||
local half_height = height / 2
|
||||
|
||||
return function(local_x, local_y, world_x, world_y, surface)
|
||||
local local_y2 = ((local_y + half_height) % height) - half_height
|
||||
local row_pos = math.floor(local_y / height + 0.5)
|
||||
local row_i = row_pos % rows + 1
|
||||
local row = pattern[row_i] or {}
|
||||
|
||||
local local_x2 = ((local_x + half_width) % width) - half_width
|
||||
local col_pos = math.floor(local_x / width + 0.5)
|
||||
local col_i = col_pos % columns + 1
|
||||
|
||||
local shape = row[col_i] or empty_builder
|
||||
|
||||
local tile, entity = shape(local_x2, local_y2, world_x, world_y, surface)
|
||||
if tile then return tile, entity end
|
||||
|
||||
local col_i_left = (col_pos - 1) % columns + 1
|
||||
shape = row[col_i_left] or empty_builder
|
||||
tile, entity = shape(local_x2 + width, local_y2, world_x, world_y, surface)
|
||||
if tile then return tile, entity end
|
||||
|
||||
local col_i_right = (col_pos + 1) % columns + 1
|
||||
shape = row[col_i_right] or empty_builder
|
||||
tile, entity = shape(local_x2 - width, local_y2, world_x, world_y, surface)
|
||||
if tile then return tile, entity end
|
||||
|
||||
local row_i_up = (row_pos - 1) % rows + 1
|
||||
local row_up = pattern[row_i_up] or {}
|
||||
shape = row_up[col_i]
|
||||
tile, entity = shape(local_x2, local_y2 + height, world_x, world_y, surface)
|
||||
if tile then return tile, entity end
|
||||
|
||||
local row_i_down = (row_pos + 1) % rows + 1
|
||||
local row_down = pattern[row_i_down] or {}
|
||||
shape = row_down[col_i]
|
||||
tile, entity = shape(local_x2, local_y2 - height, world_x, world_y, surface)
|
||||
if tile then return tile, entity end
|
||||
end
|
||||
end
|
||||
|
||||
function segment_pattern_builder(pattern)
|
||||
local count = #pattern
|
||||
|
||||
@ -557,7 +606,6 @@ function segment_pattern_builder(pattern)
|
||||
end
|
||||
|
||||
-- tile converters
|
||||
|
||||
function change_tile(builder, old_tile, new_tile)
|
||||
return function(local_x, local_y, world_x, world_y, surface)
|
||||
local tile, entity = builder(local_x, local_y, world_x, world_y, surface)
|
||||
@ -624,6 +672,16 @@ function spawn_fish(builder, spawn_rate)
|
||||
end
|
||||
end
|
||||
|
||||
function spawn_entity(builder, name)
|
||||
return function(local_x, local_y, world_x, world_y, surface)
|
||||
if builder(local_x, local_y, world_x, world_y, surface) then
|
||||
return {name = name, position = {world_x, world_y}}
|
||||
else
|
||||
return nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function apply_effect(builder, func)
|
||||
return function(local_x, local_y, world_x, world_y, surface)
|
||||
local tile, entity = builder(local_x, local_y, world_x, world_y, surface)
|
||||
|
Loading…
Reference in New Issue
Block a user