Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- API GnoX Move ---
- local savePosition = true
- local lastSlot = 16
- local data = { position = {} }
- local rt = turtle.turnRight
- local lt = turtle.turnLeft
- local fd = turtle.forward
- local bk = turtle.back
- local mup = turtle.up
- local dn = turtle.down
- local up
- ----------------------------------------------------------------------
- --------------------- Data manipulation and gps ----------------------
- ----------------------------------------------------------------------
- local isGps = function()
- return gps.locate()
- end
- local parseTurtleData = function()
- if not data.position.x then
- if isGps() then
- data.position.x, data.position.y, data.position.z = gps.locate()
- data.position.f = findDirection()
- else
- print("No GPS found. Save position manually? y/n")
- if read() == "y" then
- local act = { "x", "y", "z", "f" }
- for k in ipairs(act) do
- local inp, yes = nil, nil
- while not tonumber(inp) do
- if yes then print("Not a number, try again!") end
- yes = 1
- write("Position "..act[k]..": ")
- inp = read()
- end
- data.position[act[k]] = tonumber(inp)
- end
- end
- end
- end
- return textutils.serialize(data)
- end
- save = function()
- serialized = parseTurtleData();
- file = fs.open("position", "w")
- if file then
- file.write(serialized)
- else
- print("Error in opening the file")
- end
- file.close()
- return true
- end
- load = function()
- if fs.exists("position") then
- file = fs.open("position", "r")
- if file then
- content = file.readAll()
- if content ~= "" then
- data = textutils.unserialize(content)
- file.close()
- else
- file.close()
- fs.delete("position")
- end
- end
- if not data.position.x then
- print("There was a problem in loading positions. Trying to save.")
- save()
- end
- return true
- else
- print("Could not find file 'position'!")
- return save()
- end
- end
- resetData = function()
- data = nil
- if fs.exists("position") then fs.delete("position") end
- end
- getData = function()
- return data
- end
- setAction = function( action )
- data.action = action
- save()
- end
- getAction = function()
- return data.action
- end
- positionTrack = function(state)
- savePosition = state
- end
- relative = function()
- data.position.x, data.position.y, data.position.z, data.position.f = 0, 0, 0, 0
- save()
- end
- ---------------------------------------------
- ------------------ Main ---------------------
- ---------------------------------------------
- local changeHorizontalPos = function( move )
- if savePosition then
- if data.position.f == 0 then
- data.position.z = data.position.z + move
- elseif data.position.f == 1 then
- data.position.x = data.position.x - move
- elseif data.position.f == 2 then
- data.position.z = data.position.z - move
- elseif data.position.f == 3 then
- data.position.x = data.position.x + move
- end
- end
- end
- local changeVerticalPos = function( move )
- if savePosition then
- data.position.y = data.position.y + move
- end
- end
- correctY = function( y )
- if y > 2 then
- return correctY(y - 3)
- else
- return y
- end
- end
- correctDir = function( dir )
- if savePosition then
- if dir > 3 then
- return correctDir(dir - 4)
- elseif dir < 0 then
- return correctDir(dir + 4)
- else
- return dir
- end
- end
- end
- findDirection = function()
- if isGps() then
- lastX, _, lastZ = gps.locate()
- bk()
- x, _, z = gps.locate()
- fd()
- if z < lastZ then
- return 0
- elseif x > lastX then
- return 1
- elseif z > lastZ then
- return 2
- elseif x < lastX then
- return 3
- end
- end
- end
- --------------------------------------------------------------------
- -- Moves a turtle in any way
- -- @returns void
- -- @arguments
- -- move --- type table of function
- -- n --- number of moves
- -- dig --- (optional) if turtle cannot go forward then dig
- -- attack --- (optional) same thing with attack
- -- suck --- (optional) same thing with suck
- --------------------------------------------------------------------
- local _move = function( move, n, dig, attack, suck )
- if not n then n = 1 end
- for i = 1, n do
- local count = 0
- while not move() do
- if dig and not dig() then
- if not attack() then
- count = count + 1
- if count > 10 then
- return false
- else
- suck()
- end
- end
- end
- end
- if savePosition then
- if move == fd then
- changeHorizontalPos(1)
- elseif move == bk then
- changeHorizontalPos(-1)
- elseif move == mup then
- changeVerticalPos(1)
- elseif move == dn then
- changeVerticalPos(-1)
- end
- save()
- end
- end
- return true
- end
- --------------------------------------------------------------------
- -- Turns turtle in desired way
- -- @returns integer sides
- -- @arguments
- -- side --- hand of turn
- -- n --- number of turns
- --------------------------------------------------------------------
- local _turn = function( side, n )
- if not n then n = 1 end
- for i = 1, n do
- if savePosition then
- if side == rt then
- data.position.f = correctDir(data.position.f + 1)
- elseif side == lt then
- data.position.f = correctDir(data.position.f - 1)
- end
- save()
- end
- side()
- end
- return n
- end
- local _place = function(slot, p, dig, facing)
- if type(slot) == "function" then
- for i=1, 16 do
- turtle.select(i)
- if p() then return i end
- end
- elseif facing then
- _turnTo(facing)
- end
- if slot == 1 then
- if lastSlot then
- slot = lastSlot
- end
- elseif slot == 0 then
- slot = 1
- elseif slot == -1 then
- return true
- end
- if turtle.getItemCount(slot) > 1 then
- lastSlot = slot
- turtle.select(slot)
- if not p() then
- dig()
- return p()
- end
- else
- while true do
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- turtle.select(i)
- if turtle.compareTo(slot) then
- turtle.transferTo(slot)
- turtle.select(slot)
- if turtle.getItemCount(slot) > 1 then
- if not p() then
- dig()
- return p()
- else return true end
- end
- end
- end
- end
- sleep(5)
- print("Cannot find required building block. Retrying...")
- end
- end
- end
- local _turnTo = function( to )
- from = data.position.f
- if from == 0 then
- if to == 1 then
- right()
- elseif to == 2 then
- right(2)
- elseif to == 3 then
- left()
- end
- elseif from == 1 then
- if to == 0 then
- left()
- elseif to == 2 then
- right()
- elseif to == 3 then
- right(2)
- end
- elseif from == 2 then
- if to == 0 then
- right(2)
- elseif to == 1 then
- left()
- elseif to == 3 then
- right()
- end
- elseif from == 3 then
- if to == 0 then
- right()
- elseif to == 1 then
- right(2)
- elseif to == 2 then
- left()
- end
- end
- end
- left = function( n )
- _turn(lt, n)
- end
- right = function( n )
- _turn(rt, n)
- end
- --local _go_back = function()
- -- right(2)
- -- if not turtle.dig() then
- -- if turtle.attack() then
- -- turtle.suck()
- -- end
- -- end
- -- right(2)
- -- return true
- --end
- local _go_back = function()
- addToQueue("rr!D?a:orr")
- end
- forward = function( n )
- return _move( fd, n, turtle.dig, turtle.attack, turtle.suck )
- end
- back = function( n )
- _move( bk, n, _go_back )
- end
- up = function( n )
- _move( mup, n, turtle.digUp, turtle.attackUp, turtle.suckUp )
- end
- down = function( n )
- _move( dn, n, turtle.digDown, turtle.attackDown, turtle.suckDown )
- end
- sForward = function( n )
- if not n then n = 1 end
- for i = 1, n do
- while turtle.detect() or not _move(fd) do
- up()
- end
- end
- end
- sUp = function( n )
- n = n or 1
- for i = 1, n do
- while turtle.detectUp() or not _move(mup) do
- sleep(1)
- end
- end
- end
- sDown = function( n )
- if not n then n = 1 end
- for i = 1, n do
- while turtle.detectDown() or not _move(dn) do
- sleep(1)
- end
- end
- end
- place = function()
- return _place(turtle.place)
- end
- placeUp = function()
- return _place(turtle.placeUp)
- end
- placeDown = function()
- return _place(turtle.placeDown)
- end
- cPlace = function(comparationSlot)
- _place(comparationSlot, turtle.place, turtle.dig)
- end
- cPlaceDown = function(comparationSlot)
- _place(comparationSlot, turtle.placeDown, turtle.digDown)
- end
- cPlaceUp = function(comparationSlot)
- _place(comparationSlot, turtle.placeUp, turtle.digUp)
- end
- fPlace = function(slot, facing)
- _place(slot, turtle.place, turtle.dig, facing)
- end
- fPlaceUp = function(slot, facing)
- _place(slot, turtle.placeUp, turtle.digUp, facing)
- end
- fPlaceDown = function(slot, facing)
- _place(slot, turtle.placeDown, turtle.digDown, facing)
- end
- local hasFuel = function( n )
- for i = 1, 16 do
- turtle.select(i)
- while turtle.refuel(1) do
- print(turtle.getFuelLevel())
- if turtle.getFuelLevel() >= n then
- return true
- end
- end
- end
- return false
- end
- getFuelAbove = function( n )
- if turtle.getFuelLevel() <= n then
- while not hasFuel(n) do
- sleep(5)
- end
- end
- return turtle.getFuelLevel()
- end
- goto = function( x, y, z, f )
- if savePosition then
- data.gotoBack = {}
- data.gotoBack.x = data.position.x
- data.gotoBack.y = data.position.y
- data.gotoBack.z = data.position.z
- data.gotoBack.f = data.position.f
- save()
- end
- if data.position.x > x then
- _turnTo(1)
- sForward(data.position.x - x)
- elseif data.position.x < x then
- _turnTo(3)
- sForward(x - data.position.x)
- end
- if data.position.z > z then
- _turnTo(2)
- sForward(data.position.z - z)
- elseif data.position.z < z then
- _turnTo(0)
- sForward(z - data.position.z)
- end
- if data.position.y > y then
- sDown(data.position.y - y)
- elseif data.position.y < y then
- up(y - data.position.y)
- end
- if f then
- _turnTo(f)
- end
- end
- goBack = function()
- if data.gotoBack then
- goto(data.gotoBack.x, data.gotoBack.y, data.gotoBack.z, data.gotoBack.f)
- end
- save()
- end
- local actions = {
- ["f"] = forward,
- ["b"] = back,
- ["u"] = up,
- ["d"] = down,
- ["l"] = left,
- ["r"] = right,
- ["U"] = turtle.digUp,
- ["D"] = turtle.digDown,
- ["F"] = turtle.dig,
- ["^"] = turtle.placeUp,
- ["-"] = turtle.place,
- ["_"] = turtle.placeDown,
- ["q"] = turtle.detectUp,
- ["a"] = turtle.detect,
- ["y"] = turtle.detectDown,
- ["Q"] = turtle.compareUp,
- ["A"] = turtle.compare,
- ["Y"] = turtle.compareDown,
- ["c"] = turtle.compareTo,
- ["o"] = turtle.attack,
- [";"] = cPlace,
- [","] = cPlaceDown,
- ["p"] = cPlaceUp,
- ["z"] = fPlaceUp,
- ["h"] = fPlace,
- ["n"] = fPlaceDown,
- ["s"] = turtle.select
- }
- local isValidPatternMove = function( cmd )
- for k in pairs(actions) do
- if cmd == k then
- return true
- end
- end
- return false
- end
- regExecPattern = function( pattern )
- data.pattern = pattern
- save()
- end
- addToExecQueue = function( pattern )
- data.pattern = pattern + data.pattern
- end
- exec = function( pattern )
- if not pattern then pattern = data.pattern or "" end
- while pattern ~= "" do
- actual = pattern:match ("%d*!?[%a%-_^,;]%??")
- actual = actual:find("?") and pattern:match("%d*!?[%a%-_^,;]%?%d*[%a%-_^,;]:?") or actual
- actual = actual:find(":") and pattern:match("%d*!?[%a%-_^,;]%?%d*[%a%-_^,;]:%d*[%a%-_^,;]") or actual
- cond = (actual:find(":") and actual:match("!?[%a%-_^,;]%?%d*[%a%-_^,;]:%d*[%a%-_^,;]") or actual:match("!?[%a%-_^]%?[%a%-_^]")) or false
- moves = tonumber(actual:match("%d*")) or 1
- cmd = actual:match ("[%a%-_^,;]")
- pattern = pattern:sub (actual:len() + 1 )
- if cond then
- local firstMove, secondMove
- a = cond:find("!") and 2 or 1
- if cond:find("c") then
- if not cond:find("!") then
- condition = actions[cond:sub(1, 1)](moves)
- else
- condition = not actions[cond:sub(2, 2)](moves)
- end
- else
- condition = not cond:find("!") and actions[cond:sub(1, 1)]() or not actions[cond:sub(2, 2)]()
- end
- if cond:find("%d*") then
- firstMove = cond:match("?%d*"):gsub("?", "") or 1
- secondMove = cond:match(":%d*"):gsub(":", "") or 1
- cond = cond:gsub("%?(%d*)", "?"):gsub(":(%d*)", ":")
- end
- firstCmd = isValidPatternMove(cond:sub(a+2, a+2)) and cond:sub(a+2, a+2) or false
- secondCmd = isValidPatternMove(cond:sub(a+4, a+4)) and cond:sub(a+4, a+4) or false
- if savePosition then
- data.pattern = pattern
- save()
- end
- if condition and firstCmd then
- actions[firstCmd](tonumber(firstMove))
- elseif secondCmd then
- actions[secondCmd](tonumber(secondMove))
- end
- if savePosition then
- save()
- end
- os.queueEvent("moved")
- else
- if isValidPatternMove(cmd) then
- if cmd == "," or cmd == "s" or cmd == "p" or cmd == ";" then
- actions[cmd](moves)
- else
- for i = 1, moves do
- if savePosition then
- local move = moves - i
- local tCmd = cmd
- if move == 0 then move = "" tCmd = "" end
- data.pattern = tostring(move)..tCmd..pattern
- save()
- end
- actions[cmd]()
- if savePosition then
- save()
- end
- os.queueEvent("moved")
- sleep(.002)
- end
- end
- else
- print("Not a valid move!")
- end
- end
- end
- end
- allItemCount = function()
- local count = 0
- for i = 1, 16 do
- count = count + turtle.getItemCount(i)
- end
- return count
- end
- canTakeMoreItems = function()
- for i = 1, 16 do
- if turtle.getItemCount(i) == 0 then
- return true
- end
- end
- return false
- end
- slotHasMaterial = function(slot)
- return turtle.getItemCount(slot) > 0
- end
- findSlotWithMaterial = function(comparationSlot)
- for i = 1, 16 do
- if turtle.getItemCount(i) > 1 then
- if i ~= comparationSlot then
- turtle.select(i)
- if turtle.compareTo(comparationSlot) then
- return true
- end
- end
- end
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement