forked from SleepingFox8/AM-TreeBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeBot.lua
More file actions
306 lines (284 loc) · 16.5 KB
/
Copy pathTreeBot.lua
File metadata and controls
306 lines (284 loc) · 16.5 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
-- initialization
-- import denendencies
local botTools = require ("AM-Tools/botTools")
local compTools = require ("AM-Tools/compTools")
-- prepair table for holding exported functions
local treeBot = { _version = "0.0.0" }
--initialize GLBL table if needed
if GLBL == nil then
GLBL = {}
end
--initialize SCRIPT table
--Stores global variables for just this script
local SCRIPT = {}
-- function declarations
local function axeDurabilityOk()
--function initialization
--initialize function table
local FUNC = {}
FUNC.player = getPlayer()
if FUNC.player.mainHand then
FUNC.heldDurability = FUNC.player.mainHand.maxDmg - FUNC.player.mainHand.dmg
if (FUNC.heldDurability < 50) then
log("Durability too low to continue... stopping bot")
botTools.freezeAllMotorFunctions()
disconnect()
-- not sure if stopAllScripts() is needed or even run after disconnect()ing
stopAllScripts()
else
return true
end
else
log("Main hand is empty")
botTools.freezeAllMotorFunctions()
disconnect()
stopAllScripts()
end
end
local function grabAxeWithDura()
botTools.summonItem("minecraft:diamond_axe",1,50)
sleep(100)
axeDurabilityOk()
end
local function setTimer(timerName, ms)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.timerName, FUNC.ms = timerName, ms
SCRIPT.timerStartTime[FUNC.timerName] = os.time()
SCRIPT.timerDuration[FUNC.timerName] = ms
end
local function haveTime(timerName)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.timerName = timerName
if os.difftime(os.time(),SCRIPT.timerStartTime[FUNC.timerName]) < (SCRIPT.timerDuration[FUNC.timerName] / 1000) then
return true
else
return false
end
end
local function botHasNotFallen()
--function initialization
--initialize function table
local FUNC = {}
FUNC.bX, FUNC.bY, FUNC.bZ = getPlayerPos()
if FUNC.bY < (SCRIPT.origy - 0.5) then
log("[Bots] §6* Bot Fell Off. Logging out.")
log("[Bots] §6* §cTREE HARVESTING...")
botTools.freezeAllMotorFunctions()
sleep(2000)
log("disconnecting...")
disconnect()
stopAllScripts()
else
return true
end
end
function treeBot.harvestTrees(xsize, zsize)
--function initialization
--initialize function table
local FUNC = {}
--store arguments in locally scoped table for scope safety
FUNC.xsize, FUNC.zsize = xsize, zsize
-- initialize variables
SCRIPT.interval = 5
SCRIPT.xsize = FUNC.xsize
SCRIPT.zsize = FUNC.zsize
SCRIPT.topbreaktimer = 70
SCRIPT.lowduratrigger = 40
SCRIPT.direction = 1
SCRIPT.timerStartTime = {}
SCRIPT.timerDuration = {}
--initialize chopping variables
log("[Bots] §6* §aTREE HARVESTING...")
-- get original position of player
SCRIPT.origx, SCRIPT.origy, SCRIPT.origz = getPlayerPos()
-- remove decimal places
SCRIPT.origx = math.floor(SCRIPT.origx)
SCRIPT.origy = math.floor(SCRIPT.origy)
SCRIPT.origz = math.floor(SCRIPT.origz)
-- get target postition
FUNC.targx = SCRIPT.origx + (3 * SCRIPT.direction)
FUNC.targz = SCRIPT.origz
--grid positions
FUNC.xloc = 0
FUNC.zloc = 0
-- main loop
while botHasNotFallen() and (FUNC.zloc < SCRIPT.zsize) do
--swap directions once row complete
SCRIPT.direction = SCRIPT.direction * -1
--while finishing row
while botHasNotFallen() and (((FUNC.xloc < SCRIPT.xsize) and (SCRIPT.direction == -1)) or ((FUNC.xloc > 0) and (SCRIPT.direction == 1))) do
-- harvest Tree
FUNC.treeHarvested = false
while botHasNotFallen() and FUNC.treeHarvested == false do
botTools.lookTowards(FUNC.targx,FUNC.targz)
grabAxeWithDura()
--travel to next tree
--get player position
FUNC.pX, FUNC.pY, FUNC.pZ = getPlayerPos()
-- remove decimal places
FUNC.pX = math.floor(FUNC.pX)
FUNC.pY = math.floor(FUNC.pY)
FUNC.pZ = math.floor(FUNC.pZ)
--if away from tree, get to tree
--look straight forward to quickly break log if not obscured by leaves
setTimer("lookTwordsTree",3000)
while botHasNotFallen() and haveTime("lookTwordsTree") and (((FUNC.pX - FUNC.targx) > 1) or ((FUNC.pX - FUNC.targx) < -1)) or (((FUNC.pZ - FUNC.targz) > 1) or ((FUNC.pZ - FUNC.targz) < -1)) do
--look twords tree
botTools.lookTowards(FUNC.targx,FUNC.targz)
--break possible leaves in front of bot
attack(-1)
--run forward
forward(-1)
sprint(true)
sleep(100)
--get player position
FUNC.pX, FUNC.pY, FUNC.pZ = getPlayerPos()
-- remove decimal places
FUNC.pX = math.floor(FUNC.pX)
FUNC.pY = math.floor(FUNC.pY)
FUNC.pZ = math.floor(FUNC.pZ)
end
-- stop attacking
attack(1)
--look down to break and leaves at foot level
while botHasNotFallen() and (((FUNC.pX - FUNC.targx) > 1) or ((FUNC.pX - FUNC.targx) < -1)) or (((FUNC.pZ - FUNC.targz) > 1) or ((FUNC.pZ - FUNC.targz) < -1)) do
--look twords tree
botTools.lookTowards(FUNC.targx,FUNC.targz)
--look down 30 degrees
FUNC.player = getPlayer()
look(FUNC.player.yaw, 30)
--break possible leaves in front of bot
attack(-1)
--run forward
forward(-1)
sprint(true)
sleep(100)
--get player position
FUNC.pX, FUNC.pY, FUNC.pZ = getPlayerPos()
-- remove decimal places
FUNC.pX = math.floor(FUNC.pX)
FUNC.pY = math.floor(FUNC.pY)
FUNC.pZ = math.floor(FUNC.pZ)
end
-- stop attacking
attack(1)
--harvest bottom of tree
--get player position
FUNC.pX, FUNC.pY, FUNC.pZ = getPlayerPos()
-- remove decimal places
FUNC.pX = math.floor(FUNC.pX)
FUNC.pY = math.floor(FUNC.pY)
FUNC.pZ = math.floor(FUNC.pZ)
--if next to tree
if ((((FUNC.pX == FUNC.targx + 1) or (FUNC.pX == FUNC.targx - 1)) and (FUNC.pZ == FUNC.targz)) or (((FUNC.pZ == FUNC.targz + 1) or (FUNC.pZ == FUNC.targz - 1)) and (FUNC.pX == FUNC.targx))) then
--stop walking
forward(0)
grabAxeWithDura()
--detect if tree to mine based on time it takes to walk to center of tree
setTimer("treeDetectionTimeout", 500)
--while next to tree
while botHasNotFallen() and ((((FUNC.pX == FUNC.targx + 1) or (FUNC.pX == FUNC.targx - 1)) and (FUNC.pZ == FUNC.targz)) or (((FUNC.pZ == FUNC.targz + 1) or (FUNC.pZ == FUNC.targz - 1)) and (FUNC.pX == FUNC.targx))) do
--look twords tree
botTools.lookTowards(FUNC.targx,FUNC.targz)
--break into tree
--look down 30 degrees
FUNC.player = getPlayer()
look(FUNC.player.yaw, 30)
sprint(false)
forward(-1)
--break block(s)
attack(-1)
sleep(100)
--get player position
FUNC.pX, FUNC.pY, FUNC.pZ = getPlayerPos()
-- remove decimal places
FUNC.pX = math.floor(FUNC.pX)
FUNC.pY = math.floor(FUNC.pY)
FUNC.pZ = math.floor(FUNC.pZ)
end
end
--get player position
FUNC.pX, FUNC.pY, FUNC.pZ = getPlayerPos()
-- remove decimal places
FUNC.pX = math.floor(FUNC.pX)
FUNC.pY = math.floor(FUNC.pY)
FUNC.pZ = math.floor(FUNC.pZ)
--if under tree
if ((FUNC.pX == FUNC.targx) and (FUNC.pZ == FUNC.targz)) then
FUNC.timeToChop = 0
if haveTime("treeDetectionTimeout") then
FUNC.timeToChop = 2000
else
FUNC.timeToChop = 3000
end
--harvest top of tree
--while under tree, break top section
setTimer("harvestTreeTop", FUNC.timeToChop)
while botHasNotFallen() and haveTime("harvestTreeTop") and ((FUNC.pX == FUNC.targx) and (FUNC.pZ == FUNC.targz)) do
-- stop walking
forward(0)
-- look straight up
FUNC.player = getPlayer()
look(FUNC.player.yaw,-90)
-- mine the tree
attack(-1)
sleep(100)
--get player position
FUNC.pX, FUNC.pY, FUNC.pZ = getPlayerPos()
-- remove decimal places
FUNC.pX = math.floor(FUNC.pX)
FUNC.pY = math.floor(FUNC.pY)
FUNC.pZ = math.floor(FUNC.pZ)
end
--stop chopping tree
attack(1) -- attack(0) doesn't work
sleep(100)
end
if botHasNotFallen() and ((FUNC.pX == FUNC.targx) and (FUNC.pZ == FUNC.targz)) then
--replant sappling
--grab sappling
botTools.summonItem("minecraft:birch_sapling",3)
botTools.summonItem("minecraft:oak_sapling",3)
--look straight down
FUNC.player = getPlayer()
look(FUNC.player.yaw,90)
sleep(100)
--break old sappling
attack(1)
sleep(100)
--plant sappling
use(1)
--give time for right click (plant) to register
sleep(100)
FUNC.treeHarvested = true
end
end
botTools.eatIfHungery()
--set new target
FUNC.targx = FUNC.targx + (SCRIPT.interval * (SCRIPT.direction * -1))
FUNC.xloc = FUNC.xloc + (SCRIPT.direction * -1)
end
--set new target
FUNC.targx = FUNC.targx - (SCRIPT.interval * (SCRIPT.direction * -1))
FUNC.targz = FUNC.targz - SCRIPT.interval
FUNC.zloc = FUNC.zloc + 1
-- offload Wood
look(25,0)
botTools.dropAllOfItemExcept("minecraft:oak_log")
botTools.dropAllOfItemExcept("minecraft:birch_log")
botTools.dropAllOfItemExcept("minecraft:oak_leaves")
botTools.dropAllOfItemExcept("minecraft:birch_leaves")
botTools.dropAllOfItemExcept("minecraft:stick")
botTools.dropAllOfItemExcept("minecraft:apple")
sleep(1000)
end
log("Finished harvesting trees")
log("disconnecting...")
end
return treeBot