Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Real-Time Base Monitor
- -- Shows day/night status and real-time base creation timer with seconds
- -- Configuration
- local monitor = peripheral.find("monitor") -- Automatically find the monitor
- local saveFile = "/disk/base_creation_real.txt"
- -- Color palette
- local colors = {
- blue = 2048, -- Blue
- green = 8, -- Green
- yellow = 16, -- Yellow
- white = 1, -- White
- black = 32768 -- Black
- }
- -- Load or create base creation time (using real-world time)
- local baseCreationTime
- if fs.exists(saveFile) then
- local file = fs.open(saveFile, "r")
- baseCreationTime = tonumber(file.readLine())
- file.close()
- print("Loaded base creation timestamp: " .. baseCreationTime)
- else
- baseCreationTime = os.epoch("utc") / 1000 -- Current real-world time in seconds
- local file = fs.open(saveFile, "w")
- file.writeLine(tostring(baseCreationTime))
- file.close()
- print("Created new base creation timestamp: " .. baseCreationTime)
- end
- -- Connect to the monitor
- if not monitor then
- print("ERROR: No monitor found!")
- return
- end
- -- Set up the monitor
- monitor.setTextScale(1)
- monitor.clear()
- -- Function to get day/night status
- local function getDayNightStatus()
- local time = os.time("ingame")
- if time >= 0 and time < 12000 then
- return "Day"
- else
- return "Night"
- end
- end
- -- Function to calculate time since base creation (in real-world time)
- local function getBaseAge()
- local currentTime = os.epoch("utc") / 1000 -- Current time in seconds
- local timeDiff = currentTime - baseCreationTime -- Difference in seconds
- -- Convert to days, hours, minutes, and seconds
- local days = math.floor(timeDiff / (24 * 60 * 60)) -- Seconds in a day
- local remainingSeconds = timeDiff % (24 * 60 * 60)
- local hours = math.floor(remainingSeconds / (60 * 60)) -- Seconds in an hour
- remainingSeconds = remainingSeconds % (60 * 60)
- local minutes = math.floor(remainingSeconds / 60) -- Seconds in a minute
- local seconds = math.floor(remainingSeconds % 60)
- return days, hours, minutes, seconds
- end
- -- Function to draw a simple header
- local function drawHeader()
- local width, height = monitor.getSize()
- -- Draw header background
- monitor.setBackgroundColor(colors.blue)
- monitor.setTextColor(colors.white)
- for x = 1, width do
- monitor.setCursorPos(x, 1)
- monitor.write(" ")
- end
- -- Write title
- local title = "Welcome to the Base!"
- local titleX = math.floor((width - #title) / 2)
- monitor.setCursorPos(titleX, 1)
- monitor.write(title)
- -- Reset colors
- monitor.setBackgroundColor(colors.black)
- end
- -- Main loop with error handling
- while true do
- local status, err = pcall(function()
- -- Clear the monitor
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- -- Draw header
- drawHeader()
- -- Get current status
- local dayNightStatus = getDayNightStatus()
- -- Calculate base age
- local days, hours, minutes, seconds = getBaseAge()
- -- Get monitor size
- local width, height = monitor.getSize()
- -- Display welcome message
- monitor.setTextColor(colors.green)
- local welcomeMsg = "Hope you're having a great day!"
- local welcomeX = math.floor((width - #welcomeMsg) / 2)
- monitor.setCursorPos(welcomeX, 3)
- monitor.write(welcomeMsg)
- -- Display day/night status
- monitor.setTextColor(colors.yellow)
- monitor.setCursorPos(2, 5)
- monitor.write("Current time: ")
- monitor.setTextColor(colors.white)
- monitor.write(dayNightStatus)
- -- Display base creation
- monitor.setTextColor(colors.blue)
- monitor.setCursorPos(2, 7)
- monitor.write("Base created:")
- monitor.setTextColor(colors.white)
- -- Format time with leading zeros for minutes and seconds
- local timeStr = string.format("%d days, %d hours, %02d minutes, %02d seconds ago",
- days, hours, minutes, seconds)
- -- Split into two lines if needed
- if #timeStr > width - 2 then
- monitor.setCursorPos(2, 8)
- monitor.write(string.format("%d days, %d hours,", days, hours))
- monitor.setCursorPos(2, 9)
- monitor.write(string.format("%02d minutes, %02d seconds ago", minutes, seconds))
- else
- monitor.setCursorPos(2, 8)
- monitor.write(timeStr)
- end
- -- Add real-world time note
- monitor.setTextColor(colors.green)
- monitor.setCursorPos(2, 10)
- monitor.write("(real-world time)")
- end)
- -- If there was an error, display it on the monitor
- if not status then
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- monitor.setTextColor(colors.yellow)
- monitor.setCursorPos(1, 1)
- monitor.write("Error:")
- monitor.setCursorPos(1, 2)
- monitor.write(tostring(err))
- print("Error: " .. tostring(err))
- end
- -- Update every second to ensure seconds update properly
- sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement