-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreeplant
executable file
·124 lines (110 loc) · 2.57 KB
/
treeplant
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
--- Turtle which can plant tree saplings. Orients itself using line of cobble (item in Direction Guide) and plans (Turn around guide)
local SLEEP_BETWEEN_TURNS = 300
local SLEEP_REFUEL_FAIL = 60
local DIR_LEFT = "left"
local DIR_RIGHT = "right"
local direction = DIR_LEFT
local ITEM_TO_PLANT = 1
local ITEM_FUEL = 14
local ITEM_TURN_AROUND_GUIDE = 15
local ITEM_DIRECTION_GUIDE = 16
local REFUEL_OK = "ok"
local REFUEL_ERROR = "error"
function turnFinal()
if direction == DIR_LEFT then
turtle.turnLeft()
else
turtle.turnRight()
end
end
function turnToCorrectDirection()
turtle.select(ITEM_DIRECTION_GUIDE)
for i=1,4 do
local doBreak = 0
if turtle.compare() then
turnFinal()
break
else
turtle.turnLeft()
end
end
end
function moveForward()
for i=1,10 do
if turtle.forward() then
return true
end
end
return false
end
function plant()
--turn to placing direction
if direction == DIR_LEFT then
turtle.turnLeft()
else
turtle.turnRight()
end
turtle.select(ITEM_TO_PLANT)
turtle.place()
-- turn back
if direction == DIR_LEFT then
turtle.turnRight()
else
turtle.turnLeft()
end
end
function detectTurn()
turtle.select(ITEM_TURN_AROUND_GUIDE)
return turtle.compare()
end
function reverseDirection()
if direction == DIR_LEFT then
direction = DIR_RIGHT
else
direction = DIR_LEFT
end
print("New direction"..direction)
end
--- return value: 1 - everything ok, 0 - not enough fuel
function refuel()
print("Current fuel level: "..turtle.getFuelLevel())
if turtle.getFuelLevel() < 200 then
if turtle.getItemCount(ITEM_FUEL) > 1 then
turtle.select(ITEM_FUEL)
--refuel at most 5 items, but do not leave slot empty and protect against wrong argument error (if <0 in refuel)
if turtle.refuel(math.max(0,math.min(5,turtle.getItemCount(ITEM_FUEL)-1))) then
return REFUEL_OK
else
print("Refuel failed for unknown reason")
return REFUEL_ERROR
end
else
print("Refuel failed: low on fuel and no extra fuel")
return REFUEL_ERROR
end
else
return REFUEL_OK
end
end
-------------------------------------------
while true do
turnToCorrectDirection()
while not detectTurn() do
plant()
if not moveForward() then
break
end
end
reverseDirection()
print("Sleeping")
sleep(SLEEP_BETWEEN_TURNS)
for i=2,2 do
turtle.select(i)
turtle.transferTo(1, math.max(0,turtle.getItemCount(i)-1))
end
if direction == DIR_RIGHT then
while not (refuel() == REFUEL_OK) do
sleep(SLEEP_REFUEL_FAIL)
end
end
end