@@ -296,7 +296,7 @@ function Builders.circular_spiral_grow_n_threads(in_thickness, total_thickness,
296296 end
297297end
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 >
359366function 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+ }
383396end
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 )
386407function 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