Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Todo
- -- - Add support for the Essentia Phials.
- --
- -- my libraries
- -- check dir exists, if not make it
- if not fs.exists("/libs") then--{{{
- fs.makeDir("/libs")
- end--}}}
- function update_lib(pastebin_id, dest_path)--{{{
- if fs.exists(dest_path) then
- fs.delete(dest_path)
- end
- shell.run("pastebin get " .. pastebin_id .. " " .. dest_path)
- os.loadAPI(dest_path)
- end--}}}
- update_lib("qUpykFN4", "libs/ccmisc")
- update_lib("n1S2GpfD", "libs/sorterAPI")
- update_lib("ppqTBDNK", "libs/itemdb")
- Interface = {--{{{
- interface = peripheral.wrap("back"),
- itemDB = itemdb.db,
- --powerOnIO = peripheral.wrap("io_2"),
- powerOnColor = colours.red,
- powerOnSide = "bottom",
- obsidiPowerOnIO = peripheral.wrap("io_0"),
- wirelessModem = rednet.open('left'),
- craftTimerDuration = 4,
- obsidiTimer = nil,
- }--}}}
- function Interface:powerOn()--{{{
- --self.powerOnIO.set(true)
- local active = rs.getBundledInput(self.powerOnSide)
- local newcolours = colours.combine(active, self.powerOnColor)
- rs.setBundledOutput(self.powerOnSide, newcolours)
- os.sleep(0.2)
- --self.powerTimerID = os.startTimer(0.8)
- end--}}}
- function Interface:powerOff()--{{{
- if not self.stayOnTimerID then
- local active = rs.getBundledInput(self.powerOnSide)
- local newcolours = colours.subtract(active, self.powerOnColor)
- rs.setBundledOutput(self.powerOnSide, newcolours)
- --self.powerOnIO.set(false)
- end
- end--}}}
- function Interface:powerOnFor(seconds)--{{{
- self.stayOnTimerID = os.startTimer(seconds)
- self:powerOn()
- end--}}}
- function Interface:endPowerOn()--{{{
- self.stayOnTimerID = nil
- self:powerOff()
- end--}}}
- function Interface:listAll()--{{{
- self:powerOn()
- return self.interface.listAll()
- end--}}}
- function Interface:listItems()--{{{
- self:powerOn()
- return self.interface.listItems()
- end--}}}
- function Interface:listCraft()--{{{
- self:powerOn()
- return self.interface.listCraft()
- end--}}}
- function Interface:nMostStored(n)--{{{
- local itemsStored = self:listAll()
- local id = 0
- local meta = 0
- itemsStored = table.sort(itemsStored)
- local displayCounter = 0
- for uuid,stored in pairs(itemsStored) do
- id, meta = sorterAPI.getID(uuid)
- message = tostring(amountToDisplay + 1) .. ': ' .. 'storing ' .. tostring(stored) .. ' of ' .. tostring(id) .. ':' .. tostring(meta)
- if displayCounter == n - 1 then
- break
- end
- end
- end--}}}
- -- 0 for down, 1 for up, 2 for -Z (N), 3 for +Z (S), 4 for -X (W) and 5 for +X (E).
- function Interface:retrieve(id, amount, outputDirection)--{{{
- self:powerOn()
- return self.interface.retrieve(id, amount, outputDirection)
- end--}}}
- function Interface:craft(id, amount)--{{{
- self:powerOn()
- self.interface.craft(id, amount)
- end--}}}
- function Interface:inStock(id)--{{{
- local invAvailable = self:listItems()
- local itemsInStock = 0
- if invAvailable[id] == nil then
- itemsInStock = 0
- else-- if item not found in AE or there are not enough items.
- itemsInStock = invAvailable[id]
- end
- return itemsInStock
- end--}}}
- function Interface:canCraft(id)--{{{
- local craftItems = self:listCraft()
- for key, value in pairs(craftItems) do
- if tostring(key) == tostring(id) then
- return true
- end
- end
- return false
- end--}}}
- function Interface:missingItems(id, amountRequired)--{{{
- -- get amount of item in storage
- local amountStored = self:inStock(id)
- -- if items in storage is nil then return amount required (meaning need all!)
- if amountStored == 0 then
- return amountRequired
- elseif amountStored < amountRequired then -- if number of items in storage is less than amount required
- return amountRequired - amountStored -- return amount required - items in storage
- else
- return 0
- end
- end--}}}
- function Interface:monitorStorage(id, amountRequired)--{{{
- local notenough = true
- local missingItems = self:missingItems(id, amountRequired)
- repeat
- local craftTimer = os.startTimer(self.craftTimerDuration)
- event, timerID = os.pullEvent("timer")
- missingItems = self:missingItems(id, amountRequired)
- until missingItems == 0
- end--}}}
- function Interface:retrieveCraft(id, getAmount, outputDir)--{{{
- local item = self.itemDB.itemsByUUID[id]
- local missingItems = self:missingItems(id, getAmount)
- if missingItems > 0 then
- local message = "Not enough " .. item.name .. '. Missing: ' .. missingItems .. '.'
- tc:sendError(message)
- if self:canCraft(id) then -- if we can craft the missing items
- message = 'Crafting ' .. missingItems .. ' of ' .. item.name .. '.'
- tc:sendError(message)
- self:craft(id, missingItems)
- self:monitorStorage(id, getAmount) -- monitor storage until the items are available.
- message = 'Finished crafting ' .. getAmount .. ' of ' .. item.name .. '.'
- tc:sendError(message)
- end
- end
- retrievedAmount = self:retrieve(id, getAmount, outputDir)
- if retrievedAmount > 0 then
- message = 'Retrieved ' .. retrievedAmount .. ' of ' .. item.name .. '.'
- else
- message = 'Unable to retrieve ' .. item.name .. '.'
- end
- tc:sendError(message)
- end--}}}
- function Interface:craftObsidian(amount)--{{{
- self.obsidiTimer = os.startTimer(5 * amount)
- self.obsidiPowerOnIO.set(true)
- end--}}}
- function Interface:obsidiPowerOff()--{{{
- self.obsidiPowerOnIO.set(false)
- self.obsidiTimer = nil
- end--}}}
- Thaumcraft = {--{{{
- interface = Interface,
- outputDir = 2, -- debug chest = 4, mirror = 2
- reqComputerID = 30,
- craftTimerDuration = 4,
- aspects = {--{{{
- ['metallum'] = 'Copper Nuggets',
- ['carus'] = 'Smooth Sandstone', -- Smooth Sandstone
- ['praecantatio'] = 'Chiseled Sandstone', -- Chiseled Sandstone
- ['mortuus'] = 'Bone Meal',
- ['victus'] = 'Wheat',
- ['malum'] = 'Music Disc - Stal',
- ['animus'] = 'Soul Sand',
- ['lux'] = 'Torch',
- ['potentia'] = 'Charcoal',
- ['ignis'] = 'Charcoal',
- ['lignum'] = 'Oak Wood',
- ['permutatio'] = 'Seeds',
- ['herba'] = 'Seeds',
- ['vacuos'] = 'Bowl',
- ['solum'] = 'Dirt',
- ['motus'] = 'Trapdoor',
- ['saxum'] = 'Cobblestone',
- ['volito'] = 'Feather',
- ['aura'] = 'Feather',
- ['fractus'] = 'Cobblestone',
- ['instrumentum'] = 'Flint',
- ['telum'] = 'Arrow',
- ['visum'] = 'Carrot',
- ['mutatio'] = 'Nether Wart',
- ['pannus'] = 'Wool',
- ['purus'] = 'Nether Quartz',
- ['vitreus'] = 'Glass',
- ['cognito'] = 'Paper',
- ['machina'] = 'Button',
- ['imperito'] = 'Lever',
- ['vinculum'] = 'String',
- ['sano'] = 'Golden Apple',
- ['alienis'] = 'Ender Pearl',
- ['tempus'] = 'Redstone Repeater',
- ['tenebris'] = 'Obsidian',
- ['fabrico'] = 'Crafting Table',
- },--}}}
- }--}}}
- function Thaumcraft:getAspect(aspect, amount)--{{{
- if self.aspects[aspect] ~= nil then -- has the aspect coded in.
- local getItem = self.interface.itemDB.itemsByName[self.aspects[aspect]]
- if getItem ~= nil then
- local getAmount = math.ceil(amount / getItem.aspects[aspect])
- local itemName = getItem.name
- self.interface:retrieveCraft(getItem.uuid, getAmount, self.outputDir)
- else
- tc:sendError(self.aspects[aspect] .. ' not found in the item database.')
- end
- else
- message = "No data on the " .. aspect .. " aspect."
- tc:sendError(message)
- end
- end--}}}
- function Thaumcraft:getAspects(aspects)--{{{
- if aspects == nil then
- return -1
- else
- -- check item list for items with multiple aspects we need
- for aspect, amount in pairs(aspects) do
- self:getAspect(aspect, amount)
- end
- end
- return 0
- end--}}}
- function Thaumcraft:sendError(message)--{{{
- print(message)
- rednet.send(self.reqComputerID, 'error ' .. message)
- end--}}}
- tc = Thaumcraft
- print(tostring(os.time()) .. ": rebooted!")
- while true do
- event, param1, param2, param3 = os.pullEvent()
- if event == 'rednet_message' then -- number senderid, string message, number distance
- if param1 == tc.reqComputerID then
- print(param2)
- if string.find(param2, 'getAspects ') then
- aspects = textutils.unserialize(string.sub(param2, 11))
- tc:getAspects(aspects)
- elseif param2:find('powerOnFor ') then
- local seconds = tonumber(param2:sub(11))
- tc.interface:powerOnFor(seconds)
- elseif param2:find('obsidian ') then
- local amount = tonumber(param2:sub(9))
- tc.interface:craftObsidian(amount)
- elseif string.find(param2, 'reboot') then
- shell.run("reboot")
- end
- end
- elseif event == 'key' then
- if param1 == 31 then -- s
- end
- elseif event == 'timer' then
- if param1 == tc.interface.powerTimerID then
- tc.interface:powerOff()
- elseif param1 == tc.interface.stayOnTimerID then
- tc.interface:endPowerOn()
- elseif param1 == tc.interface.obsidiTimer then
- tc.interface:obsidiPowerOff()
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement