Skip to content

Commit 0f11c2c

Browse files
committed
Extend Builders pic behavior
1 parent eff48e4 commit 0f11c2c

2 files changed

Lines changed: 45 additions & 79 deletions

File tree

map_gen/maps/danger_ores/modules/picture_builder.lua

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,6 @@ local math = require 'utils.math'
44
local random = math.random
55
local binary_search = table.binary_search
66
local bnot = bit32.bnot
7-
local floor = math.floor
8-
9-
local function decompress(pic)
10-
local data = pic.data
11-
local width = pic.width
12-
local height = pic.height
13-
14-
local uncompressed = {}
15-
16-
for y = 1, height do
17-
local row = data[y]
18-
local u_row = {}
19-
uncompressed[y] = u_row
20-
local x = 1
21-
for index = 1, #row, 2 do
22-
local key = row[index]
23-
24-
local count = row[index + 1]
25-
for _ = 1, count do
26-
u_row[x] = key
27-
x = x + 1
28-
end
29-
end
30-
end
31-
32-
return {
33-
width = width,
34-
height = height,
35-
data = uncompressed,
36-
tile_map = pic.tile_map,
37-
func_map = pic.func_map
38-
}
39-
end
407

418
local function from_name(tbl, name)
429
for _, v in pairs(tbl) do
@@ -73,40 +40,6 @@ local function ore_builder(ore_data)
7340
end
7441
end
7542

76-
local function picture(pic)
77-
local data = pic.data
78-
local width = pic.width
79-
local height = pic.height
80-
local func_map = pic.func_map
81-
local tile_map = pic.tile_map
82-
83-
-- the plus one is because lua tables are one based.
84-
local half_width = floor(width / 2) + 1
85-
local half_height = floor(height / 2) + 1
86-
return function(x, y, world)
87-
x = floor(x)
88-
y = floor(y)
89-
local x2 = x + half_width
90-
local y2 = y + half_height
91-
92-
if y2 > 0 and y2 <= height and x2 > 0 and x2 <= width then
93-
local key = data[y2][x2]
94-
local callback = func_map[key]
95-
local entity = callback and callback(x, y, world)
96-
if entity then
97-
return {
98-
tile = tile_map[key],
99-
entities = { entity }
100-
}
101-
else
102-
return tile_map[key]
103-
end
104-
else
105-
return false
106-
end
107-
end
108-
end
109-
11043
---@field pic { height: number, width: number, data: table } - the preset data from a picture
11144
---@field ores table - the ore configuration to be used
11245
---@field func_map table[number, number] - maps values of the preset to a specific ore function
@@ -117,11 +50,11 @@ return function(config)
11750
local func_map = config.func_map
11851

11952
pic.func_map = {}
120-
pic.tile_map = config.tile_map or b.default_tile_map
53+
pic.tile_map = config.tile_map
12154

12255
for key, v in pairs(func_map) do
12356
pic.func_map[key] = ore_builder(from_name(ores, v))
12457
end
12558

126-
return picture(decompress(pic))
59+
return b.picture(b.decompress(pic))
12760
end

map_gen/shared/builders.lua

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ function Builders.circular_spiral_grow_n_threads(in_thickness, total_thickness,
296296
end
297297
end
298298

299-
local tile_map = {
299+
local default_tile_map = {
300300
[1] = false,
301301
[2] = true,
302302
[3] = 'concrete',
@@ -330,7 +330,7 @@ local tile_map = {
330330
[31] = 'stone-path',
331331
[32] = 'water-green',
332332
[33] = 'water',
333-
--[[
333+
-- + 2.0 tiles
334334
[34] = 'acid-refined-concrete',
335335
[35] = 'black-refined-concrete',
336336
[36] = 'blue-refined-concrete',
@@ -351,11 +351,18 @@ local tile_map = {
351351
[51] = 'water-shallow',
352352
[52] = 'water-wube',
353353
[53] = 'yellow-refined-concrete',
354-
]]
355354
}
356-
Builders.default_tile_map = tile_map
355+
Builders.default_tile_map = default_tile_map
357356

357+
--- Decompress the Run-length encoded picture data to a full table of Width*Height
358358
--- Docs: https://github.com/Refactorio/RedMew/wiki/Using-the-Builders#buildersdecompress
359+
---@param pic
360+
---@field data uint[][]
361+
---@field width uint
362+
---@field height uint
363+
---@field func_map? table
364+
---@field tile_map? table
365+
---@return table< width: uint, height: uint, data: uint[][], tile_map?: table, func_map?: table >
359366
function Builders.decompress(pic)
360367
local data = pic.data
361368
local width = pic.width
@@ -369,36 +376,62 @@ function Builders.decompress(pic)
369376
uncompressed[y] = u_row
370377
local x = 1
371378
for index = 1, #row, 2 do
372-
local pixel = tile_map[row[index]]
373-
local count = row[index + 1]
379+
local key = row[index]
374380

381+
local count = row[index + 1]
375382
for _ = 1, count do
376-
u_row[x] = pixel
383+
u_row[x] = key
377384
x = x + 1
378385
end
379386
end
380387
end
381388

382-
return {width = width, height = height, data = uncompressed}
389+
return {
390+
width = width,
391+
height = height,
392+
data = uncompressed,
393+
tile_map = pic.tile_map,
394+
func_map = pic.func_map
395+
}
383396
end
384397

398+
--- Generates a shape from the picture data, returning tiles and entities (if any) of the given pixel
385399
--- Docs: https://github.com/Refactorio/RedMew/wiki/Using-the-Builders#builderspicture
400+
---@param pic
401+
---@field data uint[][]
402+
---@field width uint
403+
---@field height uint
404+
---@field func_map? table
405+
---@field tile_map? table - if not provided, the default one will be used instead
406+
---@return function(x, y, world)
386407
function Builders.picture(pic)
387408
local data = pic.data
388409
local width = pic.width
389410
local height = pic.height
411+
local func_map = pic.func_map or {}
412+
local tile_map = pic.tile_map or default_tile_map
390413

391414
-- the plus one is because lua tables are one based.
392415
local half_width = floor(width / 2) + 1
393416
local half_height = floor(height / 2) + 1
394-
return function(x, y)
417+
return function(x, y, world)
395418
x = floor(x)
396419
y = floor(y)
397420
local x2 = x + half_width
398421
local y2 = y + half_height
399422

400423
if y2 > 0 and y2 <= height and x2 > 0 and x2 <= width then
401-
return data[y2][x2]
424+
local key = data[y2][x2]
425+
local callback = func_map[key]
426+
local entity = callback and callback(x, y, world)
427+
if entity then
428+
return {
429+
tile = tile_map[key],
430+
entities = { entity }
431+
}
432+
else
433+
return tile_map[key]
434+
end
402435
else
403436
return false
404437
end

0 commit comments

Comments
 (0)