Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local device_name = "bigeti.ui"
- local device_display_name = "BigETI's UI"
- local width, height = term.getSize()
- local key_up = 200
- local key_down = 208
- local key_left = 203
- local key_right = 205
- local key_enter = 28
- local reactor_id = 0
- local reactors = {}
- local menu =
- {
- {
- [ "txt" ] = "On/Off",
- [ "descr" ] = "Turn reactor on/off",
- [ "val" ] = false,
- [ "func" ] = "onOff_Select",
- [ "dfunc" ] = "onOff_D"
- },
- {
- [ "txt" ] = "Eject waste",
- [ "descr" ] = "Eject waste",
- [ "val" ] = nil,
- [ "func" ] = "ejectWaste_Select"
- },
- {
- [ "txt" ] = "Eject fuel",
- [ "descr" ] = "Eject fuel",
- [ "val" ] = nil,
- [ "func" ] = "ejectFuel_Select"
- },
- {
- [ "txt" ] = "Show stats",
- [ "descr" ] = "Show reactor stats",
- [ "val" ] = nil,
- [ "func" ] = "showStats_Select"
- },
- {
- [ "txt" ] = "Settings",
- [ "descr" ] = "Software settings",
- [ "val" ] = nil,
- [ "func" ] = "settings_Select"
- },
- {
- [ "txt" ] = "Back",
- [ "descr" ] = "Back to reactors",
- [ "val" ] = nil,
- [ "func" ] = "back_Select"
- }
- }
- local settings =
- {
- {
- [ "txt" ] = "Background",
- [ "descr" ] = "Background color",
- [ "val" ] = 4,
- [ "is_color" ] = true,
- [ "func" ] = "settingsBackground_Select",
- [ "dfunc" ] = "settingsBackground_D"
- },
- {
- [ "txt" ] = "Text",
- [ "descr" ] = "Text color",
- [ "val" ] = 8,
- [ "is_color" ] = true,
- [ "func" ] = "settingsText_Select",
- [ "dfunc" ] = "settingsText_D"
- },
- {
- [ "txt" ] = "Back",
- [ "descr" ] = "Back to the menu",
- [ "val" ] = nil,
- [ "func" ] = "settingsBack_Select"
- }
- }
- local stats = {}
- local color_names =
- {
- "White",
- "Orange",
- "Magenta",
- "L-Blue",
- "Yellow",
- "Lime",
- "Pink",
- "Gray",
- "L-Gray",
- "Cyan",
- "Purple",
- "Blue",
- "Brown",
- "Green",
- "Red",
- "Black"
- }
- local selection =
- {
- [ "reactors" ] =
- {
- [ "s" ] = 1,
- [ "m" ] = reactors,
- [ "r" ] = false,
- [ "o" ] = 1
- },
- [ "menu" ] =
- {
- [ "s" ] = 1,
- [ "m" ] = menu,
- [ "r" ] = false,
- [ "o" ] = 1
- },
- [ "settings" ] =
- {
- [ "s" ] = 1,
- [ "m" ] = settings,
- [ "r" ] = false,
- [ "o" ] = 1
- },
- [ "stats" ] =
- {
- [ "s" ] = 1,
- [ "m" ] = stats,
- [ "r" ] = false,
- [ "o" ] = 1
- }
- }
- function reduceString(str, count)
- local ret = str
- local l = ret:len()
- if l > count then
- if count <= 3 then
- ret = string.rep(".", count)
- else
- ret = ret:sub(1, count - 3) .. "..."
- end
- end
- return ret
- end
- function alignString(str, count)
- local ret = reduceString(str, count)
- local l = ret:len()
- if l > 0 then
- local n = count - l
- if (n % 2) == 0 then
- ret = string.rep(" ", n / 2) .. ret .. string.rep(" ", n / 2)
- else
- ret = string.rep(" ", (n - 1) / 2) .. ret .. string.rep(" ", (n + 1) / 2)
- end
- end
- return ret
- end
- function getColorByIndex(i)
- return bit.blshift(1, i - 1)
- end
- function refreshReactors()
- reactors =
- {
- {
- [ "txt" ] = "Refresh",
- [ "descr" ] = "Refreshing...",
- [ "val" ] = nil,
- [ "func" ] = "refreshReactors"
- },
- {
- [ "txt" ] = "Settings",
- [ "descr" ] = "Software settings",
- [ "val" ] = nil,
- [ "func" ] = "settings_Select"
- },
- {
- [ "txt" ] = "Exit",
- [ "descr" ] = "Exit this software",
- [ "val" ] = nil,
- [ "func" ] = "exit_Select"
- }
- }
- selection["reactors"]["m"] = reactors
- selection["reactors"]["s"] = 1
- redraw("reactors")
- local r = {rednet.lookup("reactor.req.hello")}
- for i=1, #r do
- rednet.send(r[i], device_name, "reactor.req.name")
- local sender, msg, prot = rednet.receive("reactor.ack.name", 1)
- if tonumber(r[i]) == tonumber(sender) then
- table.insert(reactors,
- {
- [ "txt" ] = msg,
- [ "descr" ] = "Open " .. msg,
- [ "val" ] = sender,
- [ "func" ] = "reactor_Select"
- })
- end
- end
- reactors[1]["descr"] = "Refresh reactors list"
- end
- function header()
- term.setBackgroundColor(getColorByIndex(settings[1]["val"]))
- term.clear()
- term.setTextColor(getColorByIndex(settings[2]["val"]))
- term.setCursorPos(1, 1)
- print("O" .. string.rep("=", width - 2) .. "0")
- print("|" .. alignString(device_display_name, width - 2) .. "|")
- print(">" .. string.rep("-", width - 2) .. "<")
- return 3
- end
- function drawMenu(draw_lines, s)
- local ret = 1
- local m = selection[s]["m"]
- local mn = #m
- if selection[s]["s"] < selection[s]["o"] then
- selection[s]["o"] = selection[s]["s"]
- elseif selection[s]["s"] >= (selection[s]["o"] + draw_lines) then
- selection[s]["o"] = selection[s]["s"] - (draw_lines - 1)
- end
- local j = selection[s]["o"]
- for i=1, draw_lines do
- if (j <= mn) then
- local t = ""
- if selection[s]["s"] == j then
- t = t .. "[ "
- else
- t = t .. " "
- end
- t = t .. m[j]["txt"]
- if selection[s]["s"] == j then
- t = t .. " ]"
- else
- t = t .. " "
- end
- local v = ""
- if m[j]["val"] ~= nil then
- if m[j]["is_color"] == nil then
- v = tostring(m[j]["val"])
- else
- v = color_names[m[j]["val"]]
- end
- end
- local c = (width - 8) - (m[j]["txt"]:len() + v:len())
- if c > 0 then
- t = t .. string.rep(" ", c)
- end
- t = t .. v
- print("| " .. reduceString(t, width - 4) .. " |")
- ret = ret + 1
- else
- break
- end
- j = j + 1
- end
- return ret - 1
- end
- function footer(drawn_lines, s)
- local c = height - (3 + drawn_lines)
- for i=1, c do
- print("|" .. string.rep(" ", width - 2) .. "|")
- end
- print(">" .. string.rep("-", width - 2) .. "<")
- print("| " .. alignString(selection[s]["m"][selection[s]["s"]]["descr"], width - 4) .. " |")
- write("0" .. string.rep("=", width - 2) .. "0")
- end
- function redraw(s)
- local hc = header()
- footer(hc + drawMenu(height - (hc + 3), s), s)
- end
- function processMessages(s)
- redraw(s)
- local event, key_button, x, y = os.pullEvent()
- if event == "key" then
- if key_button == key_up then
- selectUp(s)
- elseif key_button == key_down then
- selectDown(s)
- elseif key_button == key_left then
- selectLeft(s)
- elseif key_button == key_right then
- selectRight(s)
- elseif key_button == key_enter then
- accept(s)
- end
- elseif event == "mouse_click" then
- y = y - (3 - (selection[s]["o"] - 1))
- if (y >= 1) and (y <= #(selection[s]["m"])) then
- if selection[s]["s"] == y then
- accept(s)
- else
- selection[s]["s"] = y
- end
- end
- elseif event == "mouse_scroll" then
- if key_button < 0 then
- selectUp(s)
- elseif key_button > 0 then
- selectDown(s)
- end
- end
- end
- function runMenu(m)
- selection[m]["r"] = true
- selection[m]["s"] = 1
- selection[m]["o"] = 1
- while selection[m]["r"] do
- processMessages(m)
- end
- end
- function selectUp(s)
- if selection[s]["s"] <= 1 then
- selection[s]["s"] = #(selection[s]["m"])
- else
- selection[s]["s"] = selection[s]["s"] - 1
- end
- end
- function selectDown(s)
- if selection[s]["s"] >= #(selection[s]["m"]) then
- selection[s]["s"] = 1
- else
- selection[s]["s"] = selection[s]["s"] + 1
- end
- end
- function selectLeft(s)
- local f = selection[s]["m"][selection[s]["s"]]["dfunc"]
- if f ~= nil then
- getfenv()[f](true)
- end
- end
- function selectRight(s)
- local f = selection[s]["m"][selection[s]["s"]]["dfunc"]
- if f ~= nil then
- getfenv()[f](false)
- end
- end
- function onOff_Select()
- rednet.send(reactor_id, not menu[1]["val"], "reactor.ctrl.active")
- local sender, msg, prot = rednet.receive("reactor.ack.active", 2)
- if sender == reactor_id then
- menu[1]["val"] = msg
- end
- end
- function onOff_D(left)
- onOff_Select()
- end
- function settings_Select()
- runMenu("settings")
- end
- function ejectWaste_Select()
- rednet.send(reactor_id, true, "reactor.ctrl.eject_waste")
- end
- function ejectFuel_Select()
- rednet.send(reactor_id, true, "reactor.ctrl.eject_fuel")
- end
- function showStats_Select()
- rednet.send(reactor_id, true, "reactor.req.stats")
- local sender, msg, prot = rednet.receive("reactor.ack.stats", 2)
- if sender == reactor_id then
- stats =
- {
- {
- [ "txt" ] = "Back",
- [ "descr" ] = "Back to menu",
- [ "val" ] = nil,
- [ "func" ] = "backStats_Select"
- }
- }
- selection["stats"]["m"] = stats
- menu[1]["val"] = msg["active"]
- table.insert(stats,
- {
- [ "txt" ] = "Active",
- [ "descr" ] = "Active = " .. tostring(msg["active"]),
- [ "val" ] = msg["active"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "Energy",
- [ "descr" ] = "Energy = " .. tostring(msg["energy"]),
- [ "val" ] = msg["energy"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "Fuel temp.",
- [ "descr" ] = "Fuel temp. = " .. tostring(msg["fuel_temp"]),
- [ "val" ] = msg["fuel_temp"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "Casing temp.",
- [ "descr" ] = "Casing temp. = " .. tostring(msg["casing_temp"]),
- [ "val" ] = msg["casing_temp"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "Fuel",
- [ "descr" ] = "Fuel = " .. tostring(msg["fuel"]),
- [ "val" ] = msg["fuel"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "Waste",
- [ "descr" ] = "Waste = " .. tostring(msg["waste"]),
- [ "val" ] = msg["waste"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "Max. fuel",
- [ "descr" ] = "Max. fuel = " .. tostring(msg["fuel_max"]),
- [ "val" ] = msg["fuel_max"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "Energy prod.",
- [ "descr" ] = "Energy prod. = " .. tostring(msg["energy_prod"]),
- [ "val" ] = msg["energy_prod"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "H-Fluid prod.",
- [ "descr" ] = "H-Fluid prod. = " .. tostring(msg["hot_fluid_prod"]),
- [ "val" ] = msg["hot_fluid_prod"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "Coolant type",
- [ "descr" ] = "Coolant type = " .. tostring(msg["coolant_type"]),
- [ "val" ] = msg["coolant_type"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "Coolant",
- [ "descr" ] = "Coolant = " .. tostring(msg["coolant"]),
- [ "val" ] = msg["coolant"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "Max. coolant",
- [ "descr" ] = "Max. coolant = " .. tostring(msg["coolant_max"]),
- [ "val" ] = msg["coolant_max"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "H-Fluid type",
- [ "descr" ] = "H-Fluid type = " .. tostring(msg["hot_fluid_type"]),
- [ "val" ] = msg["hot_fluid_type"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "H-Fluid",
- [ "descr" ] = "H-Fluid = " .. tostring(msg["hot_fluid"]),
- [ "val" ] = msg["hot_fluid"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "Max. H-Fuel",
- [ "descr" ] = "Max. H-Fuel = " .. tostring(msg["hot_fluid_max"]),
- [ "val" ] = msg["hot_fluid_max"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "F-Reactiv.",
- [ "descr" ] = "F-Reactiv. = " .. tostring(msg["fuel_reactivity"]),
- [ "val" ] = msg["fuel_reactivity"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "F-Consumed",
- [ "descr" ] = "F-Consumed = " .. tostring(msg["fuel_consumed"]),
- [ "val" ] = msg["fuel_consumed"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "A-Cooled",
- [ "descr" ] = "A-Cooled = " .. tostring(msg["actively_cooled"]),
- [ "val" ] = msg["actively_cooled"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "Min. Coord",
- [ "descr" ] = "Min. Coord = " .. tostring(msg["min_coord"]),
- [ "val" ] = msg["min_coord"],
- [ "func" ] = nil
- })
- table.insert(stats,
- {
- [ "txt" ] = "Max. Coord",
- [ "descr" ] = "Max. Coord = " .. tostring(msg["max_coord"]),
- [ "val" ] = msg["max_coord"],
- [ "func" ] = nil
- })
- --for i=0, #(msg["ctrl_rods"]) do
- -- table.insert(stats,
- -- {
- -- [ "txt" ] = "Ctrl-Rod" .. tostring(i) .. " name",
- -- [ "descr" ] = "Ctrl-Rod " .. tostring(i) .. " name = " .. msg["ctrl_rods"][i]["name"],
- -- [ "val" ] = msg["ctrl_rods"][i]["name"],
- -- [ "func" ] = nil
- -- })
- -- table.insert(stats,
- -- {
- -- [ "txt" ] = "Ctrl-Rod" .. tostring(i) .. " lvl",
- -- [ "descr" ] = "Ctrl-Rod " .. tostring(i) .. " lvl = " .. tostring(msg["ctrl_rods"][i]["lvl"]),
- -- [ "val" ] = msg["ctrl_rods"][i]["lvl"],
- -- [ "func" ] = nil
- -- })
- --end
- runMenu("stats")
- end
- end
- function exit_Select()
- selection["reactors"]["r"] = false
- end
- function reactor_Select()
- reactor_id = reactors[selection["reactors"]["s"]]["val"]
- rednet.send(reactor_id, true, "reactor.req.stats")
- local sender, msg, prot = rednet.receive("reactor.ack.stats", 2)
- if sender == reactor_id then
- menu[1]["val"] = msg["active"]
- end
- runMenu("menu")
- end
- function back_Select()
- reactor_id = 0
- selection["menu"]["r"] = false
- end
- function settingsBackground_D(left)
- local v = settings[1]["val"]
- if left then
- if v <= 1 then
- v = 16
- else
- v = v - 1
- end
- else
- if v >= 16 then
- v = 1
- else
- v = v + 1
- end
- end
- settings[1]["val"] = v
- end
- function settingsBackground_Select()
- local v = settings[1]["val"]
- if v >= 16 then
- v = 1
- else
- v = v + 1
- end
- settings[1]["val"] = v
- end
- function settingsText_D(left)
- local v = settings[2]["val"]
- if left then
- if v <= 1 then
- v = 16
- else
- v = v - 1
- end
- else
- if v >= 16 then
- v = 1
- else
- v = v + 1
- end
- end
- settings[2]["val"] = v
- end
- function settingsText_Select()
- local v = settings[2]["val"]
- if v >= 16 then
- v = 1
- else
- v = v + 1
- end
- settings[2]["val"] = v
- end
- function settingsBack_Select()
- selection["settings"]["r"] = false
- end
- function backStats_Select()
- selection["stats"]["r"] = false
- end
- function accept(s)
- local f = selection[s]["m"][selection[s]["s"]]["func"]
- if f ~= nil then
- getfenv()[f]()
- end
- end
- rednet.open("back")
- refreshReactors()
- runMenu("reactors")
- rednet.close("back")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment