From 929253e84c478fb06e482d5f9406895dcfefce58 Mon Sep 17 00:00:00 2001 From: cron Date: Sat, 21 Nov 2020 18:29:37 +0000 Subject: [PATCH] scaffold: improve API, simplify scaffolds --- clientmods/scaffold/init.lua | 60 ++++++++++++++++++++++++++-- clientmods/scaffold/sapscaffold.lua | 56 ++++---------------------- clientmods/scaffold/slowscaffold.lua | 19 +++------ 3 files changed, 70 insertions(+), 65 deletions(-) diff --git a/clientmods/scaffold/init.lua b/clientmods/scaffold/init.lua index 859f8d649..ccb787012 100644 --- a/clientmods/scaffold/init.lua +++ b/clientmods/scaffold/init.lua @@ -1,3 +1,5 @@ +-- CC0/Unlicense Emilia 2020 + scaffold = {} scaffold.registered_scaffolds = {} @@ -12,9 +14,7 @@ function scaffold.step_scaffolds() end function scaffold.template(setting, func, offset) - if not offset then - offset = {x = 0, y = -1, z = 0} - end + offset = offset or {x = 0, y = -1, z = 0} return function() if minetest.settings:get_bool(setting) then @@ -25,12 +25,64 @@ function scaffold.template(setting, func, offset) 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)) + if minetest.register_cheat then + minetest.register_cheat(name, "Scaffold", setting) + end end 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()) dofile(mpath .. "/sapscaffold.lua") dofile(mpath .. "/slowscaffold.lua") diff --git a/clientmods/scaffold/sapscaffold.lua b/clientmods/scaffold/sapscaffold.lua index 35288da29..65d2a9c83 100644 --- a/clientmods/scaffold/sapscaffold.lua +++ b/clientmods/scaffold/sapscaffold.lua @@ -1,33 +1,10 @@ -local function find_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 - -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 +-- CC0/Unlicense Emilia 2020 local 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 = { @@ -39,27 +16,10 @@ local saplings = { "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 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 node_under and not in_list(node_under.name, dirt) then - return + if scaffold.place_if_needed(dirt, below) then + scaffold.place_if_needed(saplings, lp) 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 +end) diff --git a/clientmods/scaffold/slowscaffold.lua b/clientmods/scaffold/slowscaffold.lua index d9031bc2d..9842fa9a7 100644 --- a/clientmods/scaffold/slowscaffold.lua +++ b/clientmods/scaffold/slowscaffold.lua @@ -1,3 +1,5 @@ +-- CC0/Unlicense Emilia 2020 + if minetest.settings:get("slow_blocks_per_second") == nil then minetest.settings:set("slow_blocks_per_second", 8) end @@ -43,31 +45,22 @@ local function place(pos) 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 -- minetest.get_node(wielded:get_name()) ~= nil should probably work +-- otherwise it equips armor and eats food local function can_place(pos) 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 -scaffold.register_template_scaffold("scaffold_slow", function(pos) +scaffold.register_template_scaffold("SlowScaffold", "scaffold_slow", function(pos) if can_place(pos) then place(pos) end end) -scaffold.register_template_scaffold("scaffold_check", function(pos) +scaffold.register_template_scaffold("CheckScaffold", "scaffold_check", function(pos) if can_place(pos) then minetest.place_node(pos) end end) - -if minetest.register_cheat then - minetest.register_cheat("SlowScaffold", "Scaffold", "scaffold_slow") - minetest.register_cheat("CheckScaffold", "Scaffold", "scaffold_check") -end