bigtwisty

FrameMaster

Apr 17th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. --name FrameMaster
  2. --version 1.0a
  3. local version = "1.0a"
  4. rednet.open("right")
  5.  
  6. local side = {}
  7. side.batbox = 0
  8. side.chest = 4
  9. local batteryId = {}
  10. batteryId.full = 38041
  11. batteryId.empty = 5272
  12.  
  13. local inventory = {}
  14. inventory.batbox = "Empty"
  15. inventory.chest = false
  16. inventory.chestFull = 0
  17. inventory.chestEmpty = 0
  18.  
  19. local function output(str)
  20. term.clear()
  21. term.setCursorPos(1,1)
  22. print(string.format("FrameMaster (v%s)",version))
  23. print("By BigTwisty")
  24. print("-----------------")
  25. print(str)
  26. end
  27.  
  28. local function move()
  29. redstone.setOutput("left", true)
  30. while turtle.detectUp() do
  31. os.sleep(0.1)
  32. end
  33. redstone.setOutput("left", false)
  34. redstone.setOutput("back", true)
  35. while not turtle.detectUp() do
  36. os.sleep(0.1)
  37. end
  38. redstone.setOutput("back", false)
  39. end
  40.  
  41. local function rednetBroadcastAndWait(outMessage, inMessage)
  42. while true do
  43. rednet.broadcast(outMessage)
  44. id,msg,dist = rednet.receive(0.5)
  45. if msg == inMessage then
  46. return id
  47. end
  48. end
  49. end
  50.  
  51. local sorter = peripheral.wrap("front")
  52. output("Waiting for MasterPC")
  53. rednetBroadcastAndWait("FrameMaster Init", "FrameMaster Ok")
  54.  
  55. while true do
  56. inventory.batbox = "Not Found..."
  57. x = sorter.list(side.batbox)
  58. if x ~= nil then
  59. inventory.batbox = "Empty"
  60. for id,qty in pairs(x) do
  61. if id == batteryId.full then
  62. inventory.batbox = "Full"
  63. else
  64. inventory.batbox = "Discharging"
  65. end
  66. end
  67. end
  68.  
  69. inventory.chestFull = 0
  70. inventory.chestEmpty = 0
  71.  
  72. x = sorter.list(side.chest)
  73. if x == nil then
  74. inventory.chest = false
  75. else
  76. inventory.chest = true
  77. for id,qty in pairs(x) do
  78. if id == batteryId.full then
  79. inventory.chestFull = inventory.chestFull + qty
  80. elseif id == batteryId.empty then
  81. inventory.chestEmpty = inventory.chestEmpty + qty
  82. end
  83. end
  84. end
  85.  
  86. output(string.format("\n-- Status:\nBatbox: %s",
  87. inventory.batbox))
  88. write(string.format("-- Inventory:\nCharged: %d\nEmpty: %d\n",
  89. inventory.chestFull,
  90. inventory.chestEmpty))
  91.  
  92. if inventory.batbox == "Discharging" then
  93. sorter.extract(side.batbox, batteryId.empty, side.chest, 1)
  94. elseif inventory.batbox == "Empty" then
  95. sorter.extract(side.chest, batteryId.full, side.batbox, 1)
  96. end
  97.  
  98. id,msg,dist = rednet.receive(0.3)
  99. if msg == "FrameMaster Move" then
  100. move()
  101. rednet.send(masterPC, "FrameMaster Finished")
  102. end
  103. end
Advertisement
Add Comment
Please, Sign In to add comment