Advertisement
alexnolan

ME System

Mar 30th, 2025 (edited)
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.35 KB | None | 0 0
  1. -- AE2 Crafting Monitor - optimized for getCraftingCPUs method
  2. -- Configuration
  3. local REFRESH_RATE = 0.5  -- Update every 0.5 seconds
  4. local MANUAL_CPU_COUNT = 24  -- Set this to your actual CPU count
  5.  
  6. -- Global variables
  7. local bridge = nil
  8. local monitor = nil
  9.  
  10. -- Auto-detect peripherals
  11. function findPeripherals()
  12.     local foundBridge = false
  13.     local foundMonitor = false
  14.    
  15.     print("Searching for peripherals...")
  16.    
  17.     -- Get all peripherals
  18.     local peripherals = peripheral.getNames()
  19.    
  20.     -- First look for ME Bridge
  21.     for _, name in ipairs(peripherals) do
  22.         local type = peripheral.getType(name)
  23.         -- Check for various possible ME bridge types
  24.         if type == "meBridge" or type == "me_bridge" or
  25.            type:find("ae2") or type:find("appliedenergistics") then
  26.             print("Found ME Bridge: " .. name)
  27.             bridge = peripheral.wrap(name)
  28.             foundBridge = true
  29.             break
  30.         end
  31.     end
  32.    
  33.     -- Then look for a monitor
  34.     for _, name in ipairs(peripherals) do
  35.         if peripheral.getType(name) == "monitor" then
  36.             print("Found monitor: " .. name)
  37.             monitor = peripheral.wrap(name)
  38.             monitor.setTextScale(1)
  39.             monitor.clear()
  40.             foundMonitor = true
  41.             break
  42.         end
  43.     end
  44.    
  45.     return foundBridge, foundMonitor
  46. end
  47.  
  48. -- Initialize system
  49. function init()
  50.     print("Initializing AE2 Crafting Monitor...")
  51.    
  52.     -- Auto-detect peripherals
  53.     local foundBridge, foundMonitor = findPeripherals()
  54.    
  55.     if not foundBridge then
  56.         error("ME Bridge not found! Please connect an ME Bridge peripheral.")
  57.     end
  58.    
  59.     if not foundMonitor then
  60.         print("Warning: Monitor not found, using terminal output")
  61.     end
  62.    
  63.     print("Initialization complete!")
  64. end
  65.  
  66. -- Display output to terminal or monitor
  67. function write(x, y, text, color)
  68.     if monitor then
  69.         if color then monitor.setTextColor(color) end
  70.         monitor.setCursorPos(x, y)
  71.         monitor.write(text)
  72.     else
  73.         term.setCursorPos(x, y)
  74.         if color then term.setTextColor(color) end
  75.         term.write(text)
  76.     end
  77. end
  78.  
  79. -- Center text on screen
  80. function writeCenter(y, text, color)
  81.     local width = monitor and monitor.getSize() or term.getSize()
  82.     local x = math.floor((width - #text) / 2) + 1
  83.     write(x, y, text, color)
  84. end
  85.  
  86. -- Clear screen (monitor or terminal)
  87. function clearScreen()
  88.     if monitor then
  89.         monitor.clear()
  90.     else
  91.         term.clear()
  92.     end
  93. end
  94.  
  95. -- Get crafting CPUs and their status
  96. function getCraftingStatus()
  97.     if not bridge.getCraftingCPUs then
  98.         error("getCraftingCPUs method not available")
  99.     end
  100.    
  101.     return bridge.getCraftingCPUs()
  102. end
  103.  
  104. -- Get color based on progress
  105. function getProgressColor(progress)
  106.     if progress < 0.25 then
  107.         return colors.red
  108.     elseif progress < 0.75 then
  109.         return colors.yellow
  110.     else
  111.         return colors.green
  112.     end
  113. end
  114.  
  115. -- Display crafting CPU status
  116. function displayCraftingStatus(cpus)
  117.     clearScreen()
  118.     local width = monitor and monitor.getSize() or term.getSize()
  119.    
  120.     -- Display header
  121.     writeCenter(1, "AE2 CRAFTING MONITOR", colors.yellow)
  122.     writeCenter(2, string.rep("-", width - 10), colors.yellow)
  123.    
  124.     -- Track active crafting count and collect data
  125.     local activeCPUs = 0
  126.     local itemsBeingCrafted = {}
  127.     local totalItemsRequested = 0
  128.     local totalItemsCompleted = 0
  129.    
  130.     -- Use manual CPU count if set, otherwise use detected count
  131.     local cpuCount = MANUAL_CPU_COUNT > 0 and MANUAL_CPU_COUNT or #cpus
  132.    
  133.     -- Analyze CPU data
  134.     for i, cpu in ipairs(cpus) do
  135.         -- Check if CPU is busy/crafting
  136.         local busy = false
  137.         if cpu.busy ~= nil then
  138.             busy = cpu.busy
  139.         elseif cpu.isBusy ~= nil then
  140.             busy = cpu.isBusy
  141.         elseif cpu.crafting ~= nil then
  142.             busy = cpu.crafting
  143.         end
  144.        
  145.         if busy then
  146.             activeCPUs = activeCPUs + 1
  147.            
  148.             -- Try to get what's being crafted
  149.             local itemName = "Unknown Item"
  150.             if cpu.craftingItem or cpu.item or cpu.crafting then
  151.                 local item = cpu.craftingItem or cpu.item or cpu.crafting
  152.                 if type(item) == "table" and item.name then
  153.                     itemName = item.name
  154.                 elseif type(item) == "string" then
  155.                     itemName = item
  156.                 end
  157.             end
  158.            
  159.             -- Add to items being crafted
  160.             if not itemsBeingCrafted[itemName] then
  161.                 itemsBeingCrafted[itemName] = 1
  162.             else
  163.                 itemsBeingCrafted[itemName] = itemsBeingCrafted[itemName] + 1
  164.             end
  165.            
  166.             -- Look for crafting count information
  167.             -- Try various possible field names
  168.             local itemsRequired = 0
  169.             local itemsFinished = 0
  170.            
  171.             -- Look for requested/total items
  172.             if cpu.requestedItems and type(cpu.requestedItems) == "number" then
  173.                 itemsRequired = cpu.requestedItems
  174.             elseif cpu.totalItems and type(cpu.totalItems) == "number" then
  175.                 itemsRequired = cpu.totalItems
  176.             elseif cpu.toCreate and type(cpu.toCreate) == "number" then
  177.                 itemsRequired = cpu.toCreate
  178.             elseif cpu.requested and type(cpu.requested) == "number" then
  179.                 itemsRequired = cpu.requested
  180.             end
  181.            
  182.             -- Look for completed items
  183.             if cpu.craftedItems and type(cpu.craftedItems) == "number" then
  184.                 itemsFinished = cpu.craftedItems
  185.             elseif cpu.completedItems and type(cpu.completedItems) == "number" then
  186.                 itemsFinished = cpu.completedItems
  187.             elseif cpu.created and type(cpu.created) == "number" then
  188.                 itemsFinished = cpu.created
  189.             end
  190.            
  191.             -- If we have progress percentage but not counts, try to calculate
  192.             if itemsRequired == 0 and cpu.progress and type(cpu.progress) == "number" then
  193.                 -- Try to find total count
  194.                 if cpu.amount and type(cpu.amount) == "number" then
  195.                     itemsRequired = cpu.amount
  196.                     itemsFinished = math.floor(itemsRequired * cpu.progress)
  197.                 end
  198.             end
  199.            
  200.             -- Add to totals
  201.             totalItemsRequested = totalItemsRequested + itemsRequired
  202.             totalItemsCompleted = totalItemsCompleted + itemsFinished
  203.            
  204.             -- Debug print what we found
  205.             if i == 1 then  -- Just print the first CPU to avoid spam
  206.                 print("CPU " .. i .. " info:")
  207.                 for k, v in pairs(cpu) do
  208.                     if type(v) ~= "table" and type(v) ~= "function" then
  209.                         print("  " .. k .. " = " .. tostring(v))
  210.                     end
  211.                 end
  212.             end
  213.         end
  214.     end
  215.    
  216.     -- Convert table of items to a list
  217.     local craftingItems = {}
  218.     for item, count in pairs(itemsBeingCrafted) do
  219.         table.insert(craftingItems, {name = item, count = count})
  220.     end
  221.    
  222.     -- Display system status based on activity
  223.     if activeCPUs == 0 then
  224.         -- No active crafting
  225.         writeCenter(5, "System Status: IDLE", colors.lightGray)
  226.         writeCenter(7, "No crafting operations in progress", colors.white)
  227.         writeCenter(9, "Total CPUs Available: " .. cpuCount, colors.cyan)
  228.        
  229.         -- Draw some decoration when idle
  230.         writeCenter(12, "System ready for crafting tasks", colors.green)
  231.         writeCenter(14, "[ READY ]", colors.green)
  232.     else
  233.         -- Active crafting
  234.         writeCenter(4, "System Status: ACTIVE", colors.orange)
  235.        
  236.         -- Overview
  237.         local cpuText = activeCPUs .. " of " .. cpuCount .. " CPUs active"
  238.         writeCenter(6, cpuText, colors.cyan)
  239.        
  240.         -- Display crafting completion information
  241.         writeCenter(8, "Crafting Progress:", colors.white)
  242.        
  243.         -- Show items crafted
  244.         local itemsText
  245.         if totalItemsRequested > 0 then
  246.             itemsText = totalItemsCompleted .. "/" .. totalItemsRequested .. " items crafted"
  247.         else
  248.             -- If we couldn't find item counts, display generic message
  249.             itemsText = "Items being crafted..."
  250.         end
  251.        
  252.         writeCenter(9, itemsText, colors.yellow)
  253.        
  254.         -- Calculate percentage if possible
  255.         if totalItemsRequested > 0 then
  256.             local percentage = math.floor((totalItemsCompleted / totalItemsRequested) * 100)
  257.             local percentText = "(" .. percentage .. "% complete)"
  258.             writeCenter(10, percentText, getProgressColor(percentage / 100))
  259.         end
  260.        
  261.         -- Items being crafted
  262.         writeCenter(12, "Items Being Crafted:", colors.white)
  263.        
  264.         local y = 14
  265.         for i, item in ipairs(craftingItems) do
  266.             local name = item.name
  267.             if #name > 30 then
  268.                 name = string.sub(name, 1, 27) .. "..."
  269.             end
  270.             local itemText = name .. " x" .. item.count
  271.             writeCenter(y, itemText, colors.green)
  272.             y = y + 1
  273.             if y > (monitor and monitor.getSize() or 15) - 2 then
  274.                 writeCenter(y, "... and " .. (#craftingItems - i) .. " more items", colors.lightGray)
  275.                 break
  276.             end
  277.         end
  278.     end
  279. end
  280.  
  281. -- Main monitoring function
  282. function monitorCrafting()
  283.     while true do
  284.         -- Get and display crafting status
  285.         local ok, result = pcall(getCraftingStatus)
  286.        
  287.         if ok then
  288.             if type(result) == "table" then
  289.                 displayCraftingStatus(result)
  290.             else
  291.                 clearScreen()
  292.                 write(1, 1, "Unexpected result format:", colors.red)
  293.                 write(1, 2, type(result), colors.red)
  294.             end
  295.         else
  296.             clearScreen()
  297.             write(1, 1, "Error getting crafting status:", colors.red)
  298.             write(1, 2, tostring(result), colors.red)
  299.         end
  300.        
  301.         sleep(REFRESH_RATE)
  302.     end
  303. end
  304.  
  305. -- Main program function
  306. function run()
  307.     init()
  308.     monitorCrafting()
  309. end
  310.  
  311. -- Start the program
  312. run()
  313.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement