View difference between Paste ID: zSGBT4wt and 0i8VWfnZ
SHOW: | | - or go back to the newest paste.
1
os.loadAPI("persistence")
2
currentFace = persistence.pull("currentFace")
3
currentRadius = persistence.pull("currentRadius")
4
endRadius = persistence.pull("endRadius")
5
currentX = persistence.pull("currentX")
6
currentZ = persistence.pull("currentZ")
7
digCount = persistence.pull("digCount")
8
9
10
--functions
11
12
function get()
13
  term.clear()
14
  term.setCursorPos(1,1)
15
  print("currentFace: "..persistence.pull("currentFace"))
16
  print("currentRadius: "..persistence.pull("currentRadius"))
17
  print("endRadius: "..persistence.pull("endRadius"))
18
  print("currentX: "..persistence.pull("currentX"))
19
  print("currentZ: "..persistence.pull("currentZ"))
20
  print("digCount: "..persistence.pull("digCount"))
21
  print("fuelLevel: "..turtle.getFuelLevel())
22
23
end
24
25
function left()
26
  turtle.turnLeft()
27
  currentFace = persistence.pull("currentFace")
28
  if currentFace == 0 then
29
    currentFace = persistence.store("currentFace",3)
30
    return
31
  end
32
  if currentFace == 1 then
33
    currentFace = persistence.store("currentFace",0)
34
    return
35
  end
36
  if currentFace == 2 then
37
    currentFace = persistence.store("currentFace",1)
38
    return
39
  end
40
  if currentFace == 3 then
41
    currentFace = persistence.store("currentFace",2)
42
    return
43
  end
44
end 
45
46
function right()
47
  currentFace = persistence.pull("currentFace")
48
  turtle.turnRight()
49
  if currentFace == 0 then
50
    currentFace = persistence.store("currentFace",1)
51
    return
52
  end
53
  if currentFace == 1 then
54
    currentFace = persistence.store("currentFace",2)
55
    return
56
  end
57
  if currentFace == 2 then
58
    currentFace = persistence.store("currentFace",3)
59
    return
60
  end
61
  if currentFace == 3 then
62
    currentFace = persistence.store("currentFace",0)
63
    return
64
  end
65
end 
66
67
function face0()
68
  call()
69
  if currentFace == 1 then
70
    left()
71
    return
72
  end
73
  if currentFace == 2 then
74
    left()
75
    left()
76
    return
77
  end
78
  if currentFace == 3 then
79
    right()
80
    return
81
  end
82
end
83
84
function face1()
85
  call()
86
  if currentFace == 2 then
87
    left()
88
    return
89
  end
90
  if currentFace == 3 then
91
    left()
92
    left()
93
    return
94
  end
95
  if currentFace == 0 then
96
    right()
97
    return
98
  end
99
end
100
101
function face2()
102
  call()
103
  if currentFace == 3 then
104
    left()
105
    return
106
  end
107
  if currentFace == 0 then
108
    left()
109
    left()
110
    return
111
  end
112
  if currentFace == 1 then
113
    right()
114
    return
115
  end
116
end
117
118
function face3()
119
  call()
120
  if currentFace == 0 then
121
    left()
122
    return
123
  end
124
  if currentFace == 1 then
125
    left()
126
    left()
127
    return
128
  end
129
  if currentFace == 2 then
130
    right()
131
    return
132
  end
133
end
134
135
function forward()
136
  currentFace = persistence.pull("currentFace")
137
  get()
138
  
139
  checkFuel()
140
  checkInv()
141
  turtle.digUp()
142
  turtle.digDown()
143
  while not turtle.forward() do
144
    sleep(0.1)
145
    checkInv()
146
    if turtle.dig() then
147
      digCount = persistence.pull("digCount")
148
      digCount = persistence.store("digCount",tonumber(digCount)+1)
149
    elseif not turtle.attack() then
150
      print("Something is blocking me")
151
      sleep(1)
152
    end
153
  end
154
  turtle.digUp()
155
  turtle.digDown()
156
  currentX = persistence.pull("currentX")
157
  currentZ = persistence.pull("currentZ")
158
  if currentFace == 0 then
159
    currentZ = persistence.store("currentZ",currentZ+1)
160
  end
161
  if currentFace == 1 then
162
    currentX = persistence.store("currentX",currentX-1) 
163
  end
164
  if currentFace == 2 then
165
    currentZ = persistence.store("currentZ",currentZ-1)    
166
  end
167
  if currentFace == 3 then
168
    currentX = persistence.store("currentX",currentX+1) 
169
  end
170
  get()
171
end
172
      
173
function dig()
174
  digCount = persistence.pull("digCount")
175
  checkInv()
176
  if turtle.digDown() then
177
    digCount = persistence.store("digCount", digCount+1)
178
  end
179
  while turtle.detectUp() do
180
    sleep(0)
181
    digCount = persistence.pull("digCount")
182
    checkInv()
183
    if turtle.digUp() then
184
      digCount = persistence.store("digCount",digCount+1)
185
      sleep(0.8)
186
    else
187
      print("I can't dig up")
188
      sleep(10)
189
    end   
190
  end
191
end
192
193
function checkFuel()
194
  if turtle.getFuelLevel() < 10 then
195
    refuel()
196
  end
197
end
198
199
function checkInv()
200
  if turtle.getItemCount(15) < 1 then --checks to see if refuel chest is present
201
    refuel()
202
  end
203
  if turtle.getItemCount(16) < 1 then --checks to see if unload chest is present
204
    unload()
205
  end
206
  for i=13,14 do
207
    if turtle.getItemCount(i) > 0 then -- checks slots 13 and 14 for items
208
      unload()
209
    end
210
  end
211
end
212
213
function unload()
214
  if turtle.getItemCount(16) > 0 then --if the unload chest is present
215
    if turtle.detectDown() then
216
      digCount = persistence.pull("digCount")
217
      while not turtle.digDown() and turtle.detectDown() do
218
        term.clear()
219
        term.setCursorPos(1,1)
220
        print("I can't dig down")
221
        sleep(10)
222
      end
223
      digCount = persistence.store("digCount",digCount+1)
224
    end
225
  end
226
  if turtle.getItemCount(16) < 1 then --if unload chest is missing
227
    if not turtle.detectDown() then --the chest isn't in inv or below
228
      print("I'VE LOST MY CHEST!!!")
229
      while turtle.getItemCount(16) < 1 do
230
        term.clear()
231
        term.setCursorPos(1,1)
232
        print("please put an unload chest in slot(16)...")
233
        sleep(10)
234
      end
235
    end
236
    if turtle.detectDown() then --the chest is below the turtle
237
      turtle.select(16)
238
      turtle.digDown()
239
      turtle.select(1)
240
    end
241
  end
242
  turtle.select(16)
243
  turtle.placeDown()
244
  for i=1,14 do
245
    while turtle.getItemCount(i) > 0 do
246
      sleep(0.1)
247
      turtle.select(i)
248
      if not turtle.dropDown() then
249
        sleep(10) -- waiting for space in chest
250
      end
251
    end
252
  end
253
  turtle.select(16)
254
  turtle.digDown()
255
  turtle.select(1)
256
end
257
258
function refuel()
259
  unload()
260
  if turtle.getItemCount(15) > 0 then --Refuel chest is present
261
    if turtle.detectUp() then
262
      digCount = persistence.pull("digCount")
263
      while not turtle.digUp() and turtle.detectUp() do
264
        term.clear()
265
        term.setCursorPos(1,1)
266
        print("I can't dig up for some reason")
267
        sleep(10)
268
      end
269
      digCount = persistence.store("digCount",digCount+1)
270
    end
271
  end
272
  if turtle.getItemCount(15) < 1 then --if refuel chest isn't in Inv
273
    if not turtle.detectUp() then
274
      term.clear()
275
      term.setCursorPos(1,1)
276
      print("I've lost my refuel chest")
277
      while turtle.getItemCount(15) < 1 do
278
        term.clear()
279
        term.setCursorPos(1,1)
280
        print("please put refuel chest in slot(15)")
281
        sleep(10)
282
      end
283
    end
284
    if turtle.detectUp() then
285
      turtle.select(15)
286
      turtle.digUp()
287
    end
288
  end 
289
  turtle.select(15)
290
  turtle.placeUp()
291
  while turtle.getFuelLevel() < 500 do
292
    sleep(1)
293
    turtle.select(1)
294
    if not turtle.suckUp() then
295
      term.clear()
296
      term.setCursorPos(1,1)
297
      print("Waiting for more fuel in refuel chest")
298
      sleep(10)
299
    end
300
    if turtle.getItemCount(1) > 0 then
301
      turtle.refuel(turtle.getItemCount(1))
302
    end
303
  end
304
  turtle.select(15)
305
  turtle.digUp()
306
  turtle.select(1)
307
end
308
309
function call()
310
  currentX = persistence.pull("currentX")
311
  currentZ = persistence.pull("currentZ")
312
  currenFace = persistence.pull("currentFace")
313
  digCount = persistence.pull("digCount")
314
  currentRadius = persistence.pull("currentRadius")
315
  endRadius = persistence.pull("endRadius")
316
end
317
318
function spiral() 
319
  call()
320
  if currentX==0 and currentZ==0 then
321
    if currentFace ~= 0 then
322
      face0()
323
    end
324
    print("at start")
325
    dig()
326
    forward()
327
    left()
328
    dig()
329
    currentRadius = persistence.store("currentRadius",currentRadius+1)
330
    call()
331
    get()
332
    return
333
  elseif math.abs(currentX) == math.abs(currentZ) then
334
    if currentZ > 0 and currentX < 0 then
335
      if currentFace==0 then
336
        dig()
337
        forward()
338
        face3()
339
        dig()
340
        forward()
341
        currentRadius = persistence.store("currentRadius",currentRadius+1)
342
        call()
343
        get()
344
        return
345
      else
346
        face3()
347
        dig()
348
        forward()
349
        return
350
      end
351
    elseif currentX>0 and currentZ>0 then
352
      face2()
353
      dig()
354
      forward()
355
      return
356
    elseif currentX>0 and currentZ<0 then
357
      face1()
358
      dig()
359
      forward()
360
      return
361
    elseif currentX<0 and currentZ<0 then
362
      face0()
363
      dig()
364
      forward()
365
      return
366
    end
367
  else
368
    dig()
369
    forward()
370
    return
371
  end
372
end
373
374
function spiralMine()
375
  call()
376
  while tonumber(currentRadius) < tonumber(endRadius) do
377
    sleep(0.1)
378
    spiral()
379
  end
380
end
381
382
spiralMine()