info = { type = "image", title = "Laundry line", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Washing on a line swings in gusts of wind", api = 33, } local tr = ({ sk = { ["Wind"] = "Vietor", ["Laundry"] = "Bielizeň", ["Classic"] = "Klasická", ["Pastel"] = "Pastelová", ["Sunset"] = "Západ" }, cs = { ["Wind"] = "Vítr", ["Laundry"] = "Prádlo", ["Classic"] = "Klasické", ["Pastel"] = "Pastelové", ["Sunset"] = "Západ" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "wind", label = T("Wind"), type = "int", min = 1, max = 5, default = 3 }, { key = "cols", label = T("Laundry"), type = "int", default = "classic", opts = { keys = { "classic", "pastel", "sunset" }, T("Classic"), T("Pastel"), T("Sunset") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, exp, sqrt = math.floor, math.sin, math.cos, math.exp, math.sqrt local hash32 = math.hash32 local char, byte = string.char, string.byte local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local P1X, P2X = 3, 59 local LY0, LY1 = 13, 25 local LINE_Y = {} for x = P1X, P2X do local u = (x - P1X) / (P2X - P1X) LINE_Y[x] = floor(LY0 + (LY1 - LY0) * u + 2.4 * sin(3.14159 * u) + 0.5) end local BG = nil local function build_bg() for y = 0, 47 do local t = y / 47 rect(0, y, W, 1, floor(82 + 96 * t), floor(156 + 66 * t), floor(228 + 18 * t)) end rect(51 - 3, 9 - 5, 7, 11, 255, 238, 130) rect(51 - 4, 9 - 4, 9, 9, 255, 238, 130) rect(51 - 5, 9 - 2, 11, 5, 255, 238, 130) for x = 0, W - 1 do local a = floor(41 + 3 * sin(x * 0.09 + 0.5) + 2 * sin(x * 0.21) + 0.5) rect(x, a, 1, H - a, 128, 184, 168) local b = floor(46 + 2 * sin(x * 0.13 + 2.0) + 0.5) rect(x, b, 1, H - b, 104, 172, 92) end for y = 49, H - 1 do local t = (y - 49) / 14 rect(0, y, W, 1, floor(96 - 30 * t), floor(180 - 40 * t), floor(76 - 18 * t)) end for i = 1, 14 do local h = hash32(i * 0x9E3779B1) local x, y = (h & 0x7FFF) % W, 50 + ((h >> 8) & 0x7FFF) % 13 local c = (h >> 20) & 3 if c == 0 then px(x, y, 255, 252, 240) elseif c == 1 then px(x, y, 255, 220, 80) elseif c == 2 then px(x, y, 255, 150, 170) else px(x, y, 52, 120, 52) end end rect(P1X - 1, LY0 - 3, 3, 55 - LY0 + 3, 116, 80, 52) rect(P1X - 1, LY0 - 3, 1, 55 - LY0 + 3, 156, 114, 76) rect(P2X - 1, LY1 - 3, 3, 55 - LY1 + 3, 116, 80, 52) rect(P2X - 1, LY1 - 3, 1, 55 - LY1 + 3, 156, 114, 76) rect(P1X - 2, 54, 5, 1, 58, 124, 50) rect(P2X - 2, 54, 5, 1, 58, 124, 50) for x = P1X + 1, P2X - 1 do px(x, LINE_Y[x], 92, 80, 70) end BG = gfx.snapshot(0, 0, W, H) end local ART = { shirt = { "aaaacccaaaa", "aaaaaaaaaaa", "aaaaaaaaaaa", "baaaaaaaaab", ".baaaaaaab.", "..aaaaaaa..", "..aaaaaaa..", "..aaaaaaa..", "..aaaaaaa..", "..aaaaaaa..", "..bbbbbbb.." }, stripe = { "aaaacccaaaa", "aaaaaaaaaaa", "aaaaaaaaaaa", "baaaaaaaaab", ".baaaaaaab.", "..ccccccc..", "..aaaaaaa..", "..ccccccc..", "..aaaaaaa..", "..ccccccc..", "..bbbbbbb.." }, towel = { "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "aaaaaaaaa", "ccccccccc", "aaaaaaaaa", "ccccccccc", "a.a.a.a.a" }, sock = { "ccc.", "aaa.", "aaa.", "aaa.", "aaaa", "bbbb" }, } local TYPES = { "shirt", "stripe", "towel", "sock", "sockfly" } local SPR = {} for tid = 1, 5 do local rows = ART[TYPES[tid] == "sockfly" and "sock" or TYPES[tid]] local S = { w = #rows[1], h = #rows, hang = tid ~= 5 } for r = 1, S.h do local line = {} for c = 1, S.w do local ch = rows[r]:sub(c, c) line[c] = ch == "a" and 1 or ch == "b" and 2 or ch == "c" and 3 or 0 end S[r] = line end S.cx, S.cy = S.w / 2, S.hang and 0 or S.h / 2 S.R = floor(sqrt(S.w * S.w + S.h * S.h)) + 1 SPR[tid] = S end local POSE, bake_cost = {}, 0 local function bake(tid, ang) local S = SPR[tid] local w, h, cx, cy, R = S.w, S.h, S.cx, S.cy, S.R local th = ang * TAU / 360 local c, s = cos(th), sin(th) local out, n = {}, 0 local y0 = S.hang and -3 or -R for dy = y0, R do local run, rx, rc = 0, 0, 0 for dx = -R, R + 1 do local col = 0 if dx <= R then local ux, uy = dx + 0.5, dy + 0.5 local ix, iy = floor(ux * c - uy * s + cx), floor(ux * s + uy * c + cy) if ix >= 0 and ix < w and iy >= 0 and iy < h then col = S[iy + 1][ix + 1] end end if col ~= rc then if rc ~= 0 then n = n + 1; out[n] = char(rx + 64, dy + 64, run, rc) end rx, run, rc = dx, 0, col end run = run + 1 end end bake_cost = bake_cost + (2 * R + 1) * (R - y0 + 1) return table.concat(out) end local function pose(tid, ang) local idx = (ang // 5) % 72 local key = tid * 100 + idx local P = POSE[key] if P then return P end if bake_cost == 0 or bake_cost < 500 then P = bake(tid, idx * 5) POSE[key] = P return P end for d = 1, 4 do P = POSE[tid * 100 + (idx + d) % 72] or POSE[tid * 100 + (idx - d) % 72] if P then return P end end end local function draw_pose(P, ox, oy, pal, alpha) for k = 1, #P, 4 do local x, y, len, col = byte(P, k, k + 3) local c3 = col * 3 if alpha then blend_rect(ox + x - 64, oy + y - 64, len, 1, pal[c3 - 2], pal[c3 - 1], pal[c3], alpha) else rect(ox + x - 64, oy + y - 64, len, 1, pal[c3 - 2], pal[c3 - 1], pal[c3]) end end end local PRESETS = { classic = { { 150, 90, 190 }, { 214, 58, 52 }, { 246, 198, 62 }, { 70, 120, 208 }, { 64, 160, 84 }, { 232, 232, 236 } }, pastel = { { 246, 168, 188 }, { 150, 220, 184 }, { 190, 170, 232 }, { 250, 214, 160 }, { 150, 200, 240 }, { 246, 168, 188 } }, sunset = { { 60, 150, 150 }, { 232, 100, 50 }, { 150, 74, 158 }, { 244, 136, 92 }, { 60, 150, 150 }, { 250, 176, 70 } }, } local PALS = {} local KS = 1.0 local function mix(a, b, k) return floor(a + (b - a) * k + 0.5) end function on_settings() local set = PRESETS[cfg.cols or "classic"] or PRESETS.classic for i = 1, 6 do local m = set[i] PALS[i] = { m[1], m[2], m[3], floor(m[1] * 0.78), floor(m[2] * 0.78), floor(m[3] * 0.80), mix(m[1], 255, 0.65), mix(m[2], 255, 0.65), mix(m[3], 255, 0.65) } end KS = 0.55 + 0.225 * ((cfg.wind or 3) - 1) end local ITEMS = { { tid = 4, x = 12, lean = 1.2, per = 0.8, ph = 0.0 }, { tid = 1, x = 22, lean = 1.0, per = 1.15, ph = 1.7 }, { tid = 3, x = 33, lean = 0.85, per = 1.35, ph = 3.1 }, { tid = 2, x = 44, lean = 1.0, per = 1.2, ph = 4.4 }, { tid = 4, x = 53, lean = 1.2, per = 0.85, ph = 2.2 }, { tid = 4, x = 57, lean = 1.2, per = 0.9, ph = 5.2 }, } local CYCLE, RELEASE = 36, 12.4 local function gust(t) local tc = t % CYCLE local g = 0.2 + 0.1 * sin(TAU * t / 9 + 1) + 0.06 * sin(TAU * t / 4.1) local d = (tc - 12) / 2.4 g = g + 0.9 * exp(-d * d) d = (tc - 26) / 2.0 g = g + 0.5 * exp(-d * d) d = (tc - 33) / 1.5 g = g + 0.3 * exp(-d * d) return g > 1 and 1 or g end local function bar(x0, x1, y, r, g, b) local i0, i1 = floor(x0), floor(x1) if i0 == i1 then blend_rect(i0, y, 1, 1, r, g, b, x1 - x0) return end blend_rect(i0, y, 1, 1, r, g, b, 1 - (x0 - i0)) if i1 - i0 > 1 then rect(i0 + 1, y, i1 - i0 - 1, 1, r, g, b) end if x1 > i1 then blend_rect(i1, y, 1, 1, r, g, b, x1 - i1) end end local CLOUD_ROWS = { { 4, 10 }, { 1, 14 }, { 0, 16 }, { 2, 14 } } local CLOUDS = { { y = 4, x = 8, v = 1.6 }, { y = 17, x = 40, v = 1.0 }, { y = 31, x = 62, v = 0.7 } } local function draw_clouds(t) for i = 1, 3 do local c = CLOUDS[i] local cx = (c.x + t * c.v) % (W + 24) - 20 for r = 1, 4 do local seg = CLOUD_ROWS[r] if r == 4 then bar(cx + seg[1], cx + seg[2], c.y + r - 1, 208, 228, 246) else bar(cx + seg[1], cx + seg[2], c.y + r - 1, 250, 252, 255) end end end end local STREAK = { { 10, 0 }, { 31, 53 }, { 41, 97 } } function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS bake_cost = 0 draw_clouds(t) local g = gust(t) if g > 0.4 then local a = 0.55 * (g - 0.4) / 0.6 for i = 1, 3 do local s = STREAK[i] blend_rect((t * 74 + s[2]) % 150 - 40, s[1], 8 + i * 2, 1, 255, 255, 255, a) end end local u = t % CYCLE - RELEASE for i = 1, 6 do local it = ITEMS[i] local lx = it.x local ly = LINE_Y[lx] local lean = KS * it.lean * (4 + 30 * g) local fl = KS * (1.5 + 9 * g) * sin(TAU * t / it.per + it.ph) fl = fl + KS * (0.6 + 3 * g) * sin(TAU * t / (it.per * 0.43) + it.ph * 2) local ang = floor((lean + fl) / 15 + 0.5) * 15 if ang > 65 then ang = 65 elseif ang < -10 then ang = -10 end local pal = PALS[i] local present, alpha = true, nil if i == 1 then if u >= 0 and u < 8 then present = false elseif u >= 8 and u < 9 then alpha = u - 8 end end if present then local P = pose(it.tid, ang) if P then draw_pose(P, lx, ly + 1, pal, alpha) end end rect(lx - 1, ly - 1, 2, 3, 238, 232, 214) if i == 1 and u >= 0 and u < 2.8 then local fx = 12 + 6 * u + 7 * u * u local fy = 17 - 9 * (1 - exp(-1.8 * u)) + 2.2 * sin(3.2 * u) local P = pose(5, floor(u * 220 / 5) * 5) if P then draw_pose(P, floor(fx + 0.5), floor(fy + 0.5), pal) end end end end