Advertisement
demon012

AEMan

Aug 11th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.48 KB | None | 0 0
  1. -- Todo
  2. -- - Add support for the Essentia Phials.
  3. --
  4. -- my libraries
  5. -- check dir exists, if not make it
  6. if not fs.exists("/libs") then--{{{
  7.     fs.makeDir("/libs")
  8. end--}}}
  9.  
  10. function update_lib(pastebin_id, dest_path)--{{{
  11. if fs.exists(dest_path) then
  12.     fs.delete(dest_path)
  13. end
  14. shell.run("pastebin get " .. pastebin_id  .. " " .. dest_path)
  15. os.loadAPI(dest_path)
  16. end--}}}
  17.  
  18. update_lib("qUpykFN4", "libs/ccmisc")
  19. update_lib("n1S2GpfD", "libs/sorterAPI")
  20. update_lib("ppqTBDNK", "libs/itemdb")
  21.  
  22. Interface = {--{{{
  23.     interface = peripheral.wrap("back"),
  24.     itemDB = itemdb.db,
  25.     --powerOnIO = peripheral.wrap("io_2"),
  26.     powerOnColor = colours.red,
  27.     powerOnSide = "bottom",
  28.     obsidiPowerOnIO = peripheral.wrap("io_0"),
  29.     wirelessModem = rednet.open('left'),
  30.     craftTimerDuration = 4,
  31.     obsidiTimer = nil,
  32. }--}}}
  33. function Interface:powerOn()--{{{
  34.     --self.powerOnIO.set(true)
  35.     local active = rs.getBundledInput(self.powerOnSide)
  36.     local newcolours = colours.combine(active, self.powerOnColor)
  37.     rs.setBundledOutput(self.powerOnSide, newcolours)
  38.     os.sleep(0.2)
  39.     --self.powerTimerID = os.startTimer(0.8)
  40. end--}}}
  41. function Interface:powerOff()--{{{
  42.     if not self.stayOnTimerID then
  43.         local active = rs.getBundledInput(self.powerOnSide)
  44.         local newcolours = colours.subtract(active, self.powerOnColor)
  45.         rs.setBundledOutput(self.powerOnSide, newcolours)
  46.         --self.powerOnIO.set(false)
  47.     end
  48. end--}}}
  49. function Interface:powerOnFor(seconds)--{{{
  50.     self.stayOnTimerID = os.startTimer(seconds)
  51.     self:powerOn()
  52. end--}}}
  53. function Interface:endPowerOn()--{{{
  54.     self.stayOnTimerID = nil
  55.     self:powerOff()
  56. end--}}}
  57. function Interface:listAll()--{{{
  58.     self:powerOn()
  59.     return self.interface.listAll()
  60. end--}}}
  61. function Interface:listItems()--{{{
  62.     self:powerOn()
  63.     return self.interface.listItems()
  64. end--}}}
  65. function Interface:listCraft()--{{{
  66.     self:powerOn()
  67.     return self.interface.listCraft()
  68. end--}}}
  69. function Interface:nMostStored(n)--{{{
  70.     local itemsStored = self:listAll()
  71.     local id = 0
  72.     local meta = 0
  73.  
  74.     itemsStored = table.sort(itemsStored)
  75.  
  76.     local displayCounter = 0
  77.     for uuid,stored in pairs(itemsStored) do
  78.         id, meta = sorterAPI.getID(uuid)
  79.         message = tostring(amountToDisplay + 1) .. ': ' .. 'storing ' .. tostring(stored) .. ' of ' .. tostring(id) .. ':' .. tostring(meta)
  80.         if displayCounter == n - 1 then
  81.             break
  82.         end
  83.     end
  84. end--}}}
  85.  
  86. -- 0 for down, 1 for up, 2 for -Z (N), 3 for +Z (S), 4 for -X (W) and 5 for +X (E).
  87. function Interface:retrieve(id, amount, outputDirection)--{{{
  88.     self:powerOn()
  89.     return self.interface.retrieve(id, amount, outputDirection)
  90. end--}}}
  91. function Interface:craft(id, amount)--{{{
  92.     self:powerOn()
  93.     self.interface.craft(id, amount)
  94. end--}}}
  95. function Interface:inStock(id)--{{{
  96.     local invAvailable = self:listItems()
  97.     local itemsInStock = 0
  98.  
  99.     if invAvailable[id] == nil then
  100.         itemsInStock = 0
  101.     else-- if item not found in AE or there are not enough items.
  102.         itemsInStock = invAvailable[id]
  103.     end
  104.  
  105.     return itemsInStock
  106. end--}}}
  107. function Interface:canCraft(id)--{{{
  108.     local craftItems = self:listCraft()
  109.  
  110.     for key, value in pairs(craftItems) do
  111.         if tostring(key) == tostring(id) then
  112.             return true
  113.         end
  114.     end
  115.  
  116.     return false
  117. end--}}}
  118. function Interface:missingItems(id, amountRequired)--{{{
  119.     -- get amount of item in storage
  120.     local amountStored = self:inStock(id)
  121.     -- if items in storage is nil then return amount required (meaning need all!)
  122.     if amountStored == 0 then
  123.         return amountRequired
  124.     elseif amountStored < amountRequired then -- if number of items in storage is less than amount required
  125.         return amountRequired - amountStored -- return amount required - items in storage
  126.     else
  127.         return 0
  128.     end
  129. end--}}}
  130. function Interface:monitorStorage(id, amountRequired)--{{{
  131.     local notenough = true
  132.     local missingItems = self:missingItems(id, amountRequired)
  133.  
  134.     repeat
  135.         local craftTimer = os.startTimer(self.craftTimerDuration)
  136.         event, timerID = os.pullEvent("timer")
  137.         missingItems = self:missingItems(id, amountRequired)
  138.     until missingItems == 0
  139. end--}}}
  140. function Interface:retrieveCraft(id, getAmount, outputDir)--{{{
  141.     local item = self.itemDB.itemsByUUID[id]
  142.     local missingItems = self:missingItems(id, getAmount)
  143.     if missingItems > 0 then
  144.         local message = "Not enough " .. item.name .. '. Missing: ' .. missingItems .. '.'
  145.         tc:sendError(message)
  146.  
  147.         if self:canCraft(id) then -- if we can craft the missing items
  148.             message = 'Crafting ' .. missingItems .. ' of ' .. item.name .. '.'
  149.             tc:sendError(message)
  150.             self:craft(id, missingItems)
  151.             self:monitorStorage(id, getAmount) -- monitor storage until the items are available.
  152.             message = 'Finished crafting ' .. getAmount .. ' of ' .. item.name .. '.'
  153.             tc:sendError(message)
  154.         end
  155.     end
  156.  
  157.     retrievedAmount = self:retrieve(id, getAmount, outputDir)
  158.  
  159.     if retrievedAmount > 0 then
  160.         message = 'Retrieved ' .. retrievedAmount .. ' of ' .. item.name .. '.'
  161.     else
  162.         message = 'Unable to retrieve ' .. item.name .. '.'
  163.     end
  164.     tc:sendError(message)
  165. end--}}}
  166. function Interface:craftObsidian(amount)--{{{
  167.     self.obsidiTimer = os.startTimer(5 * amount)
  168.     self.obsidiPowerOnIO.set(true)
  169. end--}}}
  170. function Interface:obsidiPowerOff()--{{{
  171.     self.obsidiPowerOnIO.set(false)
  172.     self.obsidiTimer = nil
  173. end--}}}
  174.  
  175. Thaumcraft = {--{{{
  176.     interface = Interface,
  177.     outputDir = 2, -- debug chest = 4, mirror = 2
  178.     reqComputerID = 30,
  179.     craftTimerDuration = 4,
  180.     aspects = {--{{{
  181.         ['metallum'] = 'Copper Nuggets',
  182.         ['carus'] = 'Smooth Sandstone', -- Smooth Sandstone
  183.         ['praecantatio'] = 'Chiseled Sandstone', -- Chiseled Sandstone
  184.         ['mortuus'] = 'Bone Meal',
  185.         ['victus'] = 'Wheat',
  186.         ['malum'] = 'Music Disc - Stal',
  187.         ['animus'] = 'Soul Sand',
  188.         ['lux'] = 'Torch',
  189.         ['potentia'] = 'Charcoal',
  190.         ['ignis'] = 'Charcoal',
  191.         ['lignum'] = 'Oak Wood',
  192.         ['permutatio'] = 'Seeds',
  193.         ['herba'] = 'Seeds',
  194.         ['vacuos'] = 'Bowl',
  195.         ['solum'] = 'Dirt',
  196.         ['motus'] = 'Trapdoor',
  197.         ['saxum'] = 'Cobblestone',
  198.         ['volito'] = 'Feather',
  199.         ['aura'] = 'Feather',
  200.         ['fractus'] = 'Cobblestone',
  201.         ['instrumentum'] = 'Flint',
  202.         ['telum'] = 'Arrow',
  203.         ['visum'] = 'Carrot',
  204.         ['mutatio'] = 'Nether Wart',
  205.         ['pannus'] = 'Wool',
  206.         ['purus'] = 'Nether Quartz',
  207.         ['vitreus'] = 'Glass',
  208.         ['cognito'] = 'Paper',
  209.         ['machina'] = 'Button',
  210.         ['imperito'] = 'Lever',
  211.         ['vinculum'] = 'String',
  212.         ['sano'] = 'Golden Apple',
  213.         ['alienis'] = 'Ender Pearl',
  214.         ['tempus'] = 'Redstone Repeater',
  215.         ['tenebris'] = 'Obsidian',
  216.         ['fabrico'] = 'Crafting Table',
  217.     },--}}}
  218. }--}}}
  219. function Thaumcraft:getAspect(aspect, amount)--{{{
  220.     if self.aspects[aspect] ~= nil then -- has the aspect coded in.
  221.         local getItem = self.interface.itemDB.itemsByName[self.aspects[aspect]]
  222.         if getItem ~= nil then
  223.             local getAmount = math.ceil(amount / getItem.aspects[aspect])
  224.             local itemName = getItem.name
  225.             self.interface:retrieveCraft(getItem.uuid, getAmount, self.outputDir)
  226.         else
  227.             tc:sendError(self.aspects[aspect] .. ' not found in the item database.')
  228.         end
  229.     else
  230.         message = "No data on the " .. aspect .. " aspect."
  231.         tc:sendError(message)
  232.     end
  233. end--}}}
  234. function Thaumcraft:getAspects(aspects)--{{{
  235.     if aspects == nil then
  236.         return -1
  237.     else
  238.         -- check item list for items with multiple aspects we need
  239.         for aspect, amount in pairs(aspects) do
  240.             self:getAspect(aspect, amount)
  241.         end
  242.     end
  243.  
  244.     return 0
  245. end--}}}
  246. function Thaumcraft:sendError(message)--{{{
  247.     print(message)
  248.     rednet.send(self.reqComputerID, 'error ' .. message)
  249. end--}}}
  250.  
  251. tc = Thaumcraft
  252. print(tostring(os.time()) .. ": rebooted!")
  253. while true do
  254.     event, param1, param2, param3 = os.pullEvent()
  255.  
  256.     if event == 'rednet_message' then -- number senderid, string message, number distance
  257.         if param1 == tc.reqComputerID then
  258.             print(param2)
  259.             if string.find(param2, 'getAspects ') then
  260.                 aspects = textutils.unserialize(string.sub(param2, 11))
  261.                 tc:getAspects(aspects)
  262.             elseif param2:find('powerOnFor ') then
  263.                 local seconds = tonumber(param2:sub(11))
  264.                 tc.interface:powerOnFor(seconds)
  265.             elseif param2:find('obsidian ') then
  266.                 local amount = tonumber(param2:sub(9))
  267.                 tc.interface:craftObsidian(amount)
  268.             elseif string.find(param2, 'reboot') then
  269.                 shell.run("reboot")
  270.             end
  271.         end
  272.     elseif event == 'key' then
  273.         if param1 == 31 then -- s
  274.         end
  275.     elseif event == 'timer' then
  276.         if param1 == tc.interface.powerTimerID then
  277.             tc.interface:powerOff()
  278.         elseif param1 == tc.interface.stayOnTimerID then
  279.             tc.interface:endPowerOn()
  280.         elseif param1 == tc.interface.obsidiTimer then
  281.             tc.interface:obsidiPowerOff()
  282.         end
  283.     end
  284. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement