Advertisement
PlowmanPlow

ComputerCraft - Status and Auto-Craft

Feb 25th, 2016
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.06 KB | None | 0 0
  1. -- This is the CC modem channel
  2. -- used to receive status messages
  3. local statusChannelID = 5
  4.  
  5. -- Set this to false to prevent displaying the
  6. -- interface on a monitor even if one is attached.
  7. local useMonitor = true
  8.  
  9. -- Set this to true to enable debug messages on the terminal
  10. local debug = false
  11.  
  12. -- None of these variables should need to be changed
  13. local craftMaintainFileName = "craftMaintain.json"
  14. local craftInteractive = false
  15. local craftEditItem = nil
  16. local crafting = false
  17. local craftingItemKey = nil
  18. local craftCheckCount = 0
  19. local craftFiltering = false
  20. local myID = os.getComputerID()
  21.  
  22. -- Global tables used for states
  23. local status = {}
  24. local inventory = {}
  25. local crafts = {}
  26. local autos = {}
  27. local craftList = {}
  28. local craftMaintain = {}
  29.  
  30. -- Find Wireless Modem
  31. local modem = peripheral.find("modem", function(n, o) return o.isWireless() end)
  32. if modem ~= nil then
  33.    modem.open(statusChannelID)
  34. end
  35.  
  36. -- Find the ME Bridge from peripherals++
  37. local bridge = peripheral.find("meBridge")
  38.  
  39. -- Find Speaker (from Peripherals++) if one is attached
  40. local speaker = peripheral.find("speaker")
  41.  
  42. -- Find Monitor if one is attached
  43. local monitor = peripheral.find("monitor", function(name, object) return object.isColour() end)
  44. local monWidth, monHeight
  45. local display
  46. local clickEvent
  47. if monitor == nil or useMonitor == false then
  48.    display = term.current()
  49.    clickEvent = "mouse_click"
  50. else
  51.    display = monitor
  52.    display.setTextScale(1.0)
  53.    clickEvent = "monitor_touch"
  54. end
  55. monWidth, monHeight = display.getSize()
  56. if monWidth < 30 and monitor ~= nil then
  57.    display.setTextScale(0.5)
  58.    monWidth, monHeight = display.getSize()
  59. end
  60. display.setBackgroundColor(colors.black)
  61. display.setTextColor(colors.white)
  62.  
  63. -- Trim whitespace from the lead/trailing
  64. local function trim(s)
  65.    return s:match'^%s*(.*.%S)' or ''
  66. end
  67.  
  68. -- Returns the "pairs" of a table iteratively sorted by key
  69. function spairs(t)
  70.    -- collect the keys
  71.    local keys = {}
  72.    for k in pairs(t) do keys[#keys+1] = k end
  73.  
  74.    table.sort(keys)
  75.  
  76.    -- return the iterator function
  77.    local i = 0
  78.    return function()
  79.       i = i + 1
  80.       if keys[i] then
  81.          return keys[i], t[keys[i]]
  82.       end
  83.    end
  84. end
  85.  
  86. -- Set up windows
  87. local windowHeight = monHeight - 2
  88. local winHeader = window.create(display, 1, 1, monWidth, 2, true)
  89. local tabs = {}
  90. local tabTitles = {}
  91. local tabDims = {}
  92. tabsByLabel = {general = 1, craft = 2}
  93. tabs[1] = window.create(display, 1, 3, monWidth, windowHeight, true)
  94. tabs[2] = window.create(display, 1, 3, monWidth, windowHeight, false)
  95. local tW, tH = tabs[1].getSize()
  96. local oX, oY = tabs[1].getPosition()
  97. tabDims[1] = {width = tW, height = tH, offsetX = (oX - 1), offsetY = (oY - 1)}
  98. local tW, tH = tabs[2].getSize()
  99. local oX, oY = tabs[2].getPosition()
  100. tabDims[2] = {width = tW, height = tH, offsetX = (oX - 1), offsetY = (oY - 1)}
  101. -- Use spaces to make all the titles the same width... because I'm lazy
  102. tabTitles[1] = {label = "Base Status Monitor", phon = "Base Status Monitor"}
  103. tabTitles[2] = {label = "   ME Auto Craft   ", phon = "Ehmee Auto Craft"}
  104. local curTab = 1
  105. local craftPage = 1
  106. local craftLineCount = tabDims[tabsByLabel.craft].height - 2
  107. local buttons = {}
  108.  
  109. colors.Au = colors.yellow
  110. colors.VA = colors.lime
  111. colors.Cu = colors.orange
  112. colors.Ag = colors.white
  113. colors.Sh = colors.lightBlue
  114. colors.Bz = colors.orange
  115. colors.EA = colors.orange
  116. colors.Ye = colors.yellow
  117. colors.En = colors.green
  118. colors.Fe = colors.lightGray
  119. colors.Sn = colors.white
  120. colors.Al = colors.lightGray
  121. colors.Sg = colors.orange
  122. colors.El = colors.yellow
  123. colors.Pb = colors.cyan
  124. colors.St = colors.gray
  125. colors.RI = colors.gray
  126. colors.Os = colors.cyan
  127.  
  128. local function getMag(num)
  129.    return string.len(tostring(num))
  130. end
  131.  
  132. local function centerWrite(disp, msg, y)
  133.    local x = math.floor(monWidth/2) - math.floor(string.len(msg)/2) + 1
  134.    disp.setCursorPos(x, y)
  135.    disp.write(msg)
  136.    return x
  137. end
  138.  
  139. local function showButton(win, button)
  140.    win.setCursorPos(button.startX, button.startY)
  141.    win.setTextColor(button.fg)
  142.    win.setBackgroundColor(button.bg)
  143.    win.write(button.text)
  144. end
  145.  
  146. local function updateHeader()
  147.    winHeader.setTextColor(colors.pink)
  148.    winHeader.setBackgroundColor(colors.black)
  149.    centerWrite(winHeader, tabTitles[curTab].label, 1)
  150.    showButton(winHeader, buttons.resetStatus)
  151.    showButton(winHeader, buttons.filterCrafts)
  152.    showButton(winHeader, buttons.leftMain)
  153.    showButton(winHeader, buttons.rightMain)
  154. end
  155.  
  156. -- This updates the contents of the main status tab
  157. local function updateGeneral()
  158.    local label, value
  159.    local y = 1
  160.    for label, value in spairs(status) do
  161.       tabs[tabsByLabel.general].setCursorPos(1, y)
  162.       tabs[tabsByLabel.general].clearLine()
  163.       tabs[tabsByLabel.general].setTextColor(colors.yellow)
  164.       tabs[tabsByLabel.general].write(label .. ": ")
  165.       tabs[tabsByLabel.general].setTextColor(colors.cyan)
  166.       tabs[tabsByLabel.general].write(value .. "          ")
  167.       y = y + 1
  168.    end
  169.    local row = 1
  170.    for symbol, quantity in spairs(inventory) do
  171.       tabs[tabsByLabel.general].setCursorPos(monWidth - 8, row)
  172.       tabs[tabsByLabel.general].write("      ")
  173.       if colors[symbol] ~= nil then tabs[tabsByLabel.general].setTextColor(colors[symbol]) else tabs[tabsByLabel.general].setTextColor(colors.white) end
  174.       tabs[tabsByLabel.general].setCursorPos(monWidth - 2 - getMag(quantity), row)
  175.       tabs[tabsByLabel.general].write(math.floor(quantity))
  176.       tabs[tabsByLabel.general].setCursorPos(monWidth - 1, row)
  177.       tabs[tabsByLabel.general].write(symbol)
  178.       row = row + 1
  179.    end
  180. end
  181.  
  182. -- This repopulates the crafts table with the current craftable items in the ME network
  183. local function pollCrafts()
  184.    local rawCrafts = bridge.listCraft()
  185.    crafts = {}
  186.    for i, item in ipairs(rawCrafts) do
  187.       local key = item.name .. ":" .. item.meta
  188.       if craftFiltering == true and craftMaintain[key] ~= nil and craftMaintain[key].maintain ~= 0 then
  189.          crafts[item.displayName] = item
  190.       elseif craftFiltering == false then
  191.          crafts[item.displayName] = item
  192.       end
  193.    end
  194. end
  195.  
  196. -- This updates the Craftables tab with either a paginated list of craftable items in the ME network
  197. -- or the interactive "pop up" for setting the maintain amount for a craftable item
  198. local function updateCraft()
  199.    -- If we are in a craft dialog then we should process that and return
  200.    if craftInteractive == true then
  201.       -- If the maintain dialog buttons don't exist, make them
  202.       local tabCenter = math.ceil(tabDims[tabsByLabel.craft].width/2)
  203.       if buttons.maintCancel == nil then
  204.          buttons.maintSubTen = { startX = (tabCenter - 14), startY = 8, endX = (tabCenter - 10), endY = 8, fg = colors.black, bg = colors.orange, text = " -10 ", tab = tabsByLabel.craft, callBack = "maintSubTen" }
  205.          buttons.maintSubOne = { startX = (tabCenter - 8), startY = 8, endX = (tabCenter - 5), endY = 8, fg = colors.black, bg = colors.pink, text = " -1 ", tab = tabsByLabel.craft, callBack = "maintSubOne" }
  206.          buttons.maintSetZero = { startX = (tabCenter - 3), startY = 8, endX = (tabCenter + 2), endY = 8, fg = colors.black, bg = colors.lightBlue, text = " Zero ", tab = tabsByLabel.craft, callBack = "maintSetZero" }
  207.          buttons.maintAddOne = { startX = (tabCenter + 4), startY = 8, endX = (tabCenter + 7), endY = 8, fg = colors.black, bg = colors.pink, text = " +1 ", tab = tabsByLabel.craft, callBack = "maintAddOne" }
  208.          buttons.maintAddTen = { startX = (tabCenter + 9), startY = 8, endX = (tabCenter + 12), endY = 8, fg = colors.black, bg = colors.orange, text = " +10 ", tab = tabsByLabel.craft, callBack = "maintAddTen" }
  209.          buttons.maintCancel = { startX = (tabCenter - 9), startY = 10, endX = (tabCenter - 2), endY = 10, fg = colors.black, bg = colors.red, text = " Cancel ", tab = tabsByLabel.craft, callBack = "maintCancel" }
  210.          buttons.maintSubmit = { startX = (tabCenter + 1), startY = 10, endX = (tabCenter + 7), endY = 10, fg = colors.black, bg = colors.green, text = " Submit ", tab = tabsByLabel.craft, callBack = "maintSubmit" }
  211.          tabs[tabsByLabel.craft].setBackgroundColor(colors.black)
  212.          tabs[tabsByLabel.craft].clear()
  213.          tabs[tabsByLabel.craft].setTextColor(colors.orange)
  214.          tabs[tabsByLabel.craft].setBackgroundColor(colors.black)
  215.          centerWrite(tabs[tabsByLabel.craft], craftEditItem.displayName, 4)
  216.          showButton(tabs[buttons.maintSubTen.tab], buttons.maintSubTen)
  217.          showButton(tabs[buttons.maintSubOne.tab], buttons.maintSubOne)
  218.          showButton(tabs[buttons.maintSetZero.tab], buttons.maintSetZero)
  219.          showButton(tabs[buttons.maintAddOne.tab], buttons.maintAddOne)
  220.          showButton(tabs[buttons.maintAddTen.tab], buttons.maintAddTen)
  221.          showButton(tabs[buttons.maintCancel.tab], buttons.maintCancel)
  222.          showButton(tabs[buttons.maintSubmit.tab], buttons.maintSubmit)
  223.       end
  224.       tabs[tabsByLabel.craft].setCursorPos(1, 6)
  225.       tabs[tabsByLabel.craft].setTextColor(colors.white)
  226.       tabs[tabsByLabel.craft].setBackgroundColor(colors.black)
  227.       tabs[tabsByLabel.craft].clearLine()
  228.       centerWrite(tabs[tabsByLabel.craft], ("Maintain: " .. craftEditItem.maintain), 6)
  229.       return
  230.    end
  231.    -- Normal craftable list display here
  232.    pollCrafts()
  233.    local cY = 1
  234.    local startIndex = ((craftPage - 1) * craftLineCount) + 1
  235.    local lineColors = {colors.orange, colors.lightBlue}
  236.    tabs[tabsByLabel.craft].setBackgroundColor(colors.black)
  237.    local index = 0
  238.    craftList = {}
  239.    for name, item in spairs(crafts) do
  240.       index = index + 1
  241.       if index >= startIndex and cY <= craftLineCount then
  242.          craftList[cY] = item
  243.          lc = (cY % 2) + 1
  244.          tabs[tabsByLabel.craft].setTextColor(lineColors[lc])
  245.          tabs[tabsByLabel.craft].setCursorPos(1, cY)
  246.          tabs[tabsByLabel.craft].clearLine()
  247.          tabs[tabsByLabel.craft].write(string.sub(item.displayName, 1, tabDims[tabsByLabel.craft].width - 11))
  248.          tabs[tabsByLabel.craft].setCursorPos(tabDims[tabsByLabel.craft].width - 10, cY)
  249.          tabs[tabsByLabel.craft].write(item.amount)
  250.          local key = item.name .. ":" .. item.meta
  251.          if craftMaintain[key] ~= nil and craftMaintain[key].maintain ~= 0 then
  252.             tabs[tabsByLabel.craft].write("/" .. craftMaintain[key].maintain)
  253.          end
  254.          cY = cY + 1
  255.       end
  256.    end
  257.    showButton(tabs[buttons.leftCraft.tab], buttons.leftCraft)
  258.    showButton(tabs[buttons.rightCraft.tab], buttons.rightCraft)
  259.    showButton(tabs[buttons.filterCrafts.tab], buttons.filterCrafts)
  260.    tabs[tabsByLabel.craft].setTextColor(colors.cyan)
  261.    tabs[tabsByLabel.craft].setBackgroundColor(colors.black)
  262.    local lastPage = math.ceil(index/craftLineCount)
  263.    centerWrite(tabs[tabsByLabel.craft], craftPage .. "/" .. lastPage, tabDims[tabsByLabel.craft].height)
  264. end
  265.  
  266. -- Return nil or a button based on a click on the monitor/terminal
  267. local function identifyButton(x, y)
  268.    local but
  269.    local oX, oY
  270.    for label, button in pairs(buttons) do
  271.       if button.tab ~= 0 then
  272.          oX = tabDims[button.tab].offsetX
  273.          oY = tabDims[button.tab].offsetY
  274.       else
  275.          oX = 0
  276.          oY = 0
  277.       end
  278.       local startX = button.startX + oX
  279.       local startY = button.startY + oY
  280.       local endX = button.endX + oX
  281.       local endY = button.endY + oY
  282.       --print("Checking " .. x .. "," .. y .. " against " .. startX .. "," .. startY .. " to " .. endX .. "," .. endY .. " on tab=" .. button.tab)
  283.       if x >= startX and x <= endX and y >= startY and y <= endY and (curTab == button.tab or button.tab == 0) then
  284.          return button
  285.       end
  286.    end
  287. end
  288.  
  289. -- Checks to see if anything needs to be crafted. If any item has been crafting for 30 seconds
  290. -- then restart crafting for that item (in case something was jammed up). Only crafts one
  291. -- unique item at a time (may craft multiples of that item at once)
  292. local function checkAutoCraft()
  293.    if status["ME Crafting CPUs"] == nil or status["ME Crafting CPUs"] == 0 then return end
  294.    pollCrafts()
  295.    -- If we are currently crafting, see if we're done, or requeue if stuck
  296.    if crafting == true then
  297.       local item = crafts[craftMaintain[craftingItemKey].displayName]
  298.       local stored = item.amount
  299.       local target = craftMaintain[craftingItemKey].maintain
  300.       if debug == true then print("Item: " .. item.displayName .. " Have: " .. stored .. " Need: " .. target) end
  301.       if stored >= target then
  302.          craftingItemKey = nil
  303.          crafting = false
  304.          status["Crafting"] = "Nothing"
  305.          updateGeneral()
  306.       else
  307.          if craftCheckCount >= 5 and status["ME Crafting CPUs Busy"] == 0 then
  308.             if debug == true then print("Trying to restart crafting due to hang...") end
  309.             local toCraft
  310.             local amt = target - stored
  311.             if item.meta ~= 0 then
  312.                toCraft = item.name .. " " .. item.meta
  313.             else
  314.                toCraft = item.name
  315.             end
  316.             bridge.craft(toCraft, amt)
  317.             craftCheckCount = 0
  318.          end
  319.          return
  320.       end
  321.    end
  322.    -- If we aren't crafting, see if there is anything that needs crafting
  323.    for key, item in pairs(crafts) do
  324.       local key = item.name .. ":" .. item.meta
  325.       if crafting == false and craftMaintain[key] ~= nil and craftMaintain[key].maintain > 0 and item.amount < craftMaintain[key].maintain then
  326.          local toCraft
  327.          local amt = craftMaintain[key].maintain - item.amount
  328.          if item.meta ~= 0 then
  329.             toCraft = item.name .. " " .. item.meta
  330.          else
  331.             toCraft = item.name
  332.          end
  333.          bridge.craft(toCraft, amt)
  334.          status["Crafting"] = item.displayName
  335.          crafting = true
  336.          craftingItemKey = key
  337.          updateGeneral()
  338.       end
  339.    end
  340.    if crafting == false and status["Crafting"] ~= "Nothing" then
  341.       status["Crafting"] = "Nothing"
  342.       updateGeneral()
  343.    end
  344. end
  345.  
  346. -- Button callback functions are not set to local so they can be called from the global properties
  347. -- table (i.e. call by function name instead of the function object definition)
  348.  
  349. -- Button callback handler for the button which toggles filtering on/off for the list of craftables
  350. function filterCrafts()
  351.    craftFiltering = not craftFiltering
  352.    craftPage = 1
  353.    tabs[tabsByLabel.craft].clear()
  354.    updateCraft()
  355. end
  356.  
  357. -- Button callback handler for the Reset Status button in the header. This will simply empty the
  358. -- status{} table and let it repopulate
  359. function resetStatus()
  360.    status = {}
  361.    tabs[tabsByLabel.general].clear()
  362.    updateGeneral()
  363. end
  364.  
  365. -- Button callback handler for the Cancel button on the interactive pop up for craftable maintainers
  366. function maintCancel()
  367.    craftEditItem = nil
  368.    buttons.maintCancel = nil
  369.    craftInteractive = false
  370.    buttons.maintSubTen = nil
  371.    buttons.maintSubOne = nil
  372.    buttons.maintSetZero = nil
  373.    buttons.maintAddOne = nil
  374.    buttons.maintAddTen = nil
  375.    buttons.maintCancel = nil
  376.    buttons.maintSubmit = nil
  377.    tabs[tabsByLabel.craft].clear()
  378.    updateCraft()
  379. end
  380.  
  381. -- Button callback handler for the Submit button on the interactive pop up for craftable maintainers
  382. function maintSubmit()
  383.    local key = craftEditItem.name .. ":" .. craftEditItem.meta
  384.    craftMaintain[key] = craftEditItem
  385.    local file = fs.open(craftMaintainFileName, "w")
  386.    file.write(textutils.serialize(craftMaintain))
  387.    file.close()
  388.    craftEditItem = nil
  389.    buttons.maintCancel = nil
  390.    craftInteractive = false
  391.    buttons.maintSubTen = nil
  392.    buttons.maintSubOne = nil
  393.    buttons.maintSetZero = nil
  394.    buttons.maintAddOne = nil
  395.    buttons.maintAddTen = nil
  396.    buttons.maintCancel = nil
  397.    buttons.maintSubmit = nil
  398.    tabs[tabsByLabel.craft].clear()
  399.    updateCraft()
  400.    checkAutoCraft()
  401. end
  402.  
  403. -- Button callback handler for the "-10" button on the interactive pop up for craftable maintainers
  404. function maintSubTen()
  405.    craftEditItem.maintain = craftEditItem.maintain - 10
  406.    updateCraft()
  407. end
  408.  
  409. -- Button callback handler for the "-1" button on the interactive pop up for craftable maintainers
  410. function maintSubOne()
  411.    craftEditItem.maintain = craftEditItem.maintain - 1
  412.    updateCraft()
  413. end
  414.  
  415. -- Button callback handler for the "+1" button on the interactive pop up for craftable maintainers
  416. function maintAddOne()
  417.    craftEditItem.maintain = craftEditItem.maintain + 1
  418.    updateCraft()
  419. end
  420.  
  421. -- Button callback handler for the "+10" button on the interactive pop up for craftable maintainers
  422. function maintAddTen()
  423.    craftEditItem.maintain = craftEditItem.maintain + 10
  424.    updateCraft()
  425. end
  426.  
  427. -- Button callback handler for the Zero button on the interactive pop up for craftable maintainers
  428. function maintSetZero()
  429.    craftEditItem.maintain = 0
  430.    updateCraft()
  431. end
  432.  
  433. -- Button callback handler for the Tab Left button (top left)
  434. function tabLeft()
  435.    local origTab = curTab
  436.    curTab = curTab - 1
  437.    if curTab < 1 then curTab = 1 end
  438.    if curTab ~= origTab then
  439.       tabs[origTab].setVisible(false)
  440.       tabs[curTab].setVisible(true)
  441.       updateHeader()
  442.       --if speaker ~= nil then speaker.speak(tabTitles[curTab].phon) end
  443.    end
  444. end
  445.  
  446. -- Button callback handler for the Tab Right button (top right)
  447. function tabRight()
  448.    local origTab = curTab
  449.    curTab = curTab + 1
  450.    if curTab > #tabs then curTab = #tabs end
  451.    if curTab ~= origTab then
  452.       tabs[origTab].setVisible(false)
  453.       tabs[curTab].setVisible(true)
  454.       updateHeader()
  455.       --if speaker ~= nil then speaker.speak(tabTitles[curTab].phon) end
  456.    end
  457. end
  458.  
  459. -- Button callback handler for the Page Left button for craftables (bottom left)
  460. function craftLeft()
  461.    local origPage = craftPage
  462.    craftPage = craftPage - 1
  463.    if craftPage < 1 then craftPage = 1 end
  464.    if craftPage ~= origPage then
  465.       tabs[tabsByLabel.craft].setBackgroundColor(colors.black)
  466.       tabs[tabsByLabel.craft].clear()
  467.       updateCraft()
  468.    end
  469. end
  470.  
  471. -- Button callback handler for the Page Right button for craftables (bottom right)
  472. function craftRight()
  473.    local origPage = craftPage
  474.    local itemCount = 0
  475.    for name, item in pairs(crafts) do
  476.       itemCount = itemCount + 1
  477.    end
  478.    local lastPage = math.ceil(itemCount/craftLineCount)
  479.    craftPage = craftPage + 1
  480.    if craftPage > lastPage then craftPage = lastPage end
  481.    if craftPage ~= origPage then
  482.       tabs[tabsByLabel.craft].setBackgroundColor(colors.black)
  483.       tabs[tabsByLabel.craft].clear()
  484.       updateCraft()
  485.    end
  486. end
  487.  
  488. -- Define the buttons which are always available on all tabs
  489. -- Format: { startX pos, startY pos, endX pos, endY pos, fg color, bg color, button text, tab number, callback function name }
  490. buttons.resetStatus = { startX = 1, startY = 1, endX = 1, endY = 1, fg = colors.black, bg = colors.yellow, text = "R", tab = 0, callBack = "resetStatus" }
  491. buttons.filterCrafts = { startX = tabDims[tabsByLabel.craft].width, startY = tabDims[tabsByLabel.craft].height, endX = tabDims[tabsByLabel.craft].width, endY = tabDims[tabsByLabel.craft].height, fg = colors.black, bg = colors.yellow, text = "F", tab = tabsByLabel.craft, callBack = "filterCrafts" }
  492. buttons.leftMain = { startX = 3, startY = 1, endX = 7, endY = 1, fg = colors.black, bg = colors.lime, text = "  <  ", tab = 0, callBack = "tabLeft" }
  493. buttons.rightMain = { startX = (monWidth - 6), startY = 1, endX = (monWidth - 2), endY = 1, fg = colors.black, bg = colors.lime, text = "  >  ", tab = 0, callBack = "tabRight" }
  494. buttons.leftCraft = { startX = 6, startY = tabDims[tabsByLabel.craft].height, endX = 10, endY = tabDims[tabsByLabel.craft].height, fg = colors.black, bg = colors.yellow, text = "  <  ", tab = tabsByLabel.craft, callBack = "craftLeft" }
  495. buttons.rightCraft = { startX = (tabDims[tabsByLabel.craft].width - 9), startY = tabDims[tabsByLabel.craft].height, endX = (tabDims[tabsByLabel.craft].width - 5), endY = tabDims[tabsByLabel.craft].height, fg = colors.black, bg = colors.yellow, text = "  >  ", tab = tabsByLabel.craft, callBack = "craftRight" }
  496.  
  497. if useMonitor == true then
  498.    term.clear()
  499.    term.setCursorPos(1,1)
  500.    print("Status Monitor...")
  501.    print()
  502.    if monitor ~= nil then print("Monitor: Present") else print("Monitor: Absent") end
  503.    if bridge ~= nil then print("ME Bridge: Present") else print("ME Bridge: Absent") end
  504.    if modem ~= nil then print("Wireless Modem: Present") else print("Wireless Modem: Absent") end
  505.    if speaker ~= nil then print("Speaker: Present") else print("Speaker: Absent") end
  506.    print()
  507.    print("Press 'q' to quit")
  508. end
  509.  
  510. display.clear()
  511. updateHeader()
  512.  
  513. -- Read the persisted craftables maintain data if it exists
  514. if fs.exists(craftMaintainFileName) == true then
  515.    local file = fs.open(craftMaintainFileName, "r")
  516.    local data = file.readAll()
  517.    file.close()
  518.    craftMaintain = textutils.unserialize(data)
  519. end
  520.  
  521.  
  522. local pollTimer = os.startTimer(0)
  523. while true do
  524.    local event, p1, p2, p3, p4, p5 = os.pullEvent()
  525.    -- If 'q' is pressed, then we quit
  526.    if event == "key" then
  527.       if p1 == keys.q then
  528.          term.setCursorPos(1,11)
  529.          return
  530.       end
  531.    -- If we get a modem message, process it for status messages
  532.    elseif event == "modem_message" and p2 == statusChannelID then
  533.       local msgstatus = textutils.unserialize(p4)
  534.       for key, value in pairs(msgstatus) do
  535.          if key == "Inventory" then
  536.             inventory = value
  537.          else
  538.             status[key] = value
  539.          end
  540.       end
  541.       updateGeneral()
  542.    -- If we get a click event, see if there is an associated line (craftables) or button to process
  543.    elseif event == clickEvent then
  544.       local but = identifyButton(p2, p3)
  545.       if but ~= nil then
  546.          getfenv()[but.callBack]()
  547.       elseif curTab == tabsByLabel.craft then
  548.          local itemIndex = p3 - tabDims[tabsByLabel.craft].offsetY
  549.          if craftList[itemIndex] ~= nil then
  550.             craftEditItem = craftList[itemIndex]
  551.             local key = craftEditItem.name .. ":" .. craftEditItem.meta
  552.             if craftMaintain[key] ~= nil then
  553.                craftEditItem.maintain = craftMaintain[key].maintain
  554.             else
  555.                craftEditItem.maintain = 0
  556.             end
  557.             craftInteractive = true
  558.             updateCraft()
  559.          end
  560.       else
  561.          --print("No button found")
  562.       end
  563.    -- If we get a crafting complete message from the PPP ME Bridge then trigger a craft check
  564.    elseif event == "craftingComplete" and bridge ~= nil then
  565.       updateCraft()
  566.       updateGeneral()
  567.       --checkAutoCraft()
  568.    -- If we get our timer event then check for autocrafting and update the status pages
  569.    elseif event == "timer" and p1 == pollTimer and bridge ~= nil then
  570.       -- The initial crafting status message (for local display)
  571.       if status["Crafting"] == nil then status["Crafting"] = "Nothing" end
  572.       if crafting == true then craftCheckCount = craftCheckCount + 1 end
  573.       updateCraft()
  574.       checkAutoCraft()
  575.       local transmitStatus = {}
  576.       transmitStatus["Crafting"] = status["Crafting"]
  577.       if modem ~= nil then modem.transmit(statusChannelID, 0, textutils.serialize(transmitStatus)) end
  578.       pollTimer = os.startTimer(5)
  579.    end
  580. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement