info = { type = "image", title = "Sunburst skyline", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Turning sun rays, lit windows, passing cars", api = 33, } local ltr = ({ sk = { ["Time of day"] = "Denná doba", ["Sunset"] = "Západ slnka", ["Day"] = "Deň", ["Dawn"] = "Svitanie", ["Cars"] = "Autá", ["Ray speed"] = "Rýchlosť lúčov" }, cs = { ["Time of day"] = "Denní doba", ["Sunset"] = "Západ slunce", ["Day"] = "Den", ["Dawn"] = "Svítání", ["Cars"] = "Auta", ["Ray speed"] = "Rychlost paprsků" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "pal", label = LT("Time of day"), type = "int", default = "sunset", opts = { keys = { "sunset", "day", "dawn" }, LT("Sunset"), LT("Day"), LT("Dawn") } }, { key = "cars", label = LT("Cars"), type = "int", min = 0, max = 5, default = 3 }, { key = "rays", label = LT("Ray speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, tan = math.floor, math.tan local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local PALS = { sunset = { { 96, 56, 160 }, { 255, 160, 96 }, { 255, 238, 150 }, { 255, 204, 116 }, { 255, 224, 140 }, 0.24, { 176, 92, 136 }, { 156, 78, 126 }, { 74, 40, 106 }, { 58, 30, 90 }, { 255, 214, 100 }, { 40, 30, 66 }, { 150, 100, 136 } }, day = { { 36, 112, 232 }, { 176, 226, 255 }, { 255, 248, 170 }, { 255, 255, 226 }, { 255, 255, 255 }, 0.24, { 118, 158, 210 }, { 100, 140, 198 }, { 62, 78, 152 }, { 48, 62, 134 }, { 255, 236, 140 }, { 56, 62, 94 }, { 176, 182, 206 } }, dawn = { { 40, 60, 142 }, { 255, 188, 170 }, { 255, 232, 204 }, { 255, 212, 190 }, { 255, 236, 222 }, 0.22, { 158, 124, 184 }, { 136, 106, 170 }, { 58, 50, 112 }, { 44, 38, 96 }, { 255, 226, 140 }, { 38, 36, 74 }, { 134, 120, 164 } }, } local P = PALS.sunset local CARC = { { 230, 60, 60 }, { 250, 204, 50 }, { 240, 240, 250 }, { 40, 196, 196 }, { 156, 96, 224 } } local CX, CY = 21, 27 local ROAD = 55 local KEY = { 255, 0, 255 } local function c3(c) return c[1], c[2], c[3] end local function mulc(c, k) return floor(c[1] * k), floor(c[2] * k), floor(c[3] * k) end local SKY, FG = nil, nil local WX, WY, WP, WD, WN = {}, {}, {}, {}, 0 local function disc(cx, cy, r, R, G, B) for y = cy - floor(r), cy + floor(r) do local dy = y - cy local hw = floor(math.sqrt(r * r - dy * dy) + 0.5) rect(cx - hw, y, 2 * hw + 1, 1, R, G, B) end end local function layer(seed, wmin, wspan, hmin, hspan, base, ca, cb, windows) local x, i = -3, 0 while x < W do i = i + 1 local h = hash32(i * 0x9E3779B1 + seed) local w = wmin + umod(h, wspan) local top = base - (hmin + umod(h >> 8, hspan)) local c = (i % 2 == 0) and ca or cb rect(x, top, w, base - top, c3(c)) rect(x, top, w, 1, c[1] + 18 > 255 and 255 or c[1] + 18, c[2] + 14, c[3] + 20 > 255 and 255 or c[3] + 20) if umod(h >> 16, 3) == 0 and w >= 7 then rect(x + w // 2, top - 3, 1, 3, c3(c)) end if windows then local dr, dg, db = mulc(c, 0.72) for wx = x + 2, x + w - 2, 3 do for wy = top + 3, base - 4, 4 do rect(wx, wy, 1, 2, dr, dg, db) WN = WN + 1 WX[WN], WY[WN] = wx, wy local g = hash32(wx * 7919 + wy * 104729 + seed) WP[WN] = 180 + umod(g, 420) WD[WN] = floor(WP[WN] * (0.25 + umod(g >> 12, 50) / 100)) end end end x = x + w end end local function build() WN = 0 local T, Hz = P[1], P[2] for y = 0, ROAD - 1 do local t = y / (ROAD - 1) rect(0, y, W, 1, floor(T[1] + (Hz[1] - T[1]) * t + 0.5), floor(T[2] + (Hz[2] - T[2]) * t + 0.5), floor(T[3] + (Hz[3] - T[3]) * t + 0.5)) end disc(CX, CY, 10.4, c3(P[4])) disc(CX, CY, 7.4, c3(P[3])) SKY = gfx.snapshot(0, 0, W, H) rect(0, 0, W, H, c3(KEY)) layer(0x1234, 8, 5, 17, 11, ROAD, P[7], P[8], false) layer(0x5678, 7, 4, 12, 16, ROAD, P[9], P[10], true) rect(0, ROAD - 1, W, 1, c3(P[13])) rect(0, ROAD, W, H - ROAD, c3(P[12])) for x = 0, W - 1, 8 do rect(x, 59, 4, 1, c3(P[13])) end FG = gfx.snapshot(0, 0, W, H) end local NC = 3 function on_settings() P = PALS[cfg.pal] or PALS.sunset NC = cfg.cars or 3 SKY = nil end local CARS = {} for i = 1, 5 do local h = hash32(i * 0x85EBCA6B) CARS[i] = { dir = (i % 2 == 1) and 1 or -1, v = (0.28 + umod(h, 30) / 100) / 1.0, x0 = umod(h >> 6, 90), col = CARC[i], } CARS[i].lane = (i % 2 == 1) and 60 or 56 end local function car(c, f) local span = W + 24 local x = (c.x0 + c.dir * c.v * f) % span - 12 local ix = floor(x) local fr = x - ix local y = c.lane local R, G, B = c3(c.col) rect(ix + 1, y + 1, 7, 2, R, G, B) blend_rect(ix, y + 1, 1, 2, R, G, B, 1 - fr) blend_rect(ix + 8, y + 1, 1, 2, R, G, B, fr) rect(ix + 2, y, 4, 1, R * 7 // 10, G * 7 // 10, B * 7 // 10) px(ix + 2, y + 3, 16, 16, 24); px(ix + 6, y + 3, 16, 16, 24) if c.dir > 0 then px(ix + 8, y + 1, 255, 236, 150); px(ix, y + 1, 255, 70, 60) else px(ix, y + 1, 255, 236, 150); px(ix + 8, y + 1, 255, 70, 60) end end local SP = 0.5236 local HW = 0.105 local function ray(a, R, G, B, A) local t1, t2 = tan(a - HW), tan(a + HW) for y = CY - 11, 0, -1 do local h = CY - y local x1, x2 = CX + h * t1, CX + h * t2 if x2 > 0 and x1 < W then local i1, i2 = floor(x1), floor(x2) if i1 == i2 then blend_rect(i1, y, 1, 1, R, G, B, A * (x2 - x1)) else blend_rect(i1, y, 1, 1, R, G, B, A * (i1 + 1 - x1)) if i2 - i1 > 1 then blend_rect(i1 + 1, y, i2 - i1 - 1, 1, R, G, B, A) end blend_rect(i2, y, 1, 1, R, G, B, A * (x2 - i2)) end end end end function render() if not SKY then build() end local f = frame * STEP_HZ // FPS gfx.blit_rgb(SKY, 0, 0, W, H) local rot = (f * SP * (cfg.rays or 3) / 360) % SP for k = -3, 3 do local a = rot + k * SP if a > -1.3 and a < 1.3 then ray(a, P[5][1], P[5][2], P[5][3], P[6]) end end local c1 = (f * 0.07 + 10) % (W + 30) - 24 local c2 = (f * -0.05 + 50) % (W + 30) - 24 blend_rect(floor(c1) + 3, 8, 16, 1, 255, 240, 235, 0.5); blend_rect(floor(c1), 9, 22, 2, 255, 240, 235, 0.5) blend_rect(floor(c2) + 2, 18, 12, 1, 255, 240, 235, 0.4); blend_rect(floor(c2), 19, 16, 2, 255, 240, 235, 0.4) gfx.blit_rgb(FG, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) local lr, lg, lb = c3(P[11]) for i = 1, WN do local q = (f + WP[i] * 0.37) % WP[i] if q < WD[i] then if q < 8 then blend_rect(WX[i], WY[i], 1, 2, lr, lg, lb, 0.45) else rect(WX[i], WY[i], 1, 2, lr, lg, lb) end end end for i = 1, NC do car(CARS[i], f) end end