info = { type = "image", title = "Rain window cat", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A cat watches raindrops run down the window", api = 33, } local ltr = ({ sk = { ["Raindrops"] = "Dažďové kvapky", ["Lightning"] = "Blesky" }, cs = { ["Raindrops"] = "Dešťové kapky", ["Lightning"] = "Blesky" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "drops", label = LT("Raindrops"), type = "int", min = 3, max = 20, default = 10 }, { key = "flash", label = LT("Lightning"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin = math.floor, math.sin local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local WALL, WALL_D = { 68, 54, 96 }, { 48, 36, 70 } local FRAME, FRAME_HI = { 118, 102, 166 }, { 152, 136, 200 } local SILL, SILL_HI = { 96, 82, 140 }, { 170, 154, 214 } local GL_TOP, GL_BOT = { 30, 62, 92 }, { 94, 132, 156 } local FUR, FUR2, FUR3 = { 240, 238, 250 }, { 180, 182, 214 }, { 132, 134, 176 } local COLLAR, BELL = { 232, 96, 84 }, { 255, 214, 90 } local POT, POT_D = { 178, 100, 74 }, { 132, 70, 58 } local LEAF, LEAF_D = { 78, 170, 118 }, { 44, 112, 86 } local KEY = { 255, 0, 255 } local DROP, TRAIL = { 214, 234, 248 }, { 150, 186, 210 } local GX, GY, GW, GH = 6, 5, 52, 38 local CX = 16 local LANE_LO = 7 local function c3(c) return c[1], c[2], c[3] end local function lerp3(a, b, k) return floor(a[1] + (b[1] - a[1]) * k + 0.5), floor(a[2] + (b[2] - a[2]) * k + 0.5), floor(a[3] + (b[3] - a[3]) * k + 0.5) end local function mullions() rect(31, GY, 2, GH, c3(FRAME)) rect(31, GY, 1, GH, c3(FRAME_HI)) rect(GX, 22, GW, 2, c3(FRAME)) rect(GX, 22, GW, 1, c3(FRAME_HI)) end local BG, FG = nil, nil local function build_bg() rect(0, 0, W, H, c3(WALL)) rect(3, 2, 58, 45, c3(FRAME)) rect(3, 2, 58, 1, c3(FRAME_HI)) for y = 0, GH - 1 do rect(GX, GY + y, GW, 1, lerp3(GL_TOP, GL_BOT, y / (GH - 1))) end local B = { { 10, 37, 255, 190, 100 }, { 23, 40, 120, 220, 255 }, { 38, 35, 255, 130, 110 }, { 46, 40, 255, 200, 110 }, { 53, 32, 150, 230, 255 }, { 19, 30, 255, 220, 140 }, { 42, 29, 255, 150, 120 }, } for i = 1, #B do local b = B[i] blend_rect(b[1] - 1, b[2], 3, 1, b[3], b[4], b[5], 0.5) blend_rect(b[1], b[2] - 1, 1, 3, b[3], b[4], b[5], 0.5) blend(b[1], b[2], b[3], b[4], b[5], 0.45) end for i = 1, 14 do local h = hash32(i * 0x9E3779B1) local x, y = GX + umod(h, GW), GY + umod(h >> 8, GH) if x ~= 31 and x ~= 32 and y ~= 22 and y ~= 23 then px(x, y, c3(TRAIL)) if umod(h >> 16, 2) == 0 then px(x, y - 1, c3(DROP)) end end end mullions() rect(1, 47, 62, 1, c3(SILL_HI)) rect(1, 48, 62, 3, c3(SILL)) rect(1, 51, 62, 1, c3(WALL_D)) for x = 8, 54, 3 do rect(x, 54, 2, 9, c3(WALL_D)) rect(x + 2, 54, 1, 9, c3(FRAME)) end rect(6, 52, 52, 2, c3(WALL_D)) BG = gfx.snapshot(0, 0, W, H) end local FGX, FGY, FGW, FGH = 8, 22, 50, 25 local BODY = { 7, 8, 9, 10, 11, 12, 13, 13, 14, 14, 15, 15, 15 } local function build_fg() rect(0, 0, W, H, c3(KEY)) for i = 1, #BODY do local w, y = BODY[i], 33 + i rect(CX - w // 2, y, w, 1, c3(FUR)) if i >= 4 then rect(CX + w // 2 - 1, y, 2, 1, c3(FUR2)) end if i >= 8 then px(CX - w // 2, y, c3(FUR2)) end end rect(CX - 3, 35, 7, 1, c3(COLLAR)) px(CX + 4, 36, c3(BELL)) rect(CX - 6, 46, 4, 1, c3(FUR2)); rect(CX + 3, 46, 4, 1, c3(FUR2)) rect(48, 41, 8, 2, c3(POT)); rect(49, 43, 6, 4, c3(POT)); rect(49, 43, 1, 4, c3(POT_D)) rect(48, 41, 8, 1, c3(POT_D)) rect(51, 33, 2, 8, c3(LEAF_D)) rect(46, 33, 5, 2, c3(LEAF)); rect(45, 34, 2, 2, c3(LEAF)) rect(53, 30, 5, 2, c3(LEAF)); rect(56, 31, 2, 2, c3(LEAF)) rect(49, 27, 4, 2, c3(LEAF)); rect(48, 28, 2, 2, c3(LEAF_D)) FG = gfx.snapshot(FGX, FGY, FGW, FGH) end local LEN = 46 local DP = {} for i = 1, 24 do local h = hash32(i * 0x85EBCA6B) local v = 0.07 + umod(h, 16) / 100.0 local w = TAU / (28 + umod(h >> 6, 40)) DP[i] = { v = v, w = w, a = v / w, ph = umod(h >> 12, 628) / 100.0, T = LEN / v, off = umod(h >> 3, 600) } end local TRK = { v = 0.11, w = TAU / 52, ph = 1.1, off = 60 } TRK.a = TRK.v / TRK.w; TRK.T = LEN / TRK.v local function pos_of(d, t) local u = t + d.off local cyc = floor(u / d.T) local tt = u - cyc * d.T return d.v * tt + d.a * (sin(d.w * tt + d.ph) - sin(d.ph)), cyc end local DR, DG, DB = 214, 234, 248 local TR, TG, TB = 150, 186, 210 local function draw_drop(x, pos, big) local y = GY + pos - 1 local yi = floor(y) local f = y - yi if yi > GY + GH - 1 then return end local y0 = yi - (big and 8 or 5) if y0 < GY then y0 = GY end if yi - y0 > 0 then blend_rect(x, y0, 1, yi - y0, TR, TG, TB, big and 0.4 or 0.3) end if big then blend_rect(x, yi, 2, 1, DR, DG, DB, 0.95 * (1 - f)) blend_rect(x, yi + 1, 2, 1, DR, DG, DB, 0.95) blend_rect(x, yi + 2, 2, 1, DR, DG, DB, 0.95 * f) else blend(x, yi, DR, DG, DB, 0.95 * (1 - f)) blend(x, yi + 1, DR, DG, DB, 0.95) blend(x, yi + 2, DR, DG, DB, 0.95 * f) end end local RX, RV, RO = {}, {}, {} for i = 1, 14 do local h = hash32(i * 0xC2B2AE35) local x = GX + umod(h, GW) if x == 31 or x == 32 then x = x + 3 end RX[i], RV[i], RO[i] = x, 1.6 + umod(h >> 8, 12) / 10.0, umod(h >> 16, 500) end local HEAD_W = { 7, 9, 11, 11, 11, 11, 10, 8 } local function draw_head(dx, dy, flick) local top, x0 = 26 + dy, CX + dx for r = 1, 8 do local w, y = HEAD_W[r], top + r - 1 rect(x0 - w // 2, y, w, 1, c3(FUR)) if r >= 3 then rect(x0 + w // 2 - 1, y, 2, 1, c3(FUR2)) end end rect(x0 - 5, top - 1, 3, 1, c3(FUR)); rect(x0 - 5, top - 2, 2, 1, c3(FUR)); px(x0 - 5, top - 3, c3(FUR)) px(x0 - 4, top - 1, c3(FUR2)) if flick then rect(x0 + 3, top - 1, 4, 1, c3(FUR2)); rect(x0 + 5, top - 2, 2, 1, c3(FUR2)) else rect(x0 + 3, top - 1, 3, 1, c3(FUR2)); rect(x0 + 4, top - 2, 2, 1, c3(FUR2)); px(x0 + 5, top - 3, c3(FUR3)) end end local function draw_tail(t) local py_a, py_b for i = 0, 14 do local k = i / 14 local lift = k * k * 11 * (0.55 + 0.45 * sin(t * 0.085 - i * 0.30)) local yb = 45 - floor(lift + 0.5) local th = i >= 12 and 1 or 2 local ya = yb - th + 1 local lo, hi = ya, yb if py_a then if py_a < lo then lo = py_a end if py_b > hi then hi = py_b end end rect(24 + i, lo, 1, hi - lo + 1, c3(i >= 12 and FUR3 or FUR)) py_a, py_b = ya, yb end end local FLASH = { 0.55, 0.30, 0.10, 0.40, 0.20, 0.08 } local BOLT = { { 44, 5, 41, 11 }, { 41, 11, 44, 14 }, { 44, 14, 38, 22 }, { 41, 12, 36, 15 } } local function line(x0, y0, x1, y1) local n = math.max(math.abs(x1 - x0), math.abs(y1 - y0)) for i = 0, n do local k = n == 0 and 0 or i / n px(floor(x0 + (x1 - x0) * k + 0.5), floor(y0 + (y1 - y0) * k + 0.5), 255, 255, 236) end end local N_DROPS, FLASH_ON = 10, true function on_settings() N_DROPS = cfg.drops or 10 if cfg.flash == nil then FLASH_ON = true else FLASH_ON = cfg.flash end end function render() if not BG then build_bg(); build_fg() end local t = frame * STEP_HZ // FPS gfx.blit_rgb(BG, 0, 0, W, H) gfx.clip(GX, GY, GW, GH) for i = 1, 14 do local y = GY + (RO[i] + t * RV[i]) % (GH + 8) - 4 blend_rect(RX[i], floor(y), 1, 3, 200, 224, 244, 0.20) end for i = 1, N_DROPS do local pos, cyc = pos_of(DP[i], t) local lane = LANE_LO + umod(hash32(cyc * 0x9E3779B1 + i * 0x85EBCA6B), 50) if lane >= 29 and lane <= 34 then lane = lane + 6 end draw_drop(lane, pos, false) end local tp = pos_of(TRK, t) local tx = 27 + floor(2 * sin(tp * 0.16 + 0.8) + 0.5) draw_drop(tx, tp, true) if FLASH_ON then local f = t % 270 - 134 if f >= 0 and f < #FLASH then blend_rect(GX, GY, GW, GH, 200, 222, 255, FLASH[f + 1]) if f == 0 or f == 1 or f == 3 then for i = 1, #BOLT do line(BOLT[i][1], BOLT[i][2], BOLT[i][3], BOLT[i][4]) end end end end gfx.clip() mullions() gfx.blit_rgb(FG, FGX, FGY, FGW, FGH, c3(KEY)) draw_tail(t) local dx = tx - 26 if dx > 1 then dx = 1 end local dy = tp > 18 and 1 or 0 draw_head(dx, dy, t % 140 < 8 and (t // 2) % 2 == 0) end