Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Copyright (c) 2019, David Moore
- All rights reserved.
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright notice, this
- list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright notice,
- this list of conditions and the following disclaimer in the documentation
- and/or other materials provided with the distribution.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
- ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- The views and conclusions contained in the software and documentation are those
- of the authors and should not be interpreted as representing official policies,
- either expressed or implied, of the <project name> project.
- ]]
- -- Ladies and gentlemen, my killbot has Lotus Notes and a machine gun. It is the finest available.
- -- turn this on to enable debug logging
- logging = false
- -- where to write the things
- logfile = "/autocrafter9000.log"
- -- chest on the left of the computer, ME interface on the right
- chest = peripheral.wrap("left")
- interface = peripheral.wrap("right")
- -- time to sleep between checks
- sleepSeconds = 3
- -- how frequently, in seconds, to print an error if something cannot be crafted
- -- due to missing components
- warningFreq = 120
- function logEvent(str)
- if logging then
- f = fs.open(logfile, "a")
- f.writeLine(str)
- f.close()
- print(str)
- end
- end
- function newItemHolder(name,dmg,count,job)
- t = {}
- t["name"] = name
- t["dmg"] = 0 + dmg
- t["count"] = 0 + count
- t["job"] = job
- t["key"] = name .. "@" .. dmg
- return t
- end
- itemsPersistent = {}
- warnedItemTimes = {}
- -- get list of items in teh chest. This will return an entry for each stack
- -- so we need to process it further (for items that we want more than 64 of)
- while true do
- chestList = chest.list()
- chestTotals = {}
- for k,v in pairs(chestList) do
- theItem = newItemHolder(v.name,v.damage,v.count,nil)
- logEvent("Got chest stack " .. theItem["name"] .. "@" .. theItem["dmg"] .. " qty " .. theItem["count"])
- -- we need to add the stacks together to get real totals
- if chestTotals[theItem["key"]] then
- chestTotals[theItem["key"]] = chestTotals[theItem["key"]] + theItem["count"]
- else
- chestTotals[theItem["key"]] = theItem["count"]
- end
- -- if our persistent item store (which contains in-progress crafting jobs) does not have this item, then add it
- -- the only way we can prevent multiple redundant crafting jobs, that i have found, is to save the job object
- -- and check if it is still in progress. I haven't found a way to interrogate the interface for jobs in-progress
- if not itemsPersistent[theItem["key"]] then
- itemsPersistent[theItem["key"]] = theItem
- end
- end
- for chestItemName,chestItemCount in pairs(chestTotals) do
- craftOkay = false
- logEvent("After chest item processing, got chest item " .. chestItemName .. " qty " .. chestItemCount)
- networkItemSearchResult = interface.findItems(chestItemName)
- --logEvent(networkItemSearchResult)
- if networkItemSearchResult[1] then
- metadata = networkItemSearchResult[1].getMetadata()
- networkItemCount = metadata.count
- logEvent("Got network item " .. chestItemName .. " qty " .. networkItemCount)
- -- if there is less in the network on the right than in the chest on the left
- if networkItemCount < chestItemCount then
- diff = chestItemCount - networkItemCount
- -- check if we have a crafting job stored for this item, and whether it's finished
- if itemsPersistent[chestItemName]["job"] then
- if itemsPersistent[chestItemName]["job"].isFinished() then
- okayToCraft = true
- itemsPersistent[chestItemName]["job"] = nil
- else
- okayToCraft = false
- end
- else
- okayToCraft = true
- end
- -- check if there is an available CPU
- availableCPU = false
- cpus = interface.getCraftingCPUs()
- for k,v in pairs(cpus) do
- if v.busy == false then
- availableCPU = true
- end
- end
- if not availableCPU then
- okayToCraft = false
- -- print("No CPUs available, skipping")
- end
- if okayToCraft then
- -- save the crafting job in the persistent store
- itemsPersistent[chestItemName]["job"] = networkItemSearchResult[1].craft(diff)
- if itemsPersistent[chestItemName]["job"].status() == "missing" then
- if warnedItemTimes[chestItemName] then
- if os.clock() - warnedItemTimes[chestItemName] > warningFreq then
- okayToPrint = true
- else
- okayToPrint = false
- end
- else
- okayToPrint = true
- end
- if okayToPrint then
- warnedItemTimes[chestItemName] = os.clock()
- print("Missing Components! Cannot craft " .. diff .. " of " .. chestItemName)
- end
- else
- craftOkay = true
- end
- -- check if the job failed. haven't bothered to test this functionality yet.
- -- it should kick in if all CPUs are in use or some such
- if not itemsPersistent[chestItemName]["job"] then
- print("Could not craft ".. diff .. " of " .. chestItemName)
- craftOkay = false
- end
- else
- logEvent("Not crafting " .. chestItemName .. " because of crafting job in progress")
- craftOkay = false
- end
- else
- warnedItemTimes[chestItemName] = nil
- end
- if craftOkay then
- warnedItemTimes[chestItemName] = nil
- logEvent("Submitted order for " .. diff .. " of " .. chestItemName)
- print("Submitted order for " .. diff .. " of " .. chestItemName)
- end
- end
- end
- os.sleep(sleepSeconds)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement