Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cposition={0,0,0,0}
- local home={0,0,0,0}
- local shaftmode=false
- function modposition(axis, value)
- if (axis == 4) then
- cposition[axis] = ((cposition[axis] + value) % 4)
- else
- cposition[axis] = (cposition[axis] + value)
- end
- reportpositions()
- setfilepos("aware/position")
- end
- function setshaftmode(toggle)
- shaftmode=toggle
- end
- function moveforward()
- if turtle.forward() then
- if (cposition[4] == 0) then modposition(2,1)
- elseif (cposition[4] == 1) then modposition(1,-1)
- elseif (cposition[4] == 2) then modposition(2,-1)
- elseif (cposition[4] == 3) then modposition(1,1)
- end
- return true
- else
- return false
- end
- end
- function moveback()
- if turtle.back() then
- if (cposition[4] == 0) then modposition(2,-1)
- elseif (cposition[4] == 1) then modposition(1,1)
- elseif (cposition[4] == 2) then modposition(2,1)
- elseif (cposition[4] == 3) then modposition(1,-1)
- end
- return true
- else
- return false
- end
- end
- function moveup()
- if turtle.up() then
- modposition(3,1)
- return true
- else
- return false
- end
- end
- function movedown()
- if turtle.down() then
- modposition(3,-1)
- return true
- else
- return false
- end
- end
- function turnright()
- modposition(4,1)
- turtle.turnRight()
- end
- function turnleft()
- modposition(4,-1)
- turtle.turnLeft()
- end
- function turnto(direction)
- while cposition[4] ~= direction do
- diff = cposition[4] - direction
- if ((diff == 1) or (diff == -3)) then
- turnleft()
- else
- turnright()
- end
- end
- end
- function digmoveforward()
- if turtle.detect() then
- turtle.dig()
- end
- if shaftmode then
- if turtle.detectUp() then
- turtle.digUp()
- end
- end
- if moveforward() then
- return true
- else
- return false
- end
- end
- function digmoveup()
- if turtle.detectUp() then
- turtle.digUp()
- end
- if shaftmode then
- if turtle.detect() then
- turtle.dig()
- end
- end
- if moveup() then
- return true
- else
- return false
- end
- end
- function digmovedown()
- if turtle.detectDown() then
- turtle.digDown()
- end
- if shaftmode then
- if turtle.detect() then
- turtle.dig()
- end
- end
- if movedown() then
- return true
- else
- return false
- end
- end
- function movetoz(tarz)
- while(cposition[3] ~= tarz) do
- if cposition[3] < tarz then
- digmoveup()
- else
- digmovedown()
- end
- end
- end
- function movetox(tarx)
- while(cposition[1] ~= tarx) do
- if cposition[1] < tarx then
- turnto(3)
- else
- turnto(1)
- end
- digmoveforward()
- end
- end
- function movetoy(tary)
- while(cposition[2] ~= tary) do
- if cposition[2] < tary then
- turnto(0)
- else
- turnto(2)
- end
- digmoveforward()
- end
- end
- function moveto(tarx, tary, tarz, taro)
- if(cposition[3] > tarz) then
- movetoz(tarz)
- movetoy(tary)
- movetox(tarx)
- else
- movetox(tarx)
- movetoy(tary)
- movetoz(tarz)
- end
- turnto(taro)
- end
- function goto(destination)
- termwrite("going to"..textutils.serialize(destination))
- moveto(destination[1], destination[2], destination[3], destination[4])
- end
- function gohome()
- goto(home)
- end
- function isfull()
- for i=1, 9 do
- if turtle.getItemCount(i) == 0 then
- return false
- end
- end
- return true
- end
- function isempty()
- for i=1, 9 do
- if turtle.getItemCount(i) > 0 then
- return false
- end
- end
- return true
- end
- function dump()
- while (turtle.drop()) do
- sleep(.12)
- end
- end
- function setfilepos(filename, coords)
- file = fs.open(filename, "w")
- if (coords == null) then
- file.write(textutils.serialize(cposition))
- else
- file.write(textutils.serialize(coords))
- end
- file:close()
- end
- function getfilepos(filename)
- file = fs.open(filename, "r")
- pos = textutils.unserialize(file.readLine())
- file:close()
- return pos
- end
- function termwrite(message)
- cx, cy = term.getCursorPos()
- mx, my = term.getSize()
- term.write(message)
- if(cy == my) then
- term.scroll(1)
- term.setCursorPos(1, cy)
- else
- term.setCursorPos(1, cy + 1)
- end
- end
- function reportpositions()
- pushpos((os.getComputerID()+10000),"T",cposition)
- pushpos((os.getComputerID()+100),"H",home)
- end
- function pushpos(id, class, position)
- fposition=textutils.serialize(position)
- formatted={id,class,fposition}
- rednet.broadcast(textutils.serialize(formatted))
- end
- function sethome(coords)
- if (coords == null) then
- home = cposition
- else
- home = coords
- end
- setfilepos("aware/home", home)
- end
- function setposition(coords)
- cposition = coords
- setfilepos("aware/position", coords)
- end
- function getposition(axis)
- if (axis == null) then
- return copytable(cposition)
- else
- return cposition[axis]
- end
- end
- function gethome()
- return copytable(home)
- end
- function gpslocate()
- x,y,z = gps.locate(5)
- if (x ~= null) and (y ~= null) and (z ~= null) then
- setposition(x,y,z,0)
- return true
- else
- return false
- end
- end
- function loadposition()
- if(fs.exists("aware/position")) then
- cposition = getfilepos("aware/position")
- termwrite("aware: loaded position")
- else
- if gpslocate() then
- termwrite("aware: loaded position from gps")
- else
- setfilepos("aware/position")
- termwrite("aware: created position")
- end
- end
- end
- function loadhome()
- if(fs.exists("aware/home")) then
- home = getfilepos("aware/home")
- termwrite("aware: loaded home")
- else
- sethome()
- termwrite("aware: created home")
- end
- end
- function copytable(t)
- local t2 = {}
- for k,v in pairs(t) do
- t2[k] = v
- end
- return t2
- end
- function init()
- if(fs.isDir("aware") == false) then
- fs.makeDir("aware")
- end
- rednet.open("right")
- loadposition()
- loadhome()
- end
- init()
Advertisement
Add Comment
Please, Sign In to add comment