1
0
mirror of https://github.com/Uberi/Minetest-WorldEdit.git synced 2024-12-25 18:20:38 +01:00

Add initial support for Minetest schematic API

Thanks to kwolekr for the code
This commit is contained in:
Sfan5 2013-06-22 08:24:54 +02:00
parent d4187866db
commit a192d51a58
2 changed files with 126 additions and 1 deletions

View File

@ -277,4 +277,25 @@ Executes <code> as a Lua chunk in the global namespace.
Executes <code> as a Lua chunk in the global namespace with the variable pos available, for each node in the current WorldEdit region. Executes <code> 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 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 <file>
Save the current WorldEdit region using the Minetest Schematic format to "(world folder)/schems/<file>.mts".
//mtschemcreate some random filename
//mtschemcreate huge_base
### //mtschemplace <file>
Load nodes from "(world folder)/schems/<file>.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

View File

@ -4,6 +4,10 @@ worldedit.set_pos = {}
worldedit.pos1 = {} worldedit.pos1 = {}
worldedit.pos2 = {} worldedit.pos2 = {}
if minetest.place_schematic then
worldedit.prob_pos = {}
worldedit.prob_list = {}
end
dofile(minetest.get_modpath("worldedit_commands") .. "/mark.lua") 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.mark_pos2(name)
worldedit.set_pos[name] = nil --finished setting positions worldedit.set_pos[name] = nil --finished setting positions
worldedit.player_notify(name, "position 2 set to " .. minetest.pos_to_string(pos)) 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 end
end) end)
@ -1071,3 +1078,100 @@ minetest.register_chatcommand("/luatransform", {
end end
end, end,
}) })
if minetest.place_schematic then
minetest.register_chatcommand("/mtschemcreate", {
params = "<filename>",
description = "Creates a Minetest schematic of the box defined by position 1 and position 2, and saves it to <filename>",
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 = "<filename>",
description = "Places the Minetest schematic identified by <filename> 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