Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --==================================================================--
- --==================================================================--
- -- Holds the basics to be used in future turtle projects --
- -- Includes: --
- -- 1. movement in the six directions --
- -- 2. picking up and uses fuel from chest --
- -- 3. empty entire turtle contents to chest in front of it --
- -- --
- -- --
- -- --
- -- --
- -- --
- -- --
- -- author: jhurst created: edited: --
- --==================================================================--
- --==================================================================--
- local d, x, y, z = 1, 0, 0, 0
- --===============================================================--
- --1. movement in the six directions --
- --===============================================================--
- function moveForward(count)
- --
- --moves forward a number of blocks
- --CAN handle sand/gravel
- --counts the position
- --
- check()
- for a = 1, count, 1 do
- if d == 1 then
- y = y + 1
- end
- if d == 2 then
- x = x + 1
- end
- if d == 3 then
- y = y - 1
- end
- if d == 4 then
- x = x - 1
- end
- turtle.dig()
- while not turtle.forward() do
- turtle.dig()
- turtle.attack()
- os.sleep(1)
- end
- end
- end
- function moveBack(count)
- --
- --moves back a number of blocks
- --CAN handle sand/gravel
- --
- for b = 1, count, 1 do
- if not turtle.back() then
- if d == 1 then
- y = y - 1
- end
- if d == 3 then
- y = y + 1
- end
- if d == 2 then
- x = x - 1
- end
- if d == 4 then
- x = x + 1
- end
- turtle.turnRight()
- turtle.turnRight()
- while not turtle.forward() do
- turtle.dig()
- turtle.attack()
- os.sleep(1)
- end
- turtle.turnRight()
- turtle.turnRight()
- end
- end
- end
- function moveR(count)
- --
- --turns and moves right a number of blocks
- --CAN handle sand/gravel
- --counts the direction: N=1 and so forth
- --
- turtle.turnRight()
- d = d + 1
- if d == 5 then
- d = 1
- end
- for c = 1, count, 1 do
- while turtle.detect() do
- turtle.dig()
- os.sleep(1)
- end
- moveForward(1)
- end
- end
- function moveL(count)
- --
- --turns and moves left a number of blocks
- --CAN handle sand/gravel
- --
- turtle.turnLeft()
- d = d - 1
- if d == 0 then
- d = 4
- end
- for aa = 1, count, 1 do
- while turtle.detect() do
- turtle.dig()
- os.sleep(1)
- end
- moveForward(1)
- end
- end
- function moveDown(count)
- --
- --moves down by destroying the block underneath it
- --
- for e = 1, count, 1 do
- z = z - 1
- while not turtle.down() do
- turtle.digDown()
- turtle.attackDown()
- os.sleep(1)
- end
- end
- end
- function moveUp(count)
- --
- --moves up by destroying block above it
- --does not handle sand/gravel on top
- --
- for f = 1, count, 1 do
- z = z + 1
- while not turtle.up() do
- turtle.digUp()
- turtle.attackUp()
- os.sleep(1)
- end
- end
- end
- --===============================================================--
- --2. picking up fuel from chest and fuelling from any slot --
- --===============================================================--
- function fuelChest()
- --
- --picks up fuel from chest in FRONT of it and refuels
- --
- turtle.select(1)
- turtle.suck()
- fuel()
- end
- function fuel()
- --
- --fuels from first burnable item in inventory
- --
- fuelSlot = 1
- turtle.select(fuelSlot)
- while not turtle.refuel() do
- fuelSlot = fuelSlot + 1
- turtle.select(fuelSlot)
- end
- turtle.select(1)
- end
- --===============================================================--
- --3. empty entire turtle contents to chest in front of it --
- --===============================================================--
- function emptyToChest()
- --
- --empties entire contents of turtle to chest in FRONT of it
- --
- for slot = 2, 16, 1 do
- turtle.select(slot)
- turtle.drop()
- end
- turtle.select(1)
- if turtle.getItemCount(1) >1 then
- turtle.drop(turtle.getItemCount(1)-1)
- end
- end
- --===============================================================--
- --4. pitStop() bringing turtle to original position --
- --===============================================================--
- function pitStop(num)
- --keeps track of original coordinates
- local oldx = x
- local oldy = y
- local oldz = z
- local oldd = d
- --turns turtle to point south or d = 3
- if d ~= 3 then
- if d == 1 then
- turtle.turnRight()
- turtle.turnRight()
- end
- if d == 2 then
- turtle.turnRight()
- end
- if d == 4 then
- turtle.turnLeft()
- end
- end
- d = 3
- --moves turtle original starting position
- moveForward(y)
- turtle.turnRight()
- d = 4
- moveForward(x)
- turtle.turnRight()
- d = 1
- moveUp(math.abs(z))
- --if low on fuel, num will be 0
- --empties inventory then refuels
- if num == 0 then
- turtle.turnLeft()
- emptyToChest()
- turtle.turnLeft()
- fuelChest()
- turtle.turnLeft()
- turtle.turnLeft()
- end
- --if inventory full, num will be 1
- --empties inventory
- if num == 1 then
- turtle.turnLeft()
- emptyToChest()
- turtle.turnRight()
- end
- --returns turtle to spot to resume digging
- moveDown(math.abs(oldz))
- turtle.turnLeft()
- d = 4
- moveBack(oldx)
- turtle.turnLeft()
- d = 3
- moveBack(oldy)
- --faces turtle back to original direction
- if oldd ~= 3 then
- if oldd == 1 then
- turtle.turnRight()
- turtle.turnRight()
- end
- if oldd == 2 then
- turtle.turnLeft()
- end
- if oldd == 4 then
- turtle.turnRight()
- end
- end
- --resets coordinates to original values
- d = oldd
- y = oldy
- x = oldx
- z = oldz
- end
- function pitStopCheck()
- --returns to start if low fuel
- if (y + (x*2) - (z*2)) > turtle.getFuelLevel() - 10 then
- pitStop(0)
- end
- --returns to start if inventory is full
- --total is total number of blocks in inventory
- --totalOcc is number of used inventory slots in turtle
- local total = 0
- local totalOcc = 0
- for check = 2, 16, 1 do
- total = total + turtle.getItemCount(check)
- if turtle.getItemCount(check) > 0 then
- totalOcc = totalOcc + 1
- end
- end
- if total > 950 or totalOcc >= 14 then
- pitStop(1)
- end
- end
- function check()
- turtle.select(1)
- if not turtle.compareUp() then
- turtle.digUp()
- end
- if not turtle.compareDown() then
- turtle.digDown()
- end
- turtle.turnRight()
- if not turtle.compare() then
- turtle.dig()
- end
- turtle.turnLeft()
- turtle.turnLeft()
- if not turtle.compare() then
- turtle.dig()
- end
- turtle.turnRight()
- end
- function mine(length, width, height)
- --unloads anything in inventory and fuels
- turtle.select(2)
- turtle.turnLeft()
- emptyToChest()
- turtle.turnLeft()
- fuelChest()
- turtle.turnLeft()
- turtle.turnLeft()
- moveDown(1)
- for h = 1, height, 1 do
- for w = 1, width, 1 do
- for l = 1, length - 1, 1 do
- pitStopCheck()
- moveForward(1)
- end
- --turns turtle at the end of each row
- --if row is odd, turn right; left otherwise
- if math.fmod(h, 2) == 1 then
- if math.fmod(w, 2) == 1 then
- if w ~= width then
- moveR(3)
- turtle.turnRight()
- d = d + 1
- if d == 5 then
- d = 1
- end
- end
- else
- if w ~= width then
- moveL(3)
- turtle.turnLeft()
- d = d - 1
- if d == 0 then
- d = 4
- end
- end
- end
- else
- if math.fmod(w, 2) == 0 then
- if w ~= width then
- moveR(3)
- turtle.turnRight()
- d = d + 1
- if d == 5 then
- d = 1
- end
- end
- else
- if w ~= width then
- moveL(3)
- turtle.turnLeft()
- d = d - 1
- if d == 0 then
- d = 4
- end
- end
- end
- end
- end
- if h~=height then
- if math.fmod(h, 2) == 1 then
- moveL(1)
- turtle.turnLeft()
- d = d + 1
- if d == 5 then
- d = 1
- end
- moveDown(2)
- else
- moveR(1)
- turtle.turnRight()
- d = d + 1
- if d == 5 then
- d = 1
- end
- moveDown(2)
- end
- else
- end
- end
- --returns turtle to original location
- moveR(1)
- turtle.turnRight()
- d = d + 1
- if d == 5 then
- d = 1
- end
- moveUp(height*2 -1)
- turtle.turnLeft()
- emptyToChest()
- turtle.turnRight()
- end
- --prompts user for dimensions of quarry
- print("How many mines on each level?")
- print("(Even numbers only please:)")
- local ansWidth = tonumber(io.read())
- while math.fmod(ansWidth, 2) == 1 do
- print("Please pick an even number:")
- ansWidth = tonumber(io.read())
- end
- print("How many levels?")
- print("(Even numbers only please:)")
- local ansHeight = tonumber(io.read())
- while math.fmod(ansHeight, 2) == 1 do
- print("Please pick an even number:")
- ansHeight = tonumber(io.read())
- end
- print("How long for each mine?")
- local ansLength = tonumber(io.read())
- print("Please put any block you would like to ignore in the first turtle slot.")
- print("Type any key to continue and start the program:")
- io.read()
- mine(ansLength, ansWidth, ansHeight)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement