diff --git a/map_gen/shared/builders.lua b/map_gen/shared/builders.lua index 6e8e64f8..bf6f004a 100644 --- a/map_gen/shared/builders.lua +++ b/map_gen/shared/builders.lua @@ -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