Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Rotarycraft-Opencomputer integration system, ROIS. Made by Roxox1.
- --Thanks to SealabJaster for GUI inspiration. See: https://oc.cil.li/index.php?/topic/815-a-file-explorer/
- local component = require("component")
- local term = require("term")
- local gpu = component.gpu
- local color = require("colors")
- local event = require("event")
- local keyboard = require("keyboard")
- -- Naming
- local version = 0.1
- local name = "ROIS"
- local formattedName = name .." v" .. version
- --Screen resolution
- local scrWidth, scrHeight = gpu.getResolution()
- --Drawing values
- local startY = 2 -- Minimum y value to start drawing at
- local endY = scrHeight -- Maximum y value to draw at.
- --Status
- local status_normal = "[CTRL+C]=Close [CTRL+R]=Refresh [W]=Up [S]=Down [R]=Rename [O]=Options"
- local status_rename = "[RENAME]: "
- local status_options_industrialcoil = "[T]=Set_Torque [S]=Set_Speed [B]=Back"
- local status_set_torque = "[TORQUE]: "
- local status_set_speed = "[SPEED]: "
- local status = status_normal
- --Table to store all machines in
- local machines = {}
- --Variables to store currently selected machine information.
- local selected = 1 -- Stores the list int value of the selected machine. Think list[1], list[2]...etc
- local selectedName = "" -- Stores the currently selected machines name
- local selectedInformation = {} -- Stores the currently selectes machines information.
- --General variables
- local outputWrite = 0
- local running = true
- --Colors to use for drawing to the screen
- local colors =
- {
- bars = 0,
- text_normal = 0,
- text_highlighted = 0,
- item_cwd = 0,
- item_highlighted = 0,
- item_saved = 0,
- usePallet = false
- }
- -- Setup all of the colours used
- if (gpu.getDepth() == 1) then
- colors.bars = 1
- colors.text_normal = 1
- colors.text_highlighted = 0
- colors.item_cwd = 1
- colors.item_highlighted = 1
- colors.item_saved = 1
- elseif (gpu.getDepth() == 8) then
- colors.bars = 0x00264D
- colors.text_normal = 0xFFFFFF
- colors.text_highlighted = 0xFFFFFF
- colors.item_cwd = 0x660066
- colors.item_highlighted = 0x803300
- colors.item_saved = 0x004400
- else
- local col = require("colors")
- colors.usePallet = true
- colors.bars = col.blue
- colors.text_normal = col.white
- colors.text_highlighted = col.white
- colors.item_cwd = col.magenta
- colors.item_highlighted = col.red
- colors.item_saved = col.green
- end
- --Returns a table of information about the specified machine. Args. component address.
- local function getMachineInformation(address)
- local info = {}
- theComponent = component.proxy(address)
- info["componentObject"] = theComponent
- info["xCoordinate"], info["yCoordinate"], info["zCoordinate"] = theComponent.getCoords()
- local name = theComponent.getName()
- info["componentName"] = name
- info["componentAddress"] = address
- if(theComponent.getTanks() == nil) then
- info["canStoreLiquid"] = false
- else
- info["canStoreLiquid"] = true
- info["storedLiquidType"], info["storedLiquidAmount"], info["storedLiquidMax"] = theComponent.getTanks()
- end
- info["powerTotal"], info["powerTorque"], info["powerSpeed"] = theComponent.getPower()
- info["placedBy"] = theComponent.getPlacer()
- if(name == "DC Electric Engine" or name == "AC Electric Engine" or name == "Industrial Coil") then
- info["isEngine"] = true
- if(info["powerTotal"] > 0) then
- info["isOn"] = true
- else
- info["isOn"] = false
- end
- else
- info["isEngine"] = false
- end
- return info
- end
- --Updates the current list of machines. Does not change display names.
- local function updateMachines()
- local newMachines = {}
- for machineName, machineInformation in pairs(machines) do
- newMachines[machineName] = getMachineInformation(machineInformation["componentAddress"])
- end
- machines = newMachines
- end
- --Finds all machines, will reset display names. Use only for program initialization.
- local function findMachines()
- local allMachines = {}
- for address, componentType in component.list() do
- if(componentType == "Extractor" or componentType == "AdvancedGears" or componentType == "Engine") then
- allMachines[componentType.." (" .. address .. ")"] = getMachineInformation(address)
- end
- end
- machines = allMachines
- end
- --Returns the machines information associated with the specified address. Will return nil if there is no machine associated with that address.
- local function getMachineInformation(address)
- for key,value in pairs(machines) do
- if(value["componentAddress"] == address) then
- return value
- end
- end
- end
- --Returns the amount of machines added.
- local function getMachineListSize()
- local i = 0
- for key,value in pairs(machines) do i = i + 1 end
- return i
- end
- --Draws the list of machines to the screen
- local function drawMachineList()
- --First sort the list
- local sortedTable = {}
- for displayName, machineInfo in pairs(machines) do
- table.insert(sortedTable, displayName)
- end
- table.sort(sortedTable)
- for i = 1, #sortedTable do
- local k,v = sortedTable[i], machines[sortedTable[i]]
- if(i == selected) then
- gpu.setBackground(colors.item_highlighted, colors.usePallet)
- gpu.setForeground(colors.text_highlighted, colors.usePallet)
- selectedName = k
- selectedInformation = v
- end
- term.setCursor(1, startY + i - 1) -- xValue, yValue
- print(k)
- gpu.setBackground(0x000000)
- gpu.setForeground(colors.text_normal, colors.usePallet)
- end
- end
- local function printOut(output)
- term.setCursor(scrWidth / 2, outputWrite)
- print(output)
- outputWrite = outputWrite + 1
- end
- local function drawMachineInfo()
- outputWrite = startY + 1
- printOut(selectedName)
- outputWrite = outputWrite + 2
- if(selectedInformation["isEngine"]) then
- printOut("Engine Status:")
- if(selectedInformation["isOn"]) then
- printOut(" Output Total: " .. selectedInformation["powerTotal"])
- printOut(" Output Torque: " .. selectedInformation["powerTorque"])
- printOut(" Output Speed: " .. selectedInformation["powerSpeed"])
- else
- printOut(" This engine is not outputting power")
- if(selectedInformation["componentName"] == "DC Electric Engine") then
- printOut(" This engine requires a redstone signal!")
- end
- end
- else
- printOut("Power: ")
- if(selectedInformation["powerTotal"] > 0) then
- printOut(" Receiving Total: " .. selectedInformation["powerTotal"])
- printOut(" Receiving Torque: " .. selectedInformation["powerTorque"])
- printOut(" Receiving Speed: " .. selectedInformation["powerSpeed"])
- else
- printOut(" Machine is not receiving power")
- end
- end
- if(selectedInformation["canStoreLiquid"] == true) then
- printOut("Fluid: ")
- printOut(" Type: " .. selectedInformation["storedLiquidType"])
- printOut(" Amount: " .. selectedInformation["storedLiquidAmount"])
- printOut(" Maximum: " .. selectedInformation["storedLiquidMax"])
- end
- outputWrite = outputWrite + 1
- printOut("Coordinates: X:" .. selectedInformation["xCoordinate"] .. " Y:".. selectedInformation["yCoordinate"] .. " Z:" .. selectedInformation["zCoordinate"])
- outputWrite = outputWrite + 1
- printOut("Address: " .. selectedInformation["componentAddress"])
- outputWrite = outputWrite + 1
- printOut("MachineName: " .. selectedInformation["componentName"])
- outputWriter = outputWrite + 1
- printOut("Created by: " .. selectedInformation["placedBy"])
- end
- --Prompt the user for input. Note that this function will not refresh the screen after it's done.
- local function getInput()
- term.setCursor(#status, scrHeight)
- term.setCursorBlink(true)
- local input = require("text").trim(term.read())
- term.setCursorBlink(false)
- return input
- end
- local function drawHUD()
- local a = gpu.getBackground()
- gpu.setBackground(colors.bars, colors.usePallet)
- gpu.fill(1,1,scrWidth,1," ") --Top bar
- gpu.fill(1,endY, scrWidth, 1, " ") -- Bottom bar
- gpu.setForeground(colors.text_highlighted, colors.usePallet)
- gpu.set(1,1, "Machines (" .. getMachineListSize() .. "):")
- gpu.set(scrWidth-#formattedName, 1, formattedName)
- gpu.set(1, endY, status)
- gpu.setBackground(a)
- end
- local function refresh()
- updateMachines()
- term.clear()
- drawMachineList()
- drawMachineInfo()
- drawHUD()
- end
- local function handleInput()
- --Handle CTRL keys.
- if(status == status_normal) then
- if(keyboard.isControlDown()) then
- --Exit
- if(keyboard.isKeyDown(keyboard.keys.c)) then
- term.clear()
- running = false
- --Refresh
- elseif(keyboard.isKeyDown(keyboard.keys.r)) then
- refresh()
- end
- else
- --Go up
- if(keyboard.isKeyDown(keyboard.keys.w)) then
- if not (selected == 1) then
- selected = selected - 1
- end
- refresh()
- --Go down
- elseif (keyboard.isKeyDown(keyboard.keys.s)) then
- if not (selected == getMachineListSize()) then
- selected = selected + 1
- end
- refresh()
- --Rename
- elseif (keyboard.isKeyDown(keyboard.keys.r)) then
- status = status_rename
- refresh()
- newName = getInput()
- refresh()
- machines[newName] = machines[selectedName]
- if(selected == 1) then
- selected = 2
- else
- selected = selected - 1
- end
- machines[selectedName] = nil
- status = status_normal
- refresh()
- elseif(keyboard.isKeyDown(keyboard.keys.o)) then
- if(selectedInformation["componentName"] == "Industrial Coil") then
- status = status_options_industrialcoil
- refresh()
- end
- end
- end
- elseif(status == status_options_industrialcoil) then
- if(keyboard.isKeyDown(keyboard.keys.b)) then
- status = status_normal
- refresh()
- elseif(keyboard.isKeyDown(keyboard.keys.t)) then
- status = status_set_torque
- refresh()
- local myInput = getInput()
- status = status_options_industrialcoil
- selectedInformation["componentObject"].setTorque(tonumber(myInput))
- refresh()
- refresh()
- elseif(keyboard.isKeyDown(keyboard.keys.s)) then
- status = status_set_speed
- refresh()
- local myInput = getInput()
- status = status_options_industrialcoil
- selectedInformation["componentObject"].setSpeed(tonumber(myinput))
- refresh()
- refresh()
- end
- end
- end
- findMachines()
- refresh()
- while running do
- local _ = event.pull(0.25)
- handleInput()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement