Compare commits
5 Commits
a452e90419
...
0c128220a7
Author | SHA1 | Date |
---|---|---|
octacian | 0c128220a7 | |
octacian | a3658af5f9 | |
octacian | e0941b8f61 | |
octacian | eb0efad188 | |
octacian | 2958451136 |
247
init.lua
247
init.lua
|
@ -3,15 +3,24 @@
|
|||
advschem = {}
|
||||
|
||||
local path = minetest.get_worldpath().."/advschem.mt"
|
||||
local contexts = {}
|
||||
local marked = {}
|
||||
advschem.markers = {}
|
||||
|
||||
-- [local function] Renumber table
|
||||
local function renumber(t)
|
||||
local res = {}
|
||||
for _, i in pairs(t) do
|
||||
res[#res + 1] = i
|
||||
end
|
||||
return res
|
||||
end
|
||||
|
||||
---
|
||||
--- Formspec API
|
||||
---
|
||||
|
||||
local contexts = {}
|
||||
local form_data = {}
|
||||
local tabs = {}
|
||||
local forms = {}
|
||||
|
||||
|
@ -61,7 +70,11 @@ function advschem.show_formspec(pos, player, tab, show, ...)
|
|||
local name = player:get_player_name()
|
||||
|
||||
if show ~= false then
|
||||
local form = forms[tab].get(pos, name, ...)
|
||||
if not form_data[name] then
|
||||
form_data[name] = {}
|
||||
end
|
||||
|
||||
local form = forms[tab].get(form_data[name], pos, name, ...)
|
||||
if forms[tab].tab then
|
||||
form = form..advschem.generate_tabs(tab)
|
||||
end
|
||||
|
@ -87,8 +100,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
local handle = forms[formname[2]].handle
|
||||
local name = player:get_player_name()
|
||||
if contexts[name] then
|
||||
if not form_data[name] then
|
||||
form_data[name] = {}
|
||||
end
|
||||
|
||||
if not advschem.handle_tabs(contexts[name], name, fields) and handle then
|
||||
handle(contexts[name], name, fields)
|
||||
handle(form_data[name], contexts[name], name, fields)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -101,7 +118,7 @@ end)
|
|||
advschem.add_form("main", {
|
||||
tab = true,
|
||||
caption = "Main",
|
||||
get = function(pos, name)
|
||||
get = function(self, pos, name)
|
||||
local meta = minetest.get_meta(pos):to_table().fields
|
||||
local strpos = minetest.pos_to_string(pos)
|
||||
|
||||
|
@ -138,7 +155,7 @@ advschem.add_form("main", {
|
|||
]]..
|
||||
border_button
|
||||
end,
|
||||
handle = function(pos, name, fields)
|
||||
handle = function(self, pos, name, fields)
|
||||
local realmeta = minetest.get_meta(pos)
|
||||
local meta = realmeta:to_table().fields
|
||||
local strpos = minetest.pos_to_string(pos)
|
||||
|
@ -191,14 +208,28 @@ advschem.add_form("main", {
|
|||
local plist = minetest.deserialize(meta.prob_list)
|
||||
local probability_list = {}
|
||||
for _, i in pairs(plist) do
|
||||
local prob = i.prob
|
||||
if i.force_place == true then
|
||||
prob = prob + 128
|
||||
end
|
||||
|
||||
probability_list[#probability_list + 1] = {
|
||||
pos = minetest.string_to_pos(_),
|
||||
prob = prob,
|
||||
}
|
||||
end
|
||||
|
||||
local slist = minetest.deserialize(meta.slices)
|
||||
local slice_list = {}
|
||||
for _, i in pairs(slist) do
|
||||
slice_list[#slice_list + 1] = {
|
||||
ypos = pos.y + i.ypos,
|
||||
prob = i.prob,
|
||||
}
|
||||
end
|
||||
|
||||
local filepath = path..meta.schem_name..".mts"
|
||||
local res = minetest.create_schematic(pos1, pos2, probability_list, filepath, {})
|
||||
local res = minetest.create_schematic(pos1, pos2, probability_list, filepath, slice_list)
|
||||
|
||||
if res then
|
||||
minetest.chat_send_player(name, minetest.colorize("#00ff00",
|
||||
|
@ -238,39 +269,42 @@ advschem.add_form("main", {
|
|||
advschem.add_form("prob", {
|
||||
tab = true,
|
||||
caption = "Probability",
|
||||
get = function(pos, name)
|
||||
get = function(self, pos, name)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inventory = meta:get_inventory()
|
||||
local stack = inventory:get_stack("probability", 1)
|
||||
local stringpos = pos.x..","..pos.y..","..pos.z
|
||||
|
||||
local probchange = ""
|
||||
local form = [[
|
||||
size[8,6]
|
||||
list[nodemeta:]]..stringpos..[[;probability;0,0;1,1;]
|
||||
label[0,1;Probability is a number between 0 and 127.]
|
||||
list[current_player;main;0,1.5;8,4;]
|
||||
listring[nodemeta:]]..stringpos..[[;probability]
|
||||
listring[current_player;main]
|
||||
]]
|
||||
|
||||
if stack:get_name() ~= "" then
|
||||
local smeta = stack:get_meta():to_table().fields
|
||||
|
||||
probchange = [[
|
||||
field[1.3,0.4;2,1;probability;Probability:;]]..(smeta.advschem_prob or "255")..[[]
|
||||
form = form .. [[
|
||||
field[1.3,0.4;2,1;probability;Probability:;]]..
|
||||
(smeta.advschem_prob or "127").."]" ..
|
||||
[[
|
||||
field_close_on_enter[probability;false]
|
||||
checkbox[3.1,0.1;force_place;Force Place;]]..(smeta.advschem_force_place or "false")..[[]
|
||||
button[5,0.1;1.5,1;rst;Reset]
|
||||
button[6.5,0.1;1.5,1;save;Save]
|
||||
]]
|
||||
else
|
||||
probchange = [[
|
||||
form = form .. [[
|
||||
label[1,0.2;Insert item in slot.]
|
||||
]]
|
||||
end
|
||||
|
||||
return [[
|
||||
size[8,6]
|
||||
list[nodemeta:]]..stringpos..[[;probability;0,0;1,1;]
|
||||
label[0,1;Probability is a number between 0 and 255.]
|
||||
list[current_player;main;0,1.5;8,4;]
|
||||
listring[nodemeta:]]..stringpos..[[;probability]
|
||||
listring[current_player;main]
|
||||
]]..probchange
|
||||
return form
|
||||
end,
|
||||
handle = function(pos, name, fields)
|
||||
handle = function(self, pos, name, fields)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local inventory = meta:get_inventory()
|
||||
local stack = inventory:get_stack("probability", 1)
|
||||
|
@ -290,18 +324,27 @@ advschem.add_form("prob", {
|
|||
smeta:set_string("description", original_desc)
|
||||
elseif fields.force_place or fields.save or
|
||||
fields.key_enter_field == "probability" then
|
||||
|
||||
local prob_desc = "\nProbability: "..(fields.probability or
|
||||
smeta:get_string("advschem_prob") or "Not Set")
|
||||
-- Update probability
|
||||
if fields.probability ~= "" then
|
||||
local prob = tonumber(fields.probability)
|
||||
if prob >= 0 and prob <= 255 then
|
||||
if prob and prob >= 0 and prob <= 127 then
|
||||
smeta:set_string("advschem_prob", fields.probability)
|
||||
else
|
||||
prob_desc = "\nProbability: "..(smeta:get_string("advschem_prob") or
|
||||
"Not Set")
|
||||
advschem.show_formspec(pos, minetest.get_player_by_name(name), "prob")
|
||||
end
|
||||
else
|
||||
smeta:set_string("advschem_prob", nil)
|
||||
end
|
||||
|
||||
smeta:set_string("advschem_force_place", fields.force_place)
|
||||
-- Update force place if fields value is not nil
|
||||
if fields.force_place ~= nil then
|
||||
smeta:set_string("advschem_force_place", fields.force_place)
|
||||
end
|
||||
|
||||
-- Update description
|
||||
local desc = minetest.registered_items[stack:get_name()].description
|
||||
|
@ -317,22 +360,146 @@ advschem.add_form("prob", {
|
|||
smeta:set_string("original_description", desc)
|
||||
end
|
||||
|
||||
desc = desc.."\n"..minetest.colorize("grey", "Probability: "..
|
||||
fields.probability.."\nForce Place: "..(fields.force_place or "false"))
|
||||
local force_desc = ""
|
||||
if smeta:get_string("advschem_force_place") == "true" then
|
||||
force_desc = "\n".."Force Place"
|
||||
end
|
||||
|
||||
desc = desc..minetest.colorize("grey", prob_desc..force_desc)
|
||||
|
||||
smeta:set_string("description", desc)
|
||||
end
|
||||
|
||||
-- Update itemstack
|
||||
inventory:set_stack("probability", 1, stack)
|
||||
|
||||
-- Refresh formspec
|
||||
if fields.rst then
|
||||
-- Refresh formspec on reset or force placement change
|
||||
if fields.rst or fields.force_place then
|
||||
advschem.show_formspec(pos, minetest.get_player_by_name(name), "prob")
|
||||
end
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
advschem.add_form("slice", {
|
||||
caption = "Y-Slice",
|
||||
tab = true,
|
||||
get = function(self, pos, name, visible_panel)
|
||||
local meta = minetest.get_meta(pos):to_table().fields
|
||||
|
||||
self.selected = self.selected or 1
|
||||
local selected = tostring(self.selected)
|
||||
local slice_list = minetest.deserialize(meta.slices)
|
||||
local slices = ""
|
||||
for _, i in pairs(slice_list) do
|
||||
local insert = "Y = "..tostring(i.ypos).."; Probability = "..tostring(i.prob)
|
||||
slices = slices..minetest.formspec_escape(insert)..","
|
||||
end
|
||||
slices = slices:sub(1, -2) -- Remove final comma
|
||||
|
||||
local form = [[
|
||||
size[7,6]
|
||||
table[0,0;6.8,4;slices;]]..slices..[[;]]..selected..[[]
|
||||
]]
|
||||
|
||||
if self.panel_add or self.panel_edit then
|
||||
local ypos_default, prob_default = "", ""
|
||||
local done_button = "button[5,5.18;2,1;done_add;Done]"
|
||||
if self.panel_edit then
|
||||
done_button = "button[5,5.18;2,1;done_edit;Done]"
|
||||
ypos_default = slice_list[self.selected].ypos
|
||||
prob_default = slice_list[self.selected].prob
|
||||
end
|
||||
|
||||
form = form..[[
|
||||
field[0.3,5.5;2.5,1;ypos;Y-Position (max ]]..(meta.y_size - 1)..[[):;]]..ypos_default..[[]
|
||||
field[2.8,5.5;2.5,1;prob;Probability (0-255):;]]..prob_default..[[]
|
||||
field_close_on_enter[ypos;false]
|
||||
field_close_on_enter[prob;false]
|
||||
]]..done_button
|
||||
end
|
||||
|
||||
if not self.panel_edit then
|
||||
form = form.."button[0,4;2,1;add;+ Add Slice]"
|
||||
end
|
||||
|
||||
if slices ~= "" and self.selected and not self.panel_add then
|
||||
if not self.panel_edit then
|
||||
form = form..[[
|
||||
button[2,4;2,1;remove;- Remove Slice]
|
||||
button[4,4;2,1;edit;+/- Edit Slice]
|
||||
]]
|
||||
else
|
||||
form = form..[[
|
||||
button[0,4;2,1;remove;- Remove Slice]
|
||||
button[2,4;2,1;edit;+/- Edit Slice]
|
||||
]]
|
||||
end
|
||||
end
|
||||
|
||||
return form
|
||||
end,
|
||||
handle = function(self, pos, name, fields)
|
||||
local meta = minetest.get_meta(pos)
|
||||
local player = minetest.get_player_by_name(name)
|
||||
|
||||
if fields.slices then
|
||||
local slices = fields.slices:split(":")
|
||||
self.selected = tonumber(slices[2])
|
||||
end
|
||||
|
||||
if fields.add then
|
||||
if not self.panel_add then
|
||||
self.panel_add = true
|
||||
advschem.show_formspec(pos, player, "slice")
|
||||
else
|
||||
self.panel_add = nil
|
||||
advschem.show_formspec(pos, player, "slice")
|
||||
end
|
||||
end
|
||||
|
||||
local ypos, prob = tonumber(fields.ypos), tonumber(fields.prob)
|
||||
if (fields.done_add or fields.done_edit) and fields.ypos and fields.prob and
|
||||
fields.ypos ~= "" and fields.prob ~= "" and ypos and prob and
|
||||
ypos <= (meta:get_int("y_size") - 1) and prob >= 0 and prob <= 255 then
|
||||
local slice_list = minetest.deserialize(meta:get_string("slices"))
|
||||
local index = #slice_list + 1
|
||||
if fields.done_edit then
|
||||
index = self.selected
|
||||
end
|
||||
|
||||
slice_list[index] = {ypos = ypos, prob = prob}
|
||||
|
||||
meta:set_string("slices", minetest.serialize(slice_list))
|
||||
|
||||
-- Update and show formspec
|
||||
self.panel_add = nil
|
||||
advschem.show_formspec(pos, player, "slice")
|
||||
end
|
||||
|
||||
if fields.remove and self.selected then
|
||||
local slice_list = minetest.deserialize(meta:get_string("slices"))
|
||||
slice_list[self.selected] = nil
|
||||
meta:set_string("slices", minetest.serialize(renumber(slice_list)))
|
||||
|
||||
-- Update formspec
|
||||
self.selected = 1
|
||||
self.panel_edit = nil
|
||||
advschem.show_formspec(pos, player, "slice")
|
||||
end
|
||||
|
||||
if fields.edit then
|
||||
if not self.panel_edit then
|
||||
self.panel_edit = true
|
||||
advschem.show_formspec(pos, player, "slice")
|
||||
else
|
||||
self.panel_edit = nil
|
||||
advschem.show_formspec(pos, player, "slice")
|
||||
end
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
---
|
||||
--- API
|
||||
---
|
||||
|
@ -530,9 +697,11 @@ minetest.register_on_dignode(function(pos, oldnode, player)
|
|||
local meta = minetest.get_meta(realpos)
|
||||
local prob_list = minetest.deserialize(meta:get_string("prob_list"))
|
||||
|
||||
for _, i in pairs(prob_list) do
|
||||
if _ == original_strpos then
|
||||
prob_list[_] = nil
|
||||
if prob_list then
|
||||
for _, i in pairs(prob_list) do
|
||||
if _ == original_strpos then
|
||||
prob_list[_] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -562,6 +731,7 @@ minetest.register_node("advschem:creator", {
|
|||
meta:set_string("owner", name)
|
||||
meta:set_string("infotext", "Schematic Creator\n(owned by "..name..")")
|
||||
meta:set_string("prob_list", minetest.serialize({}))
|
||||
meta:set_string("slices", minetest.serialize({}))
|
||||
|
||||
local node = minetest.get_node(pos)
|
||||
local dir = minetest.facedir_to_dir(node.param2)
|
||||
|
@ -653,6 +823,25 @@ minetest.register_entity("advschem:display", {
|
|||
end,
|
||||
})
|
||||
|
||||
-- [chatcommand] Place schematic
|
||||
minetest.register_chatcommand("placeschem", {
|
||||
description = "Place schematic at the position specified or the current "..
|
||||
"player position (loaded from "..minetest.get_worldpath().."/schems/)",
|
||||
privs = {debug = true},
|
||||
params = "[<schematic name>.mts] (<x> <y> <z>)",
|
||||
func = function(name, param)
|
||||
local schem, p = string.match(param, "^([^ ]+) *(.*)$")
|
||||
local pos = minetest.string_to_pos(p)
|
||||
|
||||
if not pos then
|
||||
pos = minetest.get_player_by_name(name):get_pos()
|
||||
end
|
||||
|
||||
return true, "Success: "..dump(minetest.place_schematic(pos,
|
||||
minetest.get_worldpath().."/schems/"..schem..".mts", "random", nil, false))
|
||||
end,
|
||||
})
|
||||
|
||||
---
|
||||
--- Load Data
|
||||
---
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
# Configures how long display entities should be kept in-world before they are
|
||||
# removed.
|
||||
advschem.display_remove (Remove Display Entities After) float 60
|
Loading…
Reference in New Issue