scaffold: improve API, simplify scaffolds
This commit is contained in:
parent
83840da8f8
commit
929253e84c
@ -1,3 +1,5 @@
|
|||||||
|
-- CC0/Unlicense Emilia 2020
|
||||||
|
|
||||||
scaffold = {}
|
scaffold = {}
|
||||||
scaffold.registered_scaffolds = {}
|
scaffold.registered_scaffolds = {}
|
||||||
|
|
||||||
@ -12,9 +14,7 @@ function scaffold.step_scaffolds()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function scaffold.template(setting, func, offset)
|
function scaffold.template(setting, func, offset)
|
||||||
if not offset then
|
offset = offset or {x = 0, y = -1, z = 0}
|
||||||
offset = {x = 0, y = -1, z = 0}
|
|
||||||
end
|
|
||||||
|
|
||||||
return function()
|
return function()
|
||||||
if minetest.settings:get_bool(setting) then
|
if minetest.settings:get_bool(setting) then
|
||||||
@ -25,12 +25,64 @@ function scaffold.template(setting, func, offset)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function scaffold.register_template_scaffold(setting, func, offset)
|
function scaffold.register_template_scaffold(name, setting, func, offset)
|
||||||
scaffold.register_scaffold(scaffold.template(setting, func, offset))
|
scaffold.register_scaffold(scaffold.template(setting, func, offset))
|
||||||
|
if minetest.register_cheat then
|
||||||
|
minetest.register_cheat(name, "Scaffold", setting)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_globalstep(scaffold.step_scaffolds)
|
minetest.register_globalstep(scaffold.step_scaffolds)
|
||||||
|
|
||||||
|
function scaffold.can_place_at(pos)
|
||||||
|
local node = minetest.get_node_or_nil(pos)
|
||||||
|
return (node and (node.name == "air" or minetest.get_node_def(node.name).buildable_to))
|
||||||
|
end
|
||||||
|
|
||||||
|
function scaffold.find_any_swap(items)
|
||||||
|
for i, v in ipairs(items) do
|
||||||
|
local n = minetest.find_item(v)
|
||||||
|
if n then
|
||||||
|
minetest.localplayer:set_wield_index(n - 1)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function scaffold.in_list(val, list)
|
||||||
|
for i, v in ipairs(list) do
|
||||||
|
if v == val then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
-- swaps to any of the items and places if need be
|
||||||
|
-- returns true if placed and in inventory or already there, false otherwise
|
||||||
|
function scaffold.place_if_needed(items, pos, place)
|
||||||
|
place = place or minetest.place_node
|
||||||
|
|
||||||
|
local node = minetest.get_node_or_nil(pos)
|
||||||
|
|
||||||
|
-- already there
|
||||||
|
if node and scaffold.in_list(node.name, items) then
|
||||||
|
return true
|
||||||
|
else
|
||||||
|
local swapped = scaffold.find_any_swap(items)
|
||||||
|
|
||||||
|
-- need to place
|
||||||
|
if swapped and scaffold.can_place_at(pos) then
|
||||||
|
place(pos)
|
||||||
|
return true
|
||||||
|
-- can't place
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local mpath = minetest.get_modpath(minetest.get_current_modname())
|
local mpath = minetest.get_modpath(minetest.get_current_modname())
|
||||||
dofile(mpath .. "/sapscaffold.lua")
|
dofile(mpath .. "/sapscaffold.lua")
|
||||||
dofile(mpath .. "/slowscaffold.lua")
|
dofile(mpath .. "/slowscaffold.lua")
|
||||||
|
@ -1,33 +1,10 @@
|
|||||||
local function find_swap(items)
|
-- CC0/Unlicense Emilia 2020
|
||||||
for i, v in ipairs(items) do
|
|
||||||
local n = minetest.find_item(v)
|
|
||||||
if n then
|
|
||||||
minetest.localplayer:set_wield_index(n - 1)
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
local function in_list(val, list)
|
|
||||||
for i, v in ipairs(list) do
|
|
||||||
if v == val then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
local function nilify_node(node)
|
|
||||||
if node and (node.name == "air" or minetest.get_node_def(node.name).buildable_to) then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
return node or {name = ""}
|
|
||||||
end
|
|
||||||
|
|
||||||
local dirt = {
|
local dirt = {
|
||||||
"mcl_core:dirt",
|
"mcl_core:dirt",
|
||||||
"mcl_core:dirt_with_grass"
|
"mcl_core:dirt_with_grass",
|
||||||
|
"mcl_core:dirt_with_grass_snow",
|
||||||
|
"mcl_core:podzol"
|
||||||
}
|
}
|
||||||
|
|
||||||
local saplings = {
|
local saplings = {
|
||||||
@ -39,27 +16,10 @@ local saplings = {
|
|||||||
"mcl_core:acaciasapling"
|
"mcl_core:acaciasapling"
|
||||||
}
|
}
|
||||||
|
|
||||||
local function sapper(below)
|
scaffold.register_template_scaffold("SapScaffold", "scaffold_saplings", function(below)
|
||||||
local lp = vector.round(minetest.localplayer:get_pos())
|
local lp = vector.round(minetest.localplayer:get_pos())
|
||||||
local node_here = nilify_node(minetest.get_node_or_nil(lp))
|
|
||||||
local node_under = nilify_node(minetest.get_node_or_nil(below))
|
|
||||||
|
|
||||||
-- if theres a node below its prob bad but its good if its dirt
|
if scaffold.place_if_needed(dirt, below) then
|
||||||
if node_under and not in_list(node_under.name, dirt) then
|
scaffold.place_if_needed(saplings, lp)
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
end)
|
||||||
if not node_under and find_swap(dirt) then
|
|
||||||
minetest.place_node(below)
|
|
||||||
end
|
|
||||||
|
|
||||||
if not node_here and find_swap(saplings) then
|
|
||||||
minetest.place_node(lp)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
scaffold.register_template_scaffold("scaffold_saplings", sapper)
|
|
||||||
|
|
||||||
if minetest.register_cheat then
|
|
||||||
minetest.register_cheat("SapScaffold", "Scaffold", "scaffold_saplings")
|
|
||||||
end
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
-- CC0/Unlicense Emilia 2020
|
||||||
|
|
||||||
if minetest.settings:get("slow_blocks_per_second") == nil then
|
if minetest.settings:get("slow_blocks_per_second") == nil then
|
||||||
minetest.settings:set("slow_blocks_per_second", 8)
|
minetest.settings:set("slow_blocks_per_second", 8)
|
||||||
end
|
end
|
||||||
@ -43,31 +45,22 @@ local function place(pos)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function can_place_at(pos)
|
|
||||||
local node = minetest.get_node_or_nil(pos)
|
|
||||||
return (node and (node.name == "air" or minetest.get_node_def(node.name).buildable_to))
|
|
||||||
end
|
|
||||||
|
|
||||||
-- should check if wield is placeable
|
-- should check if wield is placeable
|
||||||
-- minetest.get_node(wielded:get_name()) ~= nil should probably work
|
-- minetest.get_node(wielded:get_name()) ~= nil should probably work
|
||||||
|
-- otherwise it equips armor and eats food
|
||||||
local function can_place(pos)
|
local function can_place(pos)
|
||||||
local wield_empty = minetest.localplayer:get_wielded_item():is_empty()
|
local wield_empty = minetest.localplayer:get_wielded_item():is_empty()
|
||||||
return not wield_empty and can_place_at(pos)
|
return not wield_empty and scaffold.can_place_at(pos)
|
||||||
end
|
end
|
||||||
|
|
||||||
scaffold.register_template_scaffold("scaffold_slow", function(pos)
|
scaffold.register_template_scaffold("SlowScaffold", "scaffold_slow", function(pos)
|
||||||
if can_place(pos) then
|
if can_place(pos) then
|
||||||
place(pos)
|
place(pos)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
scaffold.register_template_scaffold("scaffold_check", function(pos)
|
scaffold.register_template_scaffold("CheckScaffold", "scaffold_check", function(pos)
|
||||||
if can_place(pos) then
|
if can_place(pos) then
|
||||||
minetest.place_node(pos)
|
minetest.place_node(pos)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
if minetest.register_cheat then
|
|
||||||
minetest.register_cheat("SlowScaffold", "Scaffold", "scaffold_slow")
|
|
||||||
minetest.register_cheat("CheckScaffold", "Scaffold", "scaffold_check")
|
|
||||||
end
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user