Advertisement
Doob

GOTO [OpenComputers]

Sep 22nd, 2015
325
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local robot = require('robot')
  2. local component = require('component')
  3. local rt = component.robot
  4.  
  5. local tTurn = {
  6.   ['N'] = {['E'] = 1, ['W'] = 2, ['S'] = 3},
  7.   ['E'] = {['S'] = 1, ['N'] = 2, ['W'] = 3},
  8.   ['S'] = {['W'] = 1, ['E'] = 2, ['N'] = 3},
  9.   ['W'] = {['N'] = 1, ['S'] = 2, ['E'] = 3}
  10. }
  11.  
  12. local function turn(a, b)
  13.   if tTurn[a][b] == 1 then
  14.     robot.turnRight()
  15.   elseif tTurn[a][b] == 2 then
  16.     robot.turnLeft()
  17.   elseif tTurn[a][b] == 3 then
  18.     robot.turnAround()
  19.   end
  20. end
  21.  
  22. local function pos()
  23.   return {robot.getPos()}
  24. end
  25.  
  26. local function move(d, c)
  27.   for i = 1, c do
  28.     if rt.detect(d) then
  29.       if rt.swing(d) then
  30.         os.sleep(0.1)
  31.         while rt.detect(d) do
  32.           rt.swing(d)
  33.         end
  34.       else
  35.         return false
  36.       end
  37.     end
  38.     os.sleep(0.1)
  39.     if d == 3 then
  40.       robot.forward()
  41.     elseif d == 1 then
  42.       robot.up()
  43.     elseif d == 0 then
  44.       robot.down()
  45.     end
  46.   end
  47. end
  48.  
  49. local function goto_t(x, y, z)
  50.   if pos()[1] > x then
  51.     turn(pos()[4], 'W')
  52.     move(3, pos()[1]-x)
  53.   elseif pos()[1] < x then
  54.     turn(pos()[4], 'E')
  55.     move(3, x-pos()[1])
  56.   end
  57.  
  58.   if pos()[2] > y then
  59.     move(0, pos()[2]-y)
  60.   elseif pos()[2] < y then
  61.     move(1, y-pos()[2])
  62.   end
  63.  
  64.   if pos()[3] > z then
  65.     turn(pos()[4], 'N')
  66.     move(3, pos()[3]-z)
  67.   elseif pos()[3] < z then
  68.     turn(pos()[4], 'S')
  69.     move(3, z-pos()[3])
  70.   end
  71. end
  72.  
  73. local tArgs = {...}
  74.  
  75. if #tArgs == 3 then
  76.   for o = 1, 3 do
  77.     tArgs[o] = tonumber(tArgs[o])
  78.   end
  79. else
  80.   print('Usage: goto <x> <y> <z>')
  81.   os.exit()
  82. end
  83.  
  84. goto_t(tArgs[1], tArgs[2], tArgs[3])
Advertisement
RAW Paste Data Copied
Advertisement