gnidmoo

empowerer.lua

Dec 14th, 2019
137
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local robot_api = require("robot")
  2.  
  3. -- 0: front, 1: right, 2: back, 3: left
  4. local Rotation = {
  5.   Front = 0,
  6.   Right = 1,
  7.   Back = 2,
  8.   Left = 3
  9. }
  10.  
  11. local state = {x=0, y=0, z=0, rotation=Rotation.Front}
  12.  
  13. function rotate_to(rotation)
  14.   while state.rotation ~= rotation do
  15.     robot_api.turnRight()
  16.     state.rotation = (state.rotation + 1) % 4
  17.   end
  18. end
  19.  
  20. function move_robot_to(pos)
  21.   while pos.y > state.y do
  22.     robot_api.up()
  23.     state.y = state.y + 1
  24.   end
  25.  
  26.   while pos.x < state.x do
  27.     rotate_to(Rotation.Left)
  28.     robot_api.forward()
  29.     state.x = state.x - 1
  30.   end
  31.  
  32.   while pos.x > state.x do
  33.     rotate_to(Rotation.Right)
  34.     robot_api.forward()
  35.     state.x = state.x + 1
  36.   end
  37.  
  38.   while pos.z < state.z do
  39.     rotate_to(Rotation.Back)
  40.     robot_api.forward()
  41.     state.z = state.z - 1
  42.   end
  43.  
  44.   while pos.z > state.z do
  45.     rotate_to(Rotation.Front)
  46.     robot_api.forward()
  47.     state.z = state.z + 1
  48.   end
  49.  
  50.   while pos.y < state.y do
  51.     robot_api.down()
  52.     state.y = state.y - 1
  53.   end
  54. end
  55.  
  56. move_robot_to({x=-1, y=1, z=5})
  57. os.sleep(5)
  58. move_robot_to({x=0, y=0, z=0})
RAW Paste Data