[maze] Update

This commit is contained in:
LeMagnesium 2016-08-03 23:42:09 +02:00
parent 7e8d318596
commit 1bbec248e2
No known key found for this signature in database
GPG Key ID: A54DDB5272C51E8B
1 changed files with 462 additions and 373 deletions

View File

@ -1,11 +1,6 @@
minetest.register_chatcommand("maze", { local function spawn_maze(name, param)
params = "<size_x> <size_y> <#floors> <material_floor> <material_wall> <material_ceiling>",
privs = {server = true},
description = "Create a maze near your position",
func = function(name, param)
local t1 = os.clock() local t1 = os.clock()
math.randomseed(os.time()) math.randomseed(os.time())
local player_pos = minetest.get_player_by_name(name):getpos()
local found, _, maze_size_x_st, maze_size_y_st, maze_size_l_st, material_floor, material_wall, material_ceiling = param:find("(%d+)%s+(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)%s+([^%s]+)") local found, _, maze_size_x_st, maze_size_y_st, maze_size_l_st, material_floor, material_wall, material_ceiling = param:find("(%d+)%s+(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)%s+([^%s]+)")
local min_size = 11 local min_size = 11
local maze_size_x = tonumber(maze_size_x_st) local maze_size_x = tonumber(maze_size_x_st)
@ -200,14 +195,22 @@ minetest.register_chatcommand("maze", {
pos_l = pos.l pos_l = pos.l
-- print("get back to " .. pos_x .. " / " .. pos_y .. " / " .. pos_l .. " to find another way from there") -- print("get back to " .. pos_x .. " / " .. pos_y .. " / " .. pos_l .. " to find another way from there")
end end
until pos_x == start_x and pos_y == start_y until pos_x == start_x
and pos_y == start_y
-- create partial braid maze, about 20% -- create partial braid maze, about 20%
for _, braid_pos in pairs(dead_end) do for _, braid_pos in pairs(dead_end) do
if braid_pos.x ~= treasure_x or braid_pos.y ~= treasure_y or braid_pos.l ~= treasure_l then -- treasure remains in dead end if braid_pos.x ~= treasure_x
or braid_pos.y ~= treasure_y
or braid_pos.l ~= treasure_l then -- treasure remains in dead end
-- print(braid_pos.x.."/"..braid_pos.y.."/"..braid_pos.l.." "..braid_pos.dx.."/"..braid_pos.dy) -- print(braid_pos.x.."/"..braid_pos.y.."/"..braid_pos.l.." "..braid_pos.dx.."/"..braid_pos.dy)
x = braid_pos.x + braid_pos.dx * 2 local x = braid_pos.x + braid_pos.dx * 2
y = braid_pos.y + braid_pos.dy * 2 local y = braid_pos.y + braid_pos.dy * 2
if math.random(5) == 1 and x > 0 and x < maze_size_x - 1 and y > 0 and y < maze_size_y - 1 and not maze[braid_pos.l][x][y] then if math.random(5) == 1
and x > 0
and x < maze_size_x - 1
and y > 0
and y < maze_size_y - 1
and not maze[braid_pos.l][x][y] then
-- remove wall if behind is corridor with 20% chance -- remove wall if behind is corridor with 20% chance
maze[braid_pos.l][braid_pos.x + braid_pos.dx][braid_pos.y + braid_pos.dy] = false maze[braid_pos.l][braid_pos.x + braid_pos.dx][braid_pos.y + braid_pos.dy] = false
print("removed "..braid_pos.l.."/"..braid_pos.x + braid_pos.dx.."/"..braid_pos.y + braid_pos.dy) print("removed "..braid_pos.l.."/"..braid_pos.x + braid_pos.dx.."/"..braid_pos.y + braid_pos.dy)
@ -225,50 +228,47 @@ minetest.register_chatcommand("maze", {
exit_x = exit_x - 1 exit_x = exit_x - 1
until exit_reachable until exit_reachable
local player = minetest.get_player_by_name(name)
-- get transform factors to place the maze in "look_dir" of player -- get transform factors to place the maze in "look_dir" of player
local player_dir = minetest.get_player_by_name(name):get_look_dir() local player_dir = player:get_look_dir()
local cosine = 1 local cosine = 1
local sine = 0 local sine = 0
if math.abs(player_dir.x) > math.abs(player_dir.z) then if math.abs(player_dir.x) > math.abs(player_dir.z) then
if player_dir.x > 0 then if player_dir.x <= 0 then
cosine = 1
sine = 0
else
cosine = -1 cosine = -1
sine = 0
end end
else else
if player_dir.z < 0 then
cosine = 0 cosine = 0
if player_dir.z < 0 then
sine = -1 sine = -1
else else
cosine = 0
sine = 1 sine = 1
end end
end end
-- print (cosine .. " " .. sine) -- print (cosine .. " " .. sine)
local playerpos = vector.round(player:getpos())
-- build maze in minetest-world -- build maze in minetest-world
local offset_x = 1 local min, max
local offset_y = 1 local tab = {}
local line = ""
local pos = {x = 0, y = 0, z = 0}
local change_level_down = false
local change_level_up = false
local ladder_direction = 2
for l = maze_size_l-1, 0, -1 do for l = maze_size_l-1, 0, -1 do
for y = 0, maze_size_y-1 do for y = 0, maze_size_y-1 do
if l == 0 and y == math.floor(maze_size_y / 2) then line = "<-" else line = " " end local line
for x = 0, maze_size_x - 1, 1 do if l == 0
and y == start_y then
line = "<-"
else
line = " "
end
for x = 0, maze_size_x - 1 do
local ladder_direction = 2
-- rotate the maze in players view-direction and move it to his position -- rotate the maze in players view-direction and move it to his position
pos.x = cosine * (x + 2) - sine * (y - math.floor(maze_size_y / 2)) + player_pos.x local change_level_down = false
pos.z = sine * (x + 2) + cosine * (y - math.floor(maze_size_y / 2)) + player_pos.z local change_level_up = false
pos.y = math.floor(player_pos.y + 0.5) - 1 - 3 * l
change_level_down = false
change_level_up = false
for i, v in ipairs(updowns) do for i, v in ipairs(updowns) do
if v.x == x and v.y == y and v.l == l then if v.x == x
and v.y == y then
if v.l == l then
change_level_down = true change_level_down = true
-- find direction for the ladders -- find direction for the ladders
ladder_direction = 2 ladder_direction = 2
@ -276,142 +276,219 @@ minetest.register_chatcommand("maze", {
if maze[l][x + 1][y] and maze[l + 1][x + 1][y] then ladder_direction = 2 end if maze[l][x + 1][y] and maze[l + 1][x + 1][y] then ladder_direction = 2 end
if maze[l][x][y - 1] and maze[l + 1][x][y - 1] then ladder_direction = 5 end if maze[l][x][y - 1] and maze[l + 1][x][y - 1] then ladder_direction = 5 end
if maze[l][x][y + 1] and maze[l + 1][x][y + 1] then ladder_direction = 4 end if maze[l][x][y + 1] and maze[l + 1][x][y + 1] then ladder_direction = 4 end
end elseif v.l == l - 1 then
if v.x == x and v.y == y and v.l == l - 1 then
change_level_up = true change_level_up = true
-- find direction for the ladders -- find direction for the ladders
if maze[l][x][y + 1] then
ladder_direction = 4
elseif maze[l][x][y - 1] then
ladder_direction = 5
elseif maze[l][x + 1][y] then
ladder_direction = 2 ladder_direction = 2
if maze[l][x - 1][y] then ladder_direction = 3 end elseif maze[l][x - 1][y] then
if maze[l][x + 1][y] then ladder_direction = 2 end ladder_direction = 3
if maze[l][x][y - 1] then ladder_direction = 5 end else
if maze[l][x][y + 1] then ladder_direction = 4 end ladder_direction = 2
end
end
end end
end end
-- rotate direction for the ladders -- rotate direction for the ladders
if cosine == -1 then
if ladder_direction == 2 then ladder_direction = 3
elseif ladder_direction == 3 then ladder_direction = 2
elseif ladder_direction == 4 then ladder_direction = 5
elseif ladder_direction == 5 then ladder_direction = 4 end
end
if sine == -1 then
if ladder_direction == 2 then ladder_direction = 5
elseif ladder_direction == 3 then ladder_direction = 4
elseif ladder_direction == 4 then ladder_direction = 2
elseif ladder_direction == 5 then ladder_direction = 3 end
end
if sine == 1 then if sine == 1 then
if ladder_direction == 2 then ladder_direction = 4 if ladder_direction == 2 then ladder_direction = 4
elseif ladder_direction == 3 then ladder_direction = 5 elseif ladder_direction == 3 then ladder_direction = 5
elseif ladder_direction == 4 then ladder_direction = 3 elseif ladder_direction == 4 then ladder_direction = 3
elseif ladder_direction == 5 then ladder_direction = 2 end elseif ladder_direction == 5 then ladder_direction = 2 end
elseif sine == -1 then
if ladder_direction == 2 then ladder_direction = 5
elseif ladder_direction == 3 then ladder_direction = 4
elseif ladder_direction == 4 then ladder_direction = 2
elseif ladder_direction == 5 then ladder_direction = 3 end
elseif cosine == -1 then
if ladder_direction == 2 then ladder_direction = 3
elseif ladder_direction == 3 then ladder_direction = 2
elseif ladder_direction == 4 then ladder_direction = 5
elseif ladder_direction == 5 then ladder_direction = 4 end
end end
local pos = vector.add(playerpos, {
x = cosine * (x + 2) - sine * (y - start_y),
z = sine * (x + 2) + cosine * (y - start_y),
y = - 1 - 3 * l
})
if not min then
min = vector.new(pos)
max = vector.new(pos)
end
min.x = math.min(min.x, pos.x)
min.y = math.min(min.y, pos.y)
min.z = math.min(min.z, pos.z)
max.x = math.max(max.x, pos.x)
max.z = math.max(max.z, pos.z)
if not change_level_down then if not change_level_down then
minetest.add_node(pos, {type = "node", name = material_floor}) table.insert(tab, {{pos.x, pos.y, pos.z}, 1})
end end
pos.y = pos.y + 1
local letter = " "
if maze[l][x][y] then if maze[l][x][y] then
line = "X" .. line --line = "X" .. line
letter = "██"
table.insert(tab, {{pos.x, pos.y, pos.z}, 2})
pos.y = pos.y + 1 pos.y = pos.y + 1
minetest.add_node(pos, {type = "node", name = material_wall}) table.insert(tab, {{pos.x, pos.y, pos.z}, 2})
else
-- if change_level_down then minetest.add_node(pos, {name = "default:ladder", param2 = ladder_direction}) end
if change_level_up then
table.insert(tab, {{pos.x, pos.y, pos.z}, 4, ladder_direction})
letter = "╞╡"
else
table.insert(tab, {{pos.x, pos.y, pos.z}, 0})
end
pos.y = pos.y + 1 pos.y = pos.y + 1
minetest.add_node(pos, {type = "node", name = material_wall}) if change_level_up then
table.insert(tab, {{pos.x, pos.y, pos.z}, 4, ladder_direction})
elseif change_level_down then
table.insert(tab, {{pos.x, pos.y, pos.z}, 5, ladder_direction})
letter = "☀▤"
elseif math.random(20) == 1 then
table.insert(tab, {{pos.x, pos.y, pos.z}, 5})
letter = ""
else
table.insert(tab, {{pos.x, pos.y, pos.z}, 0})
end
end
line = letter .. line
pos.y = pos.y + 1
if change_level_up then
table.insert(tab, {{pos.x, pos.y, pos.z}, 4, ladder_direction})
else
table.insert(tab, {{pos.x, pos.y, pos.z}, 3})
end
max.y = math.max(max.y, pos.y)
end
if l == exit_l
and y == exit_y then
line = "<-" .. line
else else
line = " " .. line line = " " .. line
pos.y = pos.y + 1
minetest.add_node(pos, {type = "node", name = "air"})
-- if change_level_down then minetest.add_node(pos, {type = "node", name = "default:ladder", param2 = ladder_direction}) end
if change_level_up then minetest.add_node(pos, {type = "node", name = "default:ladder", param2 = ladder_direction}) end
pos.y = pos.y + 1
minetest.add_node(pos, {type = "node", name = "air"})
if change_level_up then
minetest.add_node(pos, {type = "node", name = "default:ladder", param2 = ladder_direction})
else
if change_level_down then
minetest.add_node(pos, {type = "node", name = "default:torch", param2 = ladder_direction})
elseif (math.random(20) == 1) then
minetest.add_node(pos, {type = "node", name = "default:torch", param2 = 6})
end end
end
end
pos.y = pos.y + 1
if change_level_up then
minetest.add_node(pos, {type = "node", name = "air"})
minetest.add_node(pos, {type = "node", name = "default:ladder", param2 = ladder_direction})
else
minetest.add_node(pos, {type = "node", name = material_ceiling})
end
end
if l==exit_l and y==exit_y then line = "<-" .. line else line = " " .. line end
print(line) print(line)
end end
end end
local c = {
[0] = minetest.get_content_id("air"),
minetest.get_content_id(material_floor),
minetest.get_content_id(material_wall),
minetest.get_content_id(material_ceiling),
minetest.get_content_id("default:ladder"),
minetest.get_content_id("default:torch"),
}
local manip = minetest.get_voxel_manip()
local emerged_pos1, emerged_pos2 = manip:read_from_map(min, max)
local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
local nodes = manip:get_data()
local param2s = manip:get_param2_data()
for _,p in pairs(tab) do
local p, typ, par = unpack(p)
p = area:index(unpack(p))
nodes[p] = c[typ]
if par then
param2s[p] = par
end
end
manip:set_data(nodes)
manip:set_param2_data(param2s)
manip:write_to_map()
manip:update_map()
-- if exit is underground, dig a hole to surface -- if exit is underground, dig a hole to surface
pos.x = cosine * (maze_size_x + 2) - sine * (exit_y - math.floor(maze_size_y / 2)) + player_pos.x local ladder_direction = 2
pos.z = sine * (maze_size_x + 2) + cosine * (exit_y - math.floor(maze_size_y / 2)) + player_pos.z
pos.y = math.floor(player_pos.y + 0.5) - 3 * exit_l
ladder_direction = 2
if cosine == -1 then ladder_direction = 3 end if cosine == -1 then ladder_direction = 3 end
if sine == -1 then ladder_direction = 5 end if sine == -1 then ladder_direction = 5 end
if sine == 1 then ladder_direction = 4 end if sine == 1 then ladder_direction = 4 end
local pos = vector.add(playerpos, {
x = cosine * (maze_size_x + 2) - sine * (exit_y - start_y),
z = sine * (maze_size_x + 2) + cosine * (exit_y - start_y),
y = - 3 * exit_l
})
local is_air = minetest.get_node_or_nil(pos) local is_air = minetest.get_node_or_nil(pos)
while is_air ~= nil while is_air
and is_air.name ~= "air" do and is_air.name ~= "air" do
minetest.add_node(pos, {name = "default:ladder", param2 = ladder_direction}) minetest.add_node(pos, {name = "default:ladder", param2 = ladder_direction})
pos.y = pos.y + 1 pos.y = pos.y + 1
is_air = minetest.get_node_or_nil(pos) is_air = minetest.get_node_or_nil(pos)
end end
-- place a chest as treasure -- place a chest as treasure
pos.x = cosine * (treasure_x + 2) - sine * (treasure_y - math.floor(maze_size_y / 2)) + player_pos.x
pos.z = sine * (treasure_x + 2) + cosine * (treasure_y - math.floor(maze_size_y / 2)) + player_pos.z
pos.y = math.floor(player_pos.y + 0.5) - 3 * treasure_l
local items = 0 local items = 0
for name, item in pairs(minetest.registered_items) do local item_list = {}
local nBegin, nEnd = string.find(name, "default:") for name in pairs(minetest.registered_items) do
if nBegin ~= nil then if string.find(name, "default:") then
items = items + 1 items = items + 1
item_list[items] = name
end end
end end
minetest.add_node(pos, {name = "default:chest", inv = invcontent}) pos = vector.add(playerpos, {
x = cosine * (treasure_x + 2) - sine * (treasure_y - start_y),
z = sine * (treasure_x + 2) + cosine * (treasure_y - start_y),
y = - 3 * treasure_l
})
minetest.add_node(pos, {name = "default:chest"})
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
for name, item in pairs(minetest.registered_items) do for _,name in pairs(item_list) do
local nBegin, nEnd = string.find(name, "default:")
if nBegin ~= nil then
if math.random(items / 5) == 1 then if math.random(items / 5) == 1 then
inv:add_item('main', name .. " 1") inv:add_item('main', name)
end
end end
end end
-- place a closer-stone to seal the entrance and exit -- place a closer-stone to seal the entrance and exit
pos.x = cosine * (start_x + 2) - sine * (start_y - math.floor(maze_size_y / 2)) + player_pos.x minetest.add_node(vector.add(playerpos, {
pos.z = sine * (start_x + 2) + cosine * (start_y - math.floor(maze_size_y / 2)) + player_pos.z x = cosine * (start_x + 2),
pos.y = math.floor(player_pos.y + 0.5) - 3 * start_l - 1 z = sine * (start_x + 2),
minetest.add_node(pos, {type = "node", name = "maze:closer"}) y = - 3 * start_l - 1
pos.x = cosine * (maze_size_x + 1) - sine * (exit_y - math.floor(maze_size_y / 2)) + player_pos.x }), {name = "maze:closer"})
pos.z = sine * (maze_size_x + 1) + cosine * (exit_y - math.floor(maze_size_y / 2)) + player_pos.z minetest.add_node(vector.add(playerpos, {
pos.y = math.floor(player_pos.y + 0.5) - 3 * exit_l - 1 x = cosine * (maze_size_x + 1) - sine * (exit_y - start_y),
minetest.add_node(pos, {type = "node", name = "maze:closer"}) z = sine * (maze_size_x + 1) + cosine * (exit_y - start_y),
y = - 3 * exit_l - 1
}), {name = "maze:closer"})
print(string.format("[maze] done after ca. %.2fs", os.clock() - t1)) print(string.format("[maze] done after ca. %.2fs", os.clock() - t1))
end, end
minetest.register_chatcommand("maze", {
params = "<size_x> <size_y> <#floors> <material_floor> <material_wall> <material_ceiling>",
privs = {server = true},
description = "Create a maze near your position",
func = spawn_maze
}) })
local maze_closer = {} -- list of all closer stones local maze_closer = {} -- list of all closer stones
local closer_available = false
-- closer stone definition -- closer stone definition
minetest.register_node("maze:closer", { minetest.register_node("maze:closer", {
tiles = {"default_cobble.png"},
inventory_image = minetest.inventorycube("default_cobble.png"),
drop = '',
material = { diggability = "not"},
description = "Closestone", description = "Closestone",
tiles = {"default_cobble.png"},
drop = "",
material = { diggability = "not"},
on_construct = function(pos)
maze_closer[#maze_closer+1] = pos
closer_available = true
end,
}) })
-- detect player walk over closer stone (abm isn't fast enough) -- detect player walk over closer stone (abm isn't fast enough)
minetest.register_globalstep(function(dtime) local function playerwalk()
local players = minetest.get_connected_players() for _,player in pairs(minetest.get_connected_players()) do
for _,pos in pairs(maze_closer) do
for _,player in pairs(players) do
local player_pos = player:getpos() local player_pos = player:getpos()
for _,pos in pairs(maze_closer) do
local dist = math.sqrt( (pos.x - player_pos.x)^2 + (pos.y - (player_pos.y - 0.5))^2 + (pos.z - player_pos.z)^2 ) local dist = math.sqrt( (pos.x - player_pos.x)^2 + (pos.y - (player_pos.y - 0.5))^2 + (pos.z - player_pos.z)^2 )
if dist<3 then -- 2.2 would be enough, just make sure if dist<3 then -- 2.2 would be enough, just make sure
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
@ -423,26 +500,38 @@ minetest.register_globalstep(function(dtime)
for i = 0,2 do for i = 0,2 do
minetest.add_node({x = pos.x, y = pos.y+i, z = pos.z},{name="default:cobble"}) minetest.add_node({x = pos.x, y = pos.y+i, z = pos.z},{name="default:cobble"})
end end
minetest.sound_play("default_chest_locked", {pos = pos})
end end
end end
end end
end end
end end
end) end
local function do_step()
local tstep
if closer_available then
tstep = 0
playerwalk()
else
tstep = 2
end
minetest.after(tstep, do_step)
end
minetest.after(5, do_step)
-- create list of all closer stones (walk over detection now in globalstep, because abm isn't called often enough -- create list of all closer stones (walk over detection now in globalstep, because abm isn't called often enough
minetest.register_abm( minetest.register_abm({
{nodenames = {"maze:closer"}, nodenames = {"maze:closer"},
interval = 1, interval = 5,
chance = 1, chance = 1,
action = function(pos) action = function(pos)
for _,closer_pos in pairs(maze_closer) do for _,closer_pos in pairs(maze_closer) do
if closer_pos.x == pos.x if vector.equals(pos, closer_pos) then
and closer_pos.y == pos.y
and closer_pos.z == pos.z then
return return
end end
end end
table.insert(maze_closer, pos) maze_closer[#maze_closer+1] = pos
closer_available = true
end, end,
}) })