Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- local path="bin/LGPS/"
- local data=path.."data"
- local current={x=nil,y=nil,z=nil,f=nil}
- local init=false
- local save=function()
- local file=fs.open(data,"w")
- file.write(current.x.."\n"..current.y.."\n"..current.z.."\n"..current.f.."\n")
- file.close()
- return true
- end
- start = function(x,y,z,f)
- x=tonumber(x)
- y=tonumber(y)
- z=tonumber(z)
- f=tonumber(f)
- if x~=nil and y~=nil and z~=nil and f~=nil then
- if f<0 or f>3 then return nil end
- local file=fs.open(data,"w")
- current.x=x
- current.y=y
- current.z=z
- current.f=f
- save()
- init=true
- return current
- end
- if gps.locate() then
- local lx,ly,lz = gps.locate()
- for i=1,4,1 do
- if turtle.detect() then
- turtle.turnLeft()
- else
- turtle.forward()
- local x,y,z = gps.locate()
- if lx==x and lz==z then return nil end
- if lx==x then
- if lz>z then
- current.f=0
- else
- current.f=2
- end
- else
- if lx>x then
- current.f=3
- else
- current.f=1
- end
- end
- local file=fs.open(data,"w")
- current.x=x
- current.y=y
- current.z=z
- save()
- init=true
- return current
- end
- end
- end
- if fs.exists(data) and not fs.isDir(data) then
- local file = fs.open(data,"r")
- for i=1,4,1 do
- local l=tonumber(file.readLine())
- if l==nil then break end
- if i==1 then
- current.x=l
- elseif i==2 then
- current.y=l
- elseif i==3 then
- current.z=l
- elseif i==4 then
- current.f=l
- init=true
- file.close()
- return current
- end
- end
- file.close()
- end
- return nil
- end
- getPos = function()
- if init then return current
- else return nil end
- end
- forward = function(i)
- if not init then return nil end
- if i==nil then i=1 end
- i=tonumber(i)
- if i==0 then return true,0
- elseif i==nil then return false,0 end
- for j=1,i,1 do
- if turtle.forward() then
- if current.f==0 then
- current.z=current.z-1
- elseif current.f==1 then
- current.x=current.x+1
- elseif current.f==2 then
- current.z=current.z+1
- else
- current.x=current.x-1
- end
- save()
- else
- return false,j
- end
- end
- return true,i
- end
- back = function(i)
- if not init then return nil end
- if i==nil then i=1 end
- i=tonumber(i)
- if i==0 then return true,0
- elseif i==nil then return false,0 end
- for j=1,i,1 do
- if turtle.back() then
- if current.f==0 then
- current.z=current.z+1
- elseif current.f==1 then
- current.x=current.x-1
- elseif current.f==2 then
- current.z=current.z-1
- else
- current.x=current.x+1
- end
- save()
- else
- return false,j
- end
- end
- return true,i
- end
- up = function(i)
- if not init then return nil end
- if i==nil then i=1 end
- i=tonumber(i)
- if i==0 then return true,0
- elseif i==nil then return false,0 end
- for j=1,i,1 do
- if turtle.up() then
- current.y=current.y+1
- save()
- else
- return false,j
- end
- end
- return true,i
- end
- down = function(i)
- if not init then return nil end
- if i==nil then i=1 end
- i=tonumber(i)
- if i==0 then return true,0
- elseif i==nil then return false,0 end
- for j=1,i,1 do
- if turtle.down() then
- current.y=current.y-1
- save()
- else
- return false,j
- end
- end
- return true,i
- end
- turnLeft = function(i)
- if not init then return nil end
- if i==nil then i=1 end
- i=tonumber(i)
- if i==0 then return true,0
- elseif i==nil then return false,0 end
- for j=1,i,1 do
- if turtle.turnLeft() then
- if current.f==0 then
- current.f=3
- else
- current.f=current.f-1
- end
- save()
- else
- return false,j
- end
- end
- return true,i
- end
- turnRight = function(i)
- if not init then return nil end
- if i==nil then i=1 end
- i=tonumber(i)
- if i==0 then return true,0
- elseif i==nil then return false,0 end
- for j=1,i,1 do
- if turtle.turnRight() then
- if current.f==3 then
- current.f=0
- else
- current.f=current.f+1
- end
- save()
- else
- return false,j
- end
- end
- return true,i
- end
- local movements={["forward"]=forward,["back"]=back,["up"]=up,["down"]=down,["turnLeft"]=turnLeft,["turnRight"]=turnRight}
- if args[1]=="set" then
- if tonumber(args[2])~=nil and tonumber(args[3])~=nil and tonumber(args[4])~=nil and tonumber(args[5])~=nil then
- if tonumber(args[5])<0 or tonumber(args[5])>3 then
- print("Invalid facing value!")
- return 1
- else
- start(args[2],args[3],args[4],args[5])
- return 0
- end
- else
- print("Insuficient number of arguments or wrong types")
- return 1
- end
- elseif args[1]=="get" then
- if start() then
- local facing={"North","East","South","West"}
- print("X: "..current.x.."\nY: "..current.y.."\nZ: "..current.z.."\nF: "..current.f.." "..facing[current.f+1])
- return 0
- else
- print("No data available!")
- return 1
- end
- elseif movements[args[1]] then
- if start() then
- local i=1
- if args[2]~=nil then
- if tonumber(args[2]) then
- i=args[2]
- else
- print("Invalid arguments!")
- return 1
- end
- end
- if movements[args[1]](i) then
- return 0
- else
- print("Movement blocked!")
- return 1
- end
- else
- print("No data available!")
- return 1
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment