mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-01-18 03:21:47 +02:00
Added maze
This commit is contained in:
parent
f582cb862c
commit
c2c443e2ce
@ -1,11 +1,10 @@
|
||||
local wall_thickness = 1
|
||||
local cell_size = 1 --must be an uneven number
|
||||
local cell_size = 3 --must be an uneven number
|
||||
|
||||
|
||||
local wall_delta = math.floor((cell_size-wall_thickness)/2)
|
||||
local chunk_size = 32
|
||||
|
||||
rects = {}
|
||||
pixels = {}
|
||||
cells = {}
|
||||
function add_tile(x, y, width, height, add_cell)
|
||||
@ -21,26 +20,34 @@ function add_tile(x, y, width, height, add_cell)
|
||||
end
|
||||
end
|
||||
|
||||
local max = 0
|
||||
function render()
|
||||
global.max = 0
|
||||
function render()
|
||||
for x,_ in pairs(pixels) do
|
||||
for y,_ in pairs(pixels[x]) do
|
||||
if y * 32 > max and y % 2 == 0 then
|
||||
max = y * 32
|
||||
if y * 32 > global.max and y % 2 == 0 then
|
||||
global.max = y * 32
|
||||
end
|
||||
rects[x*32 .. "/" .. y*32] = 1
|
||||
global.rects[x*32 .. "/" .. y*32] = 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function shuffle(t)
|
||||
for i = 1, #t - 1 do
|
||||
local r = math.random(i, #t)
|
||||
t[i], t[r] = t[r], t[i]
|
||||
end
|
||||
global.rnd_p = 1
|
||||
function psd_rnd(l, h)
|
||||
while global.shuffle_pool[global.rnd_p] < l do
|
||||
global.rnd_p = global.rnd_p + 1
|
||||
end
|
||||
local res = global.shuffle_pool[global.rnd_p]
|
||||
global.rnd_p = global.rnd_p + 1
|
||||
return res
|
||||
end
|
||||
function shuffle(t)
|
||||
for i = 1, #t - 1 do
|
||||
local r = psd_rnd(i, #t)
|
||||
t[i], t[r] = t[r], t[i]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-- builds a width-by-height grid of trues
|
||||
function initialize_grid(w, h)
|
||||
local a = {}
|
||||
@ -52,29 +59,29 @@ function initialize_grid(w, h)
|
||||
end
|
||||
return a
|
||||
end
|
||||
|
||||
|
||||
-- average of a and b
|
||||
function avg(a, b)
|
||||
return (a + b) / 2
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
dirs = {
|
||||
{x = 0, y = -2}, -- north
|
||||
{x = 2, y = 0}, -- east
|
||||
{x = -2, y = 0}, -- west
|
||||
{x = 0, y = 2}, -- south
|
||||
}
|
||||
|
||||
|
||||
function make_maze(w, h)
|
||||
w = w or 16
|
||||
h = h or 16
|
||||
|
||||
|
||||
local map = initialize_grid(w*2+1, h*2+1)
|
||||
|
||||
|
||||
function walk(x, y)
|
||||
map[y][x] = false
|
||||
|
||||
|
||||
local d = { 1, 2, 3, 4 }
|
||||
shuffle(d)
|
||||
for i, dirnum in ipairs(d) do
|
||||
@ -86,9 +93,8 @@ function make_maze(w, h)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
walk(math.random(1, w)*2, math.random(1, h)*2)
|
||||
|
||||
walk(global.walk_seed_w, global.walk_seed_h)
|
||||
|
||||
local s = {}
|
||||
for i = 1, h*2+1 do
|
||||
for j = 1, w*2+1 do
|
||||
@ -99,7 +105,7 @@ function make_maze(w, h)
|
||||
end
|
||||
return table.concat(s)
|
||||
end
|
||||
|
||||
|
||||
local function reduce_walls()
|
||||
for x,_ in pairs(cells) do
|
||||
for y,_ in pairs(cells[x]) do
|
||||
@ -119,22 +125,41 @@ local function reduce_walls()
|
||||
end
|
||||
end
|
||||
function init()
|
||||
game.print(game.tick)
|
||||
-- math.randomseed( 78579837297 )
|
||||
make_maze(40, 40)
|
||||
if not global.walk_seed_w then global.walk_seed_w = math.random(1, 50)*2 end
|
||||
if not global.rects then global.rects = {} end
|
||||
if not global.walk_seed_h then global.walk_seed_h = math.random(1, 50)*2 end
|
||||
if not global.shuffle_pool then
|
||||
global.shuffle_pool = {}
|
||||
for i=1,20000 do
|
||||
global.shuffle_pool[i] = math.random(1, 4)
|
||||
end
|
||||
end
|
||||
make_maze(50, 50)
|
||||
reduce_walls()
|
||||
render()
|
||||
game.print(max)
|
||||
end
|
||||
|
||||
local function removeChunk(event)
|
||||
local surface = event.surface
|
||||
local tiles = {}
|
||||
for x = event.area.left_top.x, event.area.right_bottom.x do
|
||||
for y = event.area.left_top.y, event.area.right_bottom.y do
|
||||
table.insert(tiles, {name = "out-of-map", position = {x,y}})
|
||||
end
|
||||
end
|
||||
surface.set_tiles(tiles)
|
||||
end
|
||||
|
||||
shape_module = true
|
||||
|
||||
first = true
|
||||
function gen(event)
|
||||
function run_shape_module(event)
|
||||
if first then
|
||||
first = false
|
||||
init()
|
||||
end
|
||||
local pos = event.area.left_top
|
||||
if (rects[pos.x + max/2 .. "/" .. pos.y + max/2] == nil) then
|
||||
local pos = event.area.left_top
|
||||
if math.abs(pos.x) > 10000 or math.abs(pos.y) > 10000 or (global.rects[pos.x + global.max/2 .. "/" .. pos.y + global.max/2] == nil) then
|
||||
removeChunk(event)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -10,6 +10,7 @@ require "locale.gen_shared.simplex_noise"
|
||||
--shapes--
|
||||
--require "locale.gen_shape.right"
|
||||
--require "locale.gen_shape.up"
|
||||
require "locale.gen_shape.maze"
|
||||
--require "locale.gen_shape.spiral"
|
||||
--require "locale.gen_shape.spiral_tri"
|
||||
--require "locale.gen_shape.spiral2"
|
||||
@ -26,17 +27,17 @@ require "locale.gen_shared.simplex_noise"
|
||||
local on_chunk_generated = function(event)
|
||||
if shape_module then
|
||||
if run_shape_module(event) then
|
||||
if terrain_module then
|
||||
if terrain_module then
|
||||
run_terrain_module(event)
|
||||
elseif ores_module then
|
||||
run_ores_module(event)
|
||||
elseif ores_module then
|
||||
run_ores_module(event)
|
||||
end
|
||||
end
|
||||
else
|
||||
if terrain_module then
|
||||
run_terrain_module(event)
|
||||
elseif ores_module then
|
||||
run_ores_module(event)
|
||||
if terrain_module then
|
||||
run_terrain_module(event)
|
||||
elseif ores_module then
|
||||
run_ores_module(event)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user