Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- help functions for mining
- -- uses the functions negative and positive
- -- to turn in a negative or positive direction.
- -- negative could be turtle.turnLeft
- -- positive could be turtle.turnRight
- -- length is an integer
- function turn(positive, negative,length)
- local tmp=length%4
- if tmp==1 then
- positive()
- elseif tmp==3 then
- negative()
- elseif tmp==2 then
- -- bonus, turns in the given direction
- if length<0 then
- negative()
- negative()
- else
- positive()
- positive()
- end --else
- end --else
- return length
- end --function
- function goLoop(go, length)
- local i=0
- while i<length do
- if not go() then
- break
- end --if
- i=i+1
- end --while
- return i
- end --function
- --[[
- example use:
- digLoop(turtle.detect, turtle.dig,turtle.forward, 3, 20)
- where the turtle functions are all in the same direction
- -- Returns the distance traveled, which
- -- is equal to length if it was completely successful
- -- If length was 0 then only tries to dig.
- ]]
- function digLoop(detect,dig,go,length,tries)
- -- special case: only dig if length==0
- if length==0 then
- if clear(detect,dig) then
- return 0
- else
- return -1
- end --else
- end --if
- -- dig and go
- local i=0
- while i<length do
- if not clearAndGo(detect,dig,go,tries) then
- return i
- end --if
- i=i+1
- end --while
- return i
- end --function
- -- tries to clear the space by using the given
- -- function detect, dig.
- -- Returns true if clear, else false
- function clear(detect, dig)
- while detect() do
- if not dig() then
- return false
- end --if
- end --while
- return true
- end --function
- -- tries to clear and then go one step
- -- using the given function detect, dig, go.
- -- Returns true if managed to use the go function,
- -- else false.
- function clearAndGo(detect,dig,go,tries)
- local i=1
- while true do
- if not clear(detect,dig) then
- return false
- end --if
- if go() then
- return true
- end --if
- if i>=tries then
- return false
- end --if
- i=i+1
- end --while
- end --function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement