Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------------------------------------------------------------
- -- INITIALIZATION
- ----------------------------------------------------------------------------
- -- Initialize Monitor
- local monitor = peripheral.find("monitor")
- if not monitor then error("Monitor not found.") end
- monitor.setTextScale(0.5)
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- monitor.setCursorPos(1, 1)
- monitor.setCursorBlink(false)
- print("Monitor initialized.")
- -- Open a Rednet connection
- rednet.open("left")
- ----------------------------------------------------------------------------
- -- FUNCTIONS
- ----------------------------------------------------------------------------
- -- Table.Empty
- function table.empty (self)
- for _, _ in pairs(self) do
- return false
- end
- return true
- end
- -- Prints strings left, centered, or right justified at a specific row and
- -- specific foreground/background color. Really cool
- function mPrintRowJustified(mon, y, pos, text, ...)
- local w, h = mon.getSize()
- local fg = mon.getTextColor()
- local bg = mon.getBackgroundColor()
- local x
- if pos == "left" then
- x = 1
- elseif pos == "center" then
- x = math.floor((w - #text) / 2)
- elseif pos == "right" then
- x = w - #text
- end
- if select("#", ...) > 0 then
- mon.setTextColor(select(1, ...))
- end
- if select("#", ...) > 1 then
- mon.setBackgroundColor(select(2, ...))
- else
- mon.setBackgroundColor(colors.black)
- end
- mon.setCursorPos(x, y)
- mon.write(text)
- mon.setTextColor(fg)
- end
- -- Function to update the time display on the monitor
- function displayTimer(mon, t)
- now = os.time()
- cycle = "Day"
- cycle_Col = colors.orange
- if now >= 4 and now < 6 then
- cycle = "Sunrise"
- cycle_Col = colors.orange
- elseif now >= 6 and now < 18 then
- cycle = "Day"
- cycle_Col = colors.yellow
- elseif now >= 18 and now < 19.5 then
- cycle = "Sunset"
- cycle_Col = colors.orange
- elseif now >= 19.5 or now < 5 then
- cycle = "Night"
- cycle_Col = colors.blue
- end
- timer_Col = colors.orange
- if t < 15 then timer_Col = colors.yellow end
- if t < 5 then timer_Col = colors.red end
- mPrintRowJustified(mon, 1, "left", string.format("Time: %s [%s] ", textutils.formatTime(now, false), cycle), cycle_Col)
- if cycle ~= "Night" then mPrintRowJustified(mon, 1, "right", string.format(" Remaining: %ss", t), timer_Col)
- else mPrintRowJustified(mon, 1, "right", " Remaining: PAUSED", colors.red) end
- end
- -- Draws a line with coloured whitespaces (lol)
- function drawLine (mon, x, y, length, size, color_Bar)
- for yPos = y, y + size - 1 do
- mon.setBackgroundColor(color_Bar)
- mon.setCursorPos(x, yPos)
- mon.write(string.rep(" ", length))
- end
- end
- -- Draws a progress bar for displaying happiness and other stuff
- function drawProgressBar (mon, x, y, length, size, min_Val, max_Val, color_Bar, color_Bg)
- drawLine(mon, x, y, length, size, color_Bg)
- local bar_Size = math.floor((min_Val/max_Val) * length)
- drawLine(mon, x, y, bar_Size, size, color_Bar)
- end
- -- Function to receive and display basic Colony Info
- function displayColonyBasic (mon)
- local senderId, message, protocol = rednet.receive("Colony ID Protocol")
- col_ID = message
- mPrintRowJustified(mon, 3, "left", string.format("Colony ID: %s ", col_ID), colors.white)
- senderId, message, protocol = rednet.receive("Colony Name Protocol")
- col_Name = message
- mPrintRowJustified(mon, 3, "center", string.format(" %s ", col_Name), colors.white)
- senderId, message, protocol = rednet.receive("Colony Citizen Count Protocol")
- col_Citizen_Count = message
- mPrintRowJustified(mon, 3, "right", string.format("Citizens: %s", col_Citizen_Count), colors.white)
- senderId, message, protocol = rednet.receive("Colony Under Attack Protocol")
- col_isUnderAttack = message
- -- For Happiness Bar we need the monitor size
- local w, h = mon.getSize()
- senderId, message, protocol = rednet.receive("Colony Happiness Protocol")
- col_Happiness = message
- if not col_isUnderAttack then
- happiness_Col = colors.green
- if col_Happiness >= 0 and col_Happiness < 3 then
- happiness_Col = colors.red
- elseif col_Happiness >= 3 and col_Happiness < 6 then
- happiness_Col = colors.yellow
- elseif col_Happiness >= 6 then
- happiness_Col = colors.green
- end
- drawProgressBar(mon, 5, 6, w - 8, 1, col_Happiness, 10, happiness_Col, colors.gray)
- mPrintRowJustified(mon, 5, "center", string.format("Happiness: %s / 10", math.floor(col_Happiness)), colors.white)
- else
- drawProgressBar(mon, 5, 6, w - 8, 1, 10, 10, colors.red, colors.gray)
- mPrintRowJustified(mon, 5, "center", "Colony Under Attack!", colors.white)
- end
- end
- --[[
- Create Colonist Data
- @desc Build a table of Colonist making the request
- @return table
- ]]
- function createColonistData(colonist)
- title_words = {}
- words_in_name = 0
- colonist_job = ""
- word_count = 1
- for word in colonist:gmatch("%S+") do
- table.insert(title_words, word)
- words_in_name = words_in_name + 1
- end
- if words_in_name >= 3 then colonist_name = title_words[words_in_name-2] .. " " .. title_words[words_in_name]
- else colonist_name = colonist end
- repeat
- if colonist_job ~= "" then colonist_job = colonist_job .. " " end
- colonist_job = colonist_job .. title_words[word_count]
- word_count = word_count + 1
- until word_count > words_in_name - 3
- return { fullName = colonist, titleWords = title_words, job = colonist_job, name = colonist_name, wordsInName = words_in_name }
- end
- --[[
- Get Work Request List (from colony)
- @desc Build a table of the work request data from the colony
- @return table
- ]]
- function getWorkRequestList()
- requestList = {}
- local senderId, message, protocol = rednet.receive("Colony Requests Protocol")
- col_Requests = message
- workRequests = col_Requests
- for w in pairs(workRequests) do
- name = workRequests[w].name -- the name of the count/item being requested
- colonist = createColonistData(workRequests[w].target)
- desc = workRequests[w].desc -- the request description
- item = {}
- -- create the filter item for the transfer request through the bridge
- if workRequests[w].items and workRequests[w].items[1] then
- if not workRequests[w].items[1].nbt or table.empty(workRequests[w].items[1].nbt) then
- item = { name = workRequests[w].items[1].name, count = workRequests[w].count, displayName = workRequests[w].items[1].displayName}
- else
- item = { name = workRequests[w].items[1].name, count = workRequests[w].count, displayName = workRequests[w].items[1].displayName, nbt = workRequests[w].items[1].nbt}
- end
- end
- -- how many items are needed to fulfill this request?
- needed = workRequests[w].count
- local newRecord = {}
- newRecord.name = name
- newRecord.desc = desc
- newRecord.needed = needed
- newRecord.item = item
- newRecord.colonist = colonist
- table.insert(requestList, newRecord)
- end
- return requestList
- end
- --[[
- Display List
- @desc Update the monitor with the work request items currently in the system
- @return void
- ]]
- function displayList(mon, listName, itemList)
- -- show the list header first
- mPrintRowJustified(mon, row, "center", listName, colors.white)
- row = row + 1
- for e in pairs(itemList) do
- record = itemList[e]
- text = string.format("%s", record.name)
- mPrintRowJustified(mon, row, "left", text, record.color)
- mPrintRowJustified(mon, row, "right", " " .. record.colonist, record.color)
- row = row + 1
- end
- -- add a space at the end of the list
- row = row + 1
- end
- function scanWorkRequests(mon, bridge, direction)
- builder_list = {}
- nonbuilder_list = {}
- equipment_list = {}
- requestList = getWorkRequestList()
- for j, data in ipairs(requestList) do
- color = colors.blue
- provided = 0
- -- ---------------------------------------------------------------------
- -- Build the newList data
- -- ---------------------------------------------------------------------
- -- create the target text
- expectedList = "Builder"
- colonist = data.colonist.name
- if not string.find(data.colonist.fullName, "Builder") then
- expectedList = ""
- colonist = data.colonist.job .. " " .. data.colonist.name
- if data.colonist.wordsInName < 3 then
- colonist = data.colonist.name
- end
- end
- -- create the name
- listName = data.name
- if string.find(data.desc, "level") then
- expectedList = "Equipment"
- level = "Any Level"
- if string.find(data.desc, "with maximal level: Leather") then level = "Leather" end
- if string.find(data.desc, "with maximal level: Gold") then level = "Gold" end
- if string.find(data.desc, "with maximal level: Chain") then level = "Chain" end
- if string.find(data.desc, "with maximal level: Wood or Gold") then level = "Wood or Gold" end
- if string.find(data.desc, "with maximal level: Stone") then level = "Stone" end
- if string.find(data.desc, "with maximal level: Iron") then level = "Iron" end
- if string.find(data.desc, "with maximal level: Diamond") then level = "Diamond" end
- listName = level .. " " .. data.name
- if level == "Any Level" then listName = data.name .. " of any level" end
- end
- -- create the new list table defining what is inserted into a specific list
- newList = { name=listName, colonist=colonist, needed=data.needed, provided=provided, color=color}
- if expectedList == "Equipment" then
- table.insert(equipment_list, newList)
- elseif expectedList == "Builder" then
- table.insert(builder_list, newList)
- else
- table.insert(nonbuilder_list, newList)
- end
- -- ---------------------------------------------------------------------
- end
- -- Show the various lists on the attached monitor.
- mon.clear()
- row = 8
- if not table.empty(builder_list) then displayList(mon, "Builder Requests", builder_list) end
- if not table.empty(nonbuilder_list) then displayList(mon, "Nonbuilder Requests", nonbuilder_list) end
- if not table.empty(equipment_list) then displayList(mon, "Equipment", equipment_list) end
- -- no requests
- if row == 8 then
- mPrintRowJustified(mon, row, "center", "No Open Requests", colors.white)
- end
- end
- ----------------------------------------------------------------------------
- -- MAIN
- ----------------------------------------------------------------------------
- -- Scan for requests periodically. This will catch any updates that were
- -- triggered from the previous scan. Right-clicking on the monitor will
- -- trigger an immediate scan and reset the timer.
- local time_between_runs = 30
- local current_Run = time_between_runs
- monitor.clear()
- scanWorkRequests(monitor)
- displayColonyBasic(monitor)
- displayTimer(monitor, current_Run)
- local TIMER = os.startTimer(1)
- while true do
- local e = {os.pullEvent()}
- if e[1] == "timer" and e[2] == TIMER then
- now = os.time()
- if now >= 6 and now < 19.5 then
- current_Run = current_Run - 1
- if current_Run <= 0 then
- monitor.clear()
- scanWorkRequests(monitor)
- displayColonyBasic(monitor)
- current_Run = time_between_runs
- end
- end
- displayTimer(monitor, current_Run)
- TIMER = os.startTimer(1)
- elseif e[1] == "monitor_touch" then
- os.cancelTimer(TIMER)
- monitor.clear()
- scanWorkRequests(monitor)
- current_Run = time_between_runs
- displayColonyBasic(monitor)
- displayTimer(monitor, current_Run)
- TIMER = os.startTimer(1)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement