Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- stockman
- -- Inventory Monitor for ComputerCraft/AE ME System in TPPI
- -- version 0.9
- -- (c) Alex Shaffer / mrnosuch 2014
- -- [email protected] / nosuch.org
- -- Assumptions:
- -- Computer has two attached monitors, the primary monitor (2x3) and the energy monitor (1x2)
- -- Computer is connected to ME Controller and ME Fluid Terminal
- -- Device connections are set up in the installation part near the end (sides/device names)
- -- Known issues:
- -- Cannot seem to monitor fluid levels, so removed this
- -- Nuclear fuel levels are hardcoded (yellorium/blutonium)
- -- Future plans:
- -- Monitor assembler for being stuck.
- -- Use separate file for inventory config
- -- Prevent errors in communication with the ME system from halting program
- ver = "0.9"
- function inventoryConfig()
- invConfig = {}
- -- set up the items you want to monitor below
- -- stock is minimum to keep on hand
- -- order is the quantity used to request crafting, set to 0 if ME Assembler cannot craft.
- -- priority determines what should be crafted/listed first, lower is more urgent (1-9)
- -- It is recommended that items which are slow to craft (chipsets, for instance) are
- -- given lower priority, so quick items can be made first
- -- item names should be 16 characters or less
- -- NameMax"1234567890123456"
- invConfig["Certus Quartz"] = { id = 4362, meta = 6, stock = 320, order = 0, priority = 1 }
- invConfig["Nether Quartz"] = { id = 406, meta = 0, stock = 320, order = 0, priority = 1 }
- invConfig["N. Quartz Dust"] = { id = 4362, meta = 22, stock = 320, order = 32, priority = 5 }
- invConfig["C. Quartz Dust"] = { id = 4362, meta = 7, stock = 320, order = 32, priority = 5 }
- invConfig["Silicon"] = { id = 4362, meta = 13, stock = 128, order = 32, priority = 5 }
- invConfig["Fluix Dust"] = { id = 4362, meta = 14, stock = 256, order = 64, priority = 5 }
- invConfig["Glass"] = { id = 20, meta = 0, stock = 640, order = 64, priority = 5 }
- invConfig["Glass Pane"] = { id = 102, meta = 0, stock = 320, order = 64, priority = 5 }
- invConfig["Fused Quartz"] = { id = 704, meta = 0, stock = 320, order = 64, priority = 5 }
- invConfig["Snowball"] = { id = 332, meta = 0, stock = 128, order = 0, priority = 1 }
- invConfig["Sand"] = { id = 12, meta = 0, stock = 1024, order = 0 , priority = 1 }
- invConfig["ME Blank Patt."] = { id = 4362, meta = 16, stock = 16, order = 1, priority = 5 }
- invConfig["Turbine Rotor"] = { id = 7810, meta = 0, stock = 2, order = 1, priority = 4 }
- invConfig["Bucket"] = { id = 325, meta = 0, stock = 8, order = 1, priority = 5 }
- invConfig["ME Basic Proc."] = { id = 4362, meta = 18, stock = 16, order = 1 , priority = 6 }
- invConfig["ME Adv. Proc."] = { id = 4362, meta = 19, stock = 16, order = 1 , priority = 6 }
- invConfig["RS Gold Chip."] = { id = 19373, meta = 2, stock = 128, order = 1, priority = 9 }
- invConfig["ME Cable"] = { id = 852, meta = 14, stock = 64, order = 1, priority = 5 }
- invConfig["Bronze Ingot"] = { id = 30067, meta = 2, stock = 128, order = 1, priority = 6 }
- invConfig["Bronze Gear"] = { id = 31968, meta = 0, stock = 16, order = 1, priority = 5 }
- invConfig["ME Prc Exprt Bus"] = { id = 852, meta = 0, stock = 8, order = 1, priority = 6 }
- invConfig["ME Import Bus"] = { id = 853, meta = 3, stock = 8, order = 1, priority = 6 }
- -- procedurally add the item name to the item info
- -- it makes sense to use the item name as the key for the master list
- -- but when we reference the item details in secondary arrays and tables
- -- we may use other indexes/keys
- invCount = 0
- for itemName,itemDetails in pairs(invConfig) do
- itemDetails["name"]=itemName
- invCount = invCount + 1
- end
- return invConfig
- end
- function mainLoop()
- local invFullPollTime = 30 -- time to wait in seconds between full update cycles, to reduce lag/load
- local invLastCheck = os.clock()-invFullPollTime -- set to force a run on the first loop
- activityList = {} -- contains items with activity (low or being crafted)
- activityListLength = 0
- print("Starting main loop.")
- while true do
- sleepTime=5 -- Default time to pause between polling
- showHeader()
- clockUpdate()
- -- Do we need to do a full update?
- if(os.clock()-invLastCheck>=invFullPollTime) then
- print("Starting full inventory update...")
- resetMonitorPages()
- didCraft=updateInventory()
- print("Inventory update completed.")
- invLastCheck = os.clock() -- Set the time we did a full update
- -- if we are crafting, we want to refresh quicker and force a full update
- if(didCraft) then
- invLastCheck = invLastCheck - invFullPollTime - 1
- sleepTime = 1
- end
- end
- showMonitorPage()
- showFooter()
- showEnergyStatus()
- showSpinner()
- -- Nap for a bit to reduce load/lag
- os.sleep(sleepTime)
- end
- end
- function updateInventory()
- activityList = {} -- reset list
- didCraft = false
- local actItems = itemsInTable(activityList)
- for itemName,itemDetails in pairs(inv) do
- checkItem(itemName,itemDetails)
- -- is there an issue?
- if ( itemDetails.status~="OK" ) then
- -- can we craft more?
- if ( itemDetails.order > 0 ) then
- -- we can!
- if(not itemDetails.isCrafting) then
- itemDetails.status="QUE"
- end
- itemDetails.sortKey = "B"..itemDetails.priority..":"..itemName
- else
- itemDetails.sortKey = "A"..itemDetails.priority..":"..itemName
- end
- activityList[itemName]=itemDetails
- end
- end
- -- count up how big the activityList table is
- activityListLength = itemsInTable(activityList)
- -- Sort the list if there's anything in it
- if(activityListLength>0) then
- -- Check if assembler is idle
- local jobs = invSys.getJobList()
- local jobsPending = itemsInTable(jobs)
- print("Jobs pending = "..jobsPending)
- -- If assember is idle, request crafting of first QUE item in activity list
- -- change from QUE to FAB on that item, and update craft request time
- if(jobsPending==0) then
- -- find QUE item with highest sort key
- local bestSortKey="Z9:Z" -- Worst possible key
- local bestSortKeyRow = nil
- for i, item in pairs(activityList) do
- if(item.status=="QUE" or item.status=="FAB") then
- if (bestSortKey>item.sortKey) then
- bestSortKey=item.sortKey
- bestSortKeyRow = i
- end
- item.status="QUE"
- item.isCrafting=false
- end
- end
- -- bestSortKeyRow now contains the row of the item to craft
- if (bestSortKeyRow) then
- item = activityList[bestSortKeyRow]
- item.status="FAB"
- item.isCrafting=true
- stack = { qty = item.order, id = item.id, dmg = item.meta }
- print("Craft request for "..item.name)
- invSys.requestCrafting(stack)
- didCraft = true
- end
- else
- -- We have jobs queued on the assembler
- -- a job is defined as
- -- job.id, job.name, job.maxdmg, job.rawName, job.dmg, job.maxSize, job.ench (table), job.qty
- -- This is a debug stub
- print("Job details:")
- for k,v in ipairs(jobs) do
- print("Job "..k)
- print("id:"..v.id)
- print("name:"..v.name)
- print("maxdmg:"..v.maxdmg)
- print("rawname:"..v.rawName)
- print("dmg:"..v.dmg)
- print("maxSize:"..v.maxSize)
- -- print("ench:"..v.ench)
- print("qty:"..v.qty)
- end
- end
- end
- pagingLoadData(activityList)
- return didCraft
- end
- function checkItem(name, itemInfo)
- itemInfo.count=invSys.countOfItemType(itemInfo.id,itemInfo.meta)
- itemInfo.lastCheck=os.clock()
- if(itemInfo.count==0) then
- itemInfo.status="OUT"
- elseif(itemInfo.count<itemInfo.stock) then
- itemInfo.status="LOW"
- else
- itemInfo.status="OK"
- itemInfo.isCrafting=false
- end
- if(itemInfo.isCrafting) then
- itemInfo.status="FAB"
- end
- itemInfo.listOrder = itemInfo.priority
- end
- function pagingLoadData(data)
- -- Load into paging array
- local rowNumber = 0
- paging.rowData = {}
- paging.rowCount = 0
- for k,v in pairs(data) do
- rowNumber = rowNumber + 1
- paging.rowData[rowNumber] = v
- end
- paging.rowCount = rowNumber
- -- Sort the table by sortKey...
- table.sort( paging.rowData, function( a, b ) return a.sortKey < b.sortKey end )
- end
- function showMonitorPage()
- local data = paging.rowData
- local dataRowCount = paging.rowCount
- if (dataRowCount > 0) then
- for rowNumber = 1, paging.itemsPerPage, 1 do
- dataRowNumber = rowNumber + ((paging.currentPage - 1) * paging.itemsPerPage)
- mon.setCursorPos(1,2+rowNumber)
- if(dataRowNumber<=dataRowCount) then
- local rowNumString = string.format("%2d ",dataRowNumber)
- local rowItemStatus = fixedString(data[dataRowNumber].status,3)..": "
- local rowItemName = fixedString(data[dataRowNumber].name,16)
- local rowItemQty = string.format(" %4d",data[dataRowNumber].count)
- local statusColor = format.craft_text
- if(dataStatus=="OUT") then
- statusColor = format.out_text
- elseif (dataStatus=="LOW") then
- statusColor = format.low_text
- end
- mon.setTextColor(format.number)
- mon.write(rowNumString)
- mon.setTextColor(statusColor)
- mon.write(rowItemStatus)
- mon.setTextColor(format.name)
- mon.write(rowItemName)
- mon.setTextColor(format.quantity)
- mon.write(rowItemQty)
- else
- -- blank row!
- mon.clearLine()
- end
- end
- else
- -- clear data area
- for rowNumber = 1, paging.itemsPerPage, 1 do
- mon.setCursorPos(1,2+rowNumber)
- mon.clearLine()
- end
- -- show ALL OK message
- mon.setTextColor(format.craft_text)
- mon.setCursorPos(1,4)
- -----------12345678901234567890123456789
- mon.write(" Monitoring "..invCount.." items.")
- mon.setCursorPos(1,5)
- mon.write(" All stock levels are good.")
- end
- end
- function setMonitorPage(pageNum)
- paging.currentPage = pageNum
- end
- function nextMonitorPage()
- if (paging.currentPage == paging.pageCount) then
- paging.currentPage = 1
- else
- paging.currentPage = paging.currentPage + 1
- end
- end
- function resetMonitorPages()
- paging = {}
- paging.itemsPerPage = 7 -- Probably not right
- paging.currentPage = 1
- paging.pageCount = ((activityListLength-1) / paging.itemsPerPage ) + 1
- paging.rowData = {}
- paging.rowCount = 0
- end
- function clockUpdate()
- -- This will return the time like this "12:30 PM"
- local timeString = textutils.formatTime(os.time(), false)
- if(string.len(timeString)==7) then
- timeString = " "..timeString
- end
- local startX = monWidth - 7
- mon.setCursorPos(startX, 1)
- mon.setTextColor(format.clock)
- mon.write(timeString)
- end
- function showHeader()
- local header = "StockMan "..ver
- -- local headerLen = string.len(header)
- -- local startX = ( monWidth / 2 ) - (headerLen / 2 )
- local startX = 3
- mon.setCursorPos(startX, 1)
- mon.setTextColor(format.header)
- mon.write(header)
- end
- function showFooter()
- local jobs = invSys.getJobList()
- local jobsPending = itemsInTable(jobs)
- mon.setCursorPos(1,monHeight)
- mon.clearLine()
- local footerMsg = "Fabricator is idle."
- if jobsPending == 0 then
- mon.setTextColor(format.footer_idle)
- print("No crafting jobs running.")
- else
- mon.setTextColor(format.footer_busy)
- local job = jobs[1]
- local itemname = job.name
- itemname = fixItemName(itemname)
- if (string.len(itemname)>19) then
- itemname=string.sub(itemname,1,19)
- end
- footerMsg = "FAB: "..itemname.." ("..job.qty..")"
- print("Crafting: "..job.id.."/"..job.name.."/"..job.qty)
- end
- local footerLen = string.len(footerMsg)
- local startX = ( monWidth / 2 ) - (footerLen / 2 ) + 1
- mon.setCursorPos(startX,monHeight)
- mon.write(footerMsg)
- end
- function showEnergyStatus()
- local maxW, maxH = energyMon.getSize()
- local yellorium = invSys.countOfItemType(18006,0)
- local blutonium = invSys.countOfItemType(18006,3)
- energyMon.setCursorPos(1,4)
- energyMon.write("Yellorium: "..yellorium)
- energyMon.setCursorPos(1,5)
- energyMon.write("Blutonium: "..blutonium)
- end
- function showSpinner()
- local spinIndicator = "|/-\\"
- mon.setCursorPos(1,1)
- mon.setTextColor(format.craft_text)
- mon.write(string.sub(spinIndicator,spinner,spinner))
- spinner=spinner+1
- if(spinner>string.len(spinIndicator)) then
- spinner=1
- end
- end
- -- Some item names are "broken" and instead of being human readable nice names ("bucket")
- -- they are horrible object paths ("modname.blahblah.thethingname.name")
- -- If we see a name that ends in .name, we chop that part off, and use the string
- -- after the last remaining period
- -- This is used to clean up names we get from the job queue
- function fixItemName(name)
- local cleanName=name
- local endpart = string.sub(cleanName,-5)
- if (endpart==".name") then
- cleanName = string.sub(cleanName,1,-6)
- cleanName = string.gsub(cleanName,"%."," ")
- local lastPart = ""
- for part in string.gmatch(cleanName,"%a+") do lastPart=part end
- cleanName = lastPart
- end
- return cleanName
- end
- function fixedString(s,len)
- local r = s..string.rep(" ",len)
- r = string.sub(r,1,len)
- return r
- end
- function itemsInTable(t)
- numItems = 0
- for k,v in pairs(t) do
- numItems = numItems + 1
- end
- return numItems
- end
- function screenReset(display, backgroundColor, textColor)
- display.setBackgroundColor(backgroundColor)
- display.setTextColor(textColor)
- display.clear()
- display.setCursorPos(1,1)
- display.setTextScale(1)
- end
- function colorsInit()
- format = {}
- format.log_background = colors.green
- format.log_text = colors.black
- format.monitor_background = colors.white
- format.clock = colors.blue
- format.header = colors.gray
- format.number = colors.gray
- format.quantity = colors.gray
- format.footer_idle = colors.cyan
- format.footer_busy = colors.green
- format.footer_overdue = colors.red
- format.name = colors.black
- format.monitor_text = colors.black
- format.low_text = colors.magenta
- format.out_text = colors.red
- format.craft_text = colors.green
- format.energyMon_background = colors.green
- format.energyMon_text = colors.black
- end
- -- installation variables (also need to update the InventoryConfig data for items)
- local meControllerSide = "bottom" -- set to the side/device name that has the ME Controller
- local monitorSide = "right" -- set to side/device name that a 2h 3w advanced monitor is setup
- local fluidTerminal = "tile_block_fluid_terminal_0" -- set to the side/device name of a fluid terminal
- local energyMonitor = "monitor_0" -- set to the side/device name of a 1 x 2 monitor to show fuel levels
- -- end config
- invSys = peripheral.wrap(meControllerSide)
- fluidInvSys = peripheral.wrap(fluidTerminal)
- mon = peripheral.wrap(monitorSide)
- energyMon = peripheral.wrap(energyMonitor)
- monWidth, monHeight = mon.getSize()
- log = term.native()
- spinner = 1
- invCount = 0
- colorsInit() -- sets up format array with colors
- -- screenReset(log, format.log_background, format.log_text)
- screenReset(mon, format.monitor_background,format.monitor_text)
- screenReset(energyMon, format.energyMon_background, format.energyMon_text)
- print("Inventory Monitor starting up.")
- print("Loading inventory config...")
- inv = inventoryConfig()
- print("Inventory config loaded.")
- mainLoop()
- -- end of program
Advertisement
Add Comment
Please, Sign In to add comment