Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------vecMove API----------------------
- -----Move your turtle using vectoring-------------
- --------------------------------Functions-----------------------------------
- ---------getCoords()-----------------------------
- --load last know coordinates (dropped by detHead API)
- function getCoords()
- local coordFile=fs.open("/nav/gpsCoord","r")
- local coordData=coordFile.readAll()
- coordFile.close()
- local coordTbl=textutils.unserialize(coordData)
- local x=coordTbl[1]
- local y=coordTbl[2]
- local z=coordTbl[3]
- local d=coordTbl[4]
- print (x,", ", y,", ", z,", ", d)
- return x, y, z, d
- end
- ----------checkPos-------------------------
- --gets the current position and builds a displacement vector
- function checkPos(Dest)
- currPos=vector.new(gps.locate())
- disp=Dest-currPos
- --term.clear()
- --term.setCursorPos(1,1)
- --output to for validation
- print ("Destination: ",Dest)
- print ("Current Position: ",currPos)
- print ("Displacement: ",disp)
- print ("Movement map:")
- --For future use: determine is horizontal displacement is resolved
- local hDist=math.abs(disp.x)+math.abs(disp.z)
- return disp
- end
- -----------setHead------------------
- --requires the current heading and target heading.
- --Then spins the turtle right until the target is reached
- function setHead(current, target)
- repeat
- print("Current: ", current,". Target: ", target)
- if current~=target then
- turtle.turnRight()
- current=current+1
- if current==4 then current=0 end --Resets to South after East is reached
- end
- until current==target
- print("Heading is now ", dirTbl[current])
- return true, current
- end
- ----------shellGo-----------
- --moves the turtle with go command;
- --keeps program from producing errors
- --and/or breaking unexpected obstacles like my chests.
- --Turtle will hold position until the
- --obstacle is removed manually, or program is terminated.
- function shellGo(distance)
- shell.run("go", "forward", distance)
- end
- ----------updatePos----------
- --runs the detHead API to refresh the
- --gpsCoord (last known location) file with changes
- function updatePos(_p1)
- local origin=_p1.x," ",_p1.y," ",_p1.z
- local _p2=vector.new(gps.locate())
- local target=_p2.x," ",_p2.y," ",_p2.z
- shell.run("detHead", origin, target)
- return true, _p2
- end
- --------------------------------Process----------------------------------
- local _tArgs={...}
- rednet.open("right")
- --load directional DB--
- local dirFile=fs.open("/nav/dir.tab","r")
- local dirData=dirFile.readAll()
- dirFile.close()
- dirTbl=textutils.unserialize(dirData)
- --Input Destination--
- if #_tArgs~=3 then
- print ("Usage: vecMove.2 <x y z>")
- return
- end
- local strDestx=_tArgs[1]
- local strDesty=_tArgs[2]
- local strDestz=_tArgs[3]
- local Dest=vector.new(strDestx, strDesty, strDestz)
- local x, y, z, d=getCoords()
- local disp=checkPos(Dest)
- --EAST---------------------------
- if disp.x>0 then
- local success, dir=setHead(d,3)
- print ("Turned east? ",success," Direction: ", dirTbl[dir])
- if success==true then
- shellGo(disp.x)
- d=dir
- else
- print ("Set East Heading failed")
- end
- --WEST----------------------------
- elseif disp.x<0 then
- local success, dir=setHead(d,1)
- print ("Turned west? ", success, "Direction: ", dirTbl[dir])
- if success==true then
- shellGo(math.abs(disp.x))
- d=dir
- else
- print ("Set West Heading failed")
- end
- elseif disp.x==0 then
- print ("X axis is resolved")
- else
- print ("X Movement Error!")
- end
- local verPosUpdate, currPos=updatePos(currPos)
- checkPos(Dest)
- --SOUTH-------------------------------
- if disp.z>0 then
- local success, dir=setHead(d,0)
- print ("Turned south? ", success, "Direction: ", dirTbl[dir])
- if success==true then
- shellGo(disp.z)
- d=dir
- else
- print ("Set South Heading failed")
- end
- --NORTH-------------------------------
- elseif disp.z<0 then
- local success, dir=setHead(d,2)
- print ("Turned north? ", success, "Direction: ", dirTbl[dir])
- if success==true then
- shellGo(math.abs(disp.z))
- d=dir
- else
- print ("Set North Heading failed")
- end
- elseif disp.z==0 then
- print ("Z axis is resolved")
- else
- print ("Set Z heading failed")
- end
- print("made it through Z")
- local verPosUpdate, currPos=updatePos(currPos)
- checkPos(Dest)
- -- checkPos(Dest)
- print ("y displacement", disp.y)
- if disp.y>0 then
- shell.run("go", "up", disp.y)
- else if disp.y<0 then
- shell.run("go", "down", math.abs(disp.y))
- end
- -- checkPos(Dest)
- end
- --end
- rednet.close("right")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement