View difference between Paste ID: LpDmVaRd and 89Y1thMm
SHOW: | | - or go back to the newest paste.
1
local tArgs = {...}
2
local length = tArgs[1]
3
local numTunnels = tArgs[2]
4
local inv = true
5
local checkTorch = 0
6
local height = 0
7
local fullInvNum = 0
8
9
-- The basic function to move forward and dig
10
function tunnel()
11
  -- These used throughout to erase screen
12
  term.clear()
13
  term.setCursorPos(1,1)
14
  print("Mining...")
15
  for i=1,length do
16
    fuel()
17
    turtle.dig()
18
    -- This loop prevents gravel from screwing it all up
19
    while turtle.forward() == false do
20
      turtle.dig()
21
    end 
22
    turtle.digUp()
23
    torch()
24
  end
25
end
26
27
-- The function to turn around and go the other direction down the tunnel, also clears side tunnels
28
function turnAround()
29
  term.clear()
30
  term.setCursorPos(1,1)
31
  print("Turning around...")
32
  for k = 1,6 do
33
    fuel()
34
    turtle.dig()
35
    while turtle.forward() == false do
36
      turtle.dig()
37
    end
38
    turtle.digUp()
39
    torch()
40
  end
41
  
42
  checkInv()
43
  
44
  while turtle.back() == false do
45
    turtle.turnRight()
46
    turtle.turnRight()
47
    turtle.dig()
48
    for i = 1,3 do
49
      turtle.forward()
50
      turtle.dig()
51
    end
52
    turtle.turnRight()
53
    turtle.turnRight()
54
    for i = 1,3 do
55
      turtle.back()  
56
    end
57
  end
58
  turtle.back()
59
  turtle.back()
60
end
61
62
-- Clears the specified item from the inventory, most likely cobble, also stops on full inv
63
function checkInv()
64
  term.clear()
65
  term.setCursorPos(1,1)
66
  print("Trashing...")
67
  for y = 2,16 do
68
    turtle.select(y)
69
    if turtle.compareTo(1) then
70
      turtle.drop()
71
    end
72
  end
73
end
74
    
75
function fullInv()
76
  -- To not spill items everywhere, some credit to link0153 :)
77
  for i = 1,16 do
78
    if turtle.getItemCount(i) > 0 then
79
      fullInvNum = fullInvNum + 1
80
    end
81
  end
82
  while fullInvNum == 16 do
83
    term.clear()
84
    term.setCursorPos(1,1)
85
    print("Inventory full, please clear.")
86
    sleep(5)
87
    for i = 1,16 do
88
      if turtle.getItemCount(i) == 0 then
89
        fullInvNum = 0
90
      end
91
    end
92
  end
93
  fullInvNum = 0
94
  term.clear()
95
  term.setCursorPos(1,1)
96
  print("Mining...")
97
end
98
99
-- Checks how long since the last torch, then decides to place one above or not
100
function torch()
101
  -- Checks if low on torches
102
  if turtle.getItemCount(16) < 5 then
103
    term.clear()
104
    term.setCursorPos(1,1)
105
    print("Waiting for torches...place in bottom right.")
106
    while turtle.getItemCount(16) < 5 do
107
      sleep(2)
108
    end
109
  end
110
  term.clear()
111
  term.setCursorPos(1,1)
112
  print("Mining...")
113
  if checkTorch == 8 then
114
    fuel()
115
    turtle.select(16)
116
    turtle.up()
117
    turtle.digUp()
118
    -- This if and loop will bring the turtle back down from the placed torch
119
    if turtle.placeUp() or turtle.getItemCount(16) == 0 then
120
        for i = 0,height do
121
          fuel()
122
          turtle.down()
123
        end
124
        turtle.select(1)
125
        checkTorch = 0
126
        height = 0
127
    else
128
        height = height + 1
129
        torch()
130
    end
131
  -- If it's too soon, dont place a torch
132
  else
133
    checkTorch = checkTorch + 1
134
  end
135
end
136
137
-- Checks the fuel level, asks for more if necessary
138
function fuel()
139
  if turtle.getFuelLevel() < 50 then
140
    print ("Wating for fuel...place in bottom left.")     
141
    while turtle.getFuelLevel() < 50 do
142
      turtle.select(13)
143
      turtle.refuel()
144
      sleep(2)
145
    end
146
  end
147
  term.clear()
148
  term.setCursorPos(1,1)
149
  print("Mining...")
150
  turtle.select(1)
151
end
152
153
-- This function is a one time startup function, makes sure the turtle has the necessary materials
154
function begin()
155
  if tArgs[1] == nil or tArgs[2] == nil then
156
    term.clear()
157
    term.setCursorPos(1,1)
158
    print("Not enough arguments, should be used 'branch [length] [number of tunnels]'")
159
  end
160
  turtle.select(1)
161
  -- Checks for trash item
162
  if turtle.getItemCount(1) == 0 then
163
    print("Please place an item to trash in the top left.")
164
    while turtle.getItemCount(1) == 0 do
165
      sleep(1)
166
    end
167
    term.clear()
168
    term.setCursorPos(1,1)
169
    begin()
170
  -- Confirms trashed item
171
  else
172
    print("The item in the top left will be trashed...confirm?")
173
    os.pullEvent("key")
174
  end
175
  term.clear()
176
  term.setCursorPos(1,1)
177
end
178
179
-- From here is the main code          
180
begin()
181
-- Loops for the specified # of tunnels, one tunnel being up and back
182
for j = 1,numTunnels do
183
  tunnel()
184
  fullInv()
185
  turtle.turnLeft()
186
  turnAround()
187
  fullInv()
188
  turtle.turnLeft()
189
  tunnel()
190
  fullInv()
191
  turtle.turnRight()
192
  turnAround()
193
  fullInv()
194
  turtle.turnRight()
195
end
196
-- Resets the screen and displays a confirmation of finishing
197
term.clear()
198
term.setCursorPos(1,1)
199
print("Done!")