SirSheepe

moon

Jan 10th, 2021 (edited)
662
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. -- 26 x 20
  4.  
  5. local cobalt = dofile("cobalt/init.lua")
  6. local graph = cobalt.graphics
  7.  
  8. local str = ""
  9. local floor, max, min, sqrt = math.floor, math.max, math.min, math.sqrt
  10.  
  11. local t = os.time()
  12. local h = math.floor(t)
  13. local mt = (t - h) * 60
  14. local m = math.floor(mt)
  15.  
  16. local t0 = 0
  17.  
  18. local stars = {}
  19. local starCount = 20
  20. local debug = ""
  21.  
  22. local phases = {[0] = "Full Moon", "Waning Gibbous", "Third Quarter", "Waning Crescent", "New Moon", "Waxing Crescent", "First Quarter", "Waxing Gibbous"}
  23.  
  24. local constel = {
  25.     ["Discidia"] = {
  26.         type = "Offense",
  27.         light = 2,
  28.         days = {0, 1, 2, 3, 7}
  29.     },
  30.     ["Evorsio"] = {
  31.         type = "Destruction",
  32.         light = 2,
  33.         days = {3, 4, 5, 6, 7}
  34.     },
  35.     ["Armara"] = {
  36.         type = "Defense",
  37.         light = 2,
  38.         days = {0, 4, 5, 6, 7}
  39.     },
  40.     ["Aevitas"] = {
  41.         type = "Creation",
  42.         light = 2,
  43.         days = {0, 1, 4, 5, 6, 7}
  44.     },
  45.     ["Vicio"] = {
  46.         type = "Motion",
  47.         light = 2,
  48.         days = {0, 4, 5, 6, 7}
  49.     },
  50.     ["Pelotrio"] = {
  51.         type = "Convocation",
  52.         light = 1,
  53.         days = {}
  54.     },
  55.     ["Lucerna"] = {
  56.         type = "Light",
  57.         light = 1,
  58.         days = {0, 1, 2, 3, 4}
  59.     },
  60.     ["Mineralis"] = {
  61.         type = "Mineral",
  62.         light = 1,
  63.         days = {3, 4, 5, 6, 7}
  64.     },
  65.     ["Fornax"] = {
  66.         type = "Heat",
  67.         light = 1,
  68.         days = {1, 2, 3, 4, 5}
  69.     },
  70.     ["Bootes"] = {
  71.         type = "Herding",
  72.         light = 1,
  73.         days = {0, 1, 2, 3, 4}
  74.     },
  75.     ["Octans"] = {
  76.         type = "Light",
  77.         light = 1,
  78.         days = {3, 4, 5, 6, 7}
  79.     },
  80.     ["Ulteria"] = {
  81.         type = "???",
  82.         light = 0,
  83.         days = {}
  84.     },
  85.     ["Vorux"] = {
  86.         type = "???",
  87.         light = 0,
  88.         days = {}
  89.     },
  90.     ["Gelu"] = {
  91.         type = "???",
  92.         light = 0,
  93.         days = {}
  94.     },
  95.     ["Alcara"] = {
  96.         type = "???",
  97.         light = 0,
  98.         days = {}
  99.     },
  100.     ["Horologium"] = {
  101.         type = "???",
  102.         light = 0,
  103.         days = {}
  104.     }
  105. }
  106.  
  107. local function contains(t, i)
  108.     for j = 1, #t do
  109.         if t[j] == i then
  110.             return true
  111.         end
  112.     end
  113.     return false
  114. end
  115.  
  116. local function getConstellations(phase)
  117.     local out = {}
  118.     local names = {}
  119.     for name, t in pairs(constel) do
  120.         if contains(t.days, phase) then
  121.             out[name] = t
  122.             names[#names + 1] = name
  123.         end
  124.     end
  125.     return out, names
  126. end
  127.  
  128. function cobalt.load()
  129.     for i = 1, starCount do
  130.         stars[i] = {
  131.             (20 + 8 * math.random()) % 24,
  132.             7 + 8 * math.random(),
  133.             7 + 8 * math.random()
  134.         }
  135.     end
  136. end
  137.  
  138. function cobalt.update()
  139.     t = os.time()
  140.     h = math.floor(t)
  141.     mt = (t - h) * 60
  142.     m = math.floor(mt)
  143.  
  144.     t0 = (t0 + 0.1) % 24
  145. end
  146.  
  147. local function star(t, targetAngle, rx, ry)
  148.     local st = -((targetAngle - t) / 12 + 0.5) * math.pi
  149.     local stx = math.floor(13 + rx * math.cos(st) + 0.5)
  150.     local sty = math.floor(10 + ry * math.sin(st) + 0.5)
  151.     if sty <= 5 then
  152.         graph.setBackgroundColor("brown")
  153.         graph.setColor("white")
  154.         graph.print(".", stx, sty)
  155.     end
  156. end
  157.  
  158. local function rect(x, y, w, h)
  159.     for i = x, x + w - 1 do
  160.         for j = y, y + h - 1 do
  161.             graph.pixel(i, j)
  162.         end
  163.     end
  164. end
  165.  
  166. local function mainScreen()
  167.     term.setPaletteColour(colors.yellow, 0xfffa91)
  168.     term.setPaletteColour(colors.gray, 0x333333)
  169.     term.setPaletteColour(colors.lightGray, 0x3d3d3d)
  170.     term.setPaletteColour(colors.lightBlue, 0x86b2ff)
  171.     term.setPaletteColour(colors.orange, 0xf2ac29)
  172.     term.setPaletteColour(colors.brown, 0x151b40)
  173.     term.setPaletteColour(colors.pink, 0xe9eff5)
  174.     term.setPaletteColour(colors.purple, 0xd1d1d1)
  175.     term.setPaletteColour(colors.magenta, 0x9c9c9c)
  176.  
  177.     graph.setBackgroundColor("gray")
  178.     graph.setColor("brown")
  179.     graph.rect("fill", 0, 0, 25, 20)
  180.  
  181.     local scale = 1
  182.     local t = (os.time() * scale) % 24
  183.  
  184.     local eS = 18.5
  185.     local eE = 19.75
  186.  
  187.     local dS = 4.75
  188.     local dE = 6
  189.  
  190.     for i = 1, starCount do
  191.         star(t, stars[i][1], stars[i][2], stars[i][3])
  192.     end
  193.  
  194.     for i = -12, 13 do
  195.         for j = 0, 12 do
  196.             local a = math.atan2(j, i)
  197.             local duskMinA = ((eS - t) / 12 + 0.5) * math.pi
  198.             local duskMaxA = ((eE - t) / 12 + 0.5) * math.pi
  199.             local dawnMinA = ((dS - t) / 12 + 0.5) * math.pi
  200.             local dawnMaxA = ((dE - t) / 12 + 0.5) * math.pi
  201.             local L = math.sqrt(0.25 * i * i + j * j)
  202.  
  203.             if a >= duskMinA and a <= duskMaxA or a >= dawnMinA and a <= dawnMaxA then
  204.                 graph.setColor("orange")
  205.                 graph.pixel(12 + i, 12 - j)
  206.             elseif a <= duskMaxA and a >= dawnMinA then
  207.                 graph.setColor("lightBlue")
  208.                 graph.pixel(12 + i, 12 - j)
  209.             end
  210.         end
  211.     end
  212.  
  213.     local sun = -((12 - t) / 12 + 0.5) * math.pi
  214.     local sunx = math.floor(13 + 17 * math.cos(sun) + 0.5)
  215.     local suny = math.floor(10 + 8 * math.sin(sun) + 0.5)
  216.  
  217.     graph.setColor("yellow")
  218.     graph.pixel(sunx , suny )
  219.     graph.pixel(sunx -1, suny)
  220.     graph.pixel(sunx +1, suny)
  221.     graph.pixel(sunx , suny -1)
  222.     graph.pixel(sunx -1, suny-1)
  223.     graph.pixel(sunx +1, suny-1)
  224.  
  225.     local moon = -(0.5 - t/12) * math.pi
  226.     local moonx = math.floor(13 + 17 * math.cos(moon) + 0.5)
  227.     local moony = math.floor(10 + 8 * math.sin(moon) + 0.5)
  228.  
  229.     graph.setColor("pink")
  230.     graph.pixel(moonx , moony )
  231.     graph.pixel(moonx -1, moony)
  232.     graph.pixel(moonx +1, moony)
  233.     graph.pixel(moonx , moony -1)
  234.     graph.pixel(moonx -1, moony-1)
  235.     graph.pixel(moonx +1, moony-1)
  236.  
  237.     graph.setColor("gray")
  238.     -- graph.print("|", 12, 0)
  239.     -- graph.print("|", 12, 1)
  240.     -- graph.print("|", 12, 2)
  241.     -- graph.print("|", 12, 3)
  242.     -- graph.print("|", 12, 4)
  243.     -- graph.print("|", 12, 5)
  244.  
  245.     graph.setBackgroundColor(colors.lightBlue)
  246.     graph.setColor("lightGray")
  247.  
  248.     for j = 0, 5 do
  249.         local a = math.atan2(12-j, 0)
  250.         local duskMinA = ((eS - t) / 12 + 0.5) * math.pi
  251.         local duskMaxA = ((eE - t) / 12 + 0.5) * math.pi
  252.         local dawnMinA = ((dS - t) / 12 + 0.5) * math.pi
  253.         local dawnMaxA = ((dE - t) / 12 + 0.5) * math.pi
  254.  
  255.         graph.setBackgroundColor("brown")
  256.  
  257.         if 12 >= sunx - 1 and 12 <= sunx + 1 then
  258.             if j >= suny - 1 and j <= suny then
  259.                 graph.setBackgroundColor(colors.yellow)
  260.             else
  261.                 if a >= duskMinA and a <= duskMaxA or a >= dawnMinA and a <= dawnMaxA then
  262.                     graph.setBackgroundColor(colors.orange)
  263.                 elseif a <= duskMaxA and a >= dawnMinA then
  264.                     graph.setBackgroundColor(colors.lightBlue)
  265.                 end
  266.             end
  267.         elseif 12 >= moonx - 1 and 12 <= moonx + 1 then
  268.             if j >= moony - 1 and j <= moony then
  269.                 graph.setBackgroundColor(colors.pink)
  270.             else
  271.                 if a >= duskMinA and a <= duskMaxA or a >= dawnMinA and a <= dawnMaxA then
  272.                     graph.setBackgroundColor(colors.orange)
  273.                 elseif a <= duskMaxA and a >= dawnMinA then
  274.                     graph.setBackgroundColor(colors.lightBlue)
  275.                 end
  276.             end
  277.         else
  278.             if a >= duskMinA and a <= duskMaxA or a >= dawnMinA and a <= dawnMaxA then
  279.                 graph.setBackgroundColor(colors.orange)
  280.             elseif a <= duskMaxA and a >= dawnMinA then
  281.                 graph.setBackgroundColor(colors.lightBlue)
  282.             end
  283.         end
  284.  
  285.         graph.print("|", 12, j)
  286.     end
  287.  
  288.     graph.setBackgroundColor(colors.gray)
  289.  
  290.     graph.setColor("gray")
  291.     graph.ellipse("fill", 0, 6, 25, 6)
  292.     graph.rect("fill", 0, 10, 25, 12)
  293.  
  294.     graph.setColor("lightGray")
  295.     graph.pixel(0, 8)
  296.     graph.pixel(1, 8)
  297.     graph.line(2, 7, 5, 7)
  298.     graph.line(6, 6, 18, 6)
  299.     graph.line(19, 7, 22, 7)
  300.     graph.pixel(23, 8)
  301.     graph.pixel(24, 8)
  302.  
  303.     graph.setColor("white")
  304.     graph.print(string.format("%02d", h) .. ":" .. string.format("%02d", m), 10, 8)
  305.    
  306.     local phase = (os.day() - 1) % 8
  307.  
  308.     graph.setColor("purple")
  309.     graph.print("Tonight's Moon:", 2, 10)
  310.     graph.setColor("magenta")
  311.     graph.print("- " .. phases[phase] .. " >>", 2, 11)
  312.  
  313.     graph.setColor("purple")
  314.     graph.print("Constellations:", 2, 13)
  315.  
  316.     local list, names = getConstellations(phase)
  317.  
  318.     graph.setColor("magenta")
  319.     for i = 1, math.min(#names, 4) do
  320.         if #names == math.min(#names, 4) and i == 4 then
  321.             graph.print(" - " .. names[i] .. " >>", 2, 13 + i)
  322.         else
  323.             graph.print(" - " .. names[i], 2, 13 + i)
  324.         end
  325.     end
  326.  
  327.     if #names - 4 > 0 then
  328.         graph.print("and " .. (#names - 4) .. " more >>", 2, 13 + math.min(#names, 4) + 1)
  329.     end
  330. end
  331.  
  332. local function moonScreen()
  333.     term.setPaletteColour(colors.gray, 0x3d3d3d)
  334.     term.setPaletteColour(colors.purple, 0x333333)
  335.     term.setPaletteColour(colors.pink, 0x0e1423)
  336.     term.setPaletteColour(colors.lightGray, 0x60697d)
  337.     term.setPaletteColour(colors.cyan, 0x9ba5bd)
  338.     term.setPaletteColour(colors.blue, 0x2b2f38)
  339.  
  340.     graph.setBackgroundColor("purple")
  341.  
  342.     graph.setColor("purple")
  343.     rect(0, 0, 25, 20)
  344.     graph.setColor("gray")
  345.     rect(5, 1, 13, 9)
  346.     graph.setColor("pink")
  347.     rect(6, 2, 11, 7)
  348.  
  349.     local daysUntilSolar = 36 - (os.day() - 9) % 36
  350.     local phase = (os.day() - 1) % 8
  351.  
  352.     if phase == 0 then
  353.         graph.setColor("cyan")
  354.         rect(6, 2, 11, 7)
  355.         graph.setColor("lightGray")
  356.         rect(15, 2, 2, 7)
  357.         rect(6, 8, 11, 1)
  358.     elseif phase == 1 then
  359.         graph.setColor("cyan")
  360.         rect(6, 2, 5, 7)
  361.         rect(11, 3, 1, 5)
  362.         rect(12, 4, 1, 3)
  363.         graph.setColor("lightGray")
  364.         rect(6, 7, 5, 2)
  365.         rect(6, 7, 5, 2)
  366.         rect(10, 3, 2, 5)
  367.         rect(9, 2, 2, 1)
  368.         graph.setColor("blue")
  369.         rect(12, 4, 1, 3)
  370.         graph.pixel(11, 3)
  371.         graph.pixel(10, 2)
  372.         graph.pixel(11, 7)
  373.         rect(6, 8, 5, 1)
  374.     end
  375.  
  376.     graph.setColor("magenta")
  377.     graph.print(phases[phase], 2, 13)
  378.  
  379.     if (os.day() - 9) % 36 ~= 0 then
  380.         graph.setColor("yellow")
  381.         graph.print(tostring(daysUntilSolar), 2, 15)
  382.         graph.setColor("white")
  383.         graph.print(" days until", 2 + #tostring(daysUntilSolar), 15)
  384.         graph.print("solar eclipse", 2, 16)
  385.     else
  386.         graph.setColor("white")
  387.         graph.print("Solar Eclipse is ", 4, 15)
  388.         graph.setColor("yellow")
  389.         graph.print("Today", 9, 16)
  390.     end
  391.  
  392.     graph.setColor("white")
  393.     graph.print(string.format("%02d", h) .. ":" .. string.format("%02d", m), 9, 11)
  394. end
  395.  
  396. local screen = 1
  397.  
  398. function cobalt.draw()
  399.  
  400.     if screen == 1 then
  401.         mainScreen()
  402.     elseif screen == 2 then
  403.         moonScreen()
  404.     end
  405.    
  406.     graph.setColor("white")
  407.     graph.setBackgroundColor("white")
  408.     graph.line(25, 0, 25, 20)
  409.  
  410.     if screen == 1 then
  411.         graph.setColor("magenta")
  412.     else
  413.         graph.setColor("purple")
  414.     end
  415.     graph.print("S", 25, 0)
  416.  
  417.     if screen == 2 then
  418.         graph.setColor("magenta")
  419.     else
  420.         graph.setColor("purple")
  421.     end
  422.     graph.print("M", 25, 2)
  423.  
  424.     if screen == 3 then
  425.         graph.setColor("magenta")
  426.     else
  427.         graph.setColor("purple")
  428.     end
  429.     graph.print("C", 25, 4)
  430.    
  431.     graph.setColor("red")
  432.     graph.print(debug, 0, 0)
  433. end
  434.  
  435. function cobalt.mousepressed(x, y, m)
  436.     --debug = x .. ", " .. y .. " : " .. m
  437.     if m == 1 and x == 26 then
  438.         if y >= 1 and y <= 2 then
  439.             screen = 1
  440.         elseif y >= 3 and y <= 4 then
  441.             screen = 2
  442.         elseif y >= 5 and y <= 6 then
  443.             screen = 3
  444.         end
  445.     end
  446.  
  447.     if m == 1 and screen == 1 then
  448.         if x >= 5 and x <= 23 and y == 12 then
  449.             screen = 2
  450.         end
  451.     end
  452. end
  453.  
  454. function cobalt.keypressed(key, keyCode)
  455.     if key == "r" then
  456.         os.reboot()
  457.     end
  458. end
  459.  
  460. cobalt.init()
  461.  
  462.  
RAW Paste Data