Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --checks for file position and creates it if not
- if not fs.exists("nav.position") then
- local pos={0,0,0}
- print("Creating position file")
- file.create("nav.position",pos) end
- pos=file.read("nav.position")
- x=pos[1]
- y=pos[2]
- z=pos[3]
- --print("true")
- print("I am at |x:",x,"|y:",y,"|z:",z)
- facing={"north","south","east","west"}
- --#checks fuel
- if turtle.getFuelLevel() <= 100 then turtle.refuel(1) end
- --#Checks for file direction and creates it if not
- if not fs.exists("nav.direction") then
- file.create("nav.direction","north") end
- way=file.read("nav.direction")
- --#determines cardinal direction
- function turn(direction,count)
- counter=0
- repeat
- direction1=direction
- --print(direction1)
- counter=counter+1
- --print(counter)
- if way == "north" and direction1 == "right" then
- way="east" direction1=0
- turtle.turnRight()
- end
- if way == "north" and direction1 == "left" then
- way="west" direction1=0
- turtle.turnLeft()
- end
- if way == "south" and direction1 == "right" then
- way="west" direction1=0
- turtle.turnRight()
- end
- if way == "south" and direction1 == "left" then
- way="east" direction1=0
- turtle.turnLeft()
- end
- if way == "east" and direction1 == "right" then
- way="south" direction1=0
- turtle.turnRight()
- end
- if way == "east" and direction1 == "left" then
- way="north" direction1=0
- turtle.turnLeft()
- end
- if way == "west" and direction1 == "right" then
- way="north" direction1=0
- turtle.turnRight()
- end
- if way == "west" and direction1 == "left" then
- way="south" direction1=0
- turtle.turnLeft()
- end
- --sleep()
- until counter==count
- file.create("nav.direction",way)
- end
- function getCount()
- CD=file.read("nav.direction")
- if CD == "north" then xcv=0 zcv=1 end
- if CD == "south" then xcv=0 zcv=-1 end
- if CD == "east" then xcv=1 zcv=0 end
- if CD == "west" then xcv=-1 zcv=0 end
- return xcv,zcv
- end
- function forward(count)
- dx,dz=getCount()
- for i=1,count do
- --print(i)
- if not turtle.forward() then print("I need fuel") break end
- x=x+dx
- z=z+dz
- pos={x,y,z}
- file.create("nav.position",pos)
- --sleep()
- end
- end
- --#goes up and updates y
- function up(count)
- getCount()
- for i=1,count do
- turtle.up()
- y=y+1
- pos={x,y,z}
- file.create("nav.position",pos)
- end
- end
- --# goes down and updates Y
- function down(count)
- getCount()
- for i=1,count do
- turtle.down()
- y=y-1
- pos={x,y,z}
- file.create("nav.position",pos)
- end
- end
- --#calculates new position then navigates to
- function goto(nx,ny,nz)
- pos=file.read("nav.position")
- x=pos[1]
- y=pos[2]
- z=pos[3]
- dx=nx-x
- dy=ny-y
- dz=nz-z
- dx=tostring(dx)
- dy=tostring(dy)
- dz=tostring(dz)
- positive="%d+"
- negative="-%d+"
- print(dx)
- if dx == positive then print("true") turn("right",1) forward(dx) end
- end
- goto(6,0,0)
- print("I am now at |x:",x,"|y:",y,"|z:",z," I am facing: ",CD)
Advertisement
Add Comment
Please, Sign In to add comment