Skip to content

Commit a192d51

Browse files
committedJun 22, 2013
Add initial support for Minetest schematic API
Thanks to kwolekr for the code
1 parent d418786 commit a192d51

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed
 

‎Chat Commands.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,25 @@ Executes <code> as a Lua chunk in the global namespace.
277277
Executes <code> as a Lua chunk in the global namespace with the variable pos available, for each node in the current WorldEdit region.
278278

279279
//luatransform minetest.env:add_node(pos, {name="default:stone"})
280-
//luatransform if minetest.env:get_node(pos).name == "air" then minetest.env:add_node(pos, {name="default:water_source"})
280+
//luatransform if minetest.env:get_node(pos).name == "air" then minetest.env:add_node(pos, {name="default:water_source"})
281+
282+
### //mtschemcreate <file>
283+
284+
Save the current WorldEdit region using the Minetest Schematic format to "(world folder)/schems/<file>.mts".
285+
286+
//mtschemcreate some random filename
287+
//mtschemcreate huge_base
288+
289+
### //mtschemplace <file>
290+
291+
Load nodes from "(world folder)/schems/<file>.mts" with position 1 of the current WorldEdit region as the origin.
292+
293+
//mtschemplace some random filename
294+
//mtschemplace huge_base
295+
296+
### //mtschemprob start/finish/get
297+
298+
After using //mtschemprob start all nodes punched will bring up a text field where a probablity can be entered.
299+
This mode can be left with //mtschemprob finish. //mtschemprob get will display the probabilities saved for the nodes.
300+
301+
//mtschemprob get

‎worldedit_commands/init.lua

+104
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ worldedit.set_pos = {}
44

55
worldedit.pos1 = {}
66
worldedit.pos2 = {}
7+
if minetest.place_schematic then
8+
worldedit.prob_pos = {}
9+
worldedit.prob_list = {}
10+
end
711

812
dofile(minetest.get_modpath("worldedit_commands") .. "/mark.lua")
913

@@ -161,6 +165,9 @@ minetest.register_on_punchnode(function(pos, node, puncher)
161165
worldedit.mark_pos2(name)
162166
worldedit.set_pos[name] = nil --finished setting positions
163167
worldedit.player_notify(name, "position 2 set to " .. minetest.pos_to_string(pos))
168+
elseif worldedit.set_pos[name] == "prob" then --setting Minetest schematic node probabilities
169+
worldedit.prob_pos[name] = pos
170+
minetest.show_formspec(puncher:get_player_name(), "prob_val_enter", "field[text;;]")
164171
end
165172
end
166173
end)
@@ -1071,3 +1078,100 @@ minetest.register_chatcommand("/luatransform", {
10711078
end
10721079
end,
10731080
})
1081+
1082+
if minetest.place_schematic then
1083+
minetest.register_chatcommand("/mtschemcreate", {
1084+
params = "<filename>",
1085+
description = "Creates a Minetest schematic of the box defined by position 1 and position 2, and saves it to <filename>",
1086+
privs = {worldedit=true},
1087+
func = function(name, param)
1088+
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
1089+
if pos1 == nil or pos2 == nil then
1090+
worldedit.player_notify(name, "No region selected")
1091+
return
1092+
end
1093+
if param == nil then
1094+
worldedit.player_notify(name, "No filename specified")
1095+
return
1096+
end
1097+
1098+
local path = minetest.get_worldpath() .. "/schems"
1099+
local filename = path .. "/" .. param .. ".mts"
1100+
os.execute("mkdir \"" .. path .. "\"") --create directory if it does not already exist
1101+
1102+
local ret = minetest.create_schematic(pos1, pos2, worldedit.prob_list[name], filename)
1103+
if ret == nil then
1104+
worldedit.player_notify(name, "Failed to create Minetest schematic", false)
1105+
else
1106+
worldedit.player_notify(name, "Saved Minetest schematic to " .. param, false)
1107+
end
1108+
worldedit.prob_list[name] = {}
1109+
end,
1110+
})
1111+
1112+
minetest.register_chatcommand("/mtschemplace", {
1113+
params = "<filename>",
1114+
description = "Places the Minetest schematic identified by <filename> at WorldEdit position 1",
1115+
privs = {worldedit=true},
1116+
func = function(name, param)
1117+
local pos = worldedit.pos1[name]
1118+
if pos == nil then
1119+
worldedit.player_notify(name, "No position selected")
1120+
return
1121+
end
1122+
if param == nil then
1123+
worldedit.player_notify(name, "No filename specified")
1124+
return
1125+
end
1126+
1127+
local path = minetest.get_worldpath() .. "/schems/" .. param .. ".mts"
1128+
if minetest.place_schematic(pos, path) == nil then
1129+
worldedit.player_notify(name, "Failed to place Minetest schematic", false)
1130+
else
1131+
worldedit.player_notify(name, "Placed Minetest schematic " .. param ..
1132+
" at " .. minetest.pos_to_string(pos), false)
1133+
end
1134+
end,
1135+
})
1136+
1137+
minetest.register_chatcommand("/mtschemprob", {
1138+
params = "start/finish/get",
1139+
description = "Begins node probability entry for Minetest schematics, gets the nodes that have probabilities set, or ends node probability entry",
1140+
privs = {worldedit=true},
1141+
func = function(name, param)
1142+
if param == "start" then --start probability setting
1143+
worldedit.set_pos[name] = "prob"
1144+
worldedit.prob_list[name] = {}
1145+
worldedit.player_notify(name, "select Minetest schematic probability values by punching nodes")
1146+
elseif param == "finish" then --finish probability setting
1147+
worldedit.set_pos[name] = nil
1148+
worldedit.player_notify(name, "finished Minetest schematic probability selection")
1149+
elseif param == "get" then --get all nodes that had probabilities set on them
1150+
local text = ""
1151+
local problist = worldedit.prob_list[name]
1152+
if problist == nil then
1153+
return
1154+
end
1155+
for k,v in pairs(problist) do
1156+
local prob = math.floor(((v["prob"] / 256) * 100) * 100 + 0.5) / 100
1157+
text = text .. minetest.pos_to_string(v["pos"]) .. ": " .. prob .. "% | "
1158+
end
1159+
worldedit.player_notify(name, "Currently set node probabilities:")
1160+
worldedit.player_notify(name, text)
1161+
else
1162+
worldedit.player_notify(name, "unknown subcommand: " .. param)
1163+
end
1164+
end,
1165+
})
1166+
1167+
minetest.register_on_player_receive_fields(
1168+
function(player, formname, fields)
1169+
if (formname == "prob_val_enter") and (fields.text ~= "") then
1170+
local name = player:get_player_name()
1171+
local prob_entry = {pos=worldedit.prob_pos[name], prob=tonumber(fields.text)}
1172+
local index = table.getn(worldedit.prob_list[name]) + 1
1173+
worldedit.prob_list[name][index] = prob_entry
1174+
end
1175+
end
1176+
)
1177+
end

0 commit comments

Comments
 (0)
Please sign in to comment.