Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- API for getting and using the
- -- turtle functions in a given directions
- -- advantages:
- -- Can use direction as an input argument to your
- -- function instead of having to write one
- -- for each direction,
- -- also can get the reverse direction
- -- tip:
- -- if called function nil, then the error probably was that
- -- it was an invalid direction.
- -- the get functions returns nil if it's an invalid
- -- direction.
- -- you can see in the table in the function which
- -- directions are supported.
- -- directions
- f="f" -- forward
- b="b" -- back
- u="u" -- up
- d="d" -- down
- r="r" -- right
- l="l" -- left
- -- lookup table for: function inv(dir)
- invT={
- f=b,
- b=f,
- u=d,
- d=u,
- r=l,
- l=r,
- }
- -- returns the reverse/inverted direction
- function inv(dir)
- return invT[dir]
- end --function
- -- returns true if the direction, is r or l
- -- else returns false
- function isTurn(dir)
- return dir=="r" or dir=="l"
- end
- function isUorD(dir)
- return dir=="u" or dir=="d"
- end
- function isForB(dir)
- return dir=="f" or dir=="b"
- end
- -- lookup tables to quickly get the
- -- turtle function for a given direction
- detectT={
- f=turtle.detect,
- u=turtle.detectUp,
- d=turtle.detectDown,
- }
- compareT={
- f=turtle.compare,
- u=turtle.compareUp,
- d=turtle.compareDown,
- }
- digT={
- f=turtle.dig,
- u=turtle.digUp,
- d=turtle.digDown,
- }
- goT={
- f=turtle.forward,
- b=turtle.back,
- u=turtle.up,
- d=turtle.down,
- r=turtle.turnRight,
- l=turtle.turnLeft,
- }
- -- functions for calling the a turtle function in the
- -- given direction
- function detect(dir)
- local f=getDetect(dir)
- if f==nil then
- error("Error: Invalid direction")
- end
- return f()
- end --function
- function compare(dir)
- local f=getCompareT(dir)
- if f==nil then
- error("Error: Invalid direction")
- end
- return f()
- end --function
- function dig(dir)
- local f=getDig(dir)
- if f==nil then
- error("Error: Invalid direction")
- end
- return f()
- end --function
- function go(dir)
- local f=getGo(dir)
- if f==nil then
- error("Error: Invalid direction")
- end
- return f()
- end --function
- -- get the turtle function in the given direction
- -- useful if another function takes, a function
- -- as an input argument
- function getDetect(dir)
- return detectT[dir]
- end --function
- function getCompare(dir)
- return compareT[dir]
- end --function
- function getDig(dir)
- return digT[dir]
- end --function
- function getGo(dir)
- return goT[dir]
- end --function
- --experimental stuff
- placeT={
- f=turtle.place,
- u=turtle.placeUp,
- d=turtle.placeDown,
- }
- function getPlace(dir)
- return placeT[dir]
- end
- --message only works for forward and it's a sign
- function place(dir,message)
- local f=getPlace(dir)
- if f==nil then
- error("Error: Invalid direction")
- end
- return f(message)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement