From 8817fd38d6cac2cb883c8cf2f0c6b11ed9b53aed Mon Sep 17 00:00:00 2001 From: Duane Date: Tue, 2 Aug 2016 22:23:59 -0500 Subject: [PATCH] Clean up borders and magic numbers. --- dungeon.lua | 167 +++++++++++++++++++++++++++++----------------------- mapgen.lua | 55 +++++++++-------- 2 files changed, 121 insertions(+), 101 deletions(-) diff --git a/dungeon.lua b/dungeon.lua index 932b590..4f05135 100644 --- a/dungeon.lua +++ b/dungeon.lua @@ -1,6 +1,9 @@ local max_depth = 31000 local cells = 10 -local cell_size = 60 / cells +local border = 10 +local cell_size = math.floor((80 - border * 2) / cells) +local cells_y = math.floor(80 / cell_size) +local dead_space = 80 - cells_y * cell_size local dungeon_depth = -1 -- close to y / 80 @@ -144,6 +147,7 @@ fun_caves.dungeon = function(minp_in, maxp_in, data, p2data, area, node, heightm return end + local center_off = cell_size * 4 + border if minp_in.y > -100 then local minp, maxp = minp_in, maxp_in local stair, avg, count = nil, 0, 0 @@ -156,7 +160,7 @@ fun_caves.dungeon = function(minp_in, maxp_in, data, p2data, area, node, heightm return end - if x > minp.x + 34 and x < minp.x + 45 and z > minp.z + 34 and z < minp.z + 45 and heightmap[index] >= minp.y and heightmap[index] < maxp.y - 2 then + if x > minp.x + center_off and x < maxp.x - center_off and z > minp.z + center_off and z < maxp.z - center_off and heightmap[index] >= minp.y and heightmap[index] < maxp.y - 2 then stair = true avg = avg + heightmap[index] count = count + 1 @@ -165,13 +169,12 @@ fun_caves.dungeon = function(minp_in, maxp_in, data, p2data, area, node, heightm end avg = math.ceil(avg / count) - if stair then - for z = -1, 12 do - for y = minp.y - 2, avg do - local ivm = area:index(minp.x + 34 - 1, y, z + minp.z + 34) - for x = -1, 12 do - if x == -1 or x == 12 or z == -1 or z == 12 then + for z = -1, (cell_size * 2) do + for x = -1, (cell_size * 2) do + local ivm = area:index(x + minp.x + center_off, minp.y - 8, z + minp.z + center_off) + for y = minp.y - 8, avg do + if x == -1 or x == (cell_size * 2) or z == -1 or z == (cell_size * 2) then data[ivm] = node['default:cobble'] elseif ((x == 2 or x == 9) and z > 1 and z < 10) or ((z == 2 or z == 9) and x > 1 and x < 10) then data[ivm] = node['default:cobble'] @@ -186,10 +189,12 @@ fun_caves.dungeon = function(minp_in, maxp_in, data, p2data, area, node, heightm else t = 44 - z end - t = math.floor(t / 44 * 10) + t = math.floor(t / 44 * 10 * 2 + 0.5) / 2 if x < 2 or x > 9 or z < 2 or z > 9 then - if math.floor((y - minp.y) % 10) == t then + if math.floor((y - minp.y) % 10 * 2 + 0.5) / 2 == t then + data[ivm] = node['stairs:slab_cobble'] + elseif math.floor((y - minp.y) % 10 * 2 + 0.5) / 2 == t - 0.5 then data[ivm] = node['default:cobble'] else data[ivm] = node['air'] @@ -197,7 +202,7 @@ fun_caves.dungeon = function(minp_in, maxp_in, data, p2data, area, node, heightm end end - ivm = ivm + 1 + ivm = ivm + area.ystride end end end @@ -211,34 +216,56 @@ fun_caves.dungeon = function(minp_in, maxp_in, data, p2data, area, node, heightm end local minp = table.copy(minp_in) - minp.x = minp.x + 10 - minp.z = minp.z + 10 + minp.x = minp.x + border + minp.z = minp.z + border local maxp = table.copy(maxp_in) - maxp.x = maxp.x - 9 - maxp.z = maxp.z - 9 + maxp.x = maxp.x - border + maxp.z = maxp.z - border + center_off = center_off - border local level = math.min(6, math.ceil(maxp.y / math.floor(max_depth / -6))) local inner_floor = node['fun_caves:dungeon_floor_1'] local outer_wall = node['fun_caves:dungeon_wall_2'] local inner_wall = node['fun_caves:dungeon_wall_1'] local treasure_count = 0 + local leave_alone = {} + leave_alone[node['default:cobble']] = true + leave_alone[node['stairs:stair_cobble']] = true + leave_alone[node['stairs:slab_cobble']] = true + leave_alone['stairway'] = true for z = minp.z - 1, maxp.z + 1 do - for y = minp.y, maxp.y - 2 do + for y = minp.y, maxp.y + 1 do local ivm = area:index(minp.x - 1, y, z) for x = minp.x - 1, maxp.x + 1 do - data[ivm] = node['fun_caves:dungeon_wall_1'] + local centered_out = (z >= minp.z + center_off - 1 and z <= maxp.z - center_off + 1 and x >= minp.x + center_off - 1 and x <= maxp.x - center_off + 1) + local centered_in = (z >= minp.z + center_off and z <= maxp.z - center_off and x >= minp.x + center_off and x <= maxp.x - center_off) + if y > maxp.y - dead_space then + if centered_out and (x == minp.x + center_off - 1 or x == maxp.x - center_off + 1 or z == minp.z + center_off - 1 or z == maxp.z - center_off + 1) then + data[ivm] = node['fun_caves:dungeon_wall_1'] + elseif not centered_out and y <= maxp.y then + data[ivm] = node['default:stone'] + elseif y <= maxp.y and not leave_alone[data[ivm]] then + data[ivm] = node['air'] + end + elseif x == minp.x - 1 or x == maxp.x + 1 or z == minp.z - 1 or z == maxp.z + 1 then + data[ivm] = node['fun_caves:dungeon_wall_1'] + elseif (y == minp.y or y == maxp.y - dead_space) and not centered_in then + data[ivm] = node['fun_caves:dungeon_floor_1'] + end ivm = ivm + 1 end end end local content = {} - for cx = 0, 9 do + for cx = 0, (cells - 1) do content[cx] = {} - for cy = 0, 12 do + for cy = 0, (cells_y - 1) do content[cx][cy] = {} - for cz = 0, 9 do - if cy == 12 and (cz == 4 or cz == 5) and (cx == 4 or cx == 5) then + for cz = 0, (cells - 1) do + if cy == (cells_y - 1) and (cz == 4 or cz == 5) and (cx == 4 or cx == 5) then + content[cx][cy][cz] = 'room' + elseif cy == 0 and (cz == 4 or cz == 5) and (cx == 4 or cx == 5) then content[cx][cy][cz] = 'room' else content[cx][cy][cz] = '4way' @@ -247,34 +274,34 @@ fun_caves.dungeon = function(minp_in, maxp_in, data, p2data, area, node, heightm end end - for cy = 0, 12 do - local x, z = math.random(1,8), math.random(0,9) - while cy == 12 and (z == 4 or z == 5) and (x == 4 or x == 5) do - x, z = math.random(1,8), math.random(0,9) + for cy = 0, (cells_y - 2) do + local x, z = math.random(1,8), math.random(0,(cells - 1)) + while cy == (cells_y - 1) and (z == 4 or z == 5) and (x == 4 or x == 5) do + x, z = math.random(1,8), math.random(0,(cells - 1)) end content[x][cy][z] = 'stair1' content[x - 1][cy][z] = 'room' - if cy < 12 then + if cy < (cells_y - 1) then content[x][cy + 1][z] = 'room' content[x + 1][cy + 1][z] = 'room' end - x, z = math.random(0,9), math.random(1,8) - while cy == 12 and (z == 4 or z == 5) and (x == 4 or x == 5) do - x, z = math.random(0,9), math.random(1,8) + x, z = math.random(0,(cells - 1)), math.random(1,8) + while cy == (cells_y - 1) and (z == 4 or z == 5) and (x == 4 or x == 5) do + x, z = math.random(0,(cells - 1)), math.random(1,8) end content[x][cy][z] = 'stair0' content[x][cy][z - 1] = 'room' - if cy < 12 then + if cy < (cells_y - 1) then content[x][cy + 1][z] = 'room' content[x][cy + 1][z + 1] = 'room' end end - for cx = 0, 9 do - for cy = 0, 12 do - for cz = 0, 9 do + for cx = 0, (cells - 1) do + for cy = 0, (cells_y - 1) do + for cz = 0, (cells - 1) do if content[cx][cy][cz] == '4way' and math.random(2) == 1 then content[cx][cy][cz] = 'room' end @@ -282,33 +309,30 @@ fun_caves.dungeon = function(minp_in, maxp_in, data, p2data, area, node, heightm end end - for cz = 0, 9 do - local oz = minp.z + cz * 6 - for cy = 0, 12 do - local oy = minp.y + cy * 6 - for cx = 0, 9 do - local ox = minp.x + cx * 6 + for cz = 0, (cells - 1) do + local oz = minp.z + cz * cell_size + for cy = 0, (cells_y - 1) do + local oy = minp.y + cy * cell_size + for cx = 0, (cells - 1) do + local ox = minp.x + cx * cell_size + local centered_in = ((cx == 4 or cx == 5) and (cz == 4 or cz == 5)) - if content[cx][cy][cz] == 'stair1' then - for rz = 0, 5 do + if content[cx][cy][cz] == 'stair0' then + for rz = 0, cell_size do for ry = 0, 9 do local ivm = area:index(ox, oy + ry, oz + rz) - local bx = 0 - if cy == 0 then - bx = -1 - end - for rx = bx, 6 do - if ry == rx + 1 and (rz == 2 or rz == 3) and (cy == 12 or rx < 6) then - data[ivm] = node['stairs:stair_stone'] - p2data[ivm] = 1 - elseif (ry >= rx + 1 and ry <= rx + 5) and (rz == 2 or rz == 3) then + for rx = 0, (cell_size - 1) do + if ry == rz + 1 and (rx == 2 or rx == 3) and (cy == (cells_y - 1) or rz < cell_size) then + data[ivm] = node['stairs:stair_cobble'] + p2data[ivm] = 0 + elseif (ry >= rz + 1 and ry <= rz + 5) and (rx == 2 or rx == 3) then data[ivm] = 'stairway' - elseif ry == rx and (rz == 2 or rz == 3) then + elseif ry == rz and (rx == 2 or rx == 3) then data[ivm] = node['fun_caves:dungeon_floor_1'] - elseif rx < 6 and ry == 0 then + elseif rz < cell_size and ry == 0 then data[ivm] = node['fun_caves:dungeon_floor_1'] - elseif ry < 6 and rx < 6 then + elseif ry < cell_size and rz < cell_size then data[ivm] = node['fun_caves:dungeon_wall_1'] end @@ -316,25 +340,22 @@ fun_caves.dungeon = function(minp_in, maxp_in, data, p2data, area, node, heightm end end end - elseif content[cx][cy][cz] == 'stair0' then - for rz = -1, 6 do + elseif content[cx][cy][cz] == 'stair1' then + for rz = 0, (cell_size - 1) do for ry = 0, 9 do - if cy ~= 0 and rz == -1 then - rz = 0 - end local ivm = area:index(ox, oy + ry, oz + rz) - for rx = 0, 5 do - if ry == rz + 1 and (rx == 2 or rx == 3) and (cy == 12 or rz < 6) then + for rx = 0, cell_size do + if ry == rx + 1 and (rz == 2 or rz == 3) and (cy == (cells_y - 1) or rx < cell_size) then data[ivm] = node['stairs:stair_cobble'] - p2data[ivm] = 0 - elseif (ry >= rz + 1 and ry <= rz + 5) and (rx == 2 or rx == 3) then + p2data[ivm] = 1 + elseif (ry >= rx + 1 and ry <= rx + 5) and (rz == 2 or rz == 3) then data[ivm] = 'stairway' - elseif ry == rz and (rx == 2 or rx == 3) then + elseif ry == rx and (rz == 2 or rz == 3) then data[ivm] = node['fun_caves:dungeon_floor_1'] - elseif rz < 6 and ry == 0 then + elseif rx < cell_size and ry == 0 then data[ivm] = node['fun_caves:dungeon_floor_1'] - elseif ry < 6 and rz < 6 then + elseif ry < cell_size and rx < cell_size then data[ivm] = node['fun_caves:dungeon_wall_1'] end @@ -343,23 +364,23 @@ fun_caves.dungeon = function(minp_in, maxp_in, data, p2data, area, node, heightm end end else - for rz = 0, 5 do - for ry = 0, 5 do + for rz = 0, (cell_size - 1) do + for ry = 0, (cell_size - 1) do local ivm = area:index(ox, oy + ry, oz + rz) - for rx = 0, 5 do - if data[ivm] ~= 'stairway' and data[ivm] ~= node['stairs:stair_cobble'] then - if cy > 0 and ry == 0 then + for rx = 0, (cell_size - 1) do + if not leave_alone[data[ivm]] then + if ry == 0 and (cy > 0 or not centered_in) then if content[cx][cy][cz] == 'room' then data[ivm] = node['fun_caves:dungeon_floor_1'] else data[ivm] = node['fun_caves:dungeon_floor_2'] end - elseif content[cx][cy][cz] == 'room' and (cy < 12 or cx < 4 or cx > 5 or cz < 4 or cz > 5) and ry == 5 then - data[ivm] = node['fun_caves:dungeon_floor_1'] + elseif ry == (cell_size - 1) and (cy < (cells_y - 1) or not centered_in) then + data[ivm] = node['fun_caves:dungeon_floor_2'] elseif content[cx][cy][cz] == 'room' then data[ivm] = node['air'] - elseif content[cx][cy][cz] == '4way' and ry <= 2 and (rz == 2 or rz == 3 or rx == 2 or rx == 3) and (ox + rx >= minp.x + 2 and ox + rx <= maxp.x - 3 and oz + rz >= minp.z + 2 and oz + rz <= maxp.z - 3) then + elseif content[cx][cy][cz] == '4way' and ry <= 2 and (rz == 2 or rz == 3 or rx == 2 or rx == 3) and (ox + rx >= minp.x + 2 and ox + rx <= maxp.x - 2 and oz + rz >= minp.z + 2 and oz + rz <= maxp.z - 2) then data[ivm] = node['air'] elseif ry > 0 and content[cx][cy][cz] == '4way' then data[ivm] = node['fun_caves:dungeon_wall_1'] diff --git a/mapgen.lua b/mapgen.lua index 1665e34..0927d06 100644 --- a/mapgen.lua +++ b/mapgen.lua @@ -429,14 +429,6 @@ local function generate(p_minp, p_maxp, seed) break end - if fun_caves.dungeon then - local full - write, write_p2, full = fun_caves.dungeon(minp, maxp, data, p2data, area, node, heightmap) - if write and full then - break - end - end - if fun_caves.cavegen and fun_caves.decogen then local h2, write_cave write_cave, h2 = fun_caves.cavegen(minp, maxp, data, area, node, heightmap, underzone, ground_nodes) @@ -453,31 +445,38 @@ local function generate(p_minp, p_maxp, seed) local write_deco write_deco, write_p2 = fun_caves.decogen(minp, maxp, data, p2data, area, node, heightmap, biomemap, biome_ids, underzone) write = write or write_deco + end - if fun_caves.treegen then - local write_tree = fun_caves.treegen(minp, maxp, data, p2data, area, node) - if write_tree then - write = true - break - end + if fun_caves.treegen then + local write_tree = fun_caves.treegen(minp, maxp, data, p2data, area, node) + if write_tree then + write = true + break end + end - if fun_caves.pyramid then - local write_pyr, write_p4 = fun_caves.pyramid(minp, maxp, data, p2data, area, biomemap, biome_ids, node, heightmap) - if write_pyr then - write = true - write_p2 = write_p2 or write_p4 - break - end + if fun_caves.dungeon then + write, write_p2 = fun_caves.dungeon(minp, maxp, data, p2data, area, node, heightmap) + if write then + break end + end - if fun_caves.use_villages and biomemap and fun_caves.village then - local biome = biome_ids[biomemap[40*80+40]] - local write_vill = fun_caves.village(minp, maxp, data, p2data, area, node, biome, heightmap) - if write_vill then - write = true - break - end + if fun_caves.pyramid then + local write_pyr, write_p4 = fun_caves.pyramid(minp, maxp, data, p2data, area, biomemap, biome_ids, node, heightmap) + if write_pyr then + write = true + write_p2 = write_p2 or write_p4 + break + end + end + + if fun_caves.use_villages and biomemap and fun_caves.village then + local biome = biome_ids[biomemap[40*80+40]] + local write_vill = fun_caves.village(minp, maxp, data, p2data, area, node, biome, heightmap) + if write_vill then + write = true + break end end end