forked from joe7575/signs_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_flowers.lua
More file actions
155 lines (134 loc) · 3.79 KB
/
cmd_flowers.lua
File metadata and controls
155 lines (134 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
--[[
Signs Bot
=========
Copyright (C) 2019-2020 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
Bot flower cutting command
]]--
-- Load support for I18n.
local S = signs_bot.S
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 = {}
-- Special drop handling is necessary because of waterlily.
function signs_bot.register_flower(name)
local drop = signs_bot.lib.is_simple_node({name = name})
if drop then
Flowers[name] = drop
end
end
signs_bot.register_flower("default:bush_stem")
signs_bot.register_flower("default:acacia_bush_stem")
signs_bot.register_flower("default:pine_bush_stem")
minetest.after(1, function()
local function add_flower(name)
local def = minetest.registered_nodes[name]
if def and (def.groups.mushroom == 1 or def.groups.flower == 1) then
signs_bot.register_flower(name)
end
end
for _,def in pairs(minetest.registered_decorations) do
local name = def.decoration
if type(name) == "string" then
add_flower(name)
elseif type(name) == "table" then
for _,sub_name in ipairs(name) do
add_flower(sub_name)
end
end
end
end)
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
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)
if node.name ~= "default:papyrus" then
local drop = Flowers[node.name] or is_tree(node)
if drop then
minetest.remove_node(pos)
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
end
else
-- papyrus is a special plant that is collected upwards when cut
local count = 0
while node.name == "default:papyrus" and lib.not_protected(base_pos, pos) do
minetest.remove_node(pos)
pos = { x = pos.x, y = pos.y + 1, z = pos.z }
count = count + 1
node = minetest.get_node(pos)
end
if count > 0 then
local leftover = bot_inv_put_item(base_pos, 0, ItemStack("default:papyrus " .. count))
if leftover and leftover:get_count() > 0 then
signs_bot.lib.drop_items(mem.robot_pos, leftover)
end
end
end
end
end
signs_bot.register_botcommand("cutting", {
mod = "farming",
params = "",
num_param = 0,
description = S("Cutting flowers, papyrus,\nleaves and tree blocks\nin front of the robot\non a 3x3 field."),
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",
description = S('Sign "flowers"'),
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", {
name = S("Sign 'flowers'"),
data = {
item = "signs_bot:flowers",
text = table.concat({
S("Used to cut flowers on a 3x3 field."),
S("Place the sign in front of the field."),
S("When finished, the bot turns."),
}, "\n")
},
})
end