SHOW:
|
|
- or go back to the newest paste.
| 1 | local tArgs = { ... }
| |
| 2 | if #tArgs ~= 1 then | |
| 3 | print( "Usage: excavate <diameter>" ) | |
| 4 | return | |
| 5 | end | |
| 6 | ||
| 7 | -- Mine in a quarry pattern until we hit something we can't dig | |
| 8 | local size = tonumber( tArgs[1] ) | |
| 9 | if size < 1 then | |
| 10 | print( "Excavate diameter must be positive" ) | |
| 11 | return | |
| 12 | end | |
| 13 | ||
| 14 | local depth = 0 | |
| 15 | local collected = 0 | |
| 16 | ||
| 17 | local xPos,zPos = 0,0 | |
| 18 | local xDir,zDir = 0,1 | |
| 19 | ||
| 20 | local function dropUseless() | |
| 21 | local x = turtle.getItemCount(1); | |
| 22 | if (x > 1) then | |
| 23 | turtle.drop(x-1); | |
| 24 | end | |
| 25 | end | |
| 26 | ||
| 27 | local function collect() | |
| 28 | collected = collected + 1 | |
| 29 | if math.fmod(collected, 25) == 0 then | |
| 30 | print( "Mined "..collected.." blocks." ) | |
| 31 | end | |
| 32 | ||
| 33 | dropUseless(); | |
| 34 | ||
| 35 | for n=1,9 do | |
| 36 | if turtle.getItemCount(n) == 0 then | |
| 37 | return true | |
| 38 | end | |
| 39 | end | |
| 40 | ||
| 41 | print( "No empty slots left." ) | |
| 42 | return false | |
| 43 | end | |
| 44 | ||
| 45 | local function tryForwards() | |
| 46 | while not turtle.forward() do | |
| 47 | if turtle.dig() then | |
| 48 | if not collect() then | |
| 49 | return false | |
| 50 | end | |
| 51 | else | |
| 52 | -- give sand a chance to fall | |
| 53 | sleep(0.8) | |
| 54 | if turtle.dig() then | |
| 55 | if not collect() then | |
| 56 | return false | |
| 57 | end | |
| 58 | else | |
| 59 | return false | |
| 60 | end | |
| 61 | end | |
| 62 | end | |
| 63 | xPos = xPos + xDir | |
| 64 | zPos = zPos + zDir | |
| 65 | return true | |
| 66 | end | |
| 67 | ||
| 68 | local function tryDown() | |
| 69 | if not turtle.down() then | |
| 70 | if turtle.digDown() then | |
| 71 | if not collect() then | |
| 72 | return false | |
| 73 | end | |
| 74 | end | |
| 75 | if not turtle.down() then | |
| 76 | return false | |
| 77 | end | |
| 78 | end | |
| 79 | depth = depth + 1 | |
| 80 | if math.fmod( depth, 10 ) == 0 then | |
| 81 | print( "Descended "..depth.." metres." ) | |
| 82 | end | |
| 83 | return true | |
| 84 | end | |
| 85 | ||
| 86 | local function turnLeft() | |
| 87 | turtle.turnLeft() | |
| 88 | xDir, zDir = -zDir, xDir | |
| 89 | end | |
| 90 | ||
| 91 | local function turnRight() | |
| 92 | turtle.turnRight() | |
| 93 | xDir, zDir = zDir, -xDir | |
| 94 | end | |
| 95 | ||
| 96 | print( "Excavating..." ) | |
| 97 | ||
| 98 | local reseal = false | |
| 99 | if turtle.digDown() then | |
| 100 | reseal = true | |
| 101 | end | |
| 102 | ||
| 103 | local alternate = 0 | |
| 104 | local done = false | |
| 105 | while not done do | |
| 106 | for n=1,size do | |
| 107 | for m=1,size-1 do | |
| 108 | if not tryForwards() then | |
| 109 | done = true | |
| 110 | break | |
| 111 | end | |
| 112 | end | |
| 113 | if done then | |
| 114 | break | |
| 115 | end | |
| 116 | if n<size then | |
| 117 | if math.fmod(n + alternate,2) == 0 then | |
| 118 | turnLeft() | |
| 119 | if not tryForwards() then | |
| 120 | done = true | |
| 121 | break | |
| 122 | end | |
| 123 | turnLeft() | |
| 124 | else | |
| 125 | turnRight() | |
| 126 | if not tryForwards() then | |
| 127 | done = true | |
| 128 | break | |
| 129 | end | |
| 130 | turnRight() | |
| 131 | end | |
| 132 | end | |
| 133 | end | |
| 134 | if done then | |
| 135 | break | |
| 136 | end | |
| 137 | ||
| 138 | if size > 1 then | |
| 139 | if math.fmod(size,2) == 0 then | |
| 140 | turnRight() | |
| 141 | else | |
| 142 | if alternate == 0 then | |
| 143 | turnLeft() | |
| 144 | else | |
| 145 | turnRight() | |
| 146 | end | |
| 147 | alternate = 1 - alternate | |
| 148 | end | |
| 149 | end | |
| 150 | ||
| 151 | if not tryDown() then | |
| 152 | done = true | |
| 153 | break | |
| 154 | end | |
| 155 | end | |
| 156 | ||
| 157 | print( "Returning to surface..." ) | |
| 158 | ||
| 159 | -- Return to where we started | |
| 160 | while depth > 0 do | |
| 161 | if turtle.up() then | |
| 162 | depth = depth - 1 | |
| 163 | elseif turtle.digUp() then | |
| 164 | collect() | |
| 165 | else | |
| 166 | sleep( 0.5 ) | |
| 167 | end | |
| 168 | end | |
| 169 | ||
| 170 | if xPos > 0 then | |
| 171 | while xDir ~= -1 do | |
| 172 | turnLeft() | |
| 173 | end | |
| 174 | while xPos > 0 do | |
| 175 | if turtle.forward() then | |
| 176 | xPos = xPos - 1 | |
| 177 | elseif turtle.dig() then | |
| 178 | collect() | |
| 179 | else | |
| 180 | sleep( 0.5 ) | |
| 181 | end | |
| 182 | end | |
| 183 | end | |
| 184 | ||
| 185 | if zPos > 0 then | |
| 186 | while zDir ~= -1 do | |
| 187 | turnLeft() | |
| 188 | end | |
| 189 | while zPos > 0 do | |
| 190 | if turtle.forward() then | |
| 191 | zPos = zPos - 1 | |
| 192 | elseif turtle.dig() then | |
| 193 | collected = collected + 1 | |
| 194 | else | |
| 195 | sleep( 0.5 ) | |
| 196 | end | |
| 197 | end | |
| 198 | end | |
| 199 | while zDir ~= 1 do | |
| 200 | turnLeft() | |
| 201 | end | |
| 202 | ||
| 203 | -- Seal the hole | |
| 204 | if reseal then | |
| 205 | turtle.placeDown() | |
| 206 | end |