-- clock.lua -- (c) 2025 eightyfivenine -- repo: https://github.com/user8595/cc-scripts local hour, min = math.modf(os.time()) local minDisp = 0 local tW, tH = term.getSize() local loopFunc = true -- bg objects local bgState = 1 -- 1-7 local sunYOff, moonYOff = 0, 0 local mBgCol = colors.lightGray local starObj = {} local timeMode = 1 -- 1 = game, 2 = utc local switchFunc = true local textInfo = {} local headerCol, headerText term.clear() term.setBackgroundColor(0x8000) for i = 1, 10, 1 do table.insert(starObj, { x = math.random(1, tW), y = math.random(1, tH - 3), id = math.random(0, 1) -- 0 = blue, 1 = yellow }) end table.insert(textInfo, { "r to shuffle stars, q to close", 0, 1 }) function tableClear(t) for k in pairs(t) do t[k] = nil end end function bgRender() paintutils.drawFilledBox(0, 0, tW, tH, colors.black) if bgState == 1 then sunYOff = tH + 2 if tW > 29 and tH > 12 then moonYOff = 0 else moonYOff = tH - 13 end mBgCol = colors.lightGray for _, star in ipairs(starObj) do if star.id == 1 then paintutils.drawPixel(star.x, star.y, colors.blue) else paintutils.drawPixel(star.x, star.y, colors.yellow) end end end if bgState == 2 then if tW > 29 and tH > 12 then sunYOff = tH - 12 moonYOff = tH - 8 else sunYOff = tH - 9 moonYOff = tH - 6 end mBgCol = colors.gray for _, star in ipairs(starObj) do paintutils.drawPixel(star.x, star.y, colors.gray) end end if bgState == 3 then if tW > 29 and tH > 12 then sunYOff = tH - 15 else sunYOff = tH - 11 end moonYOff = tH - 32 mBgCol = colors.gray end if bgState == 4 then sunYOff = tH - 13 moonYOff = tH - 32 mBgCol = colors.gray end if bgState == 5 then if tW > 29 and tH > 12 then sunYOff = tH - 14 else sunYOff = tH - 12 end moonYOff = tH - 4 mBgCol = colors.gray end if bgState == 6 then if tW > 29 and tH > 12 then sunYOff = tH - 8 moonYOff = tH - 11 else sunYOff = tH - 6 moonYOff = tH - 9 end mBgCol = colors.gray for _, star in ipairs(starObj) do paintutils.drawPixel(star.x, star.y, colors.gray) end end if bgState == 7 then if tW > 29 and tH > 12 then sunYOff = tH + 2 moonYOff = tH - 16 else sunYOff = tH + 2 moonYOff = tH - 13 end mBgCol = colors.lightGray for _, star in ipairs(starObj) do if star.id == 1 then paintutils.drawPixel(star.x, star.y, colors.blue) else paintutils.drawPixel(star.x, star.y, colors.yellow) end end end -- offset by 0, then 0 = 1 if tW > 29 and tH > 12 then for i = 0, 5, 1 do paintutils.drawLine(1 * i + 1 + 2, tH - 3 - 1 * (i - 1), 1 * i + 1 + (10 - (i * 2)) + 2, tH - 3 - 1 * (i - 1), colors.green) end for i = 0, 8, 1 do paintutils.drawLine(1 * i + 1 + 6 + 2, tH - 3 - 1 * (i - 1), 1 * i + 1 + (16 - (i * 2)) + 6 + 2, tH - 3 - 1 * (i - 1), colors.brown) end else for i = 0, 2, 1 do paintutils.drawLine(1 * i + 1 + 2, tH - 3 - 1 * (i - 1), 1 * i + 1 + (4 - (i * 2)) + 2, tH - 3 - 1 * (i - 1), colors.green) end for i = 0, 3, 1 do paintutils.drawLine(1 * i + 1 + 3 + 2, tH - 3 - 1 * (i - 1), 1 * i + 1 + (6 - (i * 2)) + 3 + 2, tH - 3 - 1 * (i - 1), colors.brown) end end paintutils.drawFilledBox(tW - 9, 3 + moonYOff, tW - 3, 8 + moonYOff, mBgCol) paintutils.drawFilledBox(tW - 10, 4 + moonYOff, tW - 2, 7 + moonYOff, mBgCol) paintutils.drawPixel(tW - 4, 4 + moonYOff, colors.gray) paintutils.drawPixel(tW - 8, 5 + moonYOff, colors.gray) paintutils.drawPixel(tW - 5, 7 + moonYOff, colors.gray) paintutils.drawFilledBox(tW - 9, 3 + sunYOff, tW - 3, 8 + sunYOff, colors.yellow) paintutils.drawFilledBox(tW - 10, 4 + sunYOff, tW - 2, 7 + sunYOff, colors.yellow) end function displayRender() paintutils.drawLine(0, tH, tW, tH, colors.gray) paintutils.drawLine(tW - 8, tH, tW, tH, colors.red) paintutils.drawFilledBox(0, tH - 1, tW, tH - 1, headerCol) term.setCursorPos(2, tH - 1) term.write(headerText) term.setCursorPos(tW - 6, tH) term.setBackgroundColor(colors.red) term.write(string.format("%02d", math.floor(hour)) .. ":" .. string.format("%02d", math.floor(minDisp))) term.setCursorPos(0, 2) term.setBackgroundColor(colors.red) term.write(os.date(" %d/%m/%Y ")) for i, text in ipairs(textInfo) do term.setCursorPos(1, 1 + (i - 1)) if text[3] ~= 3 then if timeMode == 1 then term.setBackgroundColor(colors.green) else term.setBackgroundColor(colors.blue) end else term.setBackgroundColor(colors.orange) end term.write(text[1]) end end function textUpdate() if timeMode == 1 then hour, min = math.modf(os.time()) else hour, min = math.modf(os.time("local")) end minDisp = min * 60 for i, text in ipairs(textInfo) do text[2] = text[2] + 0.05 if text[2] > 2 then table.remove(textInfo, i) end end if hour >= 0 and hour < 5 then headerCol = colors.blue headerText = "It is midnight now" bgState = 1 end if hour >= 5 and hour < 6 then headerCol = colors.yellow headerText = "It is dawn before sunrise" bgState = 2 end if hour >= 6 and hour < 12 then headerCol = colors.orange headerText = "It is morning now" bgState = 3 end if hour >= 12 and hour < 15 then headerCol = colors.yellow headerText = "It is day now" bgState = 4 end if hour >= 15 and hour < 18 then headerCol = colors.orange headerText = "It is afternoon now" bgState = 5 end if hour >= 18 and hour < 19 then headerCol = colors.lightBlue headerText = "It is dusk now" bgState = 6 end if hour >= 19 and hour < 24 then headerCol = colors.lightBlue headerText = "It is night now" bgState = 7 end end function keyFunc() while true do local _, key = os.pullEvent("key") if key == keys.q then loopFunc = false switchFunc = false break end if key == keys.r then if hour >= 19 or hour >= 0 and hour <= 6 then tableClear(textInfo) table.insert(textInfo, { "randomized stars", 0, 3 }) end for _, star in ipairs(starObj) do star.x = math.random(1, tW) star.y = math.random(1, tH - 3) star.id = math.random(0, 1) end end end end function tModeSwitch() while switchFunc do local eventData = { os.pullEvent() } local event = eventData[1] if event == "mouse_click" then timeMode = timeMode + 1 tableClear(textInfo) -- on [2], 2 = 1, 3 = 2, and so on if timeMode == 3 then table.insert(textInfo, { "game time mode", 0, 2 }) end if timeMode == 2 then table.insert(textInfo, { "real time mode", 0, 1 }) end if timeMode > 2 then timeMode = 1 end if timeMode < 1 then timeMode = 2 end end end end function mainUpdate() while loopFunc do sleep(0.05) tW, tH = term.getSize() term.setBackgroundColor(0x8000) -- term.setCursorPos(2, 2) bgRender() displayRender() textUpdate() end return end parallel.waitForAll(mainUpdate, keyFunc, tModeSwitch) term.setCursorPos(1, 1) term.clear() term.setBackgroundColor(0x8000) term.clear() term.setBackgroundColor(0x8000)