Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --This code is developed by me (onix331) and is available under the GNU General Public License (Version 3)
- --For all CC-Versions Turtle-Side!
- --See http://opensource.org/licenses/GPL-3.0
- --Have fun
- space = 16--Defines the space of the turtle (If an update comes out and I don't update the program)
- ver = "2.6"--For updates
- status = "---"--You can modify the init-status that will be transferred to the host
- --It is usually not necessary to modify the next variables
- fw = 0--Unnecessary to know:P
- chest = 0--If the turtle has stopped at a chest
- stop = 0--If the turtle has stopped at a chest
- shaftrun = 0--How many shafts the turtle has run
- senderId = 65534--Because RedNet is a bitch:D
- function smartfuel()--Check for fuel and asks for refueling
- clear()
- local fuelneeded = (length * count) * 2 + count * 3 --Calculate Fuel for mining blocks
- local movefuelneeded = ((length * count) * 2 + count) * 0.6 --Calculate Fuel for movement
- local fuelneeded = fuelneeded + movefuelneeded
- if turtle.getFuelLevel() < fuelneeded then
- insert = fuelneeded - turtle.getFuelLevel()
- print ("Current fuel-level is too low (", turtle.getFuelLevel(), ")")
- print ("Needed:" , fuelneeded)
- print ("Please insert ", insert, " fuel anywhere")
- print ("When done, press ENTER")
- debugfuel = turtle.getFuelLevel()
- while true do
- local sEvent, param = os.pullEvent("key")
- if sEvent == "key" then
- if param == 28 then
- for i = 1, space do
- for i = 1, 64 do
- clear()
- if turtle.getFuelLevel() < fuelneeded then
- shell.run("refuel")
- else
- turtle.select(1)
- break
- end
- end
- if debugfuel > turtle.getFuelLevel() then
- turtle.select(debugi)
- else
- debugi = i
- turtle.select(i)
- end
- end
- turtle.select(1)
- break
- end
- end
- end
- if turtle.getFuelLevel() < fuelneeded then
- smartfuel()
- end
- end
- end
- function pair()
- print ("Sending pairing requests")
- local i = 0
- rednet.broadcast("pair")
- os.sleep(0.1)
- rednet.broadcast("pair")
- os.sleep(0.1)
- rednet.broadcast("pair")
- os.sleep(0.1)
- rednet.broadcast("pair")
- os.sleep(0.1)
- rednet.broadcast("pair")
- os.sleep(0.1)
- rednet.broadcast("pair")
- os.sleep(0.1)
- senderId, message, protocol = rednet.receive()
- if message=="succesfull" then
- print("Succesfull paired, continuing...")
- os.sleep(0.1)
- end
- end
- function clear()--To clear the screen
- term.clear()
- term.setCursorPos(1,1)
- end
- function sendinitial()--Send the initial data
- if enablerednet=="y" then
- os.sleep(1)
- rednet.send(senderId, length)
- os.sleep(0.1)
- rednet.send(senderId, count)
- os.sleep(0.1)
- rednet.send(senderId, direction)
- os.sleep(0.1)
- end
- end
- function senddata()--To send all the data
- if enablerednet=="y" then
- rednet.send(senderId, lengthleft)
- rednet.send(senderId, count)
- rednet.send(senderId, turtle.getFuelLevel())
- rednet.send(senderId, status)
- end
- end
- function main()--Main Program
- smartfuel()
- if enablerednet=="y" then
- pair()
- sendinitial()
- end
- status = "Called main"
- senddata()
- if called==1 then
- called = 0
- countn = countn - 1
- end
- while true do
- if tonumber(count)>=1 then
- senddata()
- if direction=="r" then
- fw = 1
- move()
- turnaround()
- fw = 0
- move()
- count = count - 1
- checkspace()
- elseif direction=="l" then
- fw = 1
- move()
- turnaround()
- fw = 0
- move()
- count = count - 1
- checkspace()
- end
- else
- break
- end
- end
- end
- function returnmining()--To return to the latest shaft
- status = "Return to mine..."
- senddata()
- chest = 0
- stop = 1
- if direction=="r" then
- turtle.turnRight()
- for i = 0, shaftrun do
- turtle.forward()
- turtle.forward()
- turtle.forward()
- end
- turtle.turnLeft()
- main()
- elseif direction=="l" then
- turtle.turnLeft()
- for i = 0, shaftrun do
- turtle.forward()
- turtle.forward()
- turtle.forward()
- end
- turtle.turnRight()
- main()
- end
- end
- function checkl() --To check if length is valid (>0, not a String), also transforms the string into a number
- length = tonumber(length)
- if length==nil then
- print ("Invalid number!")
- length = read()
- checkl()
- elseif length <= 0 then
- print ("Invalid number!")
- length = read()
- checkl()
- end
- end
- function checka()--To check if count is valid (>0, not a String), also transforms the string into a number
- count = tonumber(count)
- if count==nil then
- print ("Invalid number!")
- count = read()
- checka()
- elseif count <= 0 then
- print ("Invalid number!")
- count = read()
- checka()
- end
- end
- function checkr()--To check if direction is valid ((r) or (l))
- if direction=="r" then
- print ("")
- elseif direction=="l" then
- print ("")
- else
- print ("Invalid input!")
- direction = io.read()
- checkr()
- end
- end
- function checkfue()--To check if fuel is valid (ok))
- if fuel=="ok" then
- print ("")
- else
- print ("Invalid input!")
- fuel = io.read()
- checkfue()
- end
- end
- function checkred()--To check if enablerednet is valid ((y) or (n))
- if enablerednet=="y" then
- print ("")
- elseif enablerednet=="n" then
- print ("")
- else
- print ("Invalid input!")
- enablerednet = io.read()
- checkred()
- end
- end
- function move()--Basic movement function
- status = "Moving..."
- local debug = 4--Used to avoid gavel, sand, etc.
- for i = 1, length do
- senddata()
- for i = 1, debug do
- turtle.dig()
- end
- turtle.forward()
- if fw==1 then
- lengthleft = lengthleft - 1
- end
- turtle.digDown()
- for i = 1, debug do
- turtle.digUp()
- end
- for i = 1, debug do
- turtle.digDown()
- end
- end
- end
- function turnaround()--Turns the turtle at 90 degrees
- status = "Turning around..."
- senddata()
- turtle.turnRight()
- turtle.turnRight()
- end
- function nextshaftr()--Next shaft (right)
- status = "Going to next shaft..."
- senddata()
- countn = countn - 1
- if countn>=2 then
- shaftrun = shaftrun + 1
- lengthleft = lengthleftstore
- turtle.turnLeft()
- turtle.dig()
- turtle.digUp()
- turtle.digDown()
- turtle.forward()
- turtle.dig()
- turtle.digUp()
- turtle.digDown()
- turtle.forward()
- senddata()
- turtle.dig()
- turtle.digUp()
- turtle.digDown()
- turtle.forward()
- turtle.digUp()
- turtle.digDown()
- turtle.turnLeft()
- end
- end
- function nextshaftl()--Next shaft (left)
- status = "Going to next shaft..."
- senddata()
- countn = countn - 1
- if countn>=2 then
- shaftrun = shaftrun + 1
- lengthleft = lengthleftstore
- turtle.turnRight()
- turtle.dig()
- turtle.digUp()
- turtle.digDown()
- turtle.forward()
- turtle.dig()
- turtle.digUp()
- turtle.digDown()
- turtle.forward()
- senddata()
- turtle.dig()
- turtle.digUp()
- turtle.digDown()
- turtle.forward()
- turtle.digUp()
- turtle.digDown()
- turtle.turnRight()
- end
- end
- function returnpoint()--Return to initial point (Chest)
- status = "Returning to chest..."
- senddata()
- if direction=="r" then
- turtle.turnRight()
- for i = 1, shaftrun do
- turtle.forward()
- turtle.forward()
- senddata()
- turtle.forward()
- end
- if stop==1 then
- turtle.forward()
- turtle.forward()
- senddata()
- turtle.forward()
- end
- turtle.turnLeft()
- status = "Dropping things in chest..."
- senddata()
- for i = 1, space do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1)
- elseif direction=="l" then
- turtle.turnLeft()
- for i = 1, shaftrun do
- turtle.forward()
- turtle.forward()
- senddata()
- turtle.forward()
- end
- if stop==1 then
- turtle.forward()
- turtle.forward()
- senddata()
- turtle.forward()
- end
- turtle.turnRight()
- status = "Dropping things in chest..."
- senddata()
- for i = 1, space do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1)
- end
- turnaround()
- if chest==1 then
- returnmining()
- end
- end
- function checkspace()--Check for space and if necessary, return to initial point to the chest
- status = "Checking space"
- senddata()
- local spaceleft = 0
- for i = 1, space do
- if turtle.getItemSpace(i) > 10 then
- spaceleft = spaceleft + 1
- end
- end
- if spaceleft < 3 then
- chest = 1
- called = 1
- status = "Inventory full, returning to chest"
- returnpoint()
- elseif direction=="r" then
- nextshaftr()
- else
- nextshaftl()
- end
- end
- --Print header
- clear()
- print ("Strip-mining-program by onix331")
- print ("Version ", ver)
- os.sleep(2)
- --Start user input and define some variables
- clear()
- print ("Length of the single shaft(s):(Type help for help)")
- length = read()
- if length=="help" then
- print ("How long the shafts the turtle will mine be")
- length = read()
- checkl()
- else
- checkl()
- end
- lengthleft = length
- lengthleftstore = length
- clear()
- print ("Count of the shafts:(Type help for help)")
- count = read()
- if count=="help" then
- print ("How much shafts the turtle will mine")
- count = read()
- checka()
- else
- end
- countn = count
- countn = countn + 1
- clear()
- print ("Direction of the shafts (Left (l), Right (r):(Type help for help)")
- direction = read()
- if direction=="help" then
- print ("In which direction the shafts will go")
- direction = read()
- checkr()
- else
- checkr()
- end
- clear()
- print ("Use rednet? (Yes (y), No (n):(Type help for help)")
- enablerednet = read()
- if enablerednet=="help" then
- print ("If RedNet and a hostcomputer should be used (See my thread at http://www.computercraft.info/forums2/index.php?/topic/19860-stripmining-turtle-for-cc16/")
- enablerednet = read()
- else
- checkred()
- end
- if enablerednet=="y" then
- rednet.open("right")--Defines the place of the modem (Usually right on the turtle)
- end
- --Print user inputs
- clear()
- print ("Input of Length:")
- print (length)
- print ("Input of count")
- print (count)
- print ("Direction of the shafts:")
- if direction=="r" then
- print (direction, "ight")
- else
- print (direction, "eft")
- end
- print ("3")
- os.sleep(1)
- print ("2")
- os.sleep(1)
- print ("1")
- --Fire up the main program
- main()
- ende = 1
- returnpoint()
- status = "Done!"
- senddata()--Sends the status "Done!" to the host
- print ("Done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement