Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Predefinitions
- facing = "north"
- x = 0 --current turtle X
- y = 0 --current turtle Y
- z = 0 --current turtle Z
- mainY = 0 --main minning level(Y coordinate)
- mainFacing = "north" --main face(direction where the turtle will mine)
- totalBlocksMoved = 0 --counts the total of blocks the turtle has walked
- totalBlocksMined = 0 --counts the total of blocks the turtle has mined
- coordSequence = {} --Creates the table where coordinates will be stored for the AI
- -- General functions
- function table.contains(table, element)
- for _, value in pairs(table) do
- if value == element then
- return true
- end
- end
- return false
- end
- function testString(currentString,tryToFind)
- if string.find(currentString,tryToFind) then
- return true
- else
- return false
- end
- end
- --Rotation Functions
- function turn(side)
- if side == "right" then
- turtle.turnRight()
- if facing == "north" then --adjusts the direction the turtle is facing
- facing = "east"
- elseif facing == "east" then
- facing = "south"
- elseif facing == "south" then
- facing = "west"
- elseif facing == "west" then
- facing = "north"
- end
- elseif side == "left" then
- turtle.turnLeft()
- if facing == "north" then --adjusts the direction the turtle is facing
- facing = "west"
- elseif facing == "east" then
- facing = "north"
- elseif facing == "south" then
- facing = "east"
- elseif facing == "west" then
- facing = "south"
- end
- else
- print("The side where to turn is not specified!")
- end
- end
- function face(direction)
- if (direction == "north" and facing == "east") or (direction == "east" and facing == "south") or (direction == "south" and facing == "west") or (direction == "west" and facing == "north") then
- turn("left")
- elseif (facing == "north" and direction == "east") or (facing == "east" and direction == "south") or (facing == "south" and direction == "west") or (facing == "west" and direction == "north") then
- turn("right")
- elseif (facing == "north" and direction == "south") or (facing == "south" and direction == "north") or (facing == "west" and direction == "east") or (facing == "east" and direction == "west") then
- turn("right")
- turn("right")
- end
- end
- --Digging/Placing functions
- function dig()
- while turtle.detect() do
- if turtle.dig() then
- totalBlocksMined = totalBlocksMined + 1
- end
- end
- end
- function digUp()
- while turtle.detectUp() do
- if turtle.digUp() then
- totalBlocksMined = totalBlocksMined + 1
- end
- end
- end
- function digDown()
- while turtle.detectDown() do
- if turtle.digDown() then
- totalBlocksMined = totalBlocksMined + 1
- end
- end
- end
- function place()
- while not turtle.place() do
- dig()
- turtle.attack()
- end
- end
- function placeUp()
- while not turtle.placeUp() do
- digUp()
- turtle.attackUp()
- end
- end
- function placeDown()
- while not turtle.placeDown() do
- digDown()
- turtle.attackDown()
- end
- end
- --Movimentation Functions
- function forward()
- fuelTest()
- while not turtle.forward() do
- dig()
- turtle.attack()
- end
- if facing == "north" then
- z = z - 1
- elseif facing == "east" then
- x = x + 1
- elseif facing == "south" then
- z = z + 1
- elseif facing == "west" then
- x = x - 1
- end
- totalBlocksMoved = totalBlocksMoved + 1
- end
- function left()
- turn("left")
- forward()
- end
- function right()
- turn("right")
- forward()
- end
- function up()
- fuelTest()
- while not turtle.up() do
- digUp()
- turtle.attackUp()
- end
- y = y + 1
- totalBlocksMoved = totalBlocksMoved + 1
- end
- function down()
- fuelTest()
- while not turtle.down() do
- digDown()
- turtle.attackDown()
- end
- y = y - 1
- totalBlocksMoved = totalBlocksMoved + 1
- end
- function back()
- turn("right") turn("right")
- forward()
- end
- --Inspection Functions
- function isOre()
- local sucess, blockData = turtle.inspect()
- if sucess then
- if string.find(blockData.name,"ore") or string.find(blockData.name,"Ore") or string.find(blockData.name,"resource") or string.find(blockData.name,"Resource") then
- return true
- elseif string.find(blockData.name,"SearedBrick") then --Actual name of TConstruct ores, may be a bug, check it in your current version!
- return true
- else
- return false
- end
- else
- return false
- end
- end
- function isOreUp()
- local sucess, blockData = turtle.inspectUp()
- if sucess then
- if string.find(blockData.name,"ore") or string.find(blockData.name,"Ore") or string.find(blockData.name,"resource") or string.find(blockData.name,"Resource") then
- return true
- elseif string.find(blockData.name,"SearedBrick") then --Actual name of TConstruct ores, may be a bug, check it in your current version!
- return true
- else
- return false
- end
- else
- return false
- end
- end
- function isOreDown()
- local sucess, blockData = turtle.inspectDown()
- if sucess then
- if string.find(blockData.name,"ore") or string.find(blockData.name,"Ore") or string.find(blockData.name,"resource") or string.find(blockData.name,"Resource") then
- return true
- elseif string.find(blockData.name,"SearedBrick") then --Actual name of TConstruct ores, may be a bug, check it in your current version!
- return true
- else
- return false
- end
- else
- return false
- end
- end
- function isOreRight()
- turn("right")
- local result = isOre()
- turn("left")
- return result
- end
- function isOreLeft()
- turn("left")
- local result = isOre()
- turn("right")
- return result
- end
- function isOreBack()
- turn("right") turn("right")
- local result = isOre()
- turn("left") turn("left")
- return result
- end
- --Inventory Cleaning Functions
- function dropAll()
- turtle.select(15)
- placeDown()
- for slot = 1,14 do
- turtle.select(slot)
- if not turtle.dropDown() and turtle.getItemCount() > 0 then
- while not turtle.dropDown() do
- print("Please, clear space in the item enderchest!")
- sleep(10)
- end
- end
- end
- turtle.select(15)
- digDown()
- end
- --Fuel Functions
- function refuel()
- while turtle.getFuelLevel() < 20 do
- dropAll()
- turtle.select(16)
- placeDown()
- if not turtle.suckDown(1) then
- while not turtle.suckDown(1) do
- turtle.select(16)
- print("Please add a source of fuel in the enderchest!")
- end
- end
- if not turtle.refuel() then
- while not turtle.refuel() do
- print("Please, make sure that the items in the enderchest in the 16th slot is filled with a kind of solid fuel!")
- sleep(10)
- end
- end
- turtle.select(16)
- digDown()
- end
- end
- function fuelTest()
- if turtle.getFuelLevel() < 20 then
- refuel()
- end
- end
- --Positioning functions
- function headTo(targetX,targetY,targetZ)
- local lastFacing = facing -- gets which direction the turtle was facing before the function started
- while (targetX ~= x) or (targetY ~= y) or (targetZ ~= z) do
- local xDistance = (x - targetX)
- local yDistance = (y - targetY)
- local zDistance = (z - targetZ)
- if xDistance < 0 then
- face("east")
- forward()
- elseif xDistance > 0 then
- face("west")
- forward()
- end
- if yDistance < 0 then
- up()
- elseif yDistance > 0 then
- down()
- end
- if zDistance < 0 then
- face("south")
- forward()
- elseif zDistance > 0 then
- face("north")
- forward()
- end
- end
- face(lastFacing) -- makes the turtle face the direction the turtle was facing before the function started
- end
- --AI functions
- function storeCoords()
- table.insert(coordSequence, {
- x = x,
- y = y,
- z = z,
- })
- end
- function veinMiner()
- local baseX = x
- local bazeZ = z
- local veinClean = false
- while not veinClean do
- if isOre() then
- storeCoords()
- forward()
- elseif isOreUp() then
- storeCoords()
- up()
- elseif isOreDown() then
- storeCoords()
- down()
- elseif isOreLeft() then
- storeCoords()
- left()
- elseif isOreRight() then
- storeCoords()
- right()
- elseif isOreBack() then
- storeCoords()
- back()
- else
- if #coordSequence ~= 0 then
- headTo(coordSequence[#coordSequence]["x"],coordSequence[#coordSequence]["y"],coordSequence[#coordSequence]["z"])
- table.remove(coordSequence,#coordSequence)
- else
- veinClean = true
- end
- end
- end
- headTo(baseX,mainY,bazeZ)
- face(mainFacing)
- end
- --Setup
- print("Welcome to Smart Miner alpha 0.3 by KakarotoCm")
- sleep(4.2)
- print("Please, put an enderchest filled with fuel(Coal or charcoal preferably) in the selected slot")
- while turtle.getItemCount(16) < 1 do
- turtle.select(16)
- end
- print("Now, please, put an enderchest to receive the items in the selected slot!")
- while turtle.getItemCount(15) < 1 do
- turtle.select(15)
- end
- print("Now we'll need you to get us some information!")
- sleep(3.5)
- print("Stand above the turtle, press F3 and please tell us YOUR ")
- print("X coordinate(Only non decimal digits): ")
- x = read()
- print(" ")
- print("Y coordinate(Only non decimal digits): ")
- y = read()
- print(" ")
- print("Z coordinate(Only non decimal digits): ")
- z = read()
- print(" ")
- print("OK, now you need to tell us which direction the turtle is facing, to do this look in the same direction its looking and look the tab f: , remember to write everything in lower case!")
- isFacingReady = false
- while not isFacingReady do
- print("Facing direction(cardinal points): ")
- facing = read()
- if not (testString(facing,"north") or testString(facing,"south") or testString(facing,"east") or testString(facing,"west")) then
- print("You didn't write a valid answer, the valid answers are north, east, south and west(everything in lower case)")
- else
- isFacingReady = true
- end
- end
- isFacingReady = false
- print("Thank you, we will be running the debug process and will be starting in a while")
- --Pre-execution debug
- dropAll()
- refuel()
- mainFacing = facing
- mainY = y
- print("Debug process finished, starting main program!")
- --Main Program
- while true do
- veinMiner()
- forward()
- end
Advertisement
Add Comment
Please, Sign In to add comment