fun_caves/castles.lua
2016-07-19 12:00:20 -05:00

106 lines
3 KiB
Lua

local csize
fun_caves.ice_castle = function(minp, maxp, data, p2data, area, node, heightmap, biomemap, biome_ids, underzone)
csize = vector.add(vector.subtract(maxp, minp), 1)
local write = false
local write_p2 = false
local max_height = -33000
local min_height = 33000
local avg_height = 0
local avg_count = 0
local height_grid = {{}, {}, {}, {}, {}, {}, {}, {}, {}, {}}
for z = minp.z, maxp.z do
local dz = math.ceil((z - minp.z + 1) / 8)
for x = minp.x, maxp.x do
local dx = math.ceil((x - minp.x + 1) / 8)
local ivm = area:index(x, maxp.y, z)
for y = maxp.y, minp.y, -1 do
if data[ivm] ~= node['air'] then
if y > minp.y + 50 then
return
end
if y > max_height then
max_height = y
end
if y < min_height then
min_height = y
end
print(dx, dz)
if not height_grid[dx][dz] then
height_grid[dx][dz] = -33000
end
if y > height_grid[dx][dz] then
height_grid[dx][dz] = y
end
if x == minp.x + 8 or x == maxp.x - 8 or z == minp.z + 8 or z == maxp.z - 8 then
avg_height = avg_height + y
avg_count = avg_count + 1
end
break
end
ivm = ivm - area.ystride
end
end
end
avg_height = avg_height / avg_count
max_height = max_height
for z = 1, 10 do
for x = 1, 10 do
height_grid[x][z] = height_grid[x][z] + math.random(20) + 5
end
end
local index = 0
local index3d = 0
local pos = {x=0, y=0, z=0}
local math_floor = math.floor
local math_max = math.max
local math_min = math.min
local math_log = math.log
for z = minp.z + 8, maxp.z - 8 do
local dz = math.ceil((z - minp.z + 1) / 8)
local r1z = (z - minp.z) % 8
local r2z = r1z < 4 and r1z or 7 - r1z
for x = minp.x + 8, maxp.x - 8 do
local dx = math.ceil((x - minp.x + 1) / 8)
local r1x = (x - minp.x) % 8
local r2x = r1x < 4 and r1x or 7 - r1x
index = index + 1
index3d = (z - minp.z) * (csize.y + 2) * csize.x + (x - minp.x) + 1
local ivm = area:index(x, minp.y, z)
for y = minp.y, maxp.y do
if y > height_grid[dx][dz] + 2 * math.min(r2x, r2z) then
data[ivm] = node['air']
elseif r1x == 0 and (dx < 3 or height_grid[dx - 1][dz] < y) then
data[ivm] = node['default:ice']
elseif r1x == 7 and (dx > 8 or height_grid[dx + 1][dz] < y) then
data[ivm] = node['default:ice']
elseif r1z == 0 and (dz < 3 or height_grid[dx][dz - 1] < y) then
data[ivm] = node['default:ice']
elseif r1z == 7 and (dz > 8 or height_grid[dx][dz + 1] < y) then
data[ivm] = node['default:ice']
elseif y > height_grid[dx][dz] + 2 * math.min(r2x, r2z) - 2 then
data[ivm] = node['default:ice']
elseif y > min_height - 5 and (not ((dx < 5 or dx > 6) or (dz < 5 or dz > 6)) or ((y - minp.y) % 8 ~= 0 and y < height_grid[dx][dz] - 5)) then
data[ivm] = node['air']
elseif y > min_height - 5 and (y - minp.y) % 8 == 0 then
data[ivm] = node['default:ice']
elseif y > min_height - 10 then
data[ivm] = node['default:ice']
end
ivm = ivm + area.ystride
index3d = index3d + csize.x
end
end
end
return write, write_p2
end