Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local facingTable = {
- [0] = "south",
- [1]= "west",
- [2] = "north",
- [3] = "east",
- ["south"] = 0,
- ["west"] = 1,
- ["north"] = 2,
- ["east"] = 3,
- }
- local fuelTable = {
- ["Biofuel Can"] = 624,
- ["Scrap"] = 18,
- ["Coalfuel Can"] = 1824,
- ["Wooden Scaffolding"] = 18,
- ["Peat"] = 96,
- ["Wooden Tools"] = 12,
- ["Lava"] = 1200,
- ["Blaze Rod"] = 144,
- ["Wood Blocks"] = 18,
- ["Sticks"] = 6,
- ["Coal/Charcoal"] = 96,
- ["Mushroom Blocks"] = 18,
- ["Coal Coke"] = 192,
- }
- local vars = {
- currentFacing = 0
- }
- function saveVars()
- local handle = fs.open("Guidance.vars", "w")
- handle.write(textutils.serialize(vars))
- handle.close()
- end
- function loadVars()
- if fs.exists("guidance.vars") then
- local handle = fs.open("Guidance.vars", "r")
- vars = textutils.unserialize(handle.readAll())
- handle.close()
- end
- end
- function getPos()
- local currentX, currentY, currentZ = 0
- if vars.useGPS then
- rednet.open("right")
- currentX, currentY, currentZ = gps.locate(5)
- end
- if vars.deadReckon ~= nil and (currentX == nil or currentX == 0) then
- currentX = vars.deadReckon.x
- currentY = vars.deadReckon.y
- currentZ = vars.deadReckon.z
- end
- if currentX ~= nil then
- vars.deadReckon = {}
- vars.deadReckon.x = currentX
- vars.deadReckon.y = currentY
- vars.deadReckon.z = currentZ
- end
- return currentX, currentY, currentZ
- end
- function turnToFacing(f)
- while true do
- if facingTable[f] == facingTable[vars.currentFacing] then
- break
- end
- turtle.turnRight()
- vars.currentFacing = vars.currentFacing + 1
- if vars.currentFacing > 3 then
- vars.currentFacing = 0
- end
- end
- saveVars()
- end
- function setFacing(f)
- vars.currentFacing = f
- saveVars()
- end
- function useGPS(b)
- vars.useGPS = b
- saveVars()
- end
- function getVar(key, value)
- return vars[key]
- end
- function editVar(key, value)
- vars[key] = value
- end
- function calculateFuelUsage(distance, fuelType)
- return math.ceil(distance / fuelTable[fuelType]), math.ceil(distance % fuelTable[fuelType])
- end
- function goTo(x,y,z)
- rednet.open("right")
- if vars.useGPS then
- local currentX, currentY, currentZ = gps.locate(5)
- end
- if vars.deadReckon ~= nil and currentX == nil then
- currentX = vars.deadReckon.x
- currentY = vars.deadReckon.y
- currentZ = vars.deadReckon.z
- end
- if currentX ~= nil then
- vars.deadReckon = {}
- vars.deadReckon.x = currentX
- vars.deadReckon.y = currentY
- vars.deadReckon.z = currentZ
- saveVars()
- local offsetX = currentX - x
- local offsetY = currentY - y
- local offsetZ = currentZ - z
- print("Location is: "..currentX.." "..currentY.." "..currentZ)
- local distance = math.abs(offsetX)+math.abs(offsetY)+math.abs(offsetZ)
- print("Calculated distance to travel: "..distance.." blocks.")
- local coalUsed = calculateFuelUsage(distance, "Coal/Charcoal")
- if turtle.getFuelLevel() ~= "unlimited" then
- if turtle.getFuelLevel() < distance then
- print("Fuel will be required.")
- print("Please put coal in the lower left slot.")
- if coalUsed ~= 1 then
- print("You will need "..coalUsed.." units of coal.")
- else
- print("You will need 1 unit of coal.")
- end
- while true do
- if turtle.getItemCount(16) > 0 then
- turtle.select(16)
- turtle.refuel()
- if turtle.getFuelLevel() > distance then
- print("Fueling complete.")
- break
- end
- end
- os.sleep(0)
- end
- end
- end
- if offsetZ < 0 then
- turnToFacing(facingTable["south"])
- elseif offsetZ > 0 then
- turnToFacing(facingTable["north"])
- end
- for i=1, math.abs(offsetZ) do
- while true do
- if turtle.detect() then
- if not ((peripheral.getType("front") == "turtle") or (peripheral.getType("front") == "computer")) then
- turtle.dig()
- end
- else
- break
- end
- os.sleep(0)
- end
- turtle.forward()
- if offsetZ < 0 then
- vars.deadReckon.z = vars.deadReckon.z+1
- else
- vars.deadReckon.z = vars.deadReckon.z-1
- end
- end
- if offsetX < 0 then
- turnToFacing(facingTable["east"])
- elseif offsetX > 0 then
- turnToFacing(facingTable["west"])
- end
- for i=1, math.abs(offsetX) do
- while true do
- if turtle.detect() then
- if not ((peripheral.getType("front") == "turtle") or (peripheral.getType("front") == "computer")) then
- turtle.dig()
- end
- else
- break
- end
- os.sleep(0)
- end
- turtle.forward()
- if offsetX < 0 then
- vars.deadReckon.x = vars.deadReckon.x+1
- else
- vars.deadReckon.x = vars.deadReckon.x-1
- end
- end
- for i=1, math.abs(offsetY) do
- if offsetY < 0 then
- while true do
- if turtle.detectUp() then
- if not ((peripheral.getType("top") == "turtle") or (peripheral.getType("top") == "computer")) then
- turtle.digUp()
- end
- else
- break
- end
- os.sleep(0)
- end
- turtle.up()
- vars.deadReckon.y = vars.deadReckon.y+1
- elseif offsetY > 0 then
- while true do
- if turtle.detectDown() then
- if not ((peripheral.getType("bottom") == "turtle") or (peripheral.getType("bottom") == "computer")) then
- turtle.digDown()
- end
- else
- break
- end
- os.sleep(0)
- end
- turtle.down()
- vars.deadReckon.y = vars.deadReckon.y-1
- end
- end
- end
- saveVars()
- end
- function printDeadReckon()
- if vars.deadReckon ~= nil then
- print("X: "..vars.deadReckon["x"])
- print("Y: "..vars.deadReckon["y"])
- print("Z: "..vars.deadReckon["z"])
- end
- end
- function primeDeadReckon(x,y,z)
- if x == nil then
- rednet.open("right")
- local currentX, currentY, currentZ = gps.locate(5)
- vars.deadReckon = {}
- vars.deadReckon.x = currentX
- vars.deadReckon.y = currentY
- vars.deadReckon.z = currentZ
- else
- vars.deadReckon = {}
- vars.deadReckon.x = x
- vars.deadReckon.y = y
- vars.deadReckon.z = z
- end
- saveVars()
- printDeadReckon()
- end
- function setup(x,y,z,f,gps)
- if (x ~= nil) and (y ~= nil) and (z ~= nil) and (f ~= nil) then
- primeDeadReckon(x,y,z)
- setFacing(f)
- useGPS(gps)
- else
- print("Setup Utility:")
- print("Please enter the coordinates for this turtle's location:")
- write("X: ")
- local XLoc = ""
- while true do
- XLoc = tonumber(read())
- if XLoc then
- break
- end
- end
- write("Y: ")
- local YLoc = ""
- while true do
- YLoc = tonumber(read())
- if YLoc then
- break
- end
- end
- write("Z: ")
- local ZLoc = ""
- while true do
- ZLoc = tonumber(read())
- if ZLoc then
- break
- end
- end
- write("Facing: ")
- local Facing = ""
- while true do
- Facing = tonumber(read())
- if Facing then
- break
- end
- end
- primeDeadReckon(XLoc, YLoc, ZLoc)
- setFacing(Facing)
- while true do
- print("Should this turtle use GPS?")
- write(">")
- term.setCursorBlink(true)
- answer = string.lower(string.sub(read(),1,1))
- if answer == "n" then
- gps = false
- break
- elseif answer == "y" then
- gps = true
- break
- else
- local x,y = term.getCursorPos()
- print("Please provide a valid answer.")
- term.setCursorPos(1, y-1)
- end
- end
- useGPS(gps)
- print("Setup complete.")
- saveVars()
- end
- end
- loadVars()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement