Advertisement
natie3

moveAPI

Sep 13th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.74 KB | None | 0 0
  1. function create(whileFunction, action, before, after)
  2.   return function()
  3.     if before then
  4.       before()
  5.     end
  6.  
  7.     while whileFunction() do
  8.       action()
  9.       os.sleep(0.5)
  10.     end
  11.  
  12.     if after then
  13.       after()
  14.     end
  15.   end
  16. end
  17.  
  18. local mapping = {}
  19.  
  20. -- All the move actions
  21. mapping["f"] = create(turtle.detect, turtle.dig, nil, turtle.forward)
  22. mapping["l"] = create(turtle.detect, turtle.dig, turtle.turnLeft, turtle.forward)
  23. mapping["r"] = create(turtle.detect, turtle.dig, turtle.turnRight, turtle.forward)
  24. mapping["u"] = create(turtle.detectUp, turtle.digUp, nil, turtle.up)
  25. mapping["d"] = create(turtle.detectDown, turtle.digDown, nil, turtle.down)
  26. mapping["b"] = turtle.back
  27.  
  28. -- Turn direction
  29. mapping["tl"] = turtle.turnLeft
  30. mapping["tr"] = turtle.turnRight
  31. mapping["ta"] = function() turtle.turnLeft() turtle.turnLeft() end -- turn around
  32.  
  33. -- All the dig actions
  34. mapping["df"] = create(turtle.detect, turtle.dig)
  35. mapping["dl"] = create(turtle.detect, turtle.dig, turtle.turnLeft)
  36. mapping["dr"] = create(turtle.detect, turtle.dig, turtle.turnRight)
  37. mapping["du"] = create(turtle.detectUp, turtle.digUp)
  38. mapping["dd"] = create(turtle.detectDown, turtle.digDown)
  39.  
  40. -- All the place actions
  41. mapping["pf"] = turtle.place
  42. mapping["pd"] = turtle.placeDown
  43. mapping["pu"] = turtle.placeUp
  44.  
  45. function move(moveString)
  46.   for cur in moveString:gmatch("%S+") do
  47.     mapping[cur]()
  48.   end
  49. end
  50.  
  51. function repeatMoves(moveString, amount)
  52.   for i = 1, amount do
  53.     move(moveString)
  54.   end
  55. end
  56.  
  57. function moveRelative(forward, up)
  58.   if(forward < 0) then
  59.     repeatMoves("b", -1 * forward)
  60.   else
  61.     repeatMoves("f", forward)
  62.   end
  63.   if(up < 0) then
  64.     repeatMoves("d", -1 * up)
  65.   else
  66.     repeatMoves("u", up)
  67.   end
  68. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement