Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Compass api.
- Save as compass on a computer and everything should be able to be used using compass.[function]([parameters])
- API for turtles to enhance the movement of them and give them smarter navigation options.
- Made by Blipman17
- version; 1.2.0
- It is not allowed to distribute this API in any form.
- Feel free to modify all the code, but make credits then.
- (also of yours.)
- ]]--
- fuellevel = 10
- fuelmode="normal"
- function setMinFuel(level)
- fuellevel=level
- end
- --[[
- -list of valid modes; "normal", "none"
- -work in progress
- ]]--
- function setFuelMode(thingy)
- fuelmode=thingy
- end
- function fuel()
- if fuelmode=="normal" then
- if turtle.getFuelLevel()<fuellevel then
- print("insert fuel in bottom right slot")
- while turtle.getItemCount(16)<1 do
- sleep(1)
- end
- turtle.select(16)
- if turtle.refuel()==false then
- print("inserted invalid fuel source")
- while turtle.refuel()==false do
- sleep(1)
- end
- end
- end
- elseif fuelmode=="none" then
- --nothing
- elseif fuelmode=="custom" then
- -- work in progress
- end
- end
- currentplace={}
- --[[
- currentplace
- [1] = relative position on x axis
- [2] = relative position on y axis
- [3] = relative position on z axis
- [4] = relative facing (north, east, south, west)
- [5] = absolute position on x axis
- [6] = absolute position on y axis
- [7] = absolute position on z axis
- [8] = absolute facing (north, east, south, west)
- can be called without any arguments
- ]]--
- function setplace(a, b, c, d)
- if a ==nil then a=0 end
- if b ==nil then b=0 end
- if c==nil then c=0 end
- if d==nil then d="north" end
- currentplace[1]=a
- currentplace[2]=b
- currentplace[3]=c
- currentplace[4]=d
- if gps.locate()~=nil then
- currentplace[5], currentplace[6], currentplace[7] = gps.locate()
- -- gps facing code
- location={}
- if turtle.detect()==false then
- fuel()
- turtle.forward()
- location[1], location[2], location[3]=gps.locate()
- if location[3]-currentplace[7]==-1 then
- currentplace[8]="north"
- turtle.back()
- return true
- elseif location[3]-currentplace[7]==1 then
- currentplace[8]="south"
- turtle.back()
- return true
- elseif location[1]-currentplace[5]==-1 then
- currentplace[8]="west"
- turtle.back()
- return true
- else
- currentplace[8]="east"
- turtle.back()
- return true
- end
- else
- right()
- if turtle.detect()==false then
- fuel()
- turtle.forward()
- location[1], location[2], location[3]=gps.locate()
- if location[3]-currentplace[7]==-1 then
- currentplace[8]="north"
- fuel()
- turtle.back()
- left()
- return true
- elseif location[3]-currentplace[7]==1 then
- currentplace[8]="south"
- fuel()
- turtle.back()
- left()
- return true
- elseif location[1]-currentplace[5]==-1 then
- currentplace[8]="west"
- fuel()
- turtle.back()
- left()
- return true
- else
- currentplace[8]="east"
- fuel()
- turtle.back()
- left()
- return true
- end
- else
- right()
- if turtle.detect()==false then
- fuel()
- turtle.forward()
- location[1], location[2], location[3]=gps.locate()
- if location[3]-currentplace[7]==-1 then
- currentplace[8]="north"
- fuel()
- turtle.back()
- right()
- right()
- return true
- elseif location[3]-currentplace[7]==1 then
- currentplace[8]="south"
- fuel()
- turtle.back()
- right()
- right()
- return true
- elseif location[1]-currentplace[5]==-1 then
- currentplace[8]="west"
- fuel()
- turtle.back()
- right()
- right()
- return true
- else
- currentplace[8]="east"
- fuel()
- turtle.back()
- right()
- right()
- return true
- end
- else
- right()
- if turtle.detect()==false then
- fuel()
- turtle.forward()
- location[1], location[2], location[3]=gps.locate()
- if location[3]-currentplace[7]==-1 then
- currentplace[8]="north"
- fuel()
- turtle.back()
- right()
- return true
- elseif location[3]-currentplace[7]==1 then
- currentplace[8]="south"
- fuel()
- turtle.back()
- right()
- return true
- elseif location[1]-currentplace[5]==-1 then
- currentplace[8]="west"
- fuel()
- turtle.back()
- right()
- return true
- else
- currentplace[8]="east"
- fuel()
- turtle.back()
- right()
- return true
- end
- else
- right()
- return false
- end
- end
- end
- end
- -- end of gps facing code
- end
- end --of function
- --use "force" as parameter to break blocks
- function up(p)
- if turtle.detectUp()==false then
- fuel()
- turtle.up()
- if currentplace[2]~=nil then
- currentplace[2]=currentplace[2]+1
- end
- if currentplace[6]~=nil then
- currentplace[6]=currentplace[6]+1
- end
- elseif p=="force" then
- fuel()
- turtle.select(1)
- turtle.digUp()
- turtle.up()
- if currentplace[2]~=nil then
- currentplace[2]=currentplace[2]+1
- end
- if currentplace[6]~=nil then
- currentplace[6]=currentplace[6]+1
- end
- end
- end
- --use "force" as parameter to break blocks
- function down(q)
- if turtle.detectDown()==false then
- fuel()
- turtle.down()
- if currentplace[2]~=nil then
- currentplace[2]=currentplace[2]-1
- end
- if currentplace[6]~=nil then
- currentplace[6]=currentplace[6]-1
- end
- elseif q=="force" then
- turtle.select(1)
- turtle.digDown()
- fuel()
- turtle.down()
- if currentplace[2]~=nil then
- currentplace[2]=currentplace[2]-1
- end
- if currentplace[6]~=nil then
- currentplace[6]=currentplace[6]-1
- end
- end
- end
- function left()
- turtle.turnLeft()
- if currentplace[4]== "north" then
- currentplace[4]="west"
- elseif currentplace[4]== "west" then
- currentplace[4]= "south"
- elseif currentplace[4]=="south" then
- currentplace[4]="east"
- elseif currentplace[4]=="east" then
- currentplace[4]="north"
- end
- if currentplace[8]== "north" then
- currentplace[8]="west"
- elseif currentplace[8]== "west" then
- currentplace[8]= "south"
- elseif currentplace[8]=="south" then
- currentplace[8]="east"
- elseif currentplace[8]=="east" then
- currentplace[8]="north"
- end
- end
- function right()
- turtle.turnRight()
- if currentplace[4]== "north" then
- currentplace[4]="east"
- elseif currentplace[4]== "west" then
- currentplace[4]= "north"
- elseif currentplace[4]=="south" then
- currentplace[4]="west"
- elseif currentplace[4]=="east" then
- currentplace[4]="south"
- end
- if currentplace[8]== "north" then
- currentplace[8]="east"
- elseif currentplace[8]== "west" then
- currentplace[8]= "north"
- elseif currentplace[8]=="south" then
- currentplace[8]="west"
- elseif currentplace[8]=="east" then
- currentplace[8]="south"
- end
- end
- --use "force" as parameter to break blocks
- function forward(m)
- if turtle.detect()==false then
- fuel()
- turtle.forward()
- if currentplace[4]=="north" then
- currentplace[3]=currentplace[3]-1
- elseif currentplace[4]=="west" then
- currentplace[1]=currentplace[1]-1
- elseif currentplace[4]=="south" then
- currentplace[3] = currentplace[3]+1
- elseif currentplace[4]=="east" then
- currentplace[1]=currentplace[1]+1
- end
- if currentplace[8]=="north" then
- currentplace[7]=currentplace[7]-1
- elseif currentplace[8]=="west" then
- currentplace[5]=currentplace[5]-1
- elseif currentplace[8]=="south" then
- currentplace[7] = currentplace[7]+1
- elseif currentplace[8]=="east" then
- currentplace[5]=currentplace[5]+1
- end
- elseif m=="force" then
- turtle.select(1)
- turtle.dig()
- fuel()
- turtle.forward()
- if currentplace[4]=="north" then
- currentplace[3]=currentplace[3]-1
- elseif currentplace[4]=="west" then
- currentplace[1]=currentplace[1]-1
- elseif currentplace[4]=="south" then
- currentplace[3] = currentplace[3]+1
- elseif currentplace[4]=="east" then
- currentplace[1]=currentplace[1]+1
- end
- if currentplace[8]=="north" then
- currentplace[7]=currentplace[7]-1
- elseif currentplace[8]=="west" then
- currentplace[5]=currentplace[5]-1
- elseif currentplace[8]=="south" then
- currentplace[7] = currentplace[7]+1
- elseif currentplace[8]=="east" then
- currentplace[5]=currentplace[5]+1
- end
- end
- end
- function getplace()
- return currentplace
- end
- --moce, gps/relative
- function orient(mode, facing)
- if mode=="gps" then --gps mode
- if facing == currentplace[8] then
- --nothing
- elseif currentplace[8]=="north" and facing=="west" then
- left()
- elseif currentplace[8]=="west" and facing=="south" then
- left()
- elseif currentplace[8]=="south" and facing=="east" then
- left()
- elseif currentplace[8]=="east" and facing=="north" then
- left()
- else
- repeat
- right()
- until currentplace[8]==facing
- end
- else --normal mode
- if facing == currentplace[4] then
- --nothing
- elseif currentplace[4]=="north" and facing=="west" then
- left()
- elseif currentplace[4]=="west" and facing=="south" then
- left()
- elseif currentplace[4]=="south" and facing=="east" then
- left()
- elseif currentplace[4]=="east" and facing=="north" then
- left()
- else
- repeat
- right()
- until facing==currentplace[4]
- end
- -- end of mode selection
- end
- -- end of function
- end
- --mode, gps/relative r, force/nil
- --use "force" as parameter to break blocks
- function goto(mode, xAxis, yAxis, zAxis, r)
- if mode=="gps" then
- --gpsmode
- if currentplace[5] ~=nil and currentplace[6] ~=nil and currentplace[7] ~=nil and currentplace[8]~=nil then
- if xAxis-currentplace[5]>0 then
- orient("gps", "east")
- elseif xAxis-currentplace[5]<0 then
- orient("gps", "west")
- end
- while xAxis~=currentplace[5] do
- forward()
- end
- if zAxis-currentplace[7]>0 then
- orient("gps", "south")
- elseif zAxis-currentplace[7]<0 then
- orient("gps", "north")
- end
- while zAxis~=currentplace[7] do
- forward(r)
- end
- if yAxis-currentplace[6]<0 then
- while yAxis~=currentplace[6] do
- down(r)
- end
- elseif yAxis-currentplace[6]>0 then
- while yAxis~=currentplace[6] do
- up(r)
- end
- end
- else
- print("no valid mode stated")
- end
- elseif mode=="relative" then
- --relative positioning mode
- if xAxis-currentplace[1]>0 then
- orient("relative", "east")
- elseif xAxis-currentplace[1]<0 then
- orient("relative", "west")
- end
- while xAxis~=currentplace[1] do
- forward(r)
- end
- if zAxis-currentplace[3]>0 then
- orient("relative", "south")
- elseif zAxis-currentplace[3]<0 then
- orient("relative", "north")
- end
- while zAxis~=currentplace[3] do
- forward(r)
- end
- if yAxis-currentplace[2]<0 then
- while yAxis~=currentplace[2] do
- down(r)
- end
- elseif yAxis-currentplace[2]>0 then
- while yAxis~=currentplace[2] do
- up(r)
- end
- end
- else
- print("no gps signal stated")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment