Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function FirstScreen()
- term.clear()
- term.setCursorPos(1,1)
- write("My First Minining Program")
- sleep(0.5)
- end
- function setVariables()
- fuelLevel = 0
- x = 0
- z = 0
- y = 0
- f = 0
- DigX = 0
- DigZ = 0
- DigDepth = 0
- OriginX = 0
- OriginZ = 0
- OriginY = 0
- OriginF = 0
- GoBackToY = 0
- GoBackToX = 0
- GoBackToZ = 0
- GoBackToF = 0
- BlocksMined = 1
- BlocksToBeMined = 0
- BlocksMinedPercent = 0
- ErrorMovingCount = 0
- ComputerID = 0
- TurtleID = os.getComputerID()
- TurtleLabel = os.getComputerLabel()
- end
- function ClearScreen()
- term.clear()
- term.setCursorPos(1,1)
- end
- function AskForDimension()
- term.setCursorPos(1,1)
- write("Width?")
- term.setCursorPos(1,2)
- input = read()
- DigX = input
- ClearScreen()
- term.setCursorPos(1,1)
- write("Length?")
- term.setCursorPos(1,2)
- input = read()
- DigZ = input
- ClearScreen()
- --term.setCursorPos(1,1)
- --write("Depth?")
- --input = read()
- --DigDepth = input
- --ClearScreen()
- term.setCursorPos(1,1)
- write("Please place unwanted blocks in slot 1 and slot 2")
- term.setCursorPos(1,3)
- write("Press any key to continue")
- input = read()
- end
- function CalculatePercent()
- BlocksMinedPercent = tonumber(BlocksMined) / tonumber(BlocksToBeMined) * 100
- return BlocksMinedPercent
- end
- function AskForCoordinate()
- ClearScreen()
- term.setCursorPos(1,1)
- write("What is the ComputerID?")
- input = read()
- ComputerID = input
- ClearScreen()
- term.setCursorPos(1,1)
- write("What is the x-Position of the Mining Turtle?")
- input = read()
- x = input
- ClearScreen()
- term.setCursorPos(1,1)
- write("What is the z-Position of the Mining Turtle?")
- input = read()
- z = input
- ClearScreen()
- term.setCursorPos(1,1)
- write("What is the y-Position of the Mining Turtle?")
- input = read()
- y = input
- ClearScreen()
- term.setCursorPos(1,1)
- write("What is the direction of the Mining Turtle? (0 = south, 1 = west, 2 = north, 3 = east)")
- input = read()
- f = input
- ClearScreen()
- term.setCursorPos(1,1)
- write("Is that right? (yes/no)")
- term.setCursorPos(1,2)
- write("x: "..x.."")
- term.setCursorPos(1,3)
- write("z: "..z.."")
- term.setCursorPos(1,4)
- write("y: "..y.."")
- term.setCursorPos(1,5)
- write("Cardinal direction: "..CardinalPoint(f).." ("..f..")")
- term.setCursorPos(1,6)
- input = read()
- if input == "no" then
- AskForCoordinate()
- else
- ClearScreen()
- write("Nice!")
- FirstMonitorScreen()
- end
- OriginX = x
- OriginY = y
- OriginZ = z
- OriginF = f
- end
- function CardinalPoint(f)
- if tostring(f) == "0" then
- return "south"
- elseif tostring(f) == "1" then
- return "west"
- elseif tostring(f) == "2" then
- return "north"
- elseif tostring(f) == "3" then
- return "east"
- else
- return "INVALID"
- end
- end
- function ClearMonitor()
- sleep(.1)
- rednet.send(tonumber(ComputerID), "clear()")
- sleep(.1)
- end
- function OpenConnection()
- rednet.open("left")
- end
- function DetermineBlocksToBeMined()
- BlocksToBeMined = tonumber(DigX) * tonumber(DigZ) * tonumber(OriginY)
- end
- function UpdateMonitor()
- message = ""
- message = message.."SCP(13,2);"..getFuelLevel().." ;"
- message = message.."SCP(4,3);"..x.." ;"
- message = message.."SCP(4,4);"..z.." ;"
- message = message.."SCP(4,5);"..y.." ;"
- message = message.."SCP(21,6);"..CardinalPoint(f).." ("..f..") ;"
- message = message.."SCP(21,7);"..BlocksToBeMined.." ;"
- message = message.."SCP(15,8);"..BlocksMined..", "..BlocksMinedPercent.."% ;"
- rednet.send(tonumber(ComputerID), message)
- end
- function FirstMonitorScreen()
- -- ";" for the end of a message
- -- "_" for the next line, with ";" so "_;" !
- ClearMonitor()
- message = ""
- message = message.."Name: "..TurtleLabel..", ID: "..TurtleID..";_;"
- message = message.."Fuel Level: "..getFuelLevel()..";_;"
- message = message.."x: "..x..";_;"
- message = message.."z: "..z..";_;"
- message = message.."y: "..y..";_;"
- message = message.."Cardinal direction: "..CardinalPoint(f).." ("..f..");_;"
- message = message.."Blocks to be mined: "..BlocksToBeMined..";_;"
- message = message.."Blocks mined: "..BlocksMined..";_;"
- rednet.send(tonumber(ComputerID), message)
- end
- function getFuelLevel()
- fuelLevel = turtle.getFuelLevel()
- return fuelLevel
- end
- function MoveForward()
- if turtle.forward() then
- ErrorMovingCount = 0
- if tonumber(f) == tonumber(0) then
- z = z + 1
- elseif tonumber(f) == tonumber(1) then
- x = x - 1
- elseif tonumber(f) == tonumber(2) then
- z = z - 1
- elseif tonumber(f) == tonumber(3) then
- x = x + 1
- end
- else
- print("cannot move for: "..ErrorMovingCount)
- turtle.attack()
- MoveForward()
- ErrorMovingCount = ErrorMovingCount + 1
- end
- UpdateMonitor()
- end
- function digDown()
- turtle.digDown()
- BlocksMined = BlocksMined + 1
- MoveDown()
- InventoryManagement()
- end
- function digForward()
- turtle.dig()
- BlocksMined = BlocksMined + 1
- MoveForward()
- InventoryManagement()
- end
- function digUp()
- turtle.digUp()
- BlocksMined = BlocksMined + 1
- MoveDown()
- InventoryManagement()
- end
- function MoveUp()
- turtle.up()
- y = y + 1
- UpdateMonitor()
- end
- function MoveDown()
- turtle.down()
- y = y - 1
- UpdateMonitor()
- end
- function TurnRight()
- turtle.turnRight()
- if tonumber(f) == tonumber(3) then
- f = 0
- else
- f = f + 1
- end
- UpdateMonitor()
- end
- function TurnLeft()
- turtle.turnLeft()
- if f == 0 then
- f = 3
- else
- f = f - 1
- end
- UpdateMonitor()
- end
- function InventoryManagement()
- local freeSlots = 0
- if turtle.getItemCount(16) ~= 0 then
- -- Drop unwanted blocks set in slot 1
- for i = 3, 16 do
- turtle.select(i)
- if turtle.compareTo(1) then
- turtle.drop()
- end
- end
- -- Drop unwanted blocks set in slot 2
- for i = 3, 16 do
- turtle.select(i)
- if turtle.compareTo(2) then
- turtle.drop()
- end
- end
- for i = 1, 2 do
- turtle.select(i)
- turtle.drop(tonumber(turtle.getItemCount((i)) - tonumber(1)))
- end
- end
- -- -- Rearrange the Inventory
- -- --Get number of free slots
- -- for i = 3, 16 do
- -- if tonumber(turtle.getItemCount(i)) == tonumber(0) then
- -- freeSlots = freeSlots + 1
- -- end
- -- end
- -- turtle.select(16)
- -- for i = 3, 15 do
- -- if turtle.transferTo(i) then
- -- break
- -- end
- -- end
- -- setzen auf startposition
- turtle.select(1)
- if turtle.getItemCount(16) ~= 0 then
- GoToChest()
- EmptyTurtle()
- GoBackToMining()
- end
- end
- function EmptyTurtle()
- for o = 1, 2 do
- turtle.select(o)
- turtle.drop(tonumber(turtle.getItemCount(o)) - tonumber(1))
- end
- for i = 3, 16 do
- turtle.select(i)
- turtle.drop()
- end
- turtle.select(1)
- end
- function GoBackToMining()
- print("X: "..GoBackToX.." Z: "..GoBackToZ)
- TurnLeft()
- TurnLeft()
- if tonumber(GoBackToZ) > 0 then
- for i = 1, GoBackToZ do
- MoveForward()
- end
- end
- TurnRight()
- if tonumber(GoBackToX) > 0 then
- for i = 1, GoBackToX do
- MoveForward()
- end
- end
- if tonumber(GoBackToY) > 0 then
- for i = 1, GoBackToY do
- MoveDown()
- end
- end
- while tonumber(f) ~= tonumber(GoBackToF) do
- TurnRight()
- end
- GoBackToX = 0
- GoBackToY = 0
- GoBackToZ = 0
- end
- function OppositeDirection(Direction)
- if tonumber(Direction) == tonumber(0) then
- return "2"
- elseif tonumber(Direction) == tonumber(1) then
- return "3"
- elseif tonumber(Direction) == tonumber(2) then
- return "0"
- elseif tonumber(Direction) == tonumber(3) then
- return "1"
- end
- end
- function GoToChest()
- GoBackToF = f
- -- Y-Richtung
- while tonumber(y) ~= tonumber(OriginY) do
- MoveUp()
- GoBackToY = GoBackToY + 1
- end
- -- X-Richtung
- if tonumber(x) ~= tonumber(OriginX) then
- --print("x: "..x.."; OriginalX"..OriginX.."f: "..f.."; OriginalF: "..OriginF)
- if tonumber(x) <= tonumber(OriginX) then
- while tonumber(f) ~= tonumber(3) do
- TurnRight()
- end
- elseif tonumber(x) >= tonumber(OriginX) then
- while tonumber(f) ~= tonumber(1) do
- TurnRight()
- end
- end
- end
- while tonumber(x) ~= tonumber(OriginX) do
- MoveForward()
- GoBackToX = GoBackToX + 1
- -- print("x: "..x.."; OriginalX"..OriginX.."f: "..f.."; OriginalF: "..OriginF)
- end
- -- Z-Richtung
- if tonumber(z) ~= tonumber(OriginZ) then
- -- print("x: "..z.."; OriginalX"..OriginZ.."f: "..f.."; OriginalF: "..OriginF)
- if tonumber(z) <= tonumber(OriginZ) then
- while tonumber(f) ~= tonumber(0) do
- TurnRight()
- end
- elseif tonumber(z) >= tonumber(OriginZ) then
- while tonumber(f) ~= tonumber(2) do
- TurnRight()
- end
- end
- end
- while tonumber(z) ~= tonumber(OriginZ) do
- MoveForward()
- GoBackToZ = GoBackToZ + 1
- -- print("x: "..z.."; OriginalX"..OriginZ.."f: "..f.."; OriginalF: "..OriginF)
- end
- -- print("f: "..f.."Opposite: "..OppositeDirection(OriginF))
- while tonumber(f) ~= tonumber(OppositeDirection(OriginF)) do
- TurnLeft()
- print("f: "..f.."Opposite: "..OppositeDirection(OriginF))
- end
- end
- function CurveRight()
- TurnRight()
- digForward()
- TurnRight()
- end
- function CurveLeft()
- TurnLeft()
- digForward()
- TurnLeft()
- end
- function ExcavateOneLayer()
- CurveDirection = "right"
- for o = 2, DigZ do
- for i = 2, DigX do
- digForward()
- end
- if CurveDirection == "right" then
- CurveRight()
- CurveDirection = "left"
- elseif CurveDirection == "left" then
- CurveLeft()
- CurveDirection = "right"
- end
- end
- for i = 2, DigX do
- digForward()
- end
- if CurveDirection == "right" then
- TurnLeft()
- for p = 2, DigZ do
- MoveForward()
- end
- TurnLeft()
- for p = 2, DigX do
- MoveForward()
- end
- TurnLeft()
- TurnLeft()
- elseif CurveDirection == "left" then
- TurnRight()
- for p = 2, DigZ do
- MoveForward()
- end
- TurnRight()
- end
- end
- function DigToBedrock()
- while true do
- if tonumber(y) == tonumber(1) then
- break
- end
- ExcavateOneLayer()
- digDown()
- end
- ExcavateOneLayer()
- GoToChest()
- EmptyTurtle()
- end
- setVariables()
- OpenConnection()
- FirstScreen()
- AskForCoordinate()
- getFuelLevel()
- AskForDimension()
- DetermineBlocksToBeMined()
- FirstMonitorScreen()
- DigToBedrock()
- write("finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement