bigtwisty

btRobot

Jun 15th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.68 KB | None | 0 0
  1. -- btRobot
  2. -- A replacement script to make robots locationally aware
  3.  
  4. local baseRobot = require( "robot" )
  5. local compRobot = require( "component" ).robot
  6.  
  7. local position = { 0, 0, 0 }
  8. local direction = 1
  9.  
  10. local function addVector( a, b )
  11.   return { a[1] + b[1], a[2] + b[2], a[3] + b[3] }
  12. end
  13.  
  14. local offset =
  15. {
  16.   [sides.down] = {  0, -1,  0 },
  17.   [sides.up]   = {  0,  1,  0 },
  18.   [sides.negz] = {  0,  0, -1 },
  19.   [sides.posz] = {  0,  0,  1 },
  20.   [sides.negx] = { -1,  0,  0 },
  21.   [sides.posx] = {  1,  0,  0 }
  22. }
  23.  
  24. local directions =
  25. {
  26.   sides.posx,
  27.   sides.posz,
  28.   sides.negx,
  29.   sides.negz,
  30.   length = 4
  31. }
  32.  
  33. local btRobot = {}
  34.  
  35. btRobot.turnRight = function()
  36.   baseRobot.turnRight()
  37.   direction = direction + 1
  38.   if direction > 4 then direction = 1
  39. end
  40.  
  41. btRobot.turnLeft = function()
  42.   baseRobot.turnLeft()
  43.   direction = direction - 1
  44.   if direction < 1 then direction = 4
  45. end
  46.  
  47. btRobot.turnAround = function()
  48.   baseRobot.turnAround()
  49.   direction = direction + 2
  50.   if direction > 4 then
  51.     direction = direction - 4
  52.   end
  53. end
  54.  
  55. btRobot.move = function( dir )
  56.   result, reason = compRobot.move( dir )
  57.   if result then
  58.     position = addVector( position, offset[dir] )
  59.   end
  60.   return result, reason
  61. end
  62.  
  63. btRobot.up = function()
  64.   return btRobot.move( sides.up )
  65. end
  66.  
  67. btRobot.down = function()
  68.   return btRobot.move( sides.down )
  69. end
  70.  
  71. btRobot.forward = function()
  72.   return btRobot.move( sides.forward )
  73. end
  74.  
  75. btRobot.back = function()
  76.   return btRobot.move( sides.back )
  77. end
  78.  
  79. btRobot.getPosition = function()
  80.   return position[1], position[2], position[3], directions[direction]
  81. end
  82.  
  83. return setmetatable( btRobot, { __index = baseRobot } )
Advertisement
Add Comment
Please, Sign In to add comment