schematic replacements (#2)

* schematic replacements

* wip

* fixes

---------

Co-authored-by: BuckarooBanzay <BuckarooBanzay@users.noreply.github.com>
This commit is contained in:
Buckaroo Banzai 2024-02-29 19:26:34 +01:00 committed by GitHub
parent cf7442ad2e
commit c02a7fde59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 110 additions and 2 deletions

View File

@ -34,6 +34,7 @@ minetest.register_node("pick_and_place:handle", {
on_rightclick = on_rightclick, on_rightclick = on_rightclick,
drop = "", drop = "",
groups = { groups = {
oddly_breakable_by_hand = 3 oddly_breakable_by_hand = 3,
not_in_creative_inventory = 1
} }
}) })

View File

@ -8,6 +8,7 @@ dofile(MP .. "/schematic_rotate.lua")
dofile(MP .. "/schematic_flip.lua") dofile(MP .. "/schematic_flip.lua")
dofile(MP .. "/schematic_orient.lua") dofile(MP .. "/schematic_orient.lua")
dofile(MP .. "/schematic_transpose.lua") dofile(MP .. "/schematic_transpose.lua")
dofile(MP .. "/replacement.lua")
dofile(MP .. "/pointed.lua") dofile(MP .. "/pointed.lua")
dofile(MP .. "/configure.lua") dofile(MP .. "/configure.lua")
dofile(MP .. "/remove.lua") dofile(MP .. "/remove.lua")

89
replacement.lua Normal file
View File

@ -0,0 +1,89 @@
local function update_formspec(meta)
local group = meta:get_string("group")
meta:set_string("formspec", [[
size[10,8.3]
real_coordinates[true]
field[0.1,0.4;8.8,0.8;group;Group;]] .. group .. [[]
button_exit[9,0.4;0.9,0.8;set;Set]
list[context;main;0.1,1.4;8,1;]
list[current_player;main;0.1,3;8,4;]
listring[]
]])
local txt = "Replacement node"
if group and group ~= "" then
txt = txt .. " (group: '" .. group .. "')"
end
meta:set_string("infotext", txt)
end
minetest.register_node("pick_and_place:replacement", {
description = "Replacement node",
tiles = {"pick_and_place.png^[colorize:#ff0000"},
drawtype = "allfaces",
use_texture_alpha = "blend",
paramtype = "light",
sunlight_propagates = true,
groups = {
oddly_breakable_by_hand = 3
},
on_construct = function(pos)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
inv:set_size("main", 8)
update_formspec(meta)
end,
on_receive_fields = function(pos, _, fields)
if fields.set then
local meta = minetest.get_meta(pos)
meta:set_string("group", fields.group)
update_formspec(meta)
end
end
})
function pick_and_place.get_replacement_nodeid(ctx, metadata)
local group = metadata.fields.group
local selected_name
if group and group ~= "" and ctx[group] then
-- group placement
selected_name = metadata.inventory.main[ctx[group]]
else
-- random placement
local replacement_names = {}
for _, name in ipairs(metadata.inventory.main) do
if name ~= "" then
table.insert(replacement_names, name)
end
end
if #replacement_names == 0 then
-- no replacement
return
end
local i = math.random(#replacement_names)
selected_name = replacement_names[i]
-- set group context
if group and group ~= "" then
ctx[group] = i
end
end
local stack = ItemStack(selected_name)
local nodename = stack:get_name()
if not minetest.registered_nodes[nodename] then
-- node not found
return
end
local nodeid = minetest.get_content_id(nodename)
return nodeid
end

View File

@ -1,4 +1,5 @@
local air_cid = minetest.get_content_id("air") local air_cid = minetest.get_content_id("air")
local replacement_cid = minetest.get_content_id("pick_and_place:replacement")
function pick_and_place.serialize(pos1, pos2) function pick_and_place.serialize(pos1, pos2)
local manip = minetest.get_voxel_manip() local manip = minetest.get_voxel_manip()
@ -68,6 +69,7 @@ function pick_and_place.deserialize(pos1, encoded_data)
local node_data = manip:get_data() local node_data = manip:get_data()
local param2 = manip:get_param2_data() local param2 = manip:get_param2_data()
local ctx = {}
local j = 1 local j = 1
for z=pos1.z,pos2.z do for z=pos1.z,pos2.z do
for y=pos1.y,pos2.y do for y=pos1.y,pos2.y do
@ -75,7 +77,22 @@ function pick_and_place.deserialize(pos1, encoded_data)
local i = area:index(x,y,z) local i = area:index(x,y,z)
local nodeid = schematic.node_id_data[j] local nodeid = schematic.node_id_data[j]
if nodeid ~= air_cid then if nodeid == replacement_cid then
-- replacement placement
local abs_pos = {x=x, y=y, z=z}
local rel_pos = vector.subtract(abs_pos, pos1)
local pos_str = minetest.pos_to_string(rel_pos)
local metadata = schematic.metadata[pos_str]
local repl_id = pick_and_place.get_replacement_nodeid(ctx, metadata)
if repl_id then
-- set new node
node_data[i] = repl_id
param2[i] = schematic.param2_data[j]
-- clear metadata
schematic.metadata[pos_str] = nil
end
elseif nodeid ~= air_cid then
-- normal placement
node_data[i] = nodeid node_data[i] = nodeid
param2[i] = schematic.param2_data[j] param2[i] = schematic.param2_data[j]
end end