View difference between Paste ID: xX2A96J4 and 33FwqtwA
SHOW: | | - or go back to the newest paste.
1
--Sap farming turtle.
2
3
local rows = 1
4
local compass = 0
5
local x = 0
6
local y = 0
7
local z = 0
8
9
function spin()
10
  print "spinning"
11
  turtle.turnRight()
12
  turtle.turnRight()
13
  compass = (compass + 2) % 4
14
end
15
16
function forward()
17
  print "going forward"
18
  if not turtle.forward() then
19
    return false
20
  end
21
  if compass == 0 then
22
    x = x + 1
23
  elseif compass == 1 then
24
    y = y + 1
25
  elseif compass == 2 then
26
    x = x - 1
27
  elseif compass == 3 then
28
    y = y - 1
29
  else
30
    print "what are you doing!?! Not a valid compass value."
31
  end
32
  return true
33
end
34
35
function up()
36
  print "going up"
37
  if turtle.up() then
38
    z = z + 1
39
    return true
40
  else
41
    return false
42
  end
43
end
44
45
function down()
46
  "print going down"
47
  if turtle.down() then
48
    z = z - 1
49
    return true
50
  else
51
    return false
52
  end
53
end
54
55
function turnRight()
56
  print "turning right"
57
  if turtle.turnRight() then
58
    compass = (compass + 1) % 4
59
    return true
60
  else
61
    return false
62
  end
63
end
64
65
function turnLeft()
66
  print "turning left"
67
  if turtle.turnLeft() then
68
    compass = math.abs((compass - 1) % 4)
69
    return true
70
  else
71
    return false
72
  end
73
end
74
75
function emptyInv() -- may as well empty our inventory while we're here.
76
  
77
  
78
end
79
80
function recharge() --Find and use turtle recharging station.
81
  
82
  
83
end
84
85
function returnToBase()
86
  --This code will go up to a respectable Z, then go to 0 x and 0 y, then drop down to 0 z. This is the starting point, and should be where we keep the recharging station and ender chest.
87
  for i = 0, 20 do
88
    up()
89
  end
90
  repeat
91
    turnRight()
92
  until compass == 2
93
  while x ~= 0 do
94
    forward()
95
  end
96
  repeat
97
    turnRight()
98
  until compass == 3
99
  while y ~= 0 do
100
    forward()
101
  end
102
  repeat
103
    turnRight()
104
  until compass == 0
105
  while z ~= 0 do
106
    down()
107
  end
108
  emptyInv()
109
  recharge()
110
end
111
112
function rotateCounterClockwise()
113
  print "rotating counter clockwise"
114
  turnRight()
115
  forward()
116
  turnLeft()
117
  forward()
118
  turnLeft()
119
end
120
function rotateClockwise()
121
  print "rotating clockwise"
122
  turnLeft()
123
  forward()
124
  turnRight()
125
  forward()
126
  turnRight()
127
end
128-
  until tutle.detect() == false
128+
129
function harvestFrontBack()
130
  repeat
131
    turtle.dig()
132
    if not up() then
133
      turtle.digUp()
134
      up()
135
    end
136
  until not turtle.detect()
137
  forward()
138
  forward()
139
  spin()
140
  while z do
141
    if not down() then
142
      print "breaking to go down"
143
      turtle.digDown()
144
      down()
145
    end
146
    turtle.dig()
147
  end
148
end
149
150
function harvestTree() --Starting with turtle facinga a tree, it gets all the resin from each side of the tree and returns to its start position.
151
  print "Starting"
152
  harvestFrontBack()
153
  rotateCounterClockwise()
154
  harvestFrontBack()
155
  rotateClockwise()
156
end
157
158
function turn(dir)
159
  if dir % 2 == 1 then
160
    turnLeft()
161
  elseif dir % 2 == 0 then
162
    turnRight()
163
  end
164
end
165-
  forwardTwo()
165+
166
function nextRow(rows)
167
  if rows % 2 then
168
    local dir = 2
169
    turn(dir)
170
  else
171
    local dir = 3
172
    turn(dir)
173
  end
174
  forward()
175
  forward()
176
  local rowFound = false
177
  while not rowFound do
178
    if not turtle.forward() then
179
      returnToBase()
180
      sleep(3600)
181
    end
182
    turn(dir)
183
    rowFound = turtle.detect()
184
    turn(dir - 1)
185
  end
186
  rows = rows + 1
187
end
188
189
function nextTree()
190
  local detectTree = false
191
  repeat
192
    turtle.turnRight()
193
    if not turtle.forward() then
194
      nextRow()
195
      break
196
    end
197
    turnLeft()
198
  until turtle.detect()
199
end
200
201
202
203
function main()
204
  --while true do
205
    harvestTree()
206
    nextTree()
207
  --end 
208
end