Advertisement
Doob

robot API with navigation [OpenComputers]

Sep 14th, 2015
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.63 KB | None | 0 0
  1. local component = require("component")
  2. local sides = require("sides")
  3.  
  4. local robot = {}
  5.  
  6. -------------------------------------------------------------------------------
  7. -- General
  8.  
  9. function robot.name()
  10.   return component.robot.name()
  11. end
  12.  
  13. function robot.level()
  14.   if component.isAvailable("experience") then
  15.     return component.experience.level()
  16.   else
  17.     return 0
  18.   end
  19. end
  20.  
  21. function robot.getLightColor()
  22.   return component.robot.getLightColor()
  23. end
  24.  
  25. function robot.setLightColor(value)
  26.   return component.robot.setLightColor(value)
  27. end
  28.  
  29. -------------------------------------------------------------------------------
  30. -- World
  31.  
  32. function robot.detect()
  33.   return component.robot.detect(sides.front)
  34. end
  35.  
  36. function robot.detectUp()
  37.   return component.robot.detect(sides.up)
  38. end
  39.  
  40. function robot.detectDown()
  41.   return component.robot.detect(sides.down)
  42. end
  43.  
  44. -------------------------------------------------------------------------------
  45. -- Inventory
  46.  
  47. function robot.inventorySize()
  48.   return component.robot.inventorySize()
  49. end
  50.  
  51.  
  52. function robot.select(...)
  53.   return component.robot.select(...)
  54. end
  55.  
  56. function robot.count(...)
  57.   return component.robot.count(...)
  58. end
  59.  
  60. function robot.space(...)
  61.   return component.robot.space(...)
  62. end
  63.  
  64. function robot.compareTo(...)
  65.   return component.robot.compareTo(...)
  66. end
  67.  
  68. function robot.transferTo(...)
  69.   return component.robot.transferTo(...)
  70. end
  71.  
  72. -------------------------------------------------------------------------------
  73. -- Inventory + World
  74.  
  75. function robot.compare()
  76.   return component.robot.compare(sides.front)
  77. end
  78.  
  79. function robot.compareUp()
  80.   return component.robot.compare(sides.up)
  81. end
  82.  
  83. function robot.compareDown()
  84.   return component.robot.compare(sides.down)
  85. end
  86.  
  87. function robot.drop(count)
  88.   checkArg(1, count, "nil", "number")
  89.   return component.robot.drop(sides.front, count)
  90. end
  91.  
  92. function robot.dropUp(count)
  93.   checkArg(1, count, "nil", "number")
  94.   return component.robot.drop(sides.up, count)
  95. end
  96.  
  97. function robot.dropDown(count)
  98.   checkArg(1, count, "nil", "number")
  99.   return component.robot.drop(sides.down, count)
  100. end
  101.  
  102. function robot.place(side, sneaky)
  103.   checkArg(1, side, "nil", "number")
  104.   return component.robot.place(sides.front, side, sneaky ~= nil and sneaky ~= false)
  105. end
  106.  
  107. function robot.placeUp(side, sneaky)
  108.   checkArg(1, side, "nil", "number")
  109.   return component.robot.place(sides.up, side, sneaky ~= nil and sneaky ~= false)
  110. end
  111.  
  112. function robot.placeDown(side, sneaky)
  113.   checkArg(1, side, "nil", "number")
  114.   return component.robot.place(sides.down, side, sneaky ~= nil and sneaky ~= false)
  115. end
  116.  
  117. function robot.suck(count)
  118.   checkArg(1, count, "nil", "number")
  119.   return component.robot.suck(sides.front, count)
  120. end
  121.  
  122. function robot.suckUp(count)
  123.   checkArg(1, count, "nil", "number")
  124.   return component.robot.suck(sides.up, count)
  125. end
  126.  
  127. function robot.suckDown(count)
  128.   checkArg(1, count, "nil", "number")
  129.   return component.robot.suck(sides.down, count)
  130. end
  131.  
  132. -------------------------------------------------------------------------------
  133. -- Tool
  134.  
  135. function robot.durability()
  136.   return component.robot.durability()
  137. end
  138.  
  139.  
  140. function robot.swing(side, sneaky)
  141.   checkArg(1, side, "nil", "number")
  142.   return component.robot.swing(sides.front, side, sneaky ~= nil and sneaky ~= false)
  143. end
  144.  
  145. function robot.swingUp(side, sneaky)
  146.   checkArg(1, side, "nil", "number")
  147.   return component.robot.swing(sides.up, side, sneaky ~= nil and sneaky ~= false)
  148. end
  149.  
  150. function robot.swingDown(side, sneaky)
  151.   checkArg(1, side, "nil", "number")
  152.   return component.robot.swing(sides.down, side, sneaky ~= nil and sneaky ~= false)
  153. end
  154.  
  155. function robot.use(side, sneaky, duration)
  156.   checkArg(1, side, "nil", "number")
  157.   checkArg(3, duration, "nil", "number")
  158.   return component.robot.use(sides.front, side, sneaky ~= nil and sneaky ~= false, duration)
  159. end
  160.  
  161. function robot.useUp(side, sneaky, duration)
  162.   checkArg(1, side, "nil", "number")
  163.   checkArg(3, duration, "nil", "number")
  164.   return component.robot.use(sides.up, side, sneaky ~= nil and sneaky ~= false, duration)
  165. end
  166.  
  167. function robot.useDown(side, sneaky, duration)
  168.   checkArg(1, side, "nil", "number")
  169.   checkArg(3, duration, "nil", "number")
  170.   return component.robot.use(sides.down, side, sneaky ~= nil and sneaky ~= false, duration)
  171. end
  172.  
  173. -------------------------------------------------------------------------------
  174. -- Navigation
  175.  
  176. local p_trigger = false
  177.  
  178. local path = ''
  179.  
  180. local x, y, z, side = 0, 0, 0, 'N'
  181.  
  182. function robot.setPath(value)
  183.   p_trigger = value
  184. end
  185.  
  186. function robot.getPath()
  187.   return path
  188. end
  189.  
  190. function robot.getPos()
  191.   return x, y, z, side
  192. end
  193.  
  194. function robot.setPos(x1, y1, z1, s1)
  195.   x, y, z, side = tonumber(x1), tonumber(y1), tonumber(z1), string.upper(s1)
  196. end
  197.  
  198. -------------------------------------------------------------------------------
  199. -- Movement
  200.  
  201. function robot.forward()
  202.   if component.robot.move(sides.front) then
  203.     if p_trigger == true then
  204.       path = path..'F'
  205.     end
  206.     if side == 'N' then
  207.       z = z - 1
  208.     elseif side == 'E' then
  209.       x = x + 1
  210.     elseif side == 'S' then
  211.       z = z + 1
  212.     elseif side == 'W' then
  213.       x = x - 1
  214.     end
  215.     return true
  216.   else
  217.     return false
  218.   end
  219. end
  220.  
  221. function robot.back()
  222.   if component.robot.move(sides.back) then
  223.     if p_trigger == true then
  224.       path = path..'B'
  225.     end
  226.     if side == 'N' then
  227.       z = z + 1
  228.     elseif side == 'E' then
  229.       x = x - 1
  230.     elseif side == 'S' then
  231.       z = z - 1
  232.     elseif side == 'W' then
  233.       x = x + 1
  234.     end
  235.     return true
  236.   else
  237.     return false
  238.   end
  239. end
  240.  
  241. function robot.up()
  242.   if component.robot.move(sides.up) then
  243.     if p_trigger == true then
  244.       path = path..'U'
  245.     end
  246.     y = y + 1
  247.     return true
  248.   else
  249.     return false
  250.   end
  251. end
  252.  
  253. function robot.down()
  254.   if component.robot.move(sides.down) then
  255.     if p_trigger == true then
  256.       path = path..'D'
  257.     end
  258.     y = y - 1
  259.     return true
  260.   else
  261.     return false
  262.   end
  263. end
  264.  
  265. function robot.turnLeft()
  266.   if component.robot.turn(false) then
  267.     if p_trigger == true then
  268.       path = path..'L'
  269.     end
  270.     if side == 'N' then
  271.       side = 'W'
  272.     elseif side == 'E' then
  273.       side = 'N'
  274.     elseif side == 'S' then
  275.       side = 'E'
  276.     elseif side == 'W' then
  277.       side = 'S'
  278.     end
  279.     return true
  280.   else
  281.     return false
  282.   end
  283. end
  284.  
  285. function robot.turnRight()
  286.   if component.robot.turn(true) then
  287.     if p_trigger == true then
  288.       path = path..'R'
  289.     end
  290.     if side == 'N' then
  291.       side = 'E'
  292.     elseif side == 'E' then
  293.       side = 'S'
  294.     elseif side == 'S' then
  295.       side = 'W'
  296.     elseif side == 'W' then
  297.       side = 'N'
  298.     end
  299.     return true
  300.   else
  301.     return false
  302.   end
  303. end
  304.  
  305. function robot.turnAround()
  306.   local turn = math.random() < 0.5 and robot.turnLeft or robot.turnRight
  307.   return turn() and turn()
  308. end
  309.  
  310. -------------------------------------------------------------------------------
  311. -- Tank
  312.  
  313. function robot.tankCount()
  314.   return component.robot.tankCount()
  315. end
  316.  
  317. function robot.selectTank(tank)
  318.   return component.robot.selectTank(tank)
  319. end
  320.  
  321. function robot.tankLevel(...)
  322.   return component.robot.tankLevel(...)
  323. end
  324.  
  325. function robot.tankSpace(...)
  326.   return component.robot.tankSpace(...)
  327. end
  328.  
  329. function robot.compareFluidTo(...)
  330.   return component.robot.compareFluidTo(...)
  331. end
  332.  
  333. function robot.transferFluidTo(...)
  334.   return component.robot.transferFluidTo(...)
  335. end
  336.  
  337. -------------------------------------------------------------------------------
  338. -- Tank + World
  339.  
  340. function robot.compareFluid()
  341.   return component.robot.compareFluid(sides.front)
  342. end
  343.  
  344. function robot.compareFluidUp()
  345.   return component.robot.compareFluid(sides.up)
  346. end
  347.  
  348. function robot.compareFluidDown()
  349.   return component.robot.compareFluid(sides.down)
  350. end
  351.  
  352. function robot.drain(count)
  353.   checkArg(1, count, "nil", "number")
  354.   return component.robot.drain(sides.front, count)
  355. end
  356.  
  357. function robot.drainUp(count)
  358.   checkArg(1, count, "nil", "number")
  359.   return component.robot.drain(sides.up, count)
  360. end
  361.  
  362. function robot.drainDown(count)
  363.   checkArg(1, count, "nil", "number")
  364.   return component.robot.drain(sides.down, count)
  365. end
  366.  
  367. function robot.fill(count)
  368.   checkArg(1, count, "nil", "number")
  369.   return component.robot.fill(sides.front, count)
  370. end
  371.  
  372. function robot.fillUp(count)
  373.   checkArg(1, count, "nil", "number")
  374.   return component.robot.fill(sides.up, count)
  375. end
  376.  
  377. function robot.fillDown(count)
  378.   checkArg(1, count, "nil", "number")
  379.   return component.robot.fill(sides.down, count)
  380. end
  381.  
  382. -------------------------------------------------------------------------------
  383.  
  384. return robot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement