Advertisement
kurruk

Upper Storage

May 21st, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. p = peripheral.wrap("back") -- Interactive sorter change side as needed.
  2. m = peripheral.wrap("left") -- Modem change side as needed.
  3. m.open(32036)
  4.  
  5. -- Variables that you have to set.
  6.  
  7. item = "glass" -- Item that is in your storage media.
  8. recycle = 2 -- 2 = North, 3 = South, 4 = West, 5 = East
  9. order = 1
  10.  
  11. -- First start-up
  12.  
  13. if fs.exists("itemlist") == false then
  14.   h = fs.open("itemlist", "w")
  15.   h.write("0")
  16.   h.close()
  17. end
  18.  
  19. if os.getComputerLabel() == nil then
  20.   os.setComputerLabel("upper")
  21. end
  22.  
  23. function split(str, delim, maxNb)
  24.   if string.find(str, delim) == nil then
  25.     return { str }
  26.   end
  27.  
  28.   if maxNb == nil or maxNb < 1 then
  29.     maxNb = 0
  30.   end
  31.  
  32.   local result = {}
  33.   local pat = "(.-)" ..delim.. "()"
  34.   local nb = 0
  35.   local lastPos
  36.  
  37.   for part, pos in string.gmatch(str, pat) do
  38.     nb = nb + 1
  39.     result[nb] = part
  40.     lastPos = pos
  41.     if nb == maxNb then
  42.       break
  43.     end
  44.   end
  45.  
  46.   if nb ~= maxNb then
  47.     result[nb + 1] = string.sub(str,lastPos)
  48. end
  49.  
  50.   return result
  51.  
  52. end
  53.  
  54. -- Retrieves number of items in storage.
  55.  
  56. f = fs.open("itemlist", "r")
  57. count = f.readAll()
  58. count = tonumber(count)
  59. f.close()
  60.  
  61. -- Adds to number of items in storage.
  62.  
  63. function function3()
  64.   f = fs.open("itemlist", "w")
  65.   f.writeLine(tostring(count))
  66.   f.close()
  67. end
  68.  
  69. -- The function for sorting the item
  70.  
  71. function function2()
  72.   event, id, amount = os.pullEvent("isort_item")
  73.   if count == 65535 then
  74.     p.sort(recycle)
  75.   else
  76.     count = count + amount
  77.     function3()
  78.     p.sort(0)
  79.   end
  80. end
  81.  
  82.  
  83. -- The function for updating the monitor.
  84.  
  85. function function1()
  86.   event, side, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  87.   Table = {}
  88.   Table = split(message, ":")
  89.   computer = tonumber(Table[1])
  90.   if replyChannel == 33880 and computer == order then
  91.     m.transmit(33880, 32036, item..": "..tostring(count))
  92.   elseif replyChannel == 49794 and Table[1] == item then
  93.     message = tonumber(Table[2])
  94.     if message > count then
  95.       count = 0
  96.       function3()
  97.       print(count.." remaining")
  98.     else
  99.       count = count - message
  100.       function3()
  101.       print(count.." remaining")
  102.     end
  103.   end    
  104. end
  105.  
  106.  
  107. -- Main running code.
  108.  
  109. while true do
  110.  
  111.   parallel.waitForAny(function1, function2)
  112.  
  113. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement