-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreecutter.lua
More file actions
153 lines (130 loc) · 3.65 KB
/
treecutter.lua
File metadata and controls
153 lines (130 loc) · 3.65 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
---@class scm
local scm = require("./scm")
---@class Config
local Config = scm:load("config")
---@type turtleController
local turtleController = scm:load("turtleController")
---@class Vector
---@field x number
---@field y number
---@field z number
local defaults = {
["treeGap"] = {
["description"] = "Gap between trees",
["default"] = 1,
["type"] = "number"
},
["treesPerRow"] = {
["description"] = "Amount of trees per row",
["default"] = 5,
["type"] = "number"
},
["rows"] = {
["description"] = "Amount of rows",
["default"] = 1,
["type"] = "number",
},
["moveDir"] = {
["description"] = "Direction the turtle will move (e.g. from first tree to next it moves in this direction)",
["default"] = "right",
["type"] = "string",
},
["saplingName"] = {
["description"] = "Sapling name",
["default"] = "minecraft:oak_sapling",
["type"] = "string",
},
["pause"] = {
["description"] = "Pause / sleep between check-loops in seconds",
["default"] = 60,
["type"] = "number",
}
}
Config:init(defaults)
local args = {...}
if args[1] == "config" then
Config:command(args)
return
end
turtleController.canBreakBlocks = true
local checkBlock, cutAdjacent
function checkBlock()
local found, block = turtle.inspect()
if found and string.find(block.name, "log") then
turtleController:tryMove("f")
cutAdjacent()
turtleController:tryMove("b")
end
end
---@comment cuts a 3x3 area
--- checks 5x5 area
function cutAdjacent()
local function step (check)
checkBlock()
turtleController:tryMove("f")
if(check) then
checkBlock()
turtleController:tryMove("tR")
checkBlock()
turtleController:tryMove("tL")
end
end
local function line ()
step(true)
step(true)
turtleController:tryMove("tL")
end
step(false)
checkBlock()
turtleController:tryMove("tL")
step(true)
checkBlock()
turtleController:tryMove("tL")
line()
line()
line()
turtleController:compactMove("f,tL,f,tA")
--- Legend: o => unchecked, c => checked, x => mined, s => startPos
--- o c c c o
--- c x x x c
--- c x s x c
--- c x x x c
--- o c c c o
end
---@comment Checks if tree exists, sheers it,
--- cuts it, places sapling, moves back to start pos
local function cutTree()
local blockFound, inspectedBlock = turtle.inspect()
-- assuming the block is wood then continue
if (not blockFound or (inspectedBlock.name == Config:get("saplingName"))) then
return nil
end
local height = 0
turtleController:tryAction("dig")
turtleController:tryMove("f")
while turtle.detectUp() do
turtleController:tryAction("digU")
turtleController:tryMove("u")
height = height + 1
end
while height > 1 do
cutAdjacent()
turtleController:tryMove("d")
height = height - 1
end
turtleController:compactMove("d,b")
end
local moveDir = (Config:get("moveDir") == "right") and {"tR", "tL"} or {"tL", "tR"}
local j = 1
while true do
local oddEven =j % 2 == 1 and {1,2} or {2,1}
j = j == 1 and 2 or 1
cutTree()
for i = 1, Config:get("treesPerRow") - 1, 1 do
---@type string
local moveString = moveDir[oddEven[1]] .. ",f" .. tostring(Config:get("treeGap") + 1) .. "," .. moveDir[oddEven[2]]
turtleController:compactMove(moveString)
cutTree()
end
sleep(Config:get("pause"))
end