Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.unloadAPI("gxm")
- os.loadAPI ("gxm")
- local tArgs = { ... }
- local data = { start = {}, from = {} }
- if not gxm.load() then gxm.save() end
- gxm.positionTrack(true)
- loadPosition = function()
- if not fs.exists("excavateConfig") then
- data.start = gxm.getData().position
- data.start.f = gxm.correctDir(data.start.f + 2)
- file = fs.open("excavateConfig", "w")
- if file then
- file.write(textutils.serialize(data))
- file.close()
- end
- else
- file = fs.open("excavateConfig", "r")
- if file then
- data = textutils.unserialize(file.readAll())
- file.close()
- end
- end
- end
- if tArgs[1] == "-b" then
- loadPosition()
- gxm.goto(data.start.x, data.start.y, data.start.z, data.start.f - 2)
- return
- elseif tArgs[1] == "-r" then
- gxm.regExecPattern("")
- return
- end
- zSize = tonumber(tArgs[1])
- xSize = tonumber(tArgs[2]) or zSize
- ySize = tonumber(tArgs[3]) or gxm.getData().position.y or 64
- savePosition = function()
- if fs.exists("excavateConfig") then
- file = fs.open("excavateConfig", "w")
- if file then
- data.from = gxm.getData().position
- file.write(textutils.serialize(data))
- file.close()
- end
- end
- end
- excavatePattern = function()
- local excString = ""
- local alter = true
- local excMove = "UDfr"
- local lastMove = "UD3d"
- local sideX = xSize % 2 == 0
- local sideZ = zSize % 2 == 0
- local side = not sideX and not sideZ
- for i = 1, math.ceil(ySize/3) do
- if i == math.ceil(ySize/3) then
- excMove = gxm.correctY(ySize) == 2 and "U0fr" or (gxm.correctY(ySize) == 1 and "00fr" or excMove)
- end
- if i == math.ceil(ySize/3) - 1 then lastMove = gxm.correctY(ySize) == 1 and "UD2d" or lastMove end
- for j = 1, xSize do
- local cmds = ""
- cmds = excMove:sub(1, -2):rep(zSize - 2) .. excMove:rep(2)
- alter = not alter
- if j ~= xSize then
- cmds = alter and cmds:gsub("r", "l") or cmds
- else
- if not sideX and not sideZ then
- side = not side
- elseif sideX and not sideZ then
- if i == 1 then
- side = true
- elseif i % 2 == 0 then
- side = not side
- end
- elseif sideZ and not sideX then
- if i == 1 then
- side = false
- elseif (i+1) % 2 == 0 then
- side = not side
- end
- else
- side = true
- end
- cmds = side and cmds:sub(1, -6) .. "r" .. lastMove or cmds:sub(1, -6) .. "l" .. lastMove
- end
- excString = excString .. cmds
- end
- xSize, zSize = zSize, xSize
- end
- return excString:sub(1, -4):gsub("0", "")
- end
- checkInventory = function()
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- return true
- end
- end
- return false
- end
- unload = function()
- if not turtle.detect() then
- print("Nothing in front of me! Need a chest to drop my stuff! Waiting for a chest or any block as a matter of fact :).")
- while not turtle.detect() do
- sleep(2)
- end
- end
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- while turtle.getItemCount(i) ~= 0 do
- if not turtle.drop() then
- print("Chest is probably full! Waiting for more space!")
- while not turtle.drop() do
- sleep(2)
- end
- end
- end
- end
- end
- print("Unloading complete!")
- end
- checkFuel = function( limit )
- return gxm.getFuelAbove(limit)
- end
- parseAction = function()
- loadPosition()
- if gxm.getAction() == "gotoHome" then
- gxm.goto(data.start.x, data.start.y, data.start.z, data.start.f)
- loadPosition()
- unload()
- gxm.goto(data.from.x, data.from.y, data.from.z, data.from.f)
- elseif gxm.getAction() == "unloading" then
- unload()
- gxm.goto(data.from.x, data.from.y, data.from.z, data.from.f)
- elseif gxm.getAction() == "gotoBack" then
- gxm.goto(data.from.x, data.from.y, data.from.z, data.from.f)
- end
- end
- function main()
- if gxm.getData().pattern == "" or not gxm.getData().pattern then gxm.regExecPattern(excavatePattern()) end
- loadPosition()
- parseAction()
- parallel.waitForAny(
- function()
- local r = coroutine.create(gxm.exec)
- local filter
- local eventData = {}
- while true do
- if filter == nil or filter == eventData[1] or eventData[1] == "terminate" then
- _, filter = coroutine.resume(r, unpack(eventData))
- if coroutine.status(r) == "dead" then return end
- end
- if eventData[1] == "moved" then
- checkFuel(1000)
- if not checkInventory() then
- savePosition()
- gxm.setAction("gotoHome")
- gxm.goto(data.start.x, data.start.y, data.start.z, data.start.f)
- gxm.setAction("unloading")
- unload()
- gxm.setAction("goBack")
- loadPosition()
- gxm.goto(data.from.x, data.from.y, data.from.z, data.from.f)
- gxm.setAction("")
- r = coroutine.create(gxm.exec)
- _, filter = coroutine.resume(r)
- end
- end
- eventData = { os.pullEventRaw() }
- end
- gxm.goto(data.start.x, data.start.y, data.start.z, data.start.f-2)
- return
- end)
- end
- if tArgs[2] == "-t" or tArgs[3] == "-t" or tArgs[4] == "-t" then
- gxm.regExecPattern(excavatePattern())
- print(gxm.getData().pattern)
- else
- main()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement