Advertisement
Doob

robot api v2

Sep 19th, 2015
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.42 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 tSides = {'N', 'E', 'S', [0] = 'W', ['N'] = 1, ['E'] = 2, ['S'] = 3, ['W'] = 0}
  177.  
  178. local p_trigger = false
  179.  
  180. local path = ''
  181.  
  182. local x, y, z, side = 0, 0, 0, 1
  183.  
  184. function robot.setPath(value)
  185.   p_trigger = value
  186. end
  187.  
  188. function robot.getPath()
  189.   return path
  190. end
  191.  
  192. function robot.getPos()
  193.   return x, y, z, tSides[side % 4]
  194. end
  195.  
  196. function robot.setPos(x1, y1, z1, s1)
  197.   x, y, z, side = tonumber(x1), tonumber(y1), tonumber(z1), tSides[string.upper(s1)]
  198. end
  199.  
  200. -------------------------------------------------------------------------------
  201. -- Movement
  202.  
  203. function robot.forward()
  204.   if component.robot.move(sides.front) then
  205.     if p_trigger == true then
  206.       path = path..'F'
  207.     end
  208.     if side % 4 == 1 then
  209.       z = z - 1
  210.     elseif side % 4 == 2 then
  211.       x = x + 1
  212.     elseif side % 4 == 3 then
  213.       z = z + 1
  214.     elseif side % 4 == 0 then
  215.       x = x - 1
  216.     end
  217.     return true
  218.   else
  219.     return false
  220.   end
  221. end
  222.  
  223. function robot.back()
  224.   if component.robot.move(sides.back) then
  225.     if p_trigger == true then
  226.       path = path..'B'
  227.     end
  228.     if side % 4 == 1 then
  229.       z = z + 1
  230.     elseif side % 4 == 2 then
  231.       x = x - 1
  232.     elseif side % 4 == 3 then
  233.       z = z - 1
  234.     elseif side % 4 == 0 then
  235.       x = x + 1
  236.     end
  237.     return true
  238.   else
  239.     return false
  240.   end
  241. end
  242.  
  243. function robot.up()
  244.   if component.robot.move(sides.up) then
  245.     if p_trigger == true then
  246.       path = path..'U'
  247.     end
  248.     y = y + 1
  249.     return true
  250.   else
  251.     return false
  252.   end
  253. end
  254.  
  255. function robot.down()
  256.   if component.robot.move(sides.down) then
  257.     if p_trigger == true then
  258.       path = path..'D'
  259.     end
  260.     y = y - 1
  261.     return true
  262.   else
  263.     return false
  264.   end
  265. end
  266.  
  267. function robot.turnLeft()
  268.   if component.robot.turn(false) then
  269.     if p_trigger == true then
  270.       path = path..'L'
  271.     end
  272.     side = side - 1
  273.     return true
  274.   else
  275.     return false
  276.   end
  277. end
  278.  
  279. function robot.turnRight()
  280.   if component.robot.turn(true) then
  281.     if p_trigger == true then
  282.       path = path..'R'
  283.     end
  284.     side = side + 1
  285.     return true
  286.   else
  287.     return false
  288.   end
  289. end
  290.  
  291. function robot.turnAround()
  292.   local turn = math.random() < 0.5 and robot.turnLeft or robot.turnRight
  293.   return turn() and turn()
  294. end
  295.  
  296. -------------------------------------------------------------------------------
  297. -- Tank
  298.  
  299. function robot.tankCount()
  300.   return component.robot.tankCount()
  301. end
  302.  
  303. function robot.selectTank(tank)
  304.   return component.robot.selectTank(tank)
  305. end
  306.  
  307. function robot.tankLevel(...)
  308.   return component.robot.tankLevel(...)
  309. end
  310.  
  311. function robot.tankSpace(...)
  312.   return component.robot.tankSpace(...)
  313. end
  314.  
  315. function robot.compareFluidTo(...)
  316.   return component.robot.compareFluidTo(...)
  317. end
  318.  
  319. function robot.transferFluidTo(...)
  320.   return component.robot.transferFluidTo(...)
  321. end
  322.  
  323. -------------------------------------------------------------------------------
  324. -- Tank + World
  325.  
  326. function robot.compareFluid()
  327.   return component.robot.compareFluid(sides.front)
  328. end
  329.  
  330. function robot.compareFluidUp()
  331.   return component.robot.compareFluid(sides.up)
  332. end
  333.  
  334. function robot.compareFluidDown()
  335.   return component.robot.compareFluid(sides.down)
  336. end
  337.  
  338. function robot.drain(count)
  339.   checkArg(1, count, "nil", "number")
  340.   return component.robot.drain(sides.front, count)
  341. end
  342.  
  343. function robot.drainUp(count)
  344.   checkArg(1, count, "nil", "number")
  345.   return component.robot.drain(sides.up, count)
  346. end
  347.  
  348. function robot.drainDown(count)
  349.   checkArg(1, count, "nil", "number")
  350.   return component.robot.drain(sides.down, count)
  351. end
  352.  
  353. function robot.fill(count)
  354.   checkArg(1, count, "nil", "number")
  355.   return component.robot.fill(sides.front, count)
  356. end
  357.  
  358. function robot.fillUp(count)
  359.   checkArg(1, count, "nil", "number")
  360.   return component.robot.fill(sides.up, count)
  361. end
  362.  
  363. function robot.fillDown(count)
  364.   checkArg(1, count, "nil", "number")
  365.   return component.robot.fill(sides.down, count)
  366. end
  367.  
  368. -------------------------------------------------------------------------------
  369.  
  370. return robot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement