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

added segment_weighted_pattern builder

This commit is contained in:
grilledham 2019-05-13 12:21:59 +01:00 committed by James Gillham
parent 4b75706c10
commit ae31250f24

View File

@ -22,6 +22,8 @@ local tau = math.tau
local loga = math.log
local shallow_copy = table.shallow_copy
local remove = table.remove
local binary_search = table.binary_search
local bnot = bit32.bnot
-- helpers
@ -1454,15 +1456,34 @@ end
--- Docs: https://github.com/Refactorio/RedMew/wiki/Using-the-Builders#builderssegment_pattern
function Builders.segment_pattern(pattern)
local count = #pattern
local count_by_tau = count / tau
return function(x, y, world)
local angle = atan2(-y, x)
local index = floor(angle / tau * count) % count + 1
local index = floor(angle * count_by_tau) % count + 1
local shape = pattern[index] or Builders.empty_shape
return shape(x, y, world)
end
end
function Builders.segment_weighted_pattern(pattern)
local weights = Builders.prepare_weighted_array(pattern)
local total = weights.total * 0.5
return function(x, y, world)
local angle = atan2(-y, x)
local i = (angle * inv_pi + 1) * total
local index = binary_search(weights, i)
if index < 0 then
index = bnot(index)
end
local shape = pattern[index].shape or Builders.empty_shape
return shape(x, y, world)
end
end
--- Docs: https://github.com/Refactorio/RedMew/wiki/Using-the-Builders#builderspyramid_pattern
function Builders.pyramid_pattern(pattern, columns, rows, width, height)
local half_width = width / 2