Advertisement
Lupino

robot-print.lua

Jun 13th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.02 KB | None | 0 0
  1. local robot = require("robot")
  2. local shell = require("shell")
  3. local args = shell.parse(...)
  4. local component = require("component")
  5.  
  6. local slot = 1
  7. local maxSlot = 64
  8.  
  9. local itemName = ''
  10.  
  11. function getItemName(slot)
  12.     local item = component.inventory_controller.getStackInInternalSlot(slot)
  13.     if item then
  14.         return item.name
  15.     else
  16.         return ''
  17.     end
  18. end
  19.  
  20. function up()
  21.     local can, type = robot.detectUp()
  22.     if (can) then
  23.         robot.swingUp()
  24.         up()
  25.     else
  26.         robot.up()
  27.     end
  28. end
  29.  
  30. function down()
  31.     local can, type = robot.detectDown()
  32.     if (can) then
  33.         robot.swingDown()
  34.         down()
  35.     else
  36.         robot.down()
  37.     end
  38. end
  39.  
  40. function forward()
  41.     local can, type = robot.detect()
  42.     if (can) then
  43.         robot.swing()
  44.         forward()
  45.     else
  46.         robot.forward()
  47.     end
  48. end
  49.  
  50. function checkSlot()
  51.     local newItemName = getItemName(slot)
  52.     if (newItemName == itemName) then
  53.         robot.select(slot)
  54.     else
  55.         slot = slot + 1
  56.         if (slot > maxSlot) then
  57.             slot = 1
  58.         end
  59.         checkSlot()
  60.     end
  61. end
  62.  
  63. function placeDown()
  64.     local can, type = robot.detectDown()
  65.     if (can) then
  66.         robot.swingDown()
  67.         placeDown()
  68.     else
  69.         checkSlot()
  70.         robot.placeDown()
  71.     end
  72. end
  73.  
  74. function runLine(line)
  75.     local len = string.len(line)
  76.     for i = 1, len, 1 do
  77.         local byte = string.byte(line, i)
  78.         if (byte == 76) then -- L
  79.             robot.turnLeft()
  80.         elseif (byte == 82) then -- R
  81.             robot.turnRight()
  82.         elseif (byte == 70) then -- F
  83.             forward()
  84.         elseif (byte == 80) then -- P
  85.             placeDown()
  86.         elseif (byte == 85) then -- U
  87.             up()
  88.         elseif (byte == 68) then -- D
  89.             down()
  90.         end
  91.     end
  92. end
  93.  
  94. function main()
  95.     local file = io.open(args[1])
  96.     itemName = getItemName(1)
  97.     for line in file:lines() do
  98.         runLine(line)
  99.     end
  100.     file:close()
  101. end
  102.  
  103. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement