Refactor and clean-up map_maker (#306)

This commit is contained in:
ANAND 2019-03-17 07:45:29 +05:30 committed by rubenwardy
parent 721290085d
commit 7fa6da30bf

View file

@ -1,37 +1,50 @@
local storage = minetest.get_mod_storage()
local randint = math.random(100) local randint = math.random(100)
local defaults = {
mapname = "ctf_" .. randint,
mapauthor = nil,
maptitle = "Untitled Map " .. randint,
mapinitial = "",
barrier_r = 110,
barrier_rot = 0,
center = { x = 0, y = 0, z = 0, r = 115, h = 140 },
flags = {}
}
-- Reload mapmaker context from mod_storage if it exists -- Reload mapmaker context from mod_storage if it exists
local storage = minetest.get_mod_storage() local config = {
local mapname = storage:get_string("mapname") mapname = storage:get_string("mapname"),
local maptitle = storage:get_string("maptitle") maptitle = storage:get_string("maptitle"),
local mapauthor = storage:get_string("mapauthor") mapauthor = storage:get_string("mapauthor"),
local mapinitial = storage:get_string("mapinitial") mapinitial = storage:get_string("mapinitial"),
local center = storage:get_string("center") center = storage:get_string("center"),
local flag_positions = storage:get_string("flags") flags = storage:get_string("flags"),
local barrier_r = storage:get_int("barrier_r") barrier_r = storage:get_int("barrier_r"),
local center_barrier_rot = storage:get_int("center_barrier_rot") barrier_rot = storage:get_int("barrier_rot"),
barriers_placed = storage:get_int("barriers_placed") == 1
}
if mapname == "" then if config.mapname == "" then
mapname = "ctf_" .. randint config.mapname = defaults.mapname
end end
if mapauthor == "" then if config.mapauthor == "" then
mapauthor = nil config.mapauthor = defaults.mapauthor
end end
if maptitle == "" then if config.maptitle == "" then
maptitle = "Untitled Map " .. randint config.maptitle = defaults.maptitle
end end
if barrier_r == 0 then if config.barrier_r == 0 then
barrier_r = 110 config.barrier_r = defaults.barrier_r
end end
if center == "" then if config.center == "" then
center = { x = 0, y = 0, z = 0, r = 115, h = 140 } config.center = defaults.center
else else
center = minetest.parse_json(storage:get("center")) config.center = minetest.parse_json(storage:get_string("center"))
end end
if flag_positions == "" then if config.flags == "" then
flag_positions = {} config.flags = defaults.flags
else else
flag_positions = minetest.parse_json(storage:get("flags")) config.flags = minetest.parse_json(storage:get_string("flags"))
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -60,7 +73,7 @@ assert(minetest.get_modpath("worldedit") and
"worldedit and worldedit_commands are required!") "worldedit and worldedit_commands are required!")
local function check_step() local function check_step()
for _, pos in pairs(flag_positions) do for _, pos in pairs(config.flags) do
if minetest.get_node(pos).name ~= "ctf_map:flag" then if minetest.get_node(pos).name ~= "ctf_map:flag" then
minetest.set_node(pos, { name = "ctf_map:flag" }) minetest.set_node(pos, { name = "ctf_map:flag" })
end end
@ -91,18 +104,14 @@ minetest.register_node("ctf_map:flag", {
} }
}, },
groups = {oddly_breakable_by_hand=1,snappy=3}, groups = {oddly_breakable_by_hand=1,snappy=3},
on_place = function(_, _, pthing) after_place_node = function(pos)
local node = minetest.get_node(pthing.under).name table.insert(config.flags, vector.new(pos))
local ndef = minetest.registered_nodes[node] storage:set_string("flags", minetest.write_json(config.flags))
local pos = (ndef and ndef.buildable_to) and pthing.under or pthing.above
table.insert(flag_positions, vector.new(pos))
storage:set_string("flags", minetest.write_json(flag_positions))
end, end,
on_destruct = function(pos) on_destruct = function(pos)
for i, v in pairs(flag_positions) do for i, v in pairs(config.flags) do
if vector.equals(pos, v) then if vector.equals(pos, v) then
flag_positions[i] = nil config.flags[i] = nil
return return
end end
end end
@ -111,13 +120,13 @@ minetest.register_node("ctf_map:flag", {
local function to_2pos() local function to_2pos()
return { return {
x = center.x - center.r, x = config.center.x - config.center.r,
y = center.y - center.h / 2, y = config.center.y - config.center.h / 2,
z = center.z - center.r, z = config.center.z - config.center.r,
}, { }, {
x = center.x + center.r, x = config.center.x + config.center.r,
y = center.y + center.h / 2, y = config.center.y + config.center.h / 2,
z = center.z + center.r, z = config.center.z + config.center.r,
} }
end end
@ -145,24 +154,24 @@ local function we_import(name)
if pos1 and pos2 then if pos1 and pos2 then
local size = vector.subtract(pos2, pos1) local size = vector.subtract(pos2, pos1)
local r = max(size.x, size.z) / 2 local r = max(size.x, size.z) / 2
center = vector.divide(vector.add(pos1, pos2), 2) config.center = vector.divide(vector.add(pos1, pos2), 2)
center.r = r config.center.r = r
center.h = size.y config.center.h = size.y
storage:set_string("center", minetest.write_json(center)) storage:set_string("center", minetest.write_json(config.center))
end end
end end
local function get_flags() local function get_flags()
local negative = nil local negative = nil
local positive = nil local positive = nil
for _, pos in pairs(flag_positions) do for _, pos in pairs(config.flags) do
pos = vector.subtract(pos, center) pos = vector.subtract(pos, config.center)
if center_barrier_rot == 0 and pos.x < 0 or pos.z < 0 then if config.barrier_rot == 0 and pos.x < 0 or pos.z < 0 then
negative = pos negative = pos
end end
if center_barrier_rot == 0 and pos.x > 0 or pos.z > 0 then if config.barrier_rot == 0 and pos.x > 0 or pos.z > 0 then
positive = pos positive = pos
end end
end end
@ -171,14 +180,14 @@ local function get_flags()
end end
local function get_flag_status() local function get_flag_status()
if #flag_positions > 2 then if #config.flags > 2 then
return "Too many flags! (" .. #flag_positions .. "/2)" return "Too many flags! (" .. #config.flags .. "/2)"
elseif #flag_positions < 2 then elseif #config.flags < 2 then
return "Place more flags (" .. #flag_positions .. "/2)" return "Place more flags (" .. #config.flags .. "/2)"
else else
local negative, positive = get_flags() local negative, positive = get_flags()
if positive and negative then if positive and negative then
return "Flags placed (" .. #flag_positions .. "/2)" return "Flags placed (" .. #config.flags .. "/2)"
else else
return "Place one flag on each side of the barrier." return "Place one flag on each side of the barrier."
end end
@ -186,9 +195,9 @@ local function get_flag_status()
end end
local function show_gui(name) local function show_gui(name)
if not mapauthor then if not config.mapauthor then
mapauthor = name config.mapauthor = name
storage:set_string("mapauthor", mapauthor) storage:set_string("mapauthor", config.mapauthor)
end end
local formspec = { local formspec = {
@ -198,11 +207,11 @@ local function show_gui(name)
default.gui_bg_img, default.gui_bg_img,
"label[0,0;1. Select Area]", "label[0,0;1. Select Area]",
"field[0.4,1;1,1;posx;X;", center.x, "]", "field[0.4,1;1,1;posx;X;", config.center.x, "]",
"field[1.4,1;1,1;posy;Y;", center.y, "]", "field[1.4,1;1,1;posy;Y;", config.center.y, "]",
"field[2.4,1;1,1;posz;Z;", center.z, "]", "field[2.4,1;1,1;posz;Z;", config.center.z, "]",
"field[0.4,2;1.5,1;posr;R;", center.r, "]", "field[0.4,2;1.5,1;posr;R;", config.center.r, "]",
"field[1.9,2;1.5,1;posh;H;", center.h, "]", "field[1.9,2;1.5,1;posh;H;", config.center.h, "]",
"button[4.3,0.7;1.75,1;set_center;Player Pos]", "button[4.3,0.7;1.75,1;set_center;Player Pos]",
"button[6.05,0.7;1.5,1;towe;To WE]", "button[6.05,0.7;1.5,1;towe;To WE]",
"button[7.55,0.7;1.5,1;fromwe;From WE]", "button[7.55,0.7;1.5,1;fromwe;From WE]",
@ -212,8 +221,8 @@ local function show_gui(name)
"label[0,2.8;2. Place Barriers]", "label[0,2.8;2. Place Barriers]",
"label[0.1,3.3;This may take a few minutes.]", "label[0.1,3.3;This may take a few minutes.]",
"field[0.4,4.3;1,1;barrier_r;R;", barrier_r, "]", "field[0.4,4.3;1,1;barrier_r;R;", config.barrier_r, "]",
"dropdown[1.15,4.05;1,1;center_barrier_rot;X=0,Z=0;", center_barrier_rot + 1, "]", "dropdown[1.15,4.05;1,1;config.barrier_rot;X=0,Z=0;", config.barrier_rot + 1, "]",
"button[2.3,4;2,1;place_barrier;Place Barriers]", "button[2.3,4;2,1;place_barrier;Place Barriers]",
"box[4.4,2.8;0.05,2.2;#111111BB]", "box[4.4,2.8;0.05,2.2;#111111BB]",
@ -225,11 +234,14 @@ local function show_gui(name)
"box[0,5.06;8.85,0.05;#111111BB]", "box[0,5.06;8.85,0.05;#111111BB]",
"label[0,5.15;4. Meta Data]", "label[0,5.15;4. Meta Data]",
"field[0.4,6.2;8.5,1;title;Title;", minetest.formspec_escape(maptitle), "]", "field[0.4,6.2;8.5,1;title;Title;",
minetest.formspec_escape(config.maptitle), "]",
"field[0.4,7.3;8.5,1;initial;Stuff to give on (re)spawn, comma-separated itemstrings;", "field[0.4,7.3;8.5,1;initial;Stuff to give on (re)spawn, comma-separated itemstrings;",
minetest.formspec_escape(mapinitial), "]", minetest.formspec_escape(config.mapinitial), "]",
"field[0.4,8.4;4.25,1;name;File Name;" , minetest.formspec_escape(mapname), "]", "field[0.4,8.4;4.25,1;name;File Name;",
"field[4.625,8.4;4.25,1;author;Author;", minetest.formspec_escape(mapauthor), "]", minetest.formspec_escape(config.mapname), "]",
"field[4.625,8.4;4.25,1;author;Author;",
minetest.formspec_escape(config.mapauthor), "]",
"button_exit[1.3,9;3,1;close;Close]", "button_exit[1.3,9;3,1;close;Close]",
"button_exit[4.3,9;3,1;export;Export]", "button_exit[4.3,9;3,1;export;Export]",
@ -259,52 +271,52 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end end
if fields.posx then if fields.posx then
center.x = tonumber(fields.posx) config.center.x = tonumber(fields.posx)
center.y = tonumber(fields.posy) config.center.y = tonumber(fields.posy)
center.z = tonumber(fields.posz) config.center.z = tonumber(fields.posz)
center.r = tonumber(fields.posr) config.center.r = tonumber(fields.posr)
center.h = tonumber(fields.posh) config.center.h = tonumber(fields.posh)
storage:set_string("center", minetest.write_json(center)) storage:set_string("center", minetest.write_json(config.center))
end end
fields.barrier_r = tonumber(fields.barrier_r) fields.barrier_r = tonumber(fields.barrier_r)
if fields.barrier_r and fields.barrier_r ~= barrier_r then if fields.barrier_r and fields.barrier_r ~= config.barrier_r then
barrier_r = fields.barrier_r config.barrier_r = fields.barrier_r
storage:set_int("barrier_r", barrier_r) storage:set_int("barrier_r", config.barrier_r)
end end
if fields.title and fields.title ~= maptitle then if fields.title and fields.title ~= config.maptitle then
maptitle = fields.title config.maptitle = fields.title
storage:set_string("maptitle", maptitle) storage:set_string("maptitle", config.maptitle)
end end
if fields.author and fields.author ~= mapauthor then if fields.author and fields.author ~= config.mapauthor then
mapauthor = fields.author config.mapauthor = fields.author
storage:set_string("mapauthor", mapauthor) storage:set_string("mapauthor", config.mapauthor)
end end
if fields.name and fields.name ~= mapname then if fields.name and fields.name ~= config.mapname then
mapname = fields.name config.mapname = fields.name
storage:set_string("mapname", mapname) storage:set_string("mapname", config.mapname)
end end
if fields.initial and fields.initial ~= mapinitial then if fields.initial and fields.initial ~= config.mapinitial then
mapinitial = fields.initial config.mapinitial = fields.initial
storage:set_string("mapinitial", mapinitial) storage:set_string("mapinitial", config.mapinitial)
end end
if fields.center_barrier_rot and fields.center_barrier_rot ~= "" then if fields.barrier_rot and fields.barrier_rot ~= "" then
center_barrier_rot = fields.center_barrier_rot == "X=0" and 0 or 1 config.barrier_rot = fields.config.barrier_rot == "X=0" and 0 or 1
storage:set_int("center_barrier_rot", center_barrier_rot) storage:set_int("config.barrier_rot", config.barrier_rot)
end end
if fields.set_center then if fields.set_center then
local r = center.r local r = config.center.r
local h = center.h local h = config.center.h
center = vector.floor(player:get_pos()) config.center = vector.floor(player:get_pos())
center.r = r config.center.r = r
center.h = h config.center.h = h
storage:set_string("center", minetest.write_json(center)) storage:set_string("center", minetest.write_json(config.center))
end end
if fields.giveme then if fields.giveme then
@ -329,10 +341,11 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
show_progress_formspec(player_name, "Placing center barrier, this may take a while...") show_progress_formspec(player_name, "Placing center barrier, this may take a while...")
minetest.after(0.1, function() minetest.after(0.1, function()
ctf_map.place_middle_barrier(center, barrier_r, center.h, (center_barrier_rot == 0) and "x" or "z") ctf_map.place_middle_barrier(config.center, config.barrier_r,
config.center.h, (config.barrier_rot == 0) and "x" or "z")
show_progress_formspec(player_name, "Placing outer barriers, this may take a while...") show_progress_formspec(player_name, "Placing outer barriers, this may take a while...")
minetest.after(0.1, function() minetest.after(0.1, function()
ctf_map.place_outer_barrier(center, barrier_r, center.h) ctf_map.place_outer_barrier(config.center, config.barrier_r, config.center.h)
show_gui(player_name) show_gui(player_name)
end) end)
end) end)
@ -349,7 +362,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end end
if fields.export then if fields.export then
if #flag_positions ~= 2 then if #config.flags ~= 2 then
minetest.chat_send_all("You need to place two flags!") minetest.chat_send_all("You need to place two flags!")
return return
end end
@ -366,21 +379,23 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
storage:set_string("mapauthor", "") storage:set_string("mapauthor", "")
storage:set_string("mapname", "") storage:set_string("mapname", "")
storage:set_string("mapinitial", "") storage:set_string("mapinitial", "")
storage:set_string("center_barrier_rot", "") storage:set_string("config.barrier_rot", "")
storage:set_string("barrier_r", "") storage:set_string("barrier_r", "")
-- Write to .conf -- Write to .conf
local meta = Settings(path .. mapname .. ".conf") local meta = Settings(path .. config.mapname .. ".conf")
meta:set("name", maptitle) meta:set("name", config.maptitle)
meta:set("author", mapauthor) meta:set("author", config.mapauthor)
meta:set("initial_stuff", mapinitial) if config.mapinitial ~= "" then
meta:set("rotation", center_barrier_rot == 0 and "x" or "z") meta:set("initial_stuff", config.mapinitial)
meta:set("r", center.r) end
meta:set("h", center.h) meta:set("rotation", config.barrier_rot == 0 and "x" or "z")
meta:set("r", config.center.r)
meta:set("h", config.center.h)
for _, flags in pairs(flag_positions) do for _, flags in pairs(config.flags) do
local pos = vector.subtract(flags, center) local pos = vector.subtract(flags, config.center)
if center_barrier_rot == 0 then if config.barrier_rot == 0 then
local old = vector.new(pos) local old = vector.new(pos)
pos.x = old.z pos.x = old.z
pos.z = -old.x pos.z = -old.x
@ -394,11 +409,11 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
meta:write() meta:write()
minetest.after(0.1, function() minetest.after(0.1, function()
local filepath = path .. mapname .. ".mts" local filepath = path .. config.mapname .. ".mts"
if minetest.create_schematic(worldedit.pos1[player_name], if minetest.create_schematic(worldedit.pos1[player_name],
worldedit.pos2[player_name], worldedit.prob_list[player_name], worldedit.pos2[player_name], worldedit.prob_list[player_name],
filepath) then filepath) then
minetest.chat_send_all("Exported " .. mapname .. " to " .. path) minetest.chat_send_all("Exported " .. config.mapname .. " to " .. path)
minetest.close_formspec(player_name, "") minetest.close_formspec(player_name, "")
else else
minetest.chat_send_all("Failed!") minetest.chat_send_all("Failed!")