Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local old_fd = old_fd or turtle.forward
- local old_bk = old_bk or turtle.back
- local old_lt = old_lt or turtle.turnLeft
- local old_rt = old_rt or turtle.turnRight
- local old_up = old_up or turtle.up
- local old_dn = old_dn or turtle.down
- saveOnMove = false
- saveOnTurn = true
- local x,y,z,fx,fz
- local filePos = {}
- if fs.exists("turtle position") then
- local f = fs.open("turtle position","r")
- filePos = textutils.unserialize(f.readAll() or "")
- f.close()
- end
- local gpsPos = {gps.locate(0)}
- if filePos.x and gpsPos[1] then
- x,y,z,fx,fz = gpsPos[1],gpsPos[2],gpsPos[3],filePos.fx,filePos.fz
- elseif gpsPos[1] then
- if not turtle.forward() then
- print("Need to move forward to get Facing")
- while not turtle.forward() do
- turtle.refuel(1)
- sleep(1)
- end
- end
- local gpsPos2 = {gps.locate(0)}
- repeat until turtle.back()
- x,y,z,fx,fz = gpsPos[1],gpsPos[2],gpsPos[3],gpsPos2[1]-gpsPos[1],gpsPos2[3]-gpsPos[3]
- elseif filePos.x then
- x,y,z,fx,fz = filePos.x,filePos.y,filePos.z,filePos.fx,filePos.fz
- else
- local display = {"X","Y","Z","FX","FZ"}
- local result = {}
- local input
- for i=1,5 do
- repeat
- write(display[i]..": ")
- input = tonumber(read())
- until input
- table.insert(result,input)
- end
- x,y,z,fx,fz = unpack(result)
- end
- local function save()
- local f = fs.open("turtle position","w")
- f.write(textutils.serialize(getPos()))
- f.close()
- end
- function turtle.forward()
- if turtle.detect() then return false,"Movement obstructed" end
- x = x + fx
- z = z + fz
- if saveOnMove then save() end
- local r,m = old_fd()
- if not r then
- x = x - fx
- z = z - fz
- if saveOnMove then save() end
- end
- return r,m
- end
- function turtle.back()
- x = x - fx
- z = z - fz
- if saveOnMove then save() end
- local r,m = old_bk()
- if not r then
- x = x + fx
- z = z + fz
- if saveOnMove then save() end
- sleep(1)
- end
- return r,m
- end
- function turtle.up()
- if turtle.detectUp() then return false,"Movement obstructed" end
- y = y + 1
- if saveOnMove then save() end
- local r,m = old_up()
- if not r then
- y = y - 1
- if saveOnMove then save() end
- end
- return r,m
- end
- function turtle.down()
- if turtle.detectDown() then return false,"Movement obstructed" end
- y = y - 1
- if saveOnMove then save() end
- local r,m = old_dn()
- if not r then
- y = y + 1
- if saveOnMove then save() end
- end
- return r,m
- end
- function turtle.turnLeft()
- fx,fz = fz,-fx
- if saveOnTurn then save() end
- return old_lt()
- end
- function turtle.turnRight()
- fx,fz = -fz,fx
- if saveOnTurn then save() end
- return old_rt()
- end
- function face(tfx,tfz)
- if tfx==-fz and tfz==fx then
- turtle.turnRight()
- end
- while tfx~=fx or tfz~=fz do
- turtle.turnLeft()
- end
- return true
- end
- function goto(tx,ty,tz,tfx,tfz,startWithDir)
- if type(tx)=="table" then
- if tx.x then
- startWithDir=ty
- tx,ty,tz,tfx,tfz = tx.x,tx.y,tx.z,tx.fx,tx.fz
- end
- if #tx>=3 then
- startWithDir=ty
- tx,ty,tz,tfx,tfz = unpack(tx)
- end
- end
- tx,ty,tz,tfx,tfz=tonumber(tx),tonumber(ty),tonumber(tz),tonumber(tfx),tonumber(tfz)
- if not (tx and ty and tz) then
- return false,"Invalid coordinates"
- end
- local dirs = {
- function()
- if x<tx then
- face(1,0)
- while x<tx and turtle.forward() do end
- end
- if x>tx then
- face(-1,0)
- while x>tx and turtle.forward() do end
- end
- return x==tx
- end,
- function()
- while y<ty and turtle.up() do end
- while y>ty and turtle.down() do end
- return y==ty
- end,
- function()
- if z<tz then
- face(0,1)
- while z<tz and turtle.forward() do end
- end
- if z>tz then
- face(0,-1)
- while z>tz and turtle.forward() do end
- end
- return z==tz
- end
- }
- local tryDir = startWithDir or 1
- local successCount=0
- repeat
- if dirs[(tryDir%3)+1]() then
- successCount = successCount + 1
- else
- successCount = 0
- end
- tryDir = tryDir + 1
- until successCount>=3 or tryDir>=30
- if successCount>=3 and tfx and tfz then
- face(tfx,tfz)
- end
- return successCount>=3
- end
- local Position = {__tostring=function() return "X:"..tostring(x).." Y:"..tostring(y).." Z:"..tostring(z).." Facing:"..getFacingName() end}
- Position.__index = Position
- function Position:goto()
- return goto(self.x,self.y,self.z,self.fx,self.fz)
- end
- Position.__call = Position.goto
- function Position:moveBy()
- return goto(x+self.x,y+self.y,z+self.z,fx,fz)
- end
- function Position:gotoPoint()
- return goto(self.x,self.y,self.z)
- end
- function Position:face()
- return face(self.fx,self.fz)
- end
- function Position:toVector()
- return vector.new(self.x,self.y,self.z)
- end
- function newPos(ax,ay,az,afx,afz)
- if type(ax)=="table" then
- if ax.x then
- ax,ay,az,afx,afz = ax.x,ax.y,ax.z,ax.fx,ax.fz
- end
- if #x>=3 then
- ax,ay,az,afx,afz = unpack(ax)
- end
- end
- local result = {x=ax,y=ay,z=az,fx=afx,fz=afz}
- setmetatable(result,Position)
- return result
- end
- function getPos()
- return newPos(x,y,z,fx,fz)
- end
- function getFacing(afx,afz)
- return (afx or fx)==0 and 1-(afz or fz) or 2+(afx or fx)
- end
- function getFacingName(afx,afz)
- local names={"South","West","North","East"}
- return names[getFacing(afx,afz)+1]
- end
Advertisement
Add Comment
Please, Sign In to add comment