techage_modpack/signs_bot/cmd_flowers.lua

127 lines
2.9 KiB
Lua
Raw Normal View History

2020-05-31 23:31:18 +03:00
--[[
Signs Bot
=========
Copyright (C) 2019-2020 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
Bot flower cutting command
]]--
2021-05-14 19:50:16 +03:00
-- Load support for I18n.
local S = signs_bot.S
2020-05-31 23:31:18 +03:00
local lib = signs_bot.lib
local bot_inv_put_item = signs_bot.bot_inv_put_item
local bot_inv_take_item = signs_bot.bot_inv_take_item
local Flowers = {}
2020-10-25 23:32:47 +03:00
-- Special drop handling is necessary because of waterlily.
2020-05-31 23:31:18 +03:00
function signs_bot.register_flower(name)
2020-10-25 23:32:47 +03:00
local drop = signs_bot.lib.is_simple_node({name = name})
if drop then
Flowers[name] = drop
end
2020-05-31 23:31:18 +03:00
end
minetest.after(1, function()
2020-09-13 14:24:06 +03:00
for _,def in pairs(minetest.registered_decorations) do
local name = def.decoration
if name and type(name) == "string" then
2020-07-21 18:45:15 +03:00
local mod = string.split(name, ":")[1]
2020-09-13 14:24:06 +03:00
if mod == "flowers" or mod == "bakedclay" then -- Bakedclay also registers flowers as decoration.
2020-05-31 23:31:18 +03:00
signs_bot.register_flower(name)
end
end
end
end)
2021-05-14 19:50:16 +03:00
local function is_tree(node)
if minetest.get_item_group(node.name, "tree") == 1 then
return signs_bot.handle_drop_like_a_player(node)
end
if minetest.get_item_group(node.name, "leaves") == 1 then
return signs_bot.handle_drop_like_a_player(node)
end
end
2020-05-31 23:31:18 +03:00
local function harvesting(base_pos, mem)
local pos = mem.pos_tbl and mem.pos_tbl[mem.steps]
mem.steps = (mem.steps or 1) + 1
if pos and lib.not_protected(base_pos, pos) then
local node = minetest.get_node_or_nil(pos)
2021-05-14 19:50:16 +03:00
local drop = Flowers[node.name] or is_tree(node)
2020-10-25 23:32:47 +03:00
if drop then
2020-05-31 23:31:18 +03:00
minetest.remove_node(pos)
2021-05-14 19:50:16 +03:00
local leftover = bot_inv_put_item(base_pos, 0, ItemStack(drop))
if leftover and leftover:get_count() > 0 then
signs_bot.lib.drop_items(mem.robot_pos, leftover)
end
2020-05-31 23:31:18 +03:00
end
end
end
signs_bot.register_botcommand("cutting", {
mod = "farming",
params = "",
num_param = 0,
2021-05-14 19:50:16 +03:00
description = S("Cutting flowers, leaves and tree blocks\nin front of the robot\non a 3x3 field."),
2020-05-31 23:31:18 +03:00
cmnd = function(base_pos, mem)
if not mem.steps then
mem.pos_tbl = signs_bot.lib.gen_position_table(mem.robot_pos, mem.robot_param2, 3, 3, 0)
mem.steps = 1
end
mem.pos_tbl = mem.pos_tbl or {}
harvesting(base_pos, mem)
if mem.steps > #mem.pos_tbl then
mem.steps = nil
return signs_bot.DONE
end
return signs_bot.BUSY
end,
})
local CMD = [[dig_sign 1
move
cutting
backward
place_sign 1
turn_around]]
signs_bot.register_sign({
name = "flowers",
2021-05-14 19:50:16 +03:00
description = S('Sign "flowers"'),
2020-05-31 23:31:18 +03:00
commands = CMD,
image = "signs_bot_sign_flowers.png",
})
minetest.register_craft({
output = "signs_bot:flowers 2",
recipe = {
{"group:wood", "default:stick", "group:wood"},
{"dye:black", "default:stick", "dye:yellow"},
{"dye:red", "", ""}
}
})
if minetest.get_modpath("doc") then
doc.add_entry("signs_bot", "flowers", {
2021-05-14 19:50:16 +03:00
name = S("Sign 'flowers'"),
2020-05-31 23:31:18 +03:00
data = {
item = "signs_bot:flowers",
text = table.concat({
2021-05-14 19:50:16 +03:00
S("Used to cut flowers on a 3x3 field."),
S("Place the sign in front of the field."),
S("When finished, the bot turns."),
2020-05-31 23:31:18 +03:00
}, "\n")
},
})
end