Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- allow for direct manipulation of the global environment of the calling script
- local env = {}
- setmetatable(env, {__index = _G})
- setfenv(2, env)
- setfenv(1, env)
- loadfile("config")()
- -- check if API already loaded
- if turtle and not turtle.act then
- turtle.act = true
- -- replace os.pullEvent to not eat events
- -- this breaks the parellel api
- os.pullEvent = function (...)
- local events = {}
- local event
- while true do
- event = {os.pullEventRaw()}
- if event[1] == "terminate" then
- error("Terminated", 0)
- end
- local matched = true
- for i = 1, arg.n do
- if arg[i] and arg[i] ~= event[i] then
- matched = false
- break
- end
- end
- if matched then
- for i, e in ipairs(events) do
- os.queueEvent(unpack(e))
- end
- return unpack(event)
- elseif event[1] == "modem_message" then
- table.insert(events, event)
- end
- end
- end
- -- replace sleep function
- sleep = function (time)
- local timer = os.startTimer(time)
- event = os.pullEvent("timer", timer)
- end
- -- track relative direction and coordinates
- turtle.x = 0
- turtle.y = 0
- turtle.z = 0
- turtle.facing = 0
- turtle.fuel = turtle.getFuelLevel()
- -- how much to change x and z when moving in a direction
- local coord_change = {[0] = { 0, 1}, -- south / forward
- [1] = {-1, 0}, -- west / right
- [2] = { 0, -1}, -- north / behind
- [3] = { 1, 0}} -- east / left
- turtle.setLocation = function (x, y, z, facing)
- turtle.x = x or 0
- turtle.y = y or 0
- turtle.z = z or 0
- turtle.facing = facing or turtle.facing
- turtle.saveLocation()
- end
- turtle.saveLocation = function (move)
- config.save(".act.location", {x=turtle.x, y=turtle.y, z=turtle.z, facing=turtle.facing,
- fuel=turtle.getFuelLevel(), selected=turtle.getSelectedSlot(), move=move})
- turtle.move = nil
- end
- turtle.loadLocation = function ()
- local loc = config.load(".act.location")
- if loc then
- turtle.x = loc.x
- turtle.y = loc.y
- turtle.z = loc.z
- turtle.facing = loc.facing
- turtle.fuel = loc.fuel
- if turtle.getSelectedSlot() ~= loc.selected then
- turtle.select(loc.selected)
- end
- turtle.move = loc.move
- end
- end
- turtle.loadLocation()
- turtle.updateLocation = function ()
- if turtle.move then
- local currFuel = turtle.getFuelLevel()
- if turtle.fuel == currFuel and turtle.move ~= "l" and turtle.move ~= "r" then
- return false -- no update occured
- elseif turtle.fuel == currFuel + 1 or turtle.move ~= "l" or turtle.move ~= "r" then
- turtle.update(turtle.move)
- return true -- location updated
- else
- -- server rollback?
- error("possible server rollback, aborted")
- return nil
- end
- else
- return false -- no update occured
- end
- end
- -- waypoints ----------------------------------------------------------------
- turtle.waypoint = {}
- turtle.loadWaypoints = function ()
- local waypoints = config.load(".act.waypoint")
- if waypoints then
- turtle.waypoint = waypoints
- end
- end
- turtle.loadWaypoints()
- turtle.setWaypoint = function (name, x, y, z, facing)
- if y ~= nil and z ~= nil then
- -- absolute coordinates
- turtle.waypoint[name] = {x=x, y=y, z=z, facing=facing}
- else
- -- current coordinates
- local useFacing = x
- if useFacing == nil then useFacing = true end
- if useFacing then
- turtle.waypoint[name] = {x=turtle.x, y=turtle.y, z=turtle.z, facing=turtle.facing}
- else
- turtle.waypoint[name] = {x=turtle.x, y=turtle.y, z=turtle.z}
- end
- end
- config.save(".act.waypoint", turtle.waypoint)
- end
- turtle.goToWaypoint = function (x, y, z, facing, priority)
- return toWaypoint(x, y, z, facing, priority, turtle.goForward, turtle.goUp, turtle.goDown)
- end
- turtle.moveToWaypoint = function (x, y, z, facing, priority)
- return toWaypoint(x, y, z, facing, priority, turtle.moveForward, turtle.moveUp, turtle.moveDown)
- end
- turtle.toWaypoint = function (x, y, z, facing, priority)
- return toWaypoint(x, y, z, facing, priority, turtle.forward, turtle.up, turtle.down)
- end
- turtle.distanceTo = function (x, y, z)
- local waypoint = {x=x, y=y, z=z}
- if type(x) == "string" then
- if turtle.waypoint[x] then
- waypoint = turtle.waypoint[x]
- else
- return nil
- end
- return math.abs(turtle.x - waypoint.x) + math.abs(turtle.y - waypoint.y) + math.abs(turtle.z - waypoint.z)
- end
- end
- function toWaypoint (x, y, z, facing, priority, forward, up, down)
- local waypoint = {x=x, y=y, z=z, facing=facing}
- if type(x) == "string" then
- if turtle.waypoint[x] then
- waypoint = turtle.waypoint[x]
- priority = y
- else
- return false
- end
- end
- turtle.updateLocation()
- -- go to coordinates
- if type(priority) ~= "string" or
- not priority:find("x") or
- not priority:find("y") or
- not priority:find("z") or
- #priority ~= 3 then
- priority = "yxz"
- end
- for c in priority:gmatch(".") do
- if c == "y" then
- -- move up or down
- local dy = waypoint.y - turtle.y
- if dy > 0 then
- for i = 1, dy do
- up()
- end
- elseif dy < 0 then
- for i = 1, -dy do
- down()
- end
- end
- elseif c == "x" then
- -- move east or west
- local dx = waypoint.x - turtle.x
- if dx > 0 then -- east
- turtle.face(3)
- for i = 1, dx do
- forward()
- end
- elseif dx < 0 then -- west
- turtle.face(1)
- for i = 1, -dx do
- forward()
- end
- end
- elseif c == "z" then
- -- move north or south
- local dz = waypoint.z - turtle.z
- if dz > 0 then -- south
- turtle.face(0)
- for i = 1, dz do
- forward()
- end
- elseif dz < 0 then -- north
- turtle.face(2)
- for i = 1, -dz do
- forward()
- end
- end
- end
- end
- -- face proper direction
- if waypoint.facing then
- turtle.face(waypoint.facing)
- end
- return true
- end
- turtle.gps = function (getFacing)
- local x, y, z = gps.locate()
- if x ~= nil then
- turtle.x = x
- turtle.y = y
- turtle.z = z
- if getFacing then
- turtle.forward()
- local dx, dy, dz = gps.locate()
- if dx ~= nil then
- if x == dx then
- if z < dz then
- turtle.facing = 0 --south
- else
- turtle.facing = 2 --north
- end
- else
- if x < dx then
- turtle.facing = 3 --east
- else
- turtle.facing = 1 --west
- end
- end
- end
- turtle.back()
- end
- return true
- else
- return false
- end
- end
- turtle.update = function (move)
- if move == "f" then
- turtle.x = turtle.x + coord_change[turtle.facing][1]
- turtle.z = turtle.z + coord_change[turtle.facing][2]
- turtle.fuel = turtle.fuel - 1
- elseif move == "b" then
- turtle.x = turtle.x - coord_change[turtle.facing][1]
- turtle.z = turtle.z - coord_change[turtle.facing][2]
- turtle.fuel = turtle.fuel - 1
- elseif move == "u" then
- turtle.y = turtle.y + 1
- turtle.fuel = turtle.fuel - 1
- elseif move == "d" then
- turtle.y = turtle.y - 1
- turtle.fuel = turtle.fuel - 1
- elseif move == "l" then
- turtle.facing = (turtle.facing - 1) % 4
- elseif move == "r" then
- turtle.facing = (turtle.facing + 1) % 4
- end
- turtle.saveLocation()
- end
- turtle.face = function (facing)
- local new_facing = (facing - turtle.facing) % 4
- if new_facing == 1 then
- turtle.turnRight()
- elseif new_facing == 2 then
- turtle.turnRight()
- turtle.turnRight()
- elseif new_facing == 3 then
- turtle.turnLeft()
- end
- end
- -- replace movement functions
- turtle._turnLeft = turtle.turnLeft
- turtle.turnLeft = function ()
- turtle.saveLocation("l")
- if turtle._turnLeft() then
- turtle.update("l")
- return true
- else
- turtle.saveLocation()
- return false
- end
- end
- turtle._turnRight = turtle.turnRight
- turtle.turnRight = function ()
- turtle.saveLocation("r")
- if turtle._turnRight() then
- turtle.update("r")
- return true
- else
- turtle.saveLocation()
- return false
- end
- end
- turtle._forward = turtle.forward
- turtle.forward = function ()
- turtle.saveLocation("f")
- if turtle._forward() then
- turtle.update("f")
- return true
- else
- turtle.saveLocation()
- return false
- end
- end
- turtle._back = turtle.back
- turtle.back = function ()
- turtle.saveLocation("b")
- if turtle._back() then
- turtle.update("b")
- return true
- else
- turtle.saveLocation()
- return false
- end
- end
- turtle._up = turtle.up
- turtle.up = function ()
- turtle.saveLocation("u")
- if turtle._up() then
- turtle.update("u")
- return true
- else
- turtle.saveLocation()
- return false
- end
- end
- turtle._down = turtle.down
- turtle.down = function ()
- turtle.saveLocation("d")
- if turtle._down() then
- turtle.update("d")
- return true
- else
- turtle.saveLocation()
- return false
- end
- end
- turtle._refuel = turtle.refuel
- turtle.refuel = function (...)
- turtle._refuel(...)
- turtle.fuel = turtle.getFuelLevel()
- turtle.saveLocation()
- end
- turtle._select = turtle.select
- turtle.select = function (slot)
- if turtle._select(slot) then
- turtle.selected = slot
- return slot
- end
- end
- turtle._getItemSpace = turtle.getItemSpace
- turtle.getItemSpace = function (slot)
- slot = slot or turtle.getSelectedSlot()
- return turtle._getItemSpace(slot)
- end
- turtle._getItemCount = turtle.getItemCount
- turtle.getItemCount = function (slot)
- slot = slot or turtle.getSelectedSlot()
- return turtle._getItemCount(slot)
- end
- -- Go, move or wait to be cleared
- turtle.goForward = function ()
- while not turtle.forward() do
- sleep(1)
- end
- return true
- end
- turtle.goBack = function ()
- while not turtle.back() do
- sleep(1)
- end
- return true
- end
- turtle.goUp = function ()
- while not turtle.up() do
- sleep(1)
- end
- return true
- end
- turtle.goDown = function ()
- while not turtle.down() do
- sleep(1)
- end
- return true
- end
- -- Move, move or dig or attack until moved
- turtle.moveForward = function ()
- while not turtle.forward() do
- if turtle.detect() then
- turtle.dig()
- else
- turtle.attack()
- end
- end
- return true
- end
- turtle.moveUp = function ()
- while not turtle.up() do
- if turtle.detectUp() then
- turtle.digUp()
- else
- turtle.attackUp()
- end
- end
- return true
- end
- turtle.moveDown = function ()
- while not turtle.down() do
- if turtle.detectDown() then
- turtle.digDown()
- else
- turtle.attackDown()
- end
- end
- return true
- end
- turtle.findSimilar = function ()
- for s = 1, 16 do
- if s ~= turtle.getSelectedSlot() then
- if turtle.compareTo(s) then
- return s
- end
- end
- end
- return nil
- end
- -- Build, place block with automatic resupply if needed
- turtle.build = function ()
- if turtle.getItemCount(turtle.getSelectedSlot()) == 1 then
- local resupplySlot = turtle.findSimilar()
- if resupplySlot then
- if turtle.place() then
- local currentSlot = turtle.getSelectedSlot()
- turtle.select(resupplySlot)
- turtle.transferTo(currentSlot, turtle.getItemCount(resupplySlot))
- turtle.select(currentSlot)
- return true
- else
- return false
- end
- else
- return turtle.place()
- end
- else
- return turtle.place()
- end
- end
- turtle.buildUp = function ()
- if turtle.getItemCount(turtle.getSelectedSlot()) == 1 then
- local resupplySlot = turtle.findSimilar()
- if resupplySlot then
- if turtle.placeUp() then
- local currentSlot = turtle.getSelectedSlot()
- turtle.select(resupplySlot)
- turtle.transferTo(currentSlot, turtle.getItemCount(resupplySlot))
- turtle.select(currentSlot)
- return true
- else
- return false
- end
- else
- return turtle.placeUp()
- end
- else
- return turtle.placeUp()
- end
- end
- turtle.buildDown = function ()
- if turtle.getItemCount(turtle.getSelectedSlot()) == 1 then
- local resupplySlot = turtle.findSimilar()
- if resupplySlot then
- if turtle.placeDown() then
- local currentSlot = turtle.getSelectedSlot()
- turtle.select(resupplySlot)
- turtle.transferTo(currentSlot, turtle.getItemCount(resupplySlot))
- turtle.select(currentSlot)
- return true
- else
- return false
- end
- else
- return turtle.placeDown()
- end
- else
- return turtle.placeDown()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment