info = { type = "image", title = "Moon library", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Moonlight and dust in a quiet old library", api = 33, } local ltr = ({ sk = { ["Dust"] = "Prach", ["Clouds"] = "Oblaky", ["Moonlight"] = "Mesačné svetlo", ["None"] = "Žiadne", ["Rare"] = "Zriedka", ["Often"] = "Často", ["Silver"] = "Strieborné", ["Blue"] = "Modré", ["Amber"] = "Jantárové" }, cs = { ["Dust"] = "Prach", ["Clouds"] = "Oblaka", ["Moonlight"] = "Měsíční světlo", ["None"] = "Žádná", ["Rare"] = "Vzácně", ["Often"] = "Často", ["Silver"] = "Stříbrné", ["Blue"] = "Modré", ["Amber"] = "Jantarové" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "dust", label = LT("Dust"), type = "int", min = 0, max = 5, default = 3 }, { key = "clouds", label = LT("Clouds"), type = "int", default = 1, opts = { LT("None"), LT("Rare"), LT("Often") } }, { key = "tone", label = LT("Moonlight"), type = "int", default = "silver", opts = { keys = { "silver", "blue", "amber" }, LT("Silver"), LT("Blue"), LT("Amber") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, sqrt, abs, min, max = math.floor, math.sin, math.sqrt, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local KEY = { 255, 0, 255 } local TONES = { silver = { moon = { 240, 244, 255 }, h1 = { 52, 86, 180 }, h2 = { 30, 52, 130 }, beam = { 170, 200, 255 } }, blue = { moon = { 214, 232, 255 }, h1 = { 40, 90, 210 }, h2 = { 24, 56, 160 }, beam = { 120, 170, 255 } }, amber = { moon = { 255, 236, 190 }, h1 = { 112, 96, 150 }, h2 = { 52, 56, 128 }, beam = { 255, 222, 160 } }, } local TONE = TONES.silver local function mix(a, b, t) return floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5) end local function disc(cx, cy, r, c) for dy = -r, r do local hw = floor(sqrt(r * r + 0.5 - dy * dy)) rect(cx - hw, cy + dy, 2 * hw + 1, 1, c[1], c[2], c[3]) end end local function frect(x, y, w, h, r, g, b, a) local x0, xe = floor(x), x + w local x1 = floor(xe) if x1 == x0 then blend_rect(x0, y, 1, h, r, g, b, w * a) return end local f = x - x0 if f > 0.02 then blend_rect(x0, y, 1, h, r, g, b, (1 - f) * a); x0 = x0 + 1 end if x1 > x0 then blend_rect(x0, y, x1 - x0, h, r, g, b, a) end if xe - x1 > 0.02 then blend_rect(x1, y, 1, h, r, g, b, (xe - x1) * a) end end local HW = {} for y = 6, 40 do HW[y] = y < 16 and floor(sqrt(100 - (16 - (y + 0.5)) ^ 2) + 0.5) or 10 end local function beam_l(y) return 22 - 0.8 * (y - 42) end local function beam_r(y) return 41 - 0.5 * (y - 42) end local SKY_T, SKY_B = { 8, 16, 72 }, { 34, 62, 150 } local FRAME, FRAME_L = { 98, 62, 52 }, { 150, 104, 80 } local BOOKS = { { 104, 34, 46 }, { 136, 100, 40 }, { 34, 82, 88 }, { 36, 48, 100 }, { 88, 44, 92 }, { 40, 80, 52 }, { 150, 130, 100 }, { 120, 56, 36 } } local GLASS, INT local function bake_glass() for y = 6, 40 do rect(32 - HW[y], y, 2 * HW[y], 1, mix(SKY_T, SKY_B, (y - 6) / 34)) end disc(36, 14, 8, TONE.h2); disc(36, 14, 6, TONE.h1); disc(36, 14, 4, TONE.moon) px(34, 12, 196, 206, 230); px(35, 12, 196, 206, 230); px(37, 16, 196, 206, 230); px(38, 13, 196, 206, 230) px(24, 10, 190, 200, 255); px(27, 21, 150, 170, 240); px(40, 26, 190, 200, 255); px(29, 8, 150, 170, 240) for x = 22, 41 do local top = 34 + floor(2 * sin(x * 0.9) + 1.5 * sin(x * 0.37) + 0.5) rect(x, top, 1, 41 - top, 6, 14, 40) end GLASS = snapshot(0, 0, W, H) end local function shelf(x0, x1, seed) local w = x1 - x0 + 1 rect(x0, 3, w, 3, 70, 42, 38); rect(x0, 3, w, 1, 110, 70, 56) rect(x0, 6, w, 39, 22, 16, 28) local rows = { 13, 22, 31, 40 } for r = 1, 4 do local yb = rows[r] rect(x0, yb + 1, w, 1, 100, 64, 50) local x, k = x0 + 1, 0 while x < x1 - 1 do k = k + 1 local h = hash32((r * 37 + k) * 0x9E3779B1 + seed) local bw, bh = 1 + umod(h, 3), 4 + umod(h >> 4, 5) local c = BOOKS[umod(h >> 8, 8) + 1] rect(x, yb - bh + 1, bw, bh, c[1], c[2], c[3]) if bh >= 6 then rect(x, yb - bh + 3, bw, 1, 170, 140, 70) end x = x + bw + (umod(h >> 14, 9) == 0 and 1 or 0) end end rect(x0, 42, w, 4, 64, 40, 40) end local function bake_interior() rect(0, 0, W, H, KEY[1], KEY[2], KEY[3]) rect(0, 0, W, 46, 44, 34, 62) for y = 4, 42 do local hw = HW[min(max(y, 6), 40)] rect(30 - hw, y, 2 * hw + 4, 1, FRAME[1], FRAME[2], FRAME[3]) end rect(18, 41, 28, 2, FRAME_L[1], FRAME_L[2], FRAME_L[3]) for y = 6, 40 do rect(32 - HW[y], y, 2 * HW[y], 1, KEY[1], KEY[2], KEY[3]) end rect(31, 6, 2, 35, FRAME[1], FRAME[2], FRAME[3]) for _, y in ipairs({ 16, 25, 33 }) do rect(22, y, 20, 1, FRAME[1], FRAME[2], FRAME[3]) end shelf(0, 16, 11); shelf(48, 63, 29) rect(16, 6, 1, 39, 110, 70, 56); rect(48, 6, 1, 39, 110, 70, 56) rect(0, 44, W, 2, 100, 64, 52); rect(0, 44, W, 1, 130, 86, 66) local YS = { 46, 48, 50, 52, 55, 58, 61, 64 } for i = 1, #YS - 1 do local y0, h = YS[i], YS[i + 1] - YS[i] local c = i % 2 == 1 and { 84, 52, 50 } or { 74, 46, 46 } rect(0, y0, W, h, c[1], c[2], c[3]) rect(0, y0, W, 1, 52, 32, 36) for k = 0, 2 do local x = umod(hash32((i * 5 + k) * 0x85EBCA6B), W) rect(x, y0, 1, h, 52, 32, 36) end end rect(18, 50, 30, 11, 104, 34, 56); rect(19, 51, 28, 9, 132, 80, 66); rect(20, 52, 26, 7, 104, 34, 56) rect(22, 54, 22, 3, 124, 56, 70) INT = snapshot(0, 0, W, H) end local MOTE = {} for i = 1, 20 do local h = hash32(i * 0xC2B2AE35) MOTE[i] = { fx = 0.1 + 0.8 * umod(h, 100) / 100, y = 47 + umod(h >> 8, 11), ay = 1.5 + umod(h >> 14, 20) / 10, ax = 1.5 + umod(h >> 18, 20) / 10, p1 = umod(h >> 4, 628) / 100, p2 = umod(h >> 10, 628) / 100, p3 = umod(h >> 16, 628) / 100 } end local function draw_light(I) local b = TONE.beam for y = 42, 62, 2 do local f = (y - 42) / 20 local xl, xr = beam_l(y + 1), beam_r(y + 1) blend_rect(floor(xl + 0.5), y, floor(xr - xl + 0.5), 2, b[1], b[2], b[3], 0.16 * I * (1 - 0.4 * f)) blend_rect(floor(xl + 3), y, floor(xr - xl - 5), 2, b[1], b[2], b[3], 0.12 * I * (1 - 0.4 * f)) end for y = 47, 62 do if y ~= 51 and y ~= 56 then local xl, xr = beam_l(y), beam_r(y) local xm = floor((xl + xr) * 0.5) blend_rect(floor(xl + 0.5), y, xm - floor(xl + 0.5) - 1, 1, b[1], b[2], b[3], 0.42 * I) blend_rect(xm + 1, y, floor(xr + 0.5) - xm - 1, 1, b[1], b[2], b[3], 0.42 * I) end end end local function draw_motes(t, n, I) local b = TONE.beam for i = 1, n do local m = MOTE[i] local y = floor(m.y + m.ay * sin(t * 0.31 + m.p1) + 0.5) local x = beam_l(y) + (beam_r(y) - beam_l(y)) * m.fx + m.ax * sin(t * 0.23 + m.p2) local a = I * (0.25 + 0.4 * (0.5 + 0.5 * sin(t * 1.1 + m.p3))) local ix = floor(x) local f = x - ix blend(ix, y, b[1], b[2], b[3], a * (1 - f)) blend(ix + 1, y, b[1], b[2], b[3], a * f) end end local function draw_cloud(t, P) local xc = -26 + 104 * (((t + P * 0.2) % P) / P) gfx.clip(22, 6, 20, 35) frect(xc + 6, 10, 14, 1, 120, 150, 230, 0.45) frect(xc, 11, 26, 3, 24, 36, 92, 0.8) frect(xc + 2, 14, 22, 3, 24, 36, 92, 0.75) frect(xc + 4, 17, 14, 1, 24, 36, 92, 0.5) gfx.clip() return min(1, max(0, (16 - abs(xc + 13 - 36)) / 8)) end function on_settings() TONE = TONES[cfg.tone] or TONES.silver GLASS = nil end function render() if not GLASS then bake_glass() end if not INT then bake_interior() end local t = frame / FPS blit_rgb(GLASS, 0, 0, W, H) local I = 1 local c = cfg.clouds or 1 if c > 0 then I = 1 - 0.7 * draw_cloud(t, c == 1 and 80 or 40) end blit_rgb(INT, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) draw_light(I) draw_motes(t, 4 * (cfg.dust or 3), I) end