Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Base Monitoring System
- -- Version 0.2
- -----------------------------------------------------------------------------------------------------------------
- -- Tested on Computercraft version 1.58
- -- Minecraft version 1.6.4
- -----------------------------------------------------------------------------------------------------------------
- -- NOTE: THIS IS A WORK IN PROGRESS
- -----------------------------------------------------------------------------------------------------------------
- --
- -- This code was written by Impshial and is free software:
- -- You can redistribute it and/or modify
- -- it under the terms of the GNU General Public License as published by
- -- the Free Software Foundation, either version 3 of the License, or
- -- (at your option) any later version.
- -- This program is distributed in the hope that it will be useful,
- -- but WITHOUT ANY WARRANTY; without even the implied warranty of
- -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- -- GNU General Public License for more details.
- --
- -- See https://www.gnu.org/licenses/ for details
- --
- -- Pastebin for this program: https://pastebin.com/B6kvjRxZ
- --
- -- Pastebin for the ImpShial API: https://pastebin.com/pwBMKNCb
- --
- -- Instructions:
- -- 1. Place an Advanced Computer
- -- 2. Attach wired Modem to computer
- -- 3. Set up a monitor (if there is no monitor present, the program will let you know)
- -- 4. Attach wired Modem to monitor
- -- 5. Connect computer and monitor via Network Cables
- -- 6. Right-click each modem to connect devices to network
- -- 7. Run the following command in Computer Terminal: "pastebin get B6kvjRxZ startup"
- -- 8. Run "startup" (API will be downloaded and installed in this step)
- -- 9. Attach machines via Modems and newtwork cable. Machines will automatically show up on monitor when connected
- -- 10. Right-click on machine name to get details
- --
- -----------------------------------------------------------------------------------------------------------------
- -- Version notes:
- -- Added button-click capability for machines
- -- Works with all Thermal expansion machines
- -- Works with most Minefactory Reloaded machines
- -- GPL License verbiage added
- -----------------------------------------------------------------------------------------------------------------
- -- Bugs:
- -- Right-click intermittantly works
- -- Delay between right-click and machine being selected
- -----------------------------------------------------------------------------------------------------------------
- --
- -- Begin Program
- --
- -----------------------------------------------------------------------------------------------------------------
- -- Variables
- local version = "0.2" -- Program Version
- local scale -- the text scale for the monitor
- local monX -- Monitor Width
- local monY -- Monitor Height
- local line -- Monitor line variable
- local sizeXY -- Text scale
- local mainLoop -- Main loop variable. set to < 1 to stop loop
- local tableMachines = {} -- Main machine table
- local render -- Should we render the machine list?
- local renderReason -- Why is our monitor too small?
- local clicked -- Has the user clicked a machine?
- local clickedLine -- which line was clicked
- local headerFooterColor -- Color for the header/footer bar
- local headerFooterTextColor -- Color for header/footer text
- local selectedBackColor -- Colors for selected item background
- local selectedTextColor -- Colors for selected item text
- local detailsKeyColumn -- Column to start at for details Key
- local detailsValueColumn -- Column to start at for details Value
- local debugMode -- For testing
- -----------------------------------------------------------------------------------------------------------------
- -- Local Functions
- -----------------------------------------------------------------------------------------------------------------
- -- Get number from machine/entity name
- function getMachineEntityNumber(machineName)
- lastChars = string.sub(machineName, -2)
- if string.match(lastChars, "_") then
- return string.sub(lastChars, -1)
- else
- return string.sub(lastChars, -2)
- end
- end
- -----------------------------------------------------------------------------------------------------------------
- -- Startup code
- -----------------------------------------------------------------------------------------------------------------
- -- Write startup message to Terminal
- term.clear()
- -- Check to see if the API file exists
- if fs.exists("ImpAPI") == false then
- term.setCursorPos(1,1)
- term.setTextColor(colors.red)
- term.write("Missing API File! Attempting to download...")
- term.setTextColor(colors.white)
- shell.run("pastebin get pwBMKNCb ImpAPI") -- Go out and download it
- sInput = nil
- term.setCursorPos(1,5)
- term.setTextColor(colors.green)
- term.write("API Loaded.")
- term.setCursorPos(1,7)
- term.setTextColor(colors.white)
- term.write("Press enter to continue...")
- while true do
- sInput = read()
- if sInput ~= nil or sInput ~= "" then
- break
- end
- end
- end
- os.loadAPI("ImpAPI")
- term.clear()
- ImpAPI.draw_line_term(1,1,55,colors.green)
- term.setCursorPos(1,1)
- term.write("Base Monitoring System")
- ImpAPI.draw_line_term(1,2,55,colors.black)
- term.setCursorPos(1,3)
- term.write("Version"..version)
- term.setCursorPos(1,5)
- term.write("Finding Monitor...")
- -- Find our monitor and define it
- mon=ImpAPI.monitorSearch()
- -- Check to see if a monitor exists
- if mon then -- Found it
- ImpAPI.CTWrite(1,6,"Monitor Found!",colors.green)
- ImpAPI.CTWrite(1,8,"System Running",colors.orange)
- else -- No monitor Present
- ImpAPI.CTWrite(1,6,"Monitor Not Found!",colors.red)
- ImpAPI.CTWrite(1,8,"Please Attach a Monitor",colors.red)
- term.setCursorPos(1,9)
- return
- end
- -- Set the initial text scale
- scale = 0.5
- -- Set the begining line for our text
- line = 1
- -- Set the initial text scale
- sizeXY = 1
- -- Set the main Loop variable
- mainLoop = 1
- -- Set our monitor to the initial text scale
- mon.setTextScale(sizeXY)
- -- Write out our machine list
- render = true
- -- Nothing has been clicked
- clicked = false
- -- Set our header/footer color
- headerFooterColor = colors.blue
- -- Set our header/footer text color
- headerFooterTextColor = colors.white
- -- Set background color for selected machine
- selectedBackColor = colors.white
- -- Set text color for selected machine
- selectedTextColor = colors.black
- -- Set the details Key column
- detailsKeyColumn = 43
- -- Set the details value column value
- detailsValueColumn = 55
- -- Debug mode?
- debugMode = false
- -----------------------------------------------------------------------------------------------------------------
- -- Main Loop
- -----------------------------------------------------------------------------------------------------------------
- while mainLoop > 0 do
- -- Get our monitor size
- monX, monY = mon.getSize()
- -- Change textScale when monitor size changes
- if monX < 62 and scale == 1 then
- scale = 0.5
- elseif monX > 142 and scale < 1 then
- scale = 1
- elseif monX < 143 and scale < 1 then
- scale = 0.5
- end
- mon.setTextScale(scale)
- -- If monitor is too small, let the user know
- if monX < 79 and scale == 0.5 then
- render = false
- renderReason = "Reason: Width"
- elseif monY < 24 and scale == .05 then
- render = false
- renderReason = "Reason: Height"
- elseif monY < 19 and scale == 1 then
- render = false
- renderReason = "Reason: Height"
- else
- render = true
- end
- -- Get all peripherals on the network
- machines=peripheral.getNames()
- --Create/Clear out main machine table
- tableMachines = {}
- if render == true then
- -----------------------------------------------------------------------------------------------------------------
- -- Loop through machines
- -----------------------------------------------------------------------------------------------------------------
- for i = 1, #machines do
- machNbr = getMachineEntityNumber(machines[i])
- if string.match(machines[i], "thermalexpansion_machine_furnace") then
- -- Check to see if furnace is active
- machine=peripheral.wrap(machines[i])
- isActive=machine.getStackInSlot(1)
- itemInSlot1 = ""
- itemQtyInSlot1 = ""
- itemInSlot2 = ""
- ItemQtyInSlot2 = ""
- activeText = ""
- if isActive then
- activeText = "Active"
- else
- activeText = "InActive"
- end
- processing=machine.getStackInSlot(1)
- processed1=machine.getStackInSlot(2)
- if processing then
- for k,v in pairs(processing) do
- if k == "name" then
- itemInSlot1 = v
- elseif k == ("qty") then
- itemQtyInSlot1 = tostring(v)
- end
- end
- else
- itemInSlot1 = "Nothing"
- itemQtyInSlot1 = "0"
- end
- if processed1 then
- for k,v in pairs(processed1) do
- if k == "name" then
- itemInSlot2 = v
- elseif k == ("qty") then
- itemQtyInSlot2 = tostring(v)
- end
- end
- else
- itemInSlot2 = "Nothing"
- itemQtyInSlot2 = "0"
- end
- table.insert(tableMachines, { ["Machine"]="Redstone Furnace",
- ["Number"]=machNbr,
- ["HasEnergy"]=true,
- ["Energy"]=machine.getEnergy(),
- ["IsActive"]=activeText,
- ["PerName"]=machines[i],
- ["MaxEnergy"]=machine.getMaxEnergy(),
- ["ItemInSlot1"]=itemInSlot1,
- ["ItemQtyInSlot1"]=itemQtyInSlot1,
- ["slot1Name"]="Processing: ",
- ["ItemInSlot2"]=itemInSlot2,
- ["ItemQtyInSlot2"]=itemQtyInSlot2,
- ["slot2Name"]="Output: ",
- ["InventorySlots"]=2})
- elseif string.match(machines[i], "thermalexpansion_machine_pulverizer") then
- -- Check to see if pulverizer is active
- -- Check to see if furnace is active
- machine=peripheral.wrap(machines[i])
- isActive=machine.getStackInSlot(1)
- itemInSlot1 = ""
- itemQtyInSlot1 = ""
- itemInSlot2 = ""
- ItemQtyInSlot2 = ""
- itemInSlot3 = ""
- ItemQtyInSlot3 = ""
- activeText = ""
- if isActive then
- activeText = "Active"
- else
- activeText = "InActive"
- end
- processing=machine.getStackInSlot(1)
- processed1=machine.getStackInSlot(2)
- processed2=machine.getStackInSlot(3)
- if processing then
- for k,v in pairs(processing) do
- if k == "name" then
- itemInSlot1 = v
- elseif k == ("qty") then
- itemQtyInSlot1 = tostring(v)
- end
- end
- else
- itemInSlot1 = "Nothing"
- itemQtyInSlot1 = "0"
- end
- if processed1 then
- for k,v in pairs(processed1) do
- if k == "name" then
- itemInSlot2 = v
- elseif k == ("qty") then
- itemQtyInSlot2 = tostring(v)
- end
- end
- else
- itemInSlot2 = "Nothing"
- itemQtyInSlot2 = "0"
- end
- if processed2 then
- for k,v in pairs(processed2) do
- if k == "name" then
- itemInSlot3 = v
- elseif k == ("qty") then
- itemQtyInSlot3 = tostring(v)
- end
- end
- else
- itemInSlot3 = "Nothing"
- itemQtyInSlot3 = "0"
- end
- table.insert(tableMachines, { ["Machine"]="Pulverizer",
- ["Number"]=machNbr,
- ["HasEnergy"]=true,
- ["Energy"]=machine.getEnergy(),
- ["IsActive"]=activeText,
- ["PerName"]=machines[i],
- ["MaxEnergy"]=machine.getMaxEnergy(),
- ["ItemInSlot1"]=itemInSlot1,
- ["ItemQtyInSlot1"]=itemQtyInSlot1,
- ["slot1Name"]="Processing: ",
- ["ItemInSlot2"]=itemInSlot2,
- ["ItemQtyInSlot2"]=itemQtyInSlot2,
- ["slot2Name"]="Output: ",
- ["ItemInSlot3"]=itemInSlot3,
- ["ItemQtyInSlot3"]=itemQtyInSlot3,
- ["slot3Name"]="Bonus: ",
- ["InventorySlots"]=3})
- elseif string.match(machines[i], "thermalexpansion_machine_smelter") then
- -- Check to see if induction smelter is active
- machine=peripheral.wrap(machines[i])
- isActive=machine.getStackInSlot(1)
- if isActive then
- tableMachines["Induction Smelter "..machNbr] = "Active"
- else
- tableMachines["Induction Smelter "..machNbr] = "Inactive"
- end
- elseif string.match(machines[i], "thermalexpansion_machine_crucible") then
- -- Check to see if pulverizer is active
- machine=peripheral.wrap(machines[i])
- isActive=machine.getStackInSlot(1)
- if isActive then
- tableMachines["Magma Crucible "..machNbr] = "Active"
- else
- tableMachines["Magma Crucible "..machNbr] = "Inactive"
- end
- elseif string.match(machines[i], "thermalexpansion_machine_transposer") then
- -- Check to see if pulverizer is active
- machine=peripheral.wrap(machines[i])
- isActive=machine.getStackInSlot(1)
- if isActive then
- tableMachines["Fluid Transposer "..machNbr] = "Active"
- else
- tableMachines["Fluid Transposer "..machNbr] = "Inactive"
- end
- end
- end
- -----------------------------------------------------------------------------------------------------------------
- -- Write static text to main monitor
- -----------------------------------------------------------------------------------------------------------------
- mon.clear()
- -- Header
- ImpAPI.draw_line(1, line, monX, headerFooterColor, mon)
- ImpAPI.CWrite(1, line, mon, "Networked Machines", headerFooterTextColor)
- ImpAPI.CWrite(29, line, mon, "Status", headerFooterTextColor)
- -- Center details location based on monitor width
- startLeft = 41 + ((monX - 41) / 2 - 3)
- ImpAPI.CWrite(startLeft, line, mon, "Details", headerFooterTextColor)
- line = line + 2
- -- Details line
- for l = 2,(monY -1) do
- ImpAPI.draw_line(40,l, 1, headerFooterColor, mon)
- end
- -- Footer
- ImpAPI.draw_line(1, monY, monX, headerFooterColor, mon)
- ImpAPI.CWrite(1,monY,mon,"Impshial Base Monitor: v"..version, headerFooterTextColor)
- ImpAPI.draw_line(1, (monY+1), monX, colors.black, mon)
- -----------------------------------------------------------------------------------------------------------------
- -- Begin machine loop
- -----------------------------------------------------------------------------------------------------------------
- for i = 1,#tableMachines do
- machName = tableMachines[i].Machine
- machNumber = tableMachines[i].Number
- machHasEnergy = tableMachines[i].HasEnergy
- invSlots = tableMachines[i].InventorySlots
- machColor = colors.white
- if clicked == true and clickedLine == line then
- -- machine selected...
- ImpAPI.draw_line(1,line, 39, selectedBackColor, mon)
- else
- -- machine NOT selected...
- ImpAPI.draw_line(1,line, 39, colors.black, mon)
- end
- mon.setCursorPos(2,line)
- if clicked == true and clickedLine == line then
- machColor = selectedTextColor
- else
- if string.match(machName,"Furnace") then
- machColor = colors.cyan
- elseif string.match(machName,"Pulverizer") then
- machColor = colors.orange
- elseif string.match(machName,"Induction") then
- machColor = colors.lime
- elseif string.match(machName,"Crucible") then
- machColor = colors.lightBlue
- elseif string.match(machName,"Transposer") then
- machColor = colors.lightGray
- end
- end
- ImpAPI.CWrite(2,line,mon, machName.." "..machNumber, machColor)
- -- Status
- if tableMachines[i].IsActive == "InActive" then
- ImpAPI.CWrite(28,line,mon, tableMachines[i].IsActive, colors.red)
- else
- ImpAPI.CWrite(28,line,mon, tableMachines[i].IsActive, colors.green)
- end
- -----------------------------------------------------------------------------------------------------------------
- -- Details
- -----------------------------------------------------------------------------------------------------------------
- detailsLine = 3
- if clicked == true and clickedLine == line then
- -- Machine Name
- ImpAPI.draw_line(detailsKeyColumn, detailsLine, (monX - detailsKeyColumn), colors.black, mon)
- ImpAPI.CWrite(detailsKeyColumn, detailsLine, mon, "Machine: ", colors.orange)
- ImpAPI.CWrite(detailsValueColumn, detailsLine, mon, machName.." "..machNumber, colors.white)
- detailsLine = detailsLine + 1
- -- Energy (if applicable)
- if machHasEnergy == true then
- ImpAPI.draw_line(detailsKeyColumn, detailsLine, (monX - detailsKeyColumn), colors.black, mon)
- ImpAPI.CWrite(detailsKeyColumn, detailsLine, mon, "Energy: ", colors.orange)
- ImpAPI.CWrite(detailsValueColumn, detailsLine, mon, tableMachines[i].Energy.."/"..tableMachines[i].MaxEnergy, colors.white)
- detailsLine = detailsLine + 2
- else
- detailsLine = detailsLine + 3
- end
- -- Slot 1 Item
- if invSlots > 0 then
- ImpAPI.draw_line(detailsKeyColumn, detailsLine, (monX - detailsKeyColumn), colors.black, mon)
- ImpAPI.CWrite(detailsKeyColumn, detailsLine, mon, tableMachines[i].slot1Name, colors.orange)
- ImpAPI.CWrite(detailsValueColumn, detailsLine, mon, tableMachines[i].ItemInSlot1, colors.white)
- detailsLine = detailsLine + 1
- -- Slot 1 Qty
- ImpAPI.draw_line(detailsKeyColumn, detailsLine, (monX - detailsKeyColumn), colors.black, mon)
- ImpAPI.CWrite(detailsKeyColumn, detailsLine, mon, "Qty: ", colors.orange)
- ImpAPI.CWrite(detailsValueColumn, detailsLine, mon, tableMachines[i].ItemQtyInSlot1, colors.white)
- detailsLine = detailsLine + 2
- end
- if invSlots > 1 then
- -- Slot 2 Item
- ImpAPI.draw_line(detailsKeyColumn, detailsLine, (monX - detailsKeyColumn), colors.black, mon)
- ImpAPI.CWrite(detailsKeyColumn, detailsLine, mon, tableMachines[i].slot2Name, colors.orange)
- ImpAPI.CWrite(detailsValueColumn, detailsLine, mon, tableMachines[i].ItemInSlot2, colors.white)
- detailsLine = detailsLine + 1
- -- Slot 2 Qty
- ImpAPI.draw_line(detailsKeyColumn, detailsLine, (monX - detailsKeyColumn), colors.black, mon)
- ImpAPI.CWrite(detailsKeyColumn, detailsLine, mon, "Qty: ", colors.orange)
- ImpAPI.CWrite(detailsValueColumn, detailsLine, mon, tableMachines[i].ItemQtyInSlot2, colors.white)
- detailsLine = detailsLine + 2
- end
- if invSlots > 2 then
- -- Slot 3 Item
- ImpAPI.draw_line(detailsKeyColumn, detailsLine, (monX - detailsKeyColumn), colors.black, mon)
- ImpAPI.CWrite(detailsKeyColumn, detailsLine, mon, tableMachines[i].slot3Name, colors.orange)
- ImpAPI.CWrite(detailsValueColumn, detailsLine, mon, tableMachines[i].ItemInSlot3, colors.white)
- detailsLine = detailsLine + 1
- -- Slot 3 Qty
- ImpAPI.draw_line(detailsKeyColumn, detailsLine, (monX - detailsKeyColumn), colors.black, mon)
- ImpAPI.CWrite(detailsKeyColumn, detailsLine, mon, "Qty: ", colors.orange)
- ImpAPI.CWrite(detailsValueColumn, detailsLine, mon, tableMachines[i].ItemQtyInSlot3, colors.white)
- detailsLine = detailsLine + 2
- end
- elseif clicked == false then
- ImpAPI.draw_line(detailsKeyColumn, detailsLine, (monX - detailsKeyColumn), colors.black, mon)
- ImpAPI.CWrite(detailsKeyColumn, detailsLine, mon, "No Machine Selected", colors.red)
- detailsLine = detailsLine + 1
- end
- ImpAPI.draw_line(1,line+1, 39, colors.black, mon)
- line = line + 1
- end
- -----------------------------------------------------------------------------------------------------------------
- -- Watch for mouse events
- -----------------------------------------------------------------------------------------------------------------
- local myTimer = os.startTimer(.2)
- local exitConditionVariable = false
- while not exitConditionVariable do
- myEvent = {os.pullEvent()}
- if myEvent[1] == "monitor_touch" then
- xClicked=myEvent[3]
- yClicked=myEvent[4]
- --ImpAPI.CWrite(1,27,mon,ImpAPI.tablelength(tableMachines) + 2,colors.orange)
- if yClicked > 2 and yClicked <= (ImpAPI.tablelength(tableMachines) + 2) then
- clicked = true
- clickedLine = yClicked
- else
- clicked = false
- end
- --button.checkxy(myEvent[3],myEvent[4])
- elseif myEvent[1] == "timer" and myEvent[2] == myTimer then
- exitConditionVariable = true
- end
- end
- -- Debug for monitor size
- if debugMode then
- mon.setCursorPos(1,line)
- mon.write("S: "..sizeXY)
- line = line + 1
- mon.setCursorPos(1,line)
- mon.write("H: "..monY)
- line = line + 1
- mon.setCursorPos(1,line)
- mon.write("W: "..monX)
- end
- else
- mon.clear()
- ImpAPI.draw_line(1,1,monX, colors.red, mon)
- mon.setCursorPos(1,1)
- mon.setTextColor(colors.white)
- mon.write("Monitor Too Small")
- ImpAPI.draw_line(1,2,monX, colors.black, mon)
- mon.setCursorPos(1,3)
- mon.write("Please increase the")
- mon.setCursorPos(1,4)
- mon.write("size of your monitor")
- mon.setCursorPos(1,5)
- mon.write(renderReason)
- end
- -- Reset line variable
- line = 1
- -- Loop timer
- sleep(.25)
- end
- -----------------------------------------------------------------------------------------------------------------
- -- End of Program
- -----------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement