Leaf decay, woodcraft wears tools.

This commit is contained in:
Aaron Suen 2019-01-05 23:11:38 -05:00
parent 6d5432bd6b
commit e2dd2de5ba
10 changed files with 163 additions and 28 deletions

View File

@ -1,6 +1,6 @@
-- LUALOCALS < ---------------------------------------------------------
local minetest, nodecore, vector
= minetest, nodecore, vector
= minetest, nodecore, vector
-- LUALOCALS > ---------------------------------------------------------
local pummeling = {}

View File

@ -1,8 +1,8 @@
-- LUALOCALS < ---------------------------------------------------------
local ipairs, math, minetest, nodecore, pairs, type
= ipairs, math, minetest, nodecore, pairs, type
local math_random
= math.random
local ipairs, math, minetest, next, nodecore, pairs, table, type
= ipairs, math, minetest, next, nodecore, pairs, table, type
local math_random, table_insert
= math.random, table.insert
-- LUALOCALS > ---------------------------------------------------------
for k, v in pairs(minetest) do
@ -23,9 +23,14 @@ end
local node_is_skip = {name = true, param2 = true, param = true, groups = true}
function nodecore.node_is(node_or_pos, match)
if not node_or_pos.name then
local p = { }
for k, v in pairs(minetest.get_node(node_or_pos)) do
node_or_pos[k] = v
p[k] = v
end
for k, v in pairs(node_or_pos) do
p[k] = v
end
node_or_pos = p
end
while type(match) == "function" do
match = match(node_or_pos)
@ -93,3 +98,74 @@ function nodecore.wieldgroup(who, group)
local caps = wielded and wielded:get_tool_capabilities()
return caps and caps.groupcaps and caps.groupcaps[group]
end
local dirs = {
{x = 1, y = 0, z = 0},
{x = -1, y = 0, z = 0},
{x = 0, y = 1, z = 0},
{x = 0, y = -1, z = 0},
{x = 0, y = 0, z = 1},
{x = 0, y = 0, z = -1},
}
function nodecore.scan_flood(pos, range, func)
local q = {pos}
local seen = { }
for d = 0, range do
local next = {}
for i, p in ipairs(q) do
local res = func(p)
if res then return res end
if res == nil then
for k, v in pairs(dirs) do
local np = {
x = p.x + v.x,
y = p.y + v.y,
z = p.z + v.z
}
local nk = minetest.hash_node_position(np)
if not seen[nk] then
seen[nk] = true
np.dir = v
table_insert(next, np)
end
end
end
end
if #next < 1 then break end
for i = 1, #next do
local j = math_random(1, #next)
next[i], next[j] = next[j], next[i]
end
q = next
end
end
function nodecore.interval(after, func)
local function go()
minetest.after(after, go)
return func()
end
minetest.after(after, go)
end
function nodecore.wear_current_tool(player, groups, qty)
local wielded = player:get_wielded_item()
if wielded then
local wdef = wielded:get_definition()
local tp = wielded:get_tool_capabilities()
local dp = minetest.get_dig_params(groups, tp)
if wdef and wdef.after_use then
wielded = wdef.after_use(wielded, player, nil, dp) or wielded
else
if not minetest.settings:get_bool("creative_mode") then
wielded:add_wear(dp.wear * (qty or 1))
if wielded:get_count() <= 0 and wdef.sound
and wdef.sound.breaks then
minetest.sound_play(wdef.sound.breaks,
{pos = pos, gain = 0.5})
end
end
end
player:set_wielded_item(wielded)
end
end

View File

@ -1,6 +1,6 @@
-- LUALOCALS < ---------------------------------------------------------
local ItemStack, minetest, nodecore, setmetatable, vector
= ItemStack, minetest, nodecore, setmetatable, vector
local ItemStack, minetest, nodecore, setmetatable, type, vector
= ItemStack, minetest, nodecore, setmetatable, type, vector
-- LUALOCALS > ---------------------------------------------------------
local modname = minetest.get_current_modname()

View File

@ -1,6 +1,6 @@
-- LUALOCALS < ---------------------------------------------------------
local ipairs, minetest, pairs
= ipairs, minetest, pairs
= ipairs, minetest, pairs
-- LUALOCALS > ---------------------------------------------------------
local modname = minetest.get_current_modname()

19
mods/nc_tree/api.lua Normal file
View File

@ -0,0 +1,19 @@
-- LUALOCALS < ---------------------------------------------------------
local ipairs, minetest, nodecore
= ipairs, minetest, nodecore
-- LUALOCALS > ---------------------------------------------------------
nodecore.register_leaf_drops, nodecore.registered_leaf_drops
= nodecore.mkreg()
function nodecore.leaf_decay(pos, node)
node = node or minetest.get_node(pos)
local t = {}
for i, v in ipairs(nodecore.registered_leaf_drops) do
t = v(pos, node, t) or t
end
local p = nodecore.pickrand(t, function(x) return x.prob end)
if not p then return end
minetest.set_node(pos, p)
return minetest.check_for_falling(pos)
end

View File

@ -1,15 +1,14 @@
-- LUALOCALS < ---------------------------------------------------------
local dofile, minetest, nodecore
= dofile, minetest, nodecore
local dofile, minetest
= dofile, minetest
-- LUALOCALS > ---------------------------------------------------------
nodecore.register_leaf_drops, nodecore.registered_leaf_drops
= nodecore.mkreg()
local modname = minetest.get_current_modname()
local path = minetest.get_modpath(modname)
dofile(path .. "/api.lua")
dofile(path .. "/node.lua")
dofile(path .. "/leafdecay.lua")
dofile(path .. "/stick.lua")

View File

@ -0,0 +1,46 @@
-- LUALOCALS < ---------------------------------------------------------
local ipairs, math, minetest, nodecore
= ipairs, math, minetest, nodecore
local math_random
= math.random
-- LUALOCALS > ---------------------------------------------------------
local modname = minetest.get_current_modname()
local seen = {}
local q = {}
minetest.register_abm({
label = "Leaf Decay",
interval = 1,
chance = 10,
nodenames = {modname .. ":leaves"},
action = function(pos)
local h = minetest.hash_node_position(pos)
if seen[h] then return end
if #q < 100 then
q[#q + 1] = pos
else
q[math_random(1, #q)] = pos
end
seen[h] = true
end
})
nodecore.interval(1, function()
for _, pos in ipairs(q) do
if not nodecore.scan_flood(pos, 5, function(p)
if nodecore.node_is(p, modname .. ":tree") then
return true
end
if nodecore.node_is(p, modname .. ":leaves") then
return
end
return false
end) then
nodecore.leaf_decay(pos)
end
end
q = {}
seen = {}
end)

View File

@ -1,6 +1,6 @@
-- LUALOCALS < ---------------------------------------------------------
local ipairs, minetest, nodecore, type
= ipairs, minetest, nodecore, type
local minetest, nodecore, type
= minetest, nodecore, type
-- LUALOCALS > ---------------------------------------------------------
local modname = minetest.get_current_modname()
@ -41,15 +41,8 @@ minetest.register_node(modname .. ":leaves", {
}
},
alternate_solid = {
after_dig_node = function(pos, node)
node = node or minetest.get_node(pos)
local t = {}
for i, v in ipairs(nodecore.registered_leaf_drops) do
t = v(pos, node, t) or t
end
local p = nodecore.pickrand(t, function(x) return x.prob end)
if not p then return end
minetest.place_node(pos, p)
after_dig_node = function(...)
return nodecore.leaf_decay(...)
end
}
})

View File

@ -1,6 +1,6 @@
-- LUALOCALS < ---------------------------------------------------------
local ItemStack, ipairs, minetest, nodecore
= ItemStack, ipairs, minetest, nodecore
= ItemStack, ipairs, minetest, nodecore
-- LUALOCALS > ---------------------------------------------------------
local modname = minetest.get_current_modname()
@ -38,5 +38,6 @@ nodecore.extend_pummel("nc_tree:tree",
if defer > 0 then
minetest.item_drop(ItemStack(plank .. " " .. defer), nil, pos)
end
nodecore.wear_current_tool(stats.puncher, {choppy = 3}, 2)
return true
end)

View File

@ -1,6 +1,6 @@
-- LUALOCALS < ---------------------------------------------------------
local ItemStack, minetest, nodecore
= ItemStack, minetest, nodecore
= ItemStack, minetest, nodecore
-- LUALOCALS > ---------------------------------------------------------
local modname = minetest.get_current_modname()
@ -54,6 +54,7 @@ local function toolhead(name, from, group, sticks)
if sticks then
minetest.item_drop(ItemStack("nc_tree:stick " .. sticks),
nil, {x = pos.x, y = pos.y + 1, z = pos.z})
nodecore.wear_current_tool(stats.puncher, {choppy = 3})
end
return true
end)
@ -63,4 +64,4 @@ toolhead("Mallet", modname .. ":plank", "poundy", 2)
toolhead("Spade", modname .. ":toolhead_mallet", "crumbly", 1)
toolhead("Hatchet", modname .. ":toolhead_spade", "choppy", 1)
toolhead("Pick", modname .. ":toolhead_hatchet", "cracky", 2)
toolhead(nil, modname.. ":toolhead_pick", nil, 2)
toolhead(nil, modname.. ":toolhead_pick", nil, 2)