Advertisement
demon012

ComputerCraft Sorter

Jul 17th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.50 KB | None | 0 0
  1. -- Todo
  2. -- - Create and display interface on sorting computer for adding items to be sorted to a sorting database.
  3. -- -- Should allow adding items to be macerated.
  4. -- -- Should allow adding items to be furnaced.
  5. -- -- Should allow editing and deleting of existing items.
  6. -- my libraries
  7. -- check dir exists, if not make it
  8. if not fs.exists("/libs") then--{{{
  9.     fs.makeDir("/libs")
  10. end--}}}
  11.  
  12. function update_lib(pastebin_id, dest_path)--{{{
  13. if fs.exists(dest_path) then
  14.     fs.delete(dest_path)
  15. end
  16. shell.run("pastebin get " .. pastebin_id  .. " " .. dest_path)
  17. os.loadAPI(dest_path)
  18. end--}}}
  19.  
  20. update_lib("qUpykFN4", "libs/ccmisc")
  21. update_lib("n1S2GpfD", "libs/sorterAPI")
  22.  
  23. -- clear library spam
  24. term.setCursorPos(1,1)
  25. term.clear()
  26. -- end my libraries
  27.  
  28. Sorter = {--{{{
  29.     delay = 5,
  30.     stackDelay = 2,
  31.     sorter = peripheral.wrap("bottom"),
  32.     maceratorCompID = 86,
  33.     furnaceCompID = 87,
  34.     enderChestSide = 4,
  35.     maceratorSide = 2,
  36.     furnaceSide = 3,
  37.     storageSide = 0,
  38.     modem = rednet.open("top"),
  39.     macerate_items = {--{{{
  40.         --["4,0"] = true, -- Cobblestone
  41.         ["14,0"] = true, -- Gold Ore
  42.         ["15,0"] = true, -- Iron Ore
  43.         ["16,0"] = true, -- Coal Ore
  44.         ["238,0"] = true, -- Iron Ore
  45.         ["248,0"] = true, -- Tin Ore
  46.         ["249,0"] = true, -- Copper Ore
  47.         ["902,0"] = true, -- Certus Quartz Ore
  48.         ["1398,1"] = true, -- Copper Ore
  49.         ["1398,2"] = true, -- Tin Ore
  50.         ["1488,0"] = true, -- Iron Gravel Ore
  51.         ["2001,0"] = true, -- Copper Ore Ore
  52.         ["2001,1"] = true, -- Tin Ore
  53.         ["2001,2"] = true, -- Silver Ore
  54.         ["2001,3"] = true, -- Lead Ore
  55.         ["2001,4"] = true, -- Ferrous Ore
  56.         ["4060,1"] = true, -- Gelena Ore
  57.         ["4060,5"] = true, -- Bauxite Ore
  58.         ["4060,13"] = true, -- Tetrahedrite Ore
  59.         ["4060,14"] = true, -- Cassiterite Ore
  60.     },--}}}
  61.     furnace_items = {--{{{
  62.         ["363,0"] = true, -- Raw Beef
  63.         ["365,0"] = true, -- Raw Chicken
  64.         ["1475,5"] = true, -- Aluminium Ore
  65.         ["1488,4"] = true, -- Aluminium Gravel Ore
  66.         ["2402,0"] = true, -- Cinnabar Ore
  67.         --["5362,8"] = true, -- Iron Dust
  68.         --["5362,9"] = true, -- Gold Dust
  69.         ["5362,20"] = true, -- Basic Processor Assembly
  70.         --["31988","244"] = true, -- Tin Dust
  71.         ["20267,1"] = true, -- Pulverized Gold
  72.         ["20267,32"] = true, -- Pulverized Copper
  73.         ["25286,16"] = true, -- Native Iron Cluster
  74.         ["25286,17"] = true, -- Native Copper Cluster
  75.         ["25286,18"] = true, -- Native Tin Cluster
  76.         ["25286,19"] = true, -- Native Silver Cluster
  77.         ["25286,20"] = true, -- Native Lead Cluster
  78.         ["25286,31"] = true, -- Native Gold Cluster
  79.     },--}}}
  80. }--}}}
  81. function Sorter:shouldMacerate(item)--{{{
  82.     idmeta = tostring(item[1]) .. "," .. tostring(item[2])
  83.  
  84.     if self.macerate_items[idmeta] then
  85.         return true
  86.     end
  87.  
  88.     return false
  89. end--}}}
  90. function Sorter:shouldFurnace(item)--{{{
  91.     idmeta = tostring(item[1]) .. "," .. tostring(item[2])
  92.  
  93.     if self.furnace_items[idmeta] then
  94.         return true
  95.     end
  96.  
  97.     return false
  98. end--}}}
  99. function Sorter:maceratorHasSpace()--{{{
  100.     return self:hasSpace(self.maceratorCompID, "Macerator")
  101. end--}}}
  102. function Sorter:hasSpace(computerID, name)--{{{
  103.     rednet.send(computerID, 'hasSpace')
  104.     local computerid, message = rednet.receive(4)
  105.     if message then
  106.         print("Received: " .. textutils.serialize(message))
  107.         if string.find(message, tostring(computerID)) then
  108.             if string.find(message,"noSpace") then
  109.                 print(name .. " has no space")
  110.                 return false
  111.             else
  112.                 print(name .. " has space")
  113.                 return true
  114.             end
  115.         else
  116.             print("Unknown message " .. message)
  117.         end
  118.     else
  119.         print(name .. " message was nil")
  120.     end
  121. end--}}}
  122. function Sorter:furnaceHasSpace()--{{{
  123.     return self:hasSpace(self.furnaceCompID, "Furnace")
  124. end--}}}
  125. function Sorter:macerateItem(uuid, count)--{{{
  126.     self.sorter.extract(self.enderChestSide, uuid, self.maceratorSide, count)
  127. end--}}}
  128. function Sorter:furnaceItem(uuid, count)--{{{
  129.     self.sorter.extract(self.enderChestSide, uuid, self.furnaceSide, count)
  130. end--}}}
  131. function Sorter:storeItem(uuid, count)--{{{
  132.     self.sorter.extract(self.enderChestSide, uuid, self.storageSide, count)
  133. end--}}}
  134. function Sorter:list()--{{{
  135.     return self.sorter.list(self.enderChestSide)
  136. end--}}}
  137. function Sorter:toggleMacerateCobble()--{{{
  138.     if not self.macerate_items["4,0"] then
  139.         print("Macerating cobble to sand")
  140.         self.macerate_items["4,0"] = true
  141.     else
  142.         print("Not macerating cobble to sand")
  143.         self.macerate_items["4,0"] = false
  144.     end
  145. end--}}}
  146. function Sorter:mainLoop()--{{{
  147.     os.startTimer(self.delay)
  148.     while true do
  149.         event, param1, param2, param3 = os.pullEvent()
  150.         if event == 'timer' then
  151.             for uuid,count in pairs(self:list()) do
  152.             item = sorterAPI.getID(uuid)
  153.  
  154.             if self:shouldFurnace(item) then -- check if furnace
  155.                 if self:furnaceHasSpace() then
  156.                 self:furnaceItem(uuid, 64)
  157.                 end
  158.             elseif self:shouldMacerate(item) then -- check if should macerate
  159.                 os.sleep(self.stackDelay)
  160.                 if self:maceratorHasSpace() then
  161.                 self:macerateItem(uuid, 64)
  162.                 end
  163.             else -- store
  164.                 self:storeItem(uuid, count)
  165.             end
  166.             end
  167.             os.startTimer(self.delay)
  168.         elseif event == 'key' then
  169.             --print(tostring(param1))
  170.             if param1 == 46 then -- 'c'
  171.                 self:toggleMacerateCobble()
  172.             end
  173.         end
  174.     end
  175. end--}}}
  176.  
  177. sorter = Sorter
  178. sorter:mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement