Advertisement
Andronio12

/lib/robot.lua Remake

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