techage_modpack/compost/init.lua

308 lines
8.1 KiB
Lua
Raw Permalink Normal View History

2020-08-08 13:52:38 +03:00
local S = minetest.get_translator("compost")
2020-05-31 23:31:18 +03:00
compost = {}
2021-05-14 19:50:16 +03:00
local CYCLE_TIME = 30
2021-05-14 20:03:22 +03:00
local NUM_LEAVES = 2
2021-02-07 16:37:07 +03:00
2020-05-31 23:31:18 +03:00
-- Version for compatibility checks
compost.version = 1.0
if minetest.global_exists("techage") and techage.version < 0.06 then
minetest.log("error", "[compost] Compost requires techage version 0.06 or newer!")
return
end
compost.items = {}
compost.groups = {}
function compost.register_item(name)
compost.items[name] = true
end
function compost.register_group(name)
compost.groups[name] = true
end
function compost.can_compost(name)
if compost.items[name] then
return true
else
for k, i in pairs(minetest.registered_items[name].groups) do
if i > 0 then
if compost.groups[tostring(k)] then
return true
end
end
end
return false
end
end
-- grass
compost.register_item("default:grass_1")
compost.register_item("default:junglegrass")
-- leaves
compost.register_group("leaves")
-- dirt
compost.register_item("default:dirt")
compost.register_item("default:dirt_with_grass")
-- stick
compost.register_item("default:stick")
-- food
compost.register_item("farming:bread")
compost.register_item("farming:wheat")
-- groups
compost.register_group("plant")
compost.register_group("flower")
-- flowers
minetest.after(1, function()
for name,_ in pairs(minetest.registered_decorations) do
if type(name) == "string" then
local mod = string.split(name, ":")[1]
if mod == "flowers" then
compost.register_item(name)
end
end
end
end)
local function next_state(pos, elapsed)
local node = minetest.get_node(pos)
2023-11-05 15:18:27 +03:00
2020-05-31 23:31:18 +03:00
if node.name == "compost:wood_barrel_1" then
2021-02-07 16:37:07 +03:00
minetest.swap_node(pos, {name = "compost:wood_barrel_2"})
elseif node.name == "compost:wood_barrel_2" then
minetest.swap_node(pos, {name = "compost:wood_barrel_3"})
elseif node.name == "compost:wood_barrel_3" then
2020-05-31 23:31:18 +03:00
return false
end
2021-02-07 16:37:07 +03:00
return true
2020-05-31 23:31:18 +03:00
end
2021-02-07 16:37:07 +03:00
local function start_composter(pos)
local meta = minetest.get_meta(pos)
local num = meta:get_int("num") or 0
2021-05-14 20:03:22 +03:00
if num >= NUM_LEAVES then
-- NUM_LEAVES leaves for one compost node
meta:set_int("num", num - NUM_LEAVES)
2021-02-07 16:37:07 +03:00
minetest.swap_node(pos, {name = "compost:wood_barrel_1"})
minetest.get_node_timer(pos):start(CYCLE_TIME)
end
end
local function add_item(pos, stack)
local meta = minetest.get_meta(pos)
local num = meta:get_int("num") or 0
2023-11-05 15:18:27 +03:00
2021-05-14 20:03:22 +03:00
if num < NUM_LEAVES then
2021-02-07 16:37:07 +03:00
-- add futher leaves
meta:set_int("num", num + stack:get_count())
2020-05-31 23:31:18 +03:00
stack:set_count(0)
2021-02-07 16:37:07 +03:00
end
2023-11-05 15:18:27 +03:00
2021-02-07 16:37:07 +03:00
start_composter(pos)
return stack
end
local function minecart_hopper_additem(pos, stack)
if compost.can_compost(stack:get_name()) then
return add_item(pos, stack)
2020-05-31 23:31:18 +03:00
end
return stack
end
local function minecart_hopper_takeitem(pos, num)
2021-02-07 16:37:07 +03:00
minetest.swap_node(pos, {name = "compost:wood_barrel"})
start_composter(pos)
2020-05-31 23:31:18 +03:00
return ItemStack("compost:compost")
end
local function minecart_hopper_untakeitem(pos, in_dir, stack)
2021-02-07 16:37:07 +03:00
minetest.swap_node(pos, {name = "compost:wood_barrel_2"})
2020-05-31 23:31:18 +03:00
end
minetest.register_node("compost:wood_barrel", {
2020-08-08 13:52:38 +03:00
description = S("Wood Barrel"),
2020-05-31 23:31:18 +03:00
tiles = {"default_wood.png"},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {{-1/2, -1/2, -1/2, 1/2, -3/8, 1/2},
{-1/2, -1/2, -1/2, -3/8, 1/2, 1/2},
{3/8, -1/2, -1/2, 1/2, 1/2, 1/2},
{-1/2, -1/2, -1/2, 1/2, 1/2, -3/8},
{-1/2, -1/2, 3/8, 1/2, 1/2, 1/2}},
},
paramtype = "light",
is_ground_content = false,
groups = {choppy = 3},
sounds = default.node_sound_wood_defaults(),
on_punch = function(pos, node, puncher, pointed_thing)
local wielded_item = puncher:get_wielded_item():get_name()
if compost.can_compost(wielded_item) then
2021-02-07 16:37:07 +03:00
minetest.swap_node(pos, {name = "compost:wood_barrel_1"})
2020-05-31 23:31:18 +03:00
local w = puncher:get_wielded_item()
2023-11-05 15:18:27 +03:00
if not minetest.is_creative_enabled(puncher:get_player_name()) then
2020-05-31 23:31:18 +03:00
w:take_item(1)
puncher:set_wielded_item(w)
end
2021-02-07 16:37:07 +03:00
minetest.get_node_timer(pos):start(CYCLE_TIME)
2020-05-31 23:31:18 +03:00
end
end,
2021-02-07 16:37:07 +03:00
on_timer = next_state,
2020-05-31 23:31:18 +03:00
minecart_hopper_additem = minecart_hopper_additem,
minecart_hopper_untakeitem = minecart_hopper_untakeitem,
})
minetest.register_node("compost:wood_barrel_1", {
2020-08-08 13:52:38 +03:00
description = S("Wood Barrel with compost"),
2020-05-31 23:31:18 +03:00
tiles = {"default_wood.png^compost_compost_1.png", "default_wood.png"},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {{-1/2, -1/2, -1/2, 1/2, -3/8, 1/2},
{-1/2, -1/2, -1/2, -3/8, 1/2, 1/2},
{3/8, -1/2, -1/2, 1/2, 1/2, 1/2},
{-1/2, -1/2, -1/2, 1/2, 1/2, -3/8},
{-1/2, -1/2, 3/8, 1/2, 1/2, 1/2},
{-3/8, -1/2, -3/8, 3/8, 3/8, 3/8}},
},
paramtype = "light",
is_ground_content = false,
groups = {choppy = 3, not_in_creative_inventory=1},
2023-11-05 15:18:27 +03:00
drop = "compost:wood_barrel",
2020-05-31 23:31:18 +03:00
sounds = default.node_sound_wood_defaults(),
on_timer = next_state,
minecart_hopper_untakeitem = minecart_hopper_untakeitem,
})
minetest.register_node("compost:wood_barrel_2", {
2020-08-08 13:52:38 +03:00
description = S("Wood Barrel with compost"),
2020-05-31 23:31:18 +03:00
tiles = {"default_wood.png^compost_compost_2.png", "default_wood.png"},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {{-1/2, -1/2, -1/2, 1/2, -3/8, 1/2},
{-1/2, -1/2, -1/2, -3/8, 1/2, 1/2},
{3/8, -1/2, -1/2, 1/2, 1/2, 1/2},
{-1/2, -1/2, -1/2, 1/2, 1/2, -3/8},
{-1/2, -1/2, 3/8, 1/2, 1/2, 1/2},
{-3/8, -1/2, -3/8, 3/8, 3/8, 3/8}},
},
paramtype = "light",
is_ground_content = false,
groups = {choppy = 3, not_in_creative_inventory=1},
2023-11-05 15:18:27 +03:00
drop = "compost:wood_barrel",
2020-05-31 23:31:18 +03:00
sounds = default.node_sound_wood_defaults(),
on_timer = next_state,
minecart_hopper_untakeitem = minecart_hopper_untakeitem,
})
minetest.register_node("compost:wood_barrel_3", {
2020-08-08 13:52:38 +03:00
description = S("Wood Barrel"),
2020-05-31 23:31:18 +03:00
tiles = {"default_wood.png^compost_compost_3.png", "default_wood.png"},
drawtype = "nodebox",
node_box = {
type = "fixed",
fixed = {{-1/2, -1/2, -1/2, 1/2, -3/8, 1/2},
{-1/2, -1/2, -1/2, -3/8, 1/2, 1/2},
{3/8, -1/2, -1/2, 1/2, 1/2, 1/2},
{-1/2, -1/2, -1/2, 1/2, 1/2, -3/8},
{-1/2, -1/2, 3/8, 1/2, 1/2, 1/2},
{-3/8, -1/2, -3/8, 3/8, 3/8, 3/8}},
},
paramtype = "light",
is_ground_content = false,
groups = {choppy = 3, not_in_creative_inventory=1},
2023-11-05 15:18:27 +03:00
drop = "compost:wood_barrel",
2020-05-31 23:31:18 +03:00
sounds = default.node_sound_wood_defaults(),
on_punch = function(pos, node, player, pointed_thing)
local p = {x = pos.x + math.random(0, 5)/5 - 0.5, y = pos.y+1, z = pos.z + math.random(0, 5)/5 - 0.5}
minetest.add_item(p, {name = "compost:compost"})
2021-02-07 16:37:07 +03:00
minetest.swap_node(pos, {name = "compost:wood_barrel"})
2020-05-31 23:31:18 +03:00
end,
2021-02-07 16:37:07 +03:00
on_timer = next_state,
2020-05-31 23:31:18 +03:00
minecart_hopper_takeitem = minecart_hopper_takeitem,
minecart_hopper_untakeitem = minecart_hopper_untakeitem,
})
minetest.register_craft({
output = "compost:wood_barrel",
recipe = {
{"default:wood", "", "default:wood"},
{"default:wood", "", "default:wood"},
{"default:wood", "stairs:slab_wood", "default:wood"}
}
})
minetest.register_node("compost:compost", {
2020-08-08 13:52:38 +03:00
description = S("Compost"),
2020-05-31 23:31:18 +03:00
tiles = {"compost_compost.png"},
groups = {crumbly = 3},
sounds = default.node_sound_dirt_defaults(),
})
minetest.register_node("compost:garden_soil", {
2020-08-08 13:52:38 +03:00
description = S("Garden Soil"),
2020-05-31 23:31:18 +03:00
tiles = {"compost_garden_soil.png"},
groups = {crumbly = 3, soil=3, grassland = 1, wet = 1},
sounds = default.node_sound_dirt_defaults(),
})
minetest.register_craft({
output = "compost:garden_soil",
recipe = {
{"compost:compost", "compost:compost"},
}
})
minetest.register_craft({
output = "default:dirt",
recipe = {
{"compost:garden_soil"},
}
})
if minetest.global_exists("techage") then
techage.register_node(
{
2023-11-05 15:18:27 +03:00
"compost:wood_barrel",
2020-05-31 23:31:18 +03:00
"compost:wood_barrel_1",
"compost:wood_barrel_2",
"compost:wood_barrel_3",
},
{
on_pull_item = function(pos, in_dir, num)
local node = minetest.get_node(pos)
if node.name == "compost:wood_barrel_3" then
2021-02-07 16:37:07 +03:00
minetest.swap_node(pos, {name = "compost:wood_barrel"})
start_composter(pos)
2020-05-31 23:31:18 +03:00
return ItemStack("compost:compost")
end
return nil
end,
on_push_item = function(pos, in_dir, stack)
local node = minetest.get_node(pos)
if node.name == "compost:wood_barrel" and compost.can_compost(stack:get_name()) then
2021-02-07 16:37:07 +03:00
stack = add_item(pos, stack)
return stack:get_count() == 0
2020-05-31 23:31:18 +03:00
end
return false
end,
on_unpull_item = function(pos, in_dir, stack)
2021-02-07 16:37:07 +03:00
minetest.swap_node(pos, {name = "compost:wood_barrel_2"})
2020-05-31 23:31:18 +03:00
return true
end,
2023-11-05 15:18:27 +03:00
})
2020-05-31 23:31:18 +03:00
end