View difference between Paste ID: aDaDM2Kr and PxfdTBJd
SHOW: | | - or go back to the newest paste.
1-
-- Mining starts from lower right corner
1+
-- Mining starts from lower right corner of quarry heading forward
2
3-
-- digger sets this true when reach bedrock
3+
-- Turtle facing rotations
4-
local bedrock = false
4+
--   0        ^
5-
local x = 1
5+
-- 1   3      |
6-
local y = 1
6+
--   2    <---+
7-
local z = 1
7+
8
x = 1 -- Turtle position X, Y, Z
9
y = 1
10-
  local fuelDelay = 500
10+
z = 1
11-
  -- rot 0:+Z, 1:+X, 2:-Z
11+
rot = 0 -- 0:+Z, 1:+X, 2:-Z, 3: -X
12-
  local rot = 0
12+
ix = 1 -- Increase X
13-
  local ix = 1
13+
ty = 1 -- Target Y
14
bedrock = false
15
resuming = false
16
17-
  shell.run("fuel 10000 10000")
17+
function writeStatus()
18-
  turtle.select(1)
18+
  local f = fs.open("_status", "wb")
19
  f.write(1) -- Orequarry ID
20-
  turtle.digDown()
20+
  f.write(x)
21-
  if sx*sy*sz == 1 then
21+
  f.write(y)
22
  f.write(z)
23
  f.write(rot)
24-
    turtle.down()
24+
  f.write(ix+1) -- Byte is limited to 0, so -1 would loop over to 255
25-
    y = y+1
25+
  f.write(ty)
26
  f.close()
27-
  if sy > 1 then
27+
28-
    turtle.digDown()
28+
29-
    turtle.down()
29+
30-
    y = y+1
30+
  local fuelDelay = 260
31
  local oldix = ix
32
  if (sx < 1) or (sy < 1) or (sz < 1) then
33
    return
34-
    if rot == 0 then -- Z+
34+
35-
      if z >= sz then -- Max Z
35+
  
36-
        turtle.turnLeft() -- rot = 1
36+
  fuel(1000, 10000)
37-
        if ((x >= sx) and (ix == 1)) or
37+
  empty()
38-
           ((x <= 1) and (ix == -1)) then
38+
  if resuming then
39-
          -- Reached horizontal plane corner
39+
    local f = fs.open("_status", "rb")
40-
          if bedrock or (y >= sy) then
40+
    f.read() -- Orequarry ID
41-
            -- Digging finished
41+
    x = f.read()
42
    y = f.read()
43-
          else
43+
    z = f.read()
44-
            -- Move down
44+
    rot = f.read()
45-
            for i=1,3 do
45+
    ix = f.read()-1
46-
              if y < sy then
46+
    ty = f.read()
47-
                turtle.digDown()
47+
    f.close()
48-
                y = y+1
48+
    print(string.format("Status: %d %d %d %d %d", x, y, z, rot, ix))
49-
                doEmpty()
49+
  else    
50-
              end
50+
    if sx*sy*sz == 1 then return
51-
            end
51+
    elseif sy > 2 then ty = 2
52
    end
53-
        end else
53+
    if sx > 255 then sx = 255 end
54-
          -- dig 1 to side to prepare for next column
54+
    if sy > 255 then sy = 255 end
55-
          dig(sy)
55+
    if sz > 255 then sz = 255 end
56-
          x = x+ix
56+
    local f = fs.open("_area", "wb")
57
    f.write(sx)
58-
        turtle.turnLeft()
58+
    f.write(sy)
59-
        rot = 2
59+
    f.write(sz)
60
    f.close()
61-
        dig(sy)
61+
62
  
63-
    elseif rot == 2 then
63+
64-
      if z <= 1 then
64+
  sdelay = 0
65-
        turtle.
65+
66
    if sdelay == 0 then sleep(1) end
67-
      
67+
    sdelay = (sdelay+1) % 10
68
    
69
    oldix = ix
70-
    --return
70+
    if y < ty then
71
      turtle.digDown()
72
      if turtle.down() then
73
        y = y+1  writeStatus()
74
        empty()
75
      else
76
        bedrock = true
77
        ty = y
78
      end
79-
  if (y < sy) (not bedrock) and turtle.detectDown() then
79+
    elseif rot == 0 then -- Going forward
80-
    if turtle.digDown() then
80+
      if z < sz then
81-
      doEmpty()
81+
        if dig(sy) then
82
          z = z+1  writeStatus()
83
        end
84
      else -- Reached edge
85
        if ix > 0 then turnTo(1)
86
        else turnTo(3)
87
        end
88
      end
89
    elseif rot == 2 then -- Going backwards
90
      if z > 1 then
91
        if dig(sy) then
92
          z = z-1  writeStatus()
93
        end
94
      else -- Reached edge
95
        if ix > 0 then turnTo(1)
96-
  if turtle.getItemCount(13) > 0 then
96+
        else turnTo(3)
97-
    shell.run("empty")
97+
98
      end
99
    elseif rot == 1 then -- Going left
100
      if x < sx then
101
        if dig(sy) then
102-
if #param == 3 then
102+
          x = x+1  writeStatus()
103-
  quarry(0+param[1], 0+param[2], 0+param[3])
103+
104
      else
105-
  print("Usage: quarry [width] [height] [depth]")
105+
        ty = math.min(y+3, sy)
106-
  print("Example: quarry 30 99 30")
106+
        ix = -ix  writeStatus()
107-
  print("Meeting bedrock will stop the turtle.")
107+
        while turtle.detectUp() do
108-
  print("Turtle will advance forward and left")
108+
          if not turtle.digUp() then
109
            break
110
          end
111
        end
112
        if (math.abs(ty-y)<1.5) or bedrock then break end
113
      end
114
      if z < 2 then turnTo(0)
115
      else turnTo(2)
116
      end
117
    elseif rot == 3 then -- Going right
118
      if x > 1 then
119
        if dig(sy) then
120
          x = x-1  writeStatus()
121
        end
122
      else
123
        ty = math.min(y+3, sy)
124
        ix = -ix  writeStatus()
125
        while turtle.detectUp() do
126
          if not turtle.digUp() then
127
            break
128
          end
129
        end
130
        if (math.abs(ty-y)<1.5) or bedrock then break end
131
      end
132
      if z < 2 then turnTo(0)
133
      else turnTo(2)
134
      end
135
    end
136
    
137
    fuelDelay = fuelDelay-1
138
    if fuelDelay < 0 then
139
      fuelDelay = 260
140
      if turtle.getFuelLevel() < 600 then
141
        fuel(10000, 10000)
142
      end
143
    end
144
  end
145
  
146
  fs.delete("_status")
147
  fs.delete("_area")
148
  
149
  -- Return to surface
150
  
151
  -- Come up half way
152
  while y > 1+sy/2 do
153
    y = y-1
154
    turtle.digUp()  turtle.up()
155
  end
156
  
157
  -- Come to X = 1
158
  -- Turn to left
159
  turnTo(3, true) -- Turn but don't save
160
  while x > 1 do
161
    x = x-1
162
    turtle.dig()  turtle.forward()
163
  end  
164
  
165
  -- Come to Z = 1
166
  -- Turn to backwards
167
  rot = 2
168
  turtle.turnRight()
169
  while z > 1 do
170
    z = z-1
171
    turtle.dig()  turtle.forward()
172
  end  
173
  
174
  -- Come up all the way
175
  while y > 1 do
176
    y = y-1
177
    turtle.digUp()  turtle.up()
178
  end
179
  -- Finally empty items to chest
180
  doEmpty()
181
  print("Mining trip finished!")
182
end
183
184
function dig(sy)
185
  while turtle.detectUp() do
186
    if not turtle.digUp() then
187
      break
188
    end
189
  end
190
  if (y < sy) and (not bedrock) and turtle.detectDown() then
191
    if not turtle.digDown() then
192
      bedrock = true
193
    end
194
  end
195
  while turtle.detect() do
196
    if not turtle.dig() then
197
      repeat
198
        empty()
199
      until not turtle.suck()
200
      break
201
    end
202
    empty()
203
  end
204
  return turtle.forward()
205
end
206
207
function doEmpty()
208
  print("Emptying inventory...")
209
  while turtle.detectUp() do
210
    if not turtle.digUp() then
211
      break
212
    end
213
  end
214
  turtle.select(16) -- Select ender chest for placing
215
  if not turtle.placeUp() then
216
    print("Error placing ender chest")
217
    return
218
  end  
219
  for i=1,14 do
220
    turtle.select(i)
221
    turtle.dropUp()
222
  end
223
  turtle.select(16)
224
  turtle.digUp()
225
  sleep(1)
226
  print("Emptying finished")
227
end
228
229
function empty()
230
  if (turtle.getItemCount(13) > 0) or (turtle.getItemCount(14) > 0) then
231
    doEmpty()
232
  end
233
end
234
235
function fuel(minfuel, maxfuel)
236
  empty() -- Make sure slot 14 is empty
237
  write("Checking fuel...")
238
  if turtle.getFuelLevel() > minfuel then
239
    print("Fuel tank already full")
240
    return
241
  end
242
  while turtle.detectUp() do
243
    if not turtle.digUp() then break end
244
  end
245
  local chest = false
246
  turtle.select(14)
247
  while turtle.getFuelLevel() < maxfuel do
248
    turtle.refuel()
249
    if not chest then
250
      turtle.select(15)
251
      if not turtle.placeUp() then
252
        print("Error placing ender chest")
253
        break
254
      end
255
      chest = true
256
      turtle.select(14)
257
    end
258
    turtle.suckUp()
259
    print("Fuel "..turtle.getFuelLevel())
260
  end
261
  if chest then
262
    turtle.select(15)
263
    turtle.digUp()
264
  end
265
  print("Fuel tank filled")
266
end
267
268
function turnTo(newRot, nosave)
269
  function capRot(r)
270
    if r < -0.5 then r = r+4
271
    elseif r > 3.5 then r = r-4
272
    end
273
    return r
274
  end
275
276
  if rot ~= newRot then
277
    if capRot(rot-1) == newRot then
278
      turtle.turnRight()
279
    elseif capRot(rot+1) == newRot then
280
      turtle.turnLeft()
281
    else
282
      turtle.turnRight() turtle.turnRight()
283
    end
284
    rot = newRot
285
    if nosave == nil then writeStatus() end
286
  end  
287
end
288
289
-- Main program begins
290
291
param = {...}
292
293
if fs.exists("_status") and fs.exists("_area") then
294
  local f = fs.open("_status", "rb")
295
  m = f.read()
296
  f.close()
297
  if m == 1 then
298
    resuming = true
299
    local f = fs.open("_area", "rb")
300
    local sx = f.read()
301
    local sy = f.read()
302
    local sz = f.read()
303
    f.close()
304
    print(string.format("Resuming quarry (%d x %d x %d)", sx, sy, sz))
305
    quarry(sx, sy, sz)
306
    fs.delete("_status")
307
  else
308
    print("Resume is already in use for other program")
309
  end
310
else
311
  if turtle.getItemCount(16) == 0 then
312
    print("Missing enderchest (slot 16) for emptying")
313
    print("Continue anyway? [y/n] ")
314
    if read() ~= "y" then return end
315
  end
316
  if turtle.getItemCount(15) == 0 then
317
    print("Missing enderchest (slot 15) for fuel")
318
    print("Continue anyway? [y/n] ")
319
    if read() ~= "y" then return end
320
  end
321
  
322
  if #param == 3 then
323
    quarry(0+param[1], 0+param[2], 0+param[3])
324
  elseif #param == 2 then
325
    quarry(0+param[1], 255, 0+param[2])
326
  else
327
    print("Usage: quarry [width] ([height] optional) [depth]")
328
    print("Example: quarry 30 99 30")
329
    print("     or: quarry 32 64")
330
    print("Meeting bedrock will stop the turtle.")
331
    print("Turtle will advance forward and left")
332
  end
333
  fs.delete("_status")
334
end