Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- This API will contain the result of
- trying to optimize and refactor mine1 and mine2
- --]]
- os.loadAPI("theinsekt/turtle3")
- -- directions
- f="f" -- forward
- b="b" -- back
- u="u" -- up
- d="d" -- down
- tr="tr" -- turn right
- tl="tl" -- turn left
- -- can't handle negative length
- -- usage: goLoopT[dir](length)
- goLoopT={
- f=getGoLoop(f),
- b=getGoLoop(b),
- u=getGoLoop(u),
- d=getGoLoop(d),
- tr=getGoLoop(tr),
- tl=getGoLoop(tl),
- }
- --can handle negative length
- function goLoop(dir, length)
- if length<0 then
- return -goLoopT[turtle2.inv(dir)](-length)
- else
- return goLoopT[dir](length)
- end
- end
- -- help function used to get go loop in direction dir
- function getGoLoop(dir)
- local go=turtle2.getGo(dir)
- return function(length)
- local i=0
- while i<length do
- if not go() then
- return i
- end
- i=i+1
- end
- return i
- end
- end
- -- usage: digLoopT[dir](length,tries)
- digLoopT={
- f=getDigLoop(f),
- u=getDigLoop(u),
- d=getDigLoop(d),
- b=getDigBackLoop(b),
- }
- function getDigBackLoop()
- local goB=getGoLoop("b")
- local digF=getDigLoop("f")
- local turnR=turtle2.getGo("r")
- return function(length, tries)
- local length2=goB(length)
- if length2~=length then
- turnR()
- turnR()
- local length3 = length2+digF(length-length2,tries)
- turnR()
- turnR()
- return length3
- end
- return length
- end
- end
- function getDigLoop(dir)
- local detect=turtle2.getDetect(dir)
- local dig=turtle2.getDig(dir)
- local go=turtle2.getGo(dir)
- return function(length,tries)
- local i=0
- while i<length do
- if not clearAndGo(detect,dig,go,tries) then
- return i
- end
- end
- return i
- end
- end
- -- tries to clear the space by using the given
- -- functions: 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