Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------------------------------------------------------------------------------
- -- 04.22.2017 - Started scripting a basic Turtle AI to help out with strip mining.
- -- This turtle is not really very smart; it will just mine in a straight line until it runs out of fuel,
- -- A pathway 3 blocks tall, placing torches every 5 blocks along the ground (assuming there is valid ground to do so)
- -- -- Note: If the turtle runs out of torches, it will also stop.
- --
- ----------------------------------------------------------------------------------------------------------
- ----------------------------------------------------------------------------------------------------------
- -- Settings
- ----------------------------------------------------------------------------------------------------------
- local itemNameFuel = "minecraft:coal" -- The name of the fuel the turtle will be using (Can only accept 1 type of fuel.)
- local itemNameTorch = "minecraft:torch" -- The name of the torches we are using.
- local distanceBetweenTorches = 7 -- The number of squares between the torches.
- local waitForTorchesOnPlaceTorch = true -- If we are set to place a torch right now, and we don't have torches, let's wait for the torch to be put in our inventory.
- ----------------------------------------------------------------------------------------------------------
- -- Function Pointers / Documentation (Helper Functions)
- ----------------------------------------------------------------------------------------------------------
- -- Returns the index of the item in the inventory by the name provided. Returns nil if it can't be found.
- -- Note: If you are searching for a nil name, will return the first nil position.
- local ScanForItemInInventoryByName
- -- If the turtle is less than 90% fuel, will search for fuel in the turtle's inventory to refuel itself.
- local HandleFuelLevel
- -- If the turtle has a torch, place it. Sits and waits for torches to be added if it has no torches (so no mobs can spawn), if the setting is set to do so.
- local PlaceTorch
- -- StripMines; main function.
- local StripMine
- -- Infinite retries moving up.
- local RobustUp
- -- Infinitely retries moving down.
- local RobustDown
- -- Infinitely retries moving forward.
- local RobustForward
- ----------------------------------------------------------------------------------------------------------
- -- Function Definitions (Helper Functions)
- ----------------------------------------------------------------------------------------------------------
- -- Returns the index of the item in the inventory by the name provided. Returns nil if it can't be found.
- -- Note: If you are searching for a nil name, will return the first nil position.
- ScanForItemInInventoryByName = function(name, caseInsensitive)
- for i = 1, 16, 1 do
- local item = turtle.getItemDetail(i)
- if item then
- local itemName = item.name
- if caseInsensitive then
- itemName = string.lower(itemName)
- name = string.lower(name)
- end
- if name and string.match(itemName, name) and (name ~= itemNameBoneMeal or (name == itemNameBoneMeal and item.damage == itemMetaDataBoneMeal)) then
- return i
- end
- end
- if item == nil and name == nil then
- return i
- end
- end
- return nil
- end
- -- If the turtle is less than 90% fuel, will search for fuel in the turtle's inventory to refuel itself.
- HandleFuelLevel = function()
- if turtle.getFuelLevel() / turtle.getFuelLimit() < .9 then
- local archivedIndex = turtle.getSelectedSlot()
- local index = ScanForItemInInventoryByName(itemNameFuel)
- if index then
- print("Fuel is below 90%; Detected fuel in inventory. Refueling.")
- local currItem = turtle.getItemDetail(index)
- while turtle.getFuelLevel() < turtle.getFuelLimit() and currItem and currItem.name == itemNameFuel do
- turtle.select(index)
- turtle.refuel(1)
- currItem = turtle.getItemDetail(index)
- end
- print("Fuel: "..turtle.getFuelLevel().." / "..turtle.getFuelLimit().." ("..(100 * (turtle.getFuelLevel() / turtle.getFuelLimit())).."%)")
- turtle.select(archivedIndex)
- else
- if turtle.getFuelLevel() == 0 then
- print("\r\n\r\nALERT! We are out of fuel!!! Weird stuff may happen (We may even crash!) Please give me fuel (Coal or charcoal)\r\n\r\n")
- end
- end
- end
- end
- -- If the turtle has a torch, place it. Sits and waits for torches to be added if it has no torches (so no mobs can spawn), if the setting is set to do so.
- PlaceTorch = function()
- local archivedIndex = turtle.getSelectedSlot()
- local torchIndex = ScanForItemInInventoryByName(itemNameTorch)
- local success = false
- if not torchIndex and waitForTorchesOnPlaceTorch then
- while not torchIndex do
- torchIndex = ScanForItemInInventoryByName(itemNameTorch)
- print("Waiting for item by name of "..itemNameTorch.." to be placed in inventory.")
- sleep(5)
- end
- elseif not torchIndex then
- return
- end
- if torchIndex then
- turtle.select(torchIndex)
- if turtle.placeDown() then
- print("Torch Placed!")
- success = true
- else
- print("Failed to place torch; is there air below?")
- success = false
- end
- end
- turtle.select(archivedIndex)
- return success
- end
- -- StripMines; main function.
- StripMine = function()
- HandleFuelLevel()
- if turtle.getFuelLevel() == 0 then
- print("Terminating Early; turtle started up without fuel. Place fuel in turtle and run program again.")
- return
- end
- local placesMovedSinceLastTorch = distanceBetweenTorches
- while true do
- placesMovedSinceLastTorch = placesMovedSinceLastTorch + 1
- -- Dig up, place a torch if necesary.
- for i = 1, 3, 1 do
- if placesMovedSinceLastTorch >= distanceBetweenTorches and i == 2 then
- if (PlaceTorch()) then
- placesMovedSinceLastTorch = 0
- end
- end
- turtle.dig()
- if turtle.inspectUp() then
- turtle.digUp()
- end
- RobustUp()
- end
- turtle.dig()
- RobustForward()
- for i = 1, 3, 1 do
- if turtle.inspectDown() then
- turtle.digDown()
- end
- RobustDown()
- end
- HandleFuelLevel()
- end
- end
- -- Infinite retries moving up.
- RobustUp = function()
- local success = false
- while not success do
- success = turtle.up()
- end
- end
- -- Infinitely retries moving down.
- RobustDown = function()
- local success = false
- while not success do
- success = turtle.down()
- end
- end
- -- Infinitely retries moving forward.
- RobustForward = function()
- local success = false
- while not success do
- success = turtle.forward()
- end
- end
- ----------------------------------------------------------------------------------------------------------
- -- TESTING!!!
- ----------------------------------------------------------------------------------------------------------
- --HandleFuelLevel()
- --PlaceTorch()
- ----------------------------------------------------------------------------------------------------------
- -- Main EntryPoint
- ----------------------------------------------------------------------------------------------------------
- print("Executing StripMine")
- StripMine()
- print("StripMine Terminated.")
Add Comment
Please, Sign In to add comment