info = { type = "image", title = "Dash bobble", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Bobblehead dog nods on a dashboard, road ahead", api = 44, } local tr = ({ sk = { ["Coat"] = "Srsť", ["Tan"] = "Hnedá", ["Black"] = "Čierna", ["White"] = "Biela", ["Light"] = "Svetlo", ["Day"] = "Deň", ["Sunset"] = "Západ", ["Bumps"] = "Hrby" }, cs = { ["Coat"] = "Srst", ["Tan"] = "Hnědá", ["Black"] = "Černá", ["White"] = "Bílá", ["Light"] = "Světlo", ["Day"] = "Den", ["Sunset"] = "Západ", ["Bumps"] = "Hrby" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "coat", label = T("Coat"), type = "int", default = "tan", opts = { keys = { "tan", "black", "white" }, T("Tan"), T("Black"), T("White") } }, { key = "light", label = T("Light"), type = "int", default = "day", opts = { keys = { "day", "sunset" }, T("Day"), T("Sunset") } }, { key = "bumps", label = T("Bumps"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, blit_indexed, blend_mask, snapshot = gfx.blit_rgb, gfx.blit_indexed, gfx.blend_mask, gfx.snapshot local floor, sin, cos, sqrt, abs, min, max = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local char, concat = string.char, table.concat local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local KR, KG, KB = 255, 0, 255 local HY, QMAX = 19, 19 local NF = 480 local XB, YB = 42, 37 local HEAD_CY, HEAD_L = 22, 15 local function mixc(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 COATS = { tan = { t = { 214, 150, 82 }, d = { 168, 108, 58 }, l = { 246, 216, 160 }, e = { 128, 76, 44 } }, black = { t = { 74, 70, 82 }, d = { 48, 46, 58 }, l = { 190, 150, 100 }, e = { 40, 38, 48 } }, white = { t = { 246, 244, 240 }, d = { 200, 196, 196 }, l = { 255, 255, 252 }, e = { 222, 176, 140 } }, } local LIGHTS = { day = { top = { 84, 150, 230 }, low = { 196, 226, 246 }, far = { 128, 164, 206 }, near = { 74, 134, 96 }, g1 = { 92, 176, 78 }, g2 = { 74, 152, 66 }, road = { 98, 98, 108 }, road2 = { 88, 88, 98 }, cl = { 255, 255, 255 }, sun = nil }, sunset = { top = { 66, 56, 140 }, low = { 255, 172, 104 }, far = { 150, 100, 150 }, near = { 70, 62, 110 }, g1 = { 86, 118, 70 }, g2 = { 70, 100, 62 }, road = { 104, 88, 104 }, road2 = { 92, 78, 94 }, cl = { 255, 190, 170 }, sun = { 255, 232, 156 } }, } local C, L = COATS.tan, LIGHTS.day local BUMP = 1.0 local TH, PS, HD, CV = {}, {}, {}, {} local function win(t, a, b, r) local v = max(0, min(1, min((t - a) / r, (b - t) / r))) return v * v * (3 - 2 * v) end local function curv(t) return -win(t, 2, 5.6, 0.9) + 0.8 * win(t, 9.4, 12.6, 0.9) end local function pulse(t, t0) local d = (t - t0) / 0.07 return math.exp(-d * d) end local CVF, BF = {}, {} local SIM = { th = 0, vth = 0, ps = 0, vps = 0, hd = 0 } local function sim_pass(store) local dt = 1 / FPS local th, vth, ps, vps, hd = SIM.th, SIM.vth, SIM.ps, SIM.vps, SIM.hd for i = 1, NF do local c, b = CVF[i], BF[i] local f = i - 1 local ax = -c * 43 + 26 * b * BUMP * ((f / FPS) > 13 and -1 or 1) vth = vth + dt * (ax - 114 * th - 1.92 * vth); th = th + dt * vth local az = 258 * b * BUMP + 40 * sin(TAU * 3.125 * f / FPS) * BUMP vps = vps + dt * (az - 310 * ps - 5.3 * vps); ps = ps + dt * vps hd = hd + c * dt if store then TH[i], PS[i], HD[i], CV[i] = th, ps, hd, c end end SIM.th, SIM.vth, SIM.ps, SIM.vps, SIM.hd = th, vth, ps, vps, hd end local function sim_start() for i = 1, NF do local t = (i - 1) / FPS CVF[i] = curv(t) BF[i] = pulse(t, 7.3) + 0.6 * pulse(t, 13.9) + 0.5 * pulse(t, 14.45) end SIM.th, SIM.vth, SIM.ps, SIM.vps, SIM.hd = 0, 0, 0, 0, 0 sim_pass(false) end local function sim_finish() SIM.hd = 0 sim_pass(true) local drift = HD[NF] for i = 1, NF do HD[i] = HD[i] - drift * i / NF end end local SKY, FAR, NEAR, OV = nil, nil, nil, nil local function bake_scene() for y = 0, HY do rect(0, y, W, 1, mixc(L.top, L.low, y / HY)) end SKY = snapshot(0, 0, W, HY + 1) for k = 1, 2 do local h0, col, fq = (k == 1) and 5 or 3.2, (k == 1) and L.far or L.near, (k == 1) and 2 or 3 rect(0, 0, W, 12, KR, KG, KB) for x = 0, W - 1 do local a = x * TAU / 64 local h = h0 + 1.6 * sin(a * fq + k) + 1.1 * sin(a * (fq + 3) + 0.3 * k) + (k == 1 and 0 or 0.6 * sin(a * 5 + 2)) local hh = floor(h + 0.5) rect(x, 12 - hh, 1, hh, col[1], col[2], col[3]) end if k == 1 then FAR = snapshot(0, 0, W, 12) else NEAR = snapshot(0, 0, W, 12) end end end local function disc(cx, cy, r, c1, c2, c3) for y = -r, r do local w = floor(sqrt(r * r + 0.25 - y * y) + 0.5) rect(floor(cx) - w, floor(cy) + y, 2 * w + 1, 1, c1, c2, c3) end end local function bake_dash() rect(0, 0, W, H, KR, KG, KB) rect(0, 0, W, 3, 30, 26, 32); rect(0, 3, W, 1, 54, 48, 54) for y = 0, 39 do local w = 9 - floor(y * 7 / 40) rect(0, y, w, 1, 30, 26, 32); rect(w, y, 1, 1, 66, 60, 66) rect(W - w, y, w, 1, 30, 26, 32); rect(W - w - 1, y, 1, 1, 66, 60, 66) end rect(31, 3, 2, 3, 40, 36, 40); rect(26, 5, 12, 4, 26, 24, 30); rect(27, 6, 10, 2, 112, 140, 164) rect(0, 39, W, 1, 214, 170, 124) rect(0, 40, W, 3, 156, 108, 76); rect(0, 43, W, 3, 142, 98, 70); rect(0, 46, W, 2, 126, 86, 62) rect(0, 48, W, 16, 92, 60, 46) for i = 0, 2 do rect(14 + i * 5, 41, 3, 1, 70, 44, 36) end rect(50, 50, 13, 1, 70, 44, 36); rect(52, 53, 8, 5, 74, 48, 40) rect(53, 54, 6, 3, 110, 78, 62) rect(3, 43, 26, 9, 32, 30, 38) for k = 0, 1 do local cx = 10 + k * 11 disc(cx, 47, 3, 22, 26, 34) for a = 0, 4 do px(cx + floor(-3.2 * cos(a * 0.785) + 0.5), 47 - floor(3.2 * sin(a * 0.785) + 0.5), 130, 140, 160) end end local cx, cy, ro, ri = 14, 68, 18, 14 for y = 50, H - 1 do local dy = y - cy local xo = ro * ro - dy * dy if xo > 0 then local wo = floor(sqrt(xo) + 0.5) local xi = ri * ri - dy * dy local wi = xi > 0 and floor(sqrt(xi) + 0.5) or 0 if wi > 0 then rect(cx - wo, y, wo - wi, 1, 28, 26, 30); rect(cx + wi, y, wo - wi, 1, 28, 26, 30) else rect(cx - wo, y, 2 * wo, 1, 28, 26, 30) end end end rect(cx - 8, 50, 16, 1, 82, 78, 86) rect(cx - 4, 56, 8, 8, 66, 66, 78); rect(cx - 2, 58, 4, 2, 150, 150, 164) blend_rect(XB - 12, 40, 6, 6, 40, 20, 14, 0.3) rect(XB - 5, 46, 10, 2, 56, 46, 46) local tr_, td_, tl_ = C.t, C.d, C.l local rows = { { 3, 38 }, { 4, 39 }, { 5, 40 }, { 5, 41 }, { 5, 42 }, { 5, 43 }, { 5, 44 }, { 4, 45 } } for _, r in ipairs(rows) do rect(XB - r[1], r[2], 2 * r[1] + 1, 1, tr_[1], tr_[2], tr_[3]) end for y = 39, 45 do rect(XB + 3, y, 2, 1, td_[1], td_[2], td_[3]) end rect(XB - 2, 40, 4, 5, tl_[1], tl_[2], tl_[3]) rect(XB - 5, 45, 3, 2, tl_[1], tl_[2], tl_[3]); rect(XB + 2, 45, 3, 2, tl_[1], tl_[2], tl_[3]) OV = snapshot(0, 0, W, H) end local HW, HH = 17, 15 local IDX = { t = 1, d = 2, l = 3, e = 4, k = 5, w = 6, p = 7, o = 8 } local HEAD local function build_head() local g = {} for y = 0, HH - 1 do g[y] = {}; for x = 0, HW - 1 do g[y][x] = "." end end local function el(x, y, cx, cy, rx, ry) local a, b = (x - cx) / rx, (y - cy) / ry return a * a + b * b <= 1 end for y = 0, HH - 1 do for x = 0, HW - 1 do local c = "." if el(x, y, 8, 7.2, 7.3, 6.6) then c = "t" if 0.5 * (x - 8) / 7.3 + 0.5 * (y - 7.2) / 6.6 > 0.42 then c = "d" end end if el(x, y, 2.2, 8, 2.7, 5) or el(x, y, 13.8, 8, 2.7, 5) then c = "e" end if el(x, y, 8, 10, 3.7, 2.7) then c = "l" end if x >= 7 and x <= 9 and y >= 1 and y <= 6 and c ~= "." then c = "l" end g[y][x] = c end end for y = 0, HH - 1 do for x = 0, HW - 1 do if g[y][x] ~= "." and g[y][x] ~= "o" then local edge = x == 0 or y == 0 or x == HW - 1 or y == HH - 1 or g[y][x - 1] == "." or g[y][x + 1] == "." or g[y - 1][x] == "." or g[y + 1][x] == "." if edge then g[y][x] = "O" end end end end for y = 0, HH - 1 do for x = 0, HW - 1 do if g[y][x] == "O" then g[y][x] = "o" end end end local function put(x, y, c) g[y][x] = c end for _, e in ipairs({ 4, 11 }) do for y = 4, 6 do put(e, y, "k"); put(e + 1, y, "k") end put(e + (e == 4 and 0 or 1), 4, "w") end put(4, 3, "o"); put(5, 3, "o"); put(11, 3, "o"); put(12, 3, "o") for x = 7, 9 do put(x, 8, "k") end put(8, 9, "k"); put(6, 10, "k"); put(7, 11, "k"); put(9, 11, "k"); put(10, 10, "k"); put(8, 10, "k") put(8, 12, "p"); put(8, 11, "p") local rows = {} for y = 0, HH - 1 do rows[y + 1] = concat(g[y], "", 0, HW - 1) end local s = concat(rows) local cmap = { ["."] = "\0" } for ch, i in pairs(IDX) do cmap[ch] = char(i) end local hd = { cells = s:gsub(".", cmap), core = nil, mask = {}, trail = {} } local core, tm = {}, {} for y = 0, HH - 1 do for x = 0, HW - 1 do local c = s:sub(y * HW + x + 1, y * HW + x + 1) local lft = x > 0 and s:sub(y * HW + x, y * HW + x) or "." local trailing = c ~= "." and lft == "." core[#core + 1] = trailing and "\0" or (c == "." and "\0" or char(IDX[c])) tm[#tm + 1] = c end end hd.core = concat(core) for ch, k in pairs(IDX) do local m, t = {}, {} for i = 1, HW * HH do local c = tm[i] local lft = ((i - 1) % HW > 0) and tm[i - 1] or "." m[i] = c == ch and "\255" or "\0" t[i] = (c == ch and lft == ".") and "\255" or "\0" end hd.mask[k], hd.trail[k] = concat(m), concat(t) end HEAD = hd end local PALH = {} local function head_colours() PALH = { 0, 0, 0 } local cols = { C.t, C.d, C.l, C.e, { 30, 22, 26 }, { 255, 255, 255 }, { 236, 110, 130 }, { floor(C.e[1] * 0.55), floor(C.e[2] * 0.5), floor(C.e[3] * 0.5) } } for i = 1, 8 do PALH[#PALH + 1], PALH[#PALH + 2], PALH[#PALH + 3] = cols[i][1], cols[i][2], cols[i][3] end end local STAGE = 1 function on_settings() C, L = COATS[cfg.coat] or COATS.tan, LIGHTS[cfg.light] or LIGHTS.day BUMP = 0.4 + 0.3 * (cfg.bumps or 3) STAGE = 1 SKY = nil end local function span(x0, x1, y, r, g, b, a) local i0, i1 = floor(x0), floor(x1) if i0 == i1 then blend_rect(i0, y, 1, 1, r, g, b, a * (x1 - x0)) return end blend_rect(i0, y, 1, 1, r, g, b, a * (1 - (x0 - i0))) if i1 > i0 + 1 then blend_rect(i0 + 1, y, i1 - i0 - 1, 1, r, g, b, a) end if x1 > i1 then blend_rect(i1, y, 1, 1, r, g, b, a * (x1 - i1)) end end local NT = 12 local TZ, TX, TS = {}, {}, {} for i = 1, NT do local h = hash32(i * 0x9E3779B1) TZ[i] = 1 + (i - 1) * (16 / NT) TX[i] = (i % 2 == 0 and -1 or 1) * (1.5 + umod(h, 18) / 10) TS[i] = 0.8 + umod(h >> 8, 5) / 10 end local ZSPAN = 17 local function tree(x, ybase, q, s, g) local th = max(2, floor(q * 0.9 * s)) local tw = max(1, floor(q * 0.12 + 0.5)) local cw = max(2, floor(q * 0.55 * s)) rect(x - tw // 2, ybase - th // 2, tw, th // 2 + 1, 92, 60, 40) rect(x - cw // 2, ybase - th, cw, th // 2 + 1, g[1], g[2], g[3]) if cw > 3 then rect(x - cw // 2 + 1, ybase - th - 1, cw - 2, 1, g[1], g[2], g[3]) end end function render() if STAGE <= 4 then if STAGE == 1 then sim_start() elseif STAGE == 2 then sim_finish() elseif STAGE == 3 then build_head(); head_colours() else bake_scene() end STAGE = STAGE + 1 gfx.clear(0, 0, 0) gfx.ready(false) return end if STAGE == 5 then bake_dash(); STAGE = 6 end local fi = frame % NF + 1 local t = frame / FPS local cv, hd = CV[fi], HD[fi] blit_rgb(SKY, 0, 0, W, HY + 1) if L.sun then local sx = 18 - hd * 7 for y = -4, 4 do local w = sqrt(max(0, 17 - y * y)); span(sx - w, sx + w, 13 + y, L.sun[1], L.sun[2], L.sun[3], 0.95) end end for i = 1, 3 do local x = (i * 23 + t * (0.7 + 0.3 * i) - hd * 4) % (W + 20) - 10 span(x + 2, x + 9, 2 + i * 3, L.cl[1], L.cl[2], L.cl[3], 0.6) span(x, x + 11, 3 + i * 3, L.cl[1], L.cl[2], L.cl[3], 0.5) end local o1 = floor(hd * 14) % 64 blit_rgb(FAR, -o1, HY - 11, W, 12, KR, KG, KB); blit_rgb(FAR, 64 - o1, HY - 11, W, 12, KR, KG, KB) local o2 = floor(hd * 30) % 64 blit_rgb(NEAR, -o2, HY - 11, W, 12, KR, KG, KB); blit_rgb(NEAR, 64 - o2, HY - 11, W, 12, KR, KG, KB) local g1, g2, rd, rd2 = L.g1, L.g2, L.road, L.road2 for q = 1, QMAX do local y = HY + q local z = 20 / q local par = floor(0.9 * (z + 3 * t)) % 2 local u = (QMAX - q) / QMAX local xc = 32 + cv * 15 * u * u local hw = 1.3 * q + 0.3 local gc = par == 0 and g1 or g2 rect(0, y, W, 1, gc[1], gc[2], gc[3]) local x0, x1 = floor(xc - hw + 0.5), floor(xc + hw + 0.5) local rc = par == 0 and rd or rd2 rect(x0, y, x1 - x0, 1, rc[1], rc[2], rc[3]) local rw = max(1, floor(q / 8 + 0.5)) if par == 0 then rect(x0 - rw, y, rw, 1, 220, 60, 54); rect(x1, y, rw, 1, 220, 60, 54) if q >= 2 then rect(floor(xc + 0.5) - (q > 9 and 1 or 0), y, q > 9 and 2 or 1, 1, 240, 240, 226) end else rect(x0 - rw, y, rw, 1, 236, 236, 236); rect(x1, y, rw, 1, 236, 236, 236) end end blend_rect(0, HY - 1, W, 3, L.low[1], L.low[2], L.low[3], 0.35) local v = 3.2 local zs, far = {}, 1 for i = 1, NT do zs[i] = 1 + (TZ[i] - 1 - v * t) % ZSPAN if zs[i] > zs[far] then far = i end end for k = 0, NT - 1 do local i = (far - 1 - k) % NT + 1 local z = zs[i] local q = 20 / z if q <= QMAX + 3 then local u = max(0, (QMAX - q) / QMAX) local xc = 32 + cv * 15 * u * u local x = floor(xc + TX[i] * (1.3 * q + 0.3) + 0.5) tree(x, floor(HY + q + 0.5), q, TS[i], (i % 3 == 0) and L.near or { 40, 118, 52 }) end end blit_rgb(OV, 0, 0, W, H, KR, KG, KB) local sp = 0.5 + 0.35 * sin(t * 0.7) - 0.1 * abs(cv) px(10 + floor(-2 * cos(sp * 3) + 0.5), 47 - floor(2 * sin(sp * 3) + 0.5), 255, 90, 70) px(21 + floor(-2 * cos(1.1 + 0.2 * sin(t)) + 0.5), 47 - floor(2 * sin(1.1 + 0.2 * sin(t)) + 0.5), 255, 90, 70) if abs(cv) > 0.15 and (frame // 8) % 2 == 0 then px(cv < 0 and 4 or 27, 50, 90, 255, 120); px(cv < 0 and 5 or 26, 50, 90, 255, 120) end local phi = max(-0.62, min(0.62, TH[fi])) local hx = XB + HEAD_L * sin(phi) local hy = floor(HEAD_CY + HEAD_L * (1 - cos(phi)) - PS[fi] + 0.5) local hb = hy + 7 local n = YB - hb for r = 0, n do local tt = r / n local xc = XB + (hx - XB) * tt * tt local y = YB - r if r % 2 == 0 then span(xc - 2.5, xc + 2.5, y, 200, 206, 218, 1) else span(xc - 2, xc + 2, y, 104, 110, 126, 1) end end local lx = hx - 8 local ix = floor(lx) local f = lx - ix local top = hy - 7 blit_indexed(HEAD.core, PALH, ix, top, HW, HH, HW, HH, true) if f > 0.03 then for k = 1, 8 do blend_mask(HEAD.mask[k], PALH[k * 3 + 1], PALH[k * 3 + 2], PALH[k * 3 + 3], ix + 1, top, HW, HH, HW, HH, f) end end if f < 0.97 then for k = 1, 8 do blend_mask(HEAD.trail[k], PALH[k * 3 + 1], PALH[k * 3 + 2], PALH[k * 3 + 3], ix, top, HW, HH, HW, HH, 1 - f) end end end