From a192d51a58a422bb6413dc5ddb505ac881692eba Mon Sep 17 00:00:00 2001 From: Sfan5 Date: Sat, 22 Jun 2013 08:24:54 +0200 Subject: [PATCH] Add initial support for Minetest schematic API Thanks to kwolekr for the code --- Chat Commands.md | 23 +++++++- worldedit_commands/init.lua | 104 ++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) diff --git a/Chat Commands.md b/Chat Commands.md index bba8762..eee08eb 100644 --- a/Chat Commands.md +++ b/Chat Commands.md @@ -277,4 +277,25 @@ Executes as a Lua chunk in the global namespace. Executes as a Lua chunk in the global namespace with the variable pos available, for each node in the current WorldEdit region. //luatransform minetest.env:add_node(pos, {name="default:stone"}) - //luatransform if minetest.env:get_node(pos).name == "air" then minetest.env:add_node(pos, {name="default:water_source"}) + //luatransform if minetest.env:get_node(pos).name == "air" then minetest.env:add_node(pos, {name="default:water_source"}) + +### //mtschemcreate + +Save the current WorldEdit region using the Minetest Schematic format to "(world folder)/schems/.mts". + + //mtschemcreate some random filename + //mtschemcreate huge_base + +### //mtschemplace + +Load nodes from "(world folder)/schems/.mts" with position 1 of the current WorldEdit region as the origin. + + //mtschemplace some random filename + //mtschemplace huge_base + +### //mtschemprob start/finish/get + +After using //mtschemprob start all nodes punched will bring up a text field where a probablity can be entered. +This mode can be left with //mtschemprob finish. //mtschemprob get will display the probabilities saved for the nodes. + + //mtschemprob get diff --git a/worldedit_commands/init.lua b/worldedit_commands/init.lua index f610ccb..b50198e 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -4,6 +4,10 @@ worldedit.set_pos = {} worldedit.pos1 = {} worldedit.pos2 = {} +if minetest.place_schematic then + worldedit.prob_pos = {} + worldedit.prob_list = {} +end dofile(minetest.get_modpath("worldedit_commands") .. "/mark.lua") @@ -161,6 +165,9 @@ minetest.register_on_punchnode(function(pos, node, puncher) worldedit.mark_pos2(name) worldedit.set_pos[name] = nil --finished setting positions worldedit.player_notify(name, "position 2 set to " .. minetest.pos_to_string(pos)) + elseif worldedit.set_pos[name] == "prob" then --setting Minetest schematic node probabilities + worldedit.prob_pos[name] = pos + minetest.show_formspec(puncher:get_player_name(), "prob_val_enter", "field[text;;]") end end end) @@ -1071,3 +1078,100 @@ minetest.register_chatcommand("/luatransform", { end end, }) + +if minetest.place_schematic then +minetest.register_chatcommand("/mtschemcreate", { + params = "", + description = "Creates a Minetest schematic of the box defined by position 1 and position 2, and saves it to ", + privs = {worldedit=true}, + func = function(name, param) + local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] + if pos1 == nil or pos2 == nil then + worldedit.player_notify(name, "No region selected") + return + end + if param == nil then + worldedit.player_notify(name, "No filename specified") + return + end + + local path = minetest.get_worldpath() .. "/schems" + local filename = path .. "/" .. param .. ".mts" + os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist + + local ret = minetest.create_schematic(pos1, pos2, worldedit.prob_list[name], filename) + if ret == nil then + worldedit.player_notify(name, "Failed to create Minetest schematic", false) + else + worldedit.player_notify(name, "Saved Minetest schematic to " .. param, false) + end + worldedit.prob_list[name] = {} + end, +}) + +minetest.register_chatcommand("/mtschemplace", { + params = "", + description = "Places the Minetest schematic identified by at WorldEdit position 1", + privs = {worldedit=true}, + func = function(name, param) + local pos = worldedit.pos1[name] + if pos == nil then + worldedit.player_notify(name, "No position selected") + return + end + if param == nil then + worldedit.player_notify(name, "No filename specified") + return + end + + local path = minetest.get_worldpath() .. "/schems/" .. param .. ".mts" + if minetest.place_schematic(pos, path) == nil then + worldedit.player_notify(name, "Failed to place Minetest schematic", false) + else + worldedit.player_notify(name, "Placed Minetest schematic " .. param .. + " at " .. minetest.pos_to_string(pos), false) + end + end, +}) + +minetest.register_chatcommand("/mtschemprob", { + params = "start/finish/get", + description = "Begins node probability entry for Minetest schematics, gets the nodes that have probabilities set, or ends node probability entry", + privs = {worldedit=true}, + func = function(name, param) + if param == "start" then --start probability setting + worldedit.set_pos[name] = "prob" + worldedit.prob_list[name] = {} + worldedit.player_notify(name, "select Minetest schematic probability values by punching nodes") + elseif param == "finish" then --finish probability setting + worldedit.set_pos[name] = nil + worldedit.player_notify(name, "finished Minetest schematic probability selection") + elseif param == "get" then --get all nodes that had probabilities set on them + local text = "" + local problist = worldedit.prob_list[name] + if problist == nil then + return + end + for k,v in pairs(problist) do + local prob = math.floor(((v["prob"] / 256) * 100) * 100 + 0.5) / 100 + text = text .. minetest.pos_to_string(v["pos"]) .. ": " .. prob .. "% | " + end + worldedit.player_notify(name, "Currently set node probabilities:") + worldedit.player_notify(name, text) + else + worldedit.player_notify(name, "unknown subcommand: " .. param) + end + end, +}) + +minetest.register_on_player_receive_fields( + function(player, formname, fields) + if (formname == "prob_val_enter") and (fields.text ~= "") then + local name = player:get_player_name() + local prob_entry = {pos=worldedit.prob_pos[name], prob=tonumber(fields.text)} + local index = table.getn(worldedit.prob_list[name]) + 1 + worldedit.prob_list[name][index] = prob_entry + end + end +) +end