Advertisement
alexnolan

atm10

Mar 26th, 2025 (edited)
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.91 KB | None | 0 0
  1. -- Real-Time Base Monitor
  2. -- Shows day/night status and real-time base creation timer with seconds
  3.  
  4. -- Configuration
  5. local monitor = peripheral.find("monitor") -- Automatically find the monitor
  6. local saveFile = "/disk/base_creation_real.txt"
  7.  
  8. -- Color palette
  9. local colors = {
  10.   blue = 2048,    -- Blue
  11.   green = 8,      -- Green
  12.   yellow = 16,    -- Yellow
  13.   white = 1,      -- White
  14.   black = 32768   -- Black
  15. }
  16.  
  17. -- Load or create base creation time (using real-world time)
  18. local baseCreationTime
  19. if fs.exists(saveFile) then
  20.   local file = fs.open(saveFile, "r")
  21.   baseCreationTime = tonumber(file.readLine())
  22.   file.close()
  23.   print("Loaded base creation timestamp: " .. baseCreationTime)
  24. else
  25.   baseCreationTime = os.epoch("utc") / 1000 -- Current real-world time in seconds
  26.   local file = fs.open(saveFile, "w")
  27.   file.writeLine(tostring(baseCreationTime))
  28.   file.close()
  29.   print("Created new base creation timestamp: " .. baseCreationTime)
  30. end
  31.  
  32. -- Connect to the monitor
  33. if not monitor then
  34.   print("ERROR: No monitor found!")
  35.   return
  36. end
  37.  
  38. -- Set up the monitor
  39. monitor.setTextScale(1)
  40. monitor.clear()
  41.  
  42. -- Function to get day/night status
  43. local function getDayNightStatus()
  44.   local time = os.time("ingame")
  45.   if time >= 0 and time < 12000 then
  46.     return "Day"
  47.   else
  48.     return "Night"
  49.   end
  50. end
  51.  
  52. -- Function to calculate time since base creation (in real-world time)
  53. local function getBaseAge()
  54.   local currentTime = os.epoch("utc") / 1000 -- Current time in seconds
  55.   local timeDiff = currentTime - baseCreationTime -- Difference in seconds
  56.  
  57.   -- Convert to days, hours, minutes, and seconds
  58.   local days = math.floor(timeDiff / (24 * 60 * 60)) -- Seconds in a day
  59.   local remainingSeconds = timeDiff % (24 * 60 * 60)
  60.  
  61.   local hours = math.floor(remainingSeconds / (60 * 60)) -- Seconds in an hour
  62.   remainingSeconds = remainingSeconds % (60 * 60)
  63.  
  64.   local minutes = math.floor(remainingSeconds / 60) -- Seconds in a minute
  65.   local seconds = math.floor(remainingSeconds % 60)
  66.  
  67.   return days, hours, minutes, seconds
  68. end
  69.  
  70. -- Function to draw a simple header
  71. local function drawHeader()
  72.   local width, height = monitor.getSize()
  73.  
  74.   -- Draw header background
  75.   monitor.setBackgroundColor(colors.blue)
  76.   monitor.setTextColor(colors.white)
  77.  
  78.   for x = 1, width do
  79.     monitor.setCursorPos(x, 1)
  80.     monitor.write(" ")
  81.   end
  82.  
  83.   -- Write title
  84.   local title = "Welcome to the Base!"
  85.   local titleX = math.floor((width - #title) / 2)
  86.   monitor.setCursorPos(titleX, 1)
  87.   monitor.write(title)
  88.  
  89.   -- Reset colors
  90.   monitor.setBackgroundColor(colors.black)
  91. end
  92.  
  93. -- Main loop with error handling
  94. while true do
  95.   local status, err = pcall(function()
  96.     -- Clear the monitor
  97.     monitor.setBackgroundColor(colors.black)
  98.     monitor.clear()
  99.    
  100.     -- Draw header
  101.     drawHeader()
  102.    
  103.     -- Get current status
  104.     local dayNightStatus = getDayNightStatus()
  105.    
  106.     -- Calculate base age
  107.     local days, hours, minutes, seconds = getBaseAge()
  108.    
  109.     -- Get monitor size
  110.     local width, height = monitor.getSize()
  111.    
  112.     -- Display welcome message
  113.     monitor.setTextColor(colors.green)
  114.     local welcomeMsg = "Hope you're having a great day!"
  115.     local welcomeX = math.floor((width - #welcomeMsg) / 2)
  116.     monitor.setCursorPos(welcomeX, 3)
  117.     monitor.write(welcomeMsg)
  118.    
  119.     -- Display day/night status
  120.     monitor.setTextColor(colors.yellow)
  121.     monitor.setCursorPos(2, 5)
  122.     monitor.write("Current time: ")
  123.     monitor.setTextColor(colors.white)
  124.     monitor.write(dayNightStatus)
  125.    
  126.     -- Display base creation
  127.     monitor.setTextColor(colors.blue)
  128.     monitor.setCursorPos(2, 7)
  129.     monitor.write("Base created:")
  130.     monitor.setTextColor(colors.white)
  131.    
  132.     -- Format time with leading zeros for minutes and seconds
  133.     local timeStr = string.format("%d days, %d hours, %02d minutes, %02d seconds ago",
  134.                                  days, hours, minutes, seconds)
  135.    
  136.     -- Split into two lines if needed
  137.     if #timeStr > width - 2 then
  138.       monitor.setCursorPos(2, 8)
  139.       monitor.write(string.format("%d days, %d hours,", days, hours))
  140.       monitor.setCursorPos(2, 9)
  141.       monitor.write(string.format("%02d minutes, %02d seconds ago", minutes, seconds))
  142.     else
  143.       monitor.setCursorPos(2, 8)
  144.       monitor.write(timeStr)
  145.     end
  146.    
  147.     -- Add real-world time note
  148.     monitor.setTextColor(colors.green)
  149.     monitor.setCursorPos(2, 10)
  150.     monitor.write("(real-world time)")
  151.   end)
  152.  
  153.   -- If there was an error, display it on the monitor
  154.   if not status then
  155.     monitor.setBackgroundColor(colors.black)
  156.     monitor.clear()
  157.     monitor.setTextColor(colors.yellow)
  158.     monitor.setCursorPos(1, 1)
  159.     monitor.write("Error:")
  160.     monitor.setCursorPos(1, 2)
  161.     monitor.write(tostring(err))
  162.     print("Error: " .. tostring(err))
  163.   end
  164.  
  165.   -- Update every second to ensure seconds update properly
  166.   sleep(1)
  167. end
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement