info = { type = "image", title = "Elephant spray", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "An elephant strolls past and sprays its back", api = 33, } local tr = ({ sk = { ["Scene"] = "Scéna", ["Savanna"] = "Savana", ["Meadow"] = "Lúka", ["Walk"] = "Chôdza", ["Calm"] = "Pokojná", ["Brisk"] = "Svižná" }, cs = { ["Scene"] = "Scéna", ["Savanna"] = "Savana", ["Meadow"] = "Louka", ["Walk"] = "Chůze", ["Calm"] = "Klidná", ["Brisk"] = "Svižná" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "scene", label = T("Scene"), type = "int", default = "savanna", opts = { keys = { "savanna", "meadow" }, T("Savanna"), T("Meadow") } }, { key = "pace", label = T("Walk"), type = "int", default = "calm", opts = { keys = { "calm", "brisk" }, T("Calm"), T("Brisk") } }, } local rect, px, blend, blend_rect, blit = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blit_indexed local floor, ceil, sin, sqrt, max, abs = math.floor, math.ceil, math.sin, math.sqrt, math.max, math.abs local hash32, umod = math.hash32, math.umod local W, FPS = gfx.W, gfx.FPS local HOR, GY = 45, 60 local SCN = { savanna = { s0 = { 118, 152, 170 }, s1 = { 232, 206, 160 }, hill = { 190, 160, 124 }, tree = { 84, 80, 60 }, g0 = { 178, 164, 96 }, g1 = { 140, 126, 70 }, path = { 176, 138, 98 }, edge = { 148, 114, 82 }, tuft = { 112, 100, 54 }, tuftl = { 204, 190, 112 }, sun = { 255, 236, 170 } }, meadow = { s0 = { 118, 184, 226 }, s1 = { 218, 238, 230 }, hill = { 150, 196, 128 }, tree = { 70, 132, 74 }, g0 = { 128, 188, 88 }, g1 = { 92, 150, 70 }, path = { 190, 164, 116 }, edge = { 158, 130, 92 }, tuft = { 66, 124, 60 }, tuftl = { 176, 222, 110 }, sun = { 255, 250, 214 } }, } local SC local function lerp3(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, cr, cg, cb, a) for j = -r, r do local hw = floor(sqrt(r * r - j * j) + 0.5) blend_rect(cx - hw, cy + j, 2 * hw + 1, 1, cr, cg, cb, a) end end local function span(y, a, b, r, g, bl, al) local fa, fb = floor(a), floor(b) if fa == fb then if b > a then blend(fa, y, r, g, bl, (b - a) * al) end return end if a > fa then blend(fa, y, r, g, bl, (1 - a + fa) * al); fa = fa + 1 end if fb > fa then blend_rect(fa, y, fb - fa, 1, r, g, bl, al) end if b > fb then blend(fb, y, r, g, bl, (b - fb) * al) end end local BG local function build_bg() SC = SCN[cfg.scene] or SCN.savanna for y = 0, HOR - 1 do rect(0, y, W, 1, lerp3(SC.s0, SC.s1, y / (HOR - 1))) end local sun = SC.sun disc(49, 17, 10, sun[1], sun[2], sun[3], 0.14) disc(49, 17, 7, sun[1], sun[2], sun[3], 0.3) disc(49, 17, 5, sun[1], sun[2], sun[3], 1) local h = SC.hill for x = 0, W - 1 do local top = HOR - 4 - floor(2.6 * sin(x * 0.13 + 1) + 1.6 * sin(x * 0.31 + 2) + 0.5) rect(x, top, 1, HOR - top, h[1], h[2], h[3]) end local t = SC.tree if cfg.scene == "meadow" then for _, x in ipairs({ 8, 54 }) do rect(x - 1, HOR - 8, 2, 9, 96, 70, 46) for j = -6, 6 do local hw = floor(sqrt(36 - j * j) + 0.5) rect(x - hw, HOR - 15 + j + 6, 2 * hw, 1, t[1], t[2], t[3]) end rect(x - 3, HOR - 13, 4, 2, t[1] + 26, t[2] + 28, t[3] + 22) end else for _, x in ipairs({ 9, 55 }) do rect(x - 1, HOR - 9, 2, 10, t[1], t[2], t[3]) rect(x - 10, HOR - 12, 20, 2, t[1], t[2], t[3]) rect(x - 6, HOR - 14, 12, 2, t[1], t[2], t[3]) rect(x - 13, HOR - 10, 6, 1, t[1], t[2], t[3]) end end for y = HOR, 63 do rect(0, y, W, 1, lerp3(SC.g0, SC.g1, (y - HOR) / (63 - HOR))) end rect(0, 55, W, 7, SC.path[1], SC.path[2], SC.path[3]) rect(0, 55, W, 1, SC.edge[1], SC.edge[2], SC.edge[3]) rect(0, 61, W, 1, SC.edge[1], SC.edge[2], SC.edge[3]) for i = 1, 26 do local k = hash32(i * 0x9E3779B1) local x, y = umod(k, W), 47 + umod(k >> 8, 8) if i % 3 == 0 then y = 62 + umod(k >> 8, 2) end local c = (i % 2 == 0) and SC.tuft or SC.tuftl rect(x, y - 1, 1, 2, c[1], c[2], c[3]) end if cfg.scene == "meadow" then for i = 1, 14 do local k = hash32(i * 0x85EBCA6B) px(umod(k, W), 48 + umod(k >> 8, 6), 255, i % 2 == 0 and 250 or 214, i % 2 == 0 and 240 or 90) end end BG = gfx.snapshot(0, 0, W, gfx.H) end local GW, GH = 28, 24 local PAL = { 0, 0, 0, 44, 40, 50, 148, 148, 156, 112, 112, 124, 178, 164, 172, 238, 232, 214, 18, 16, 26, 206, 138, 150 } local function put(g, x, y, c) if x >= 0 and x < GW and y >= 0 and y < GH then g[y * GW + x + 1] = c end end local function box(g, x, y, w, h, c) for j = y, y + h - 1 do for i = x, x + w - 1 do put(g, i, j, c) end end end local function ell(g, cx, cy, rx, ry, c) for j = floor(cy - ry), ceil(cy + ry) do for i = floor(cx - rx), ceil(cx + rx) do local dx, dy = (i - cx) / rx, (j - cy) / ry if dx * dx + dy * dy <= 1 then put(g, i, j, c) end end end end local function poly(g, p, c, th) for k = 1, #p - 1 do local a, b = p[k], p[k + 1] local n = max(abs(b[1] - a[1]), abs(b[2] - a[2])) * 2 for s = 0, n do local u = s / n box(g, floor(a[1] + (b[1] - a[1]) * u + 0.5), floor(a[2] + (b[2] - a[2]) * u + 0.5), th, th, c) end end end local TRUNK = { hang = { { 22, 15 }, { 23, 18 }, { 23, 20 }, { 24, 21 } }, T1 = { { 22, 15 }, { 25, 12 }, { 26, 9 } }, T2 = { { 22, 15 }, { 24, 11 }, { 25, 7 }, { 23, 4 }, { 20, 3 } }, T3 = { { 22, 15 }, { 24, 11 }, { 25, 7 }, { 24, 4 }, { 21, 2 } }, } local TIP = { T2 = { 19.5, 3 }, T3 = { 20.5, 2 } } local function bake(key) local g = {} for i = 1, GW * GH do g[i] = 0 end local s1, l1, s2, l2 = 0, 0, 0, 0 if key == "A" then s1, l1, s2, l2 = 1, 1, -1, 0 elseif key == "B" then s1, l1, s2, l2 = -1, 0, 1, 1 end box(g, 3 + s2, 17, 3, 7 - l2, 3) box(g, 13 + s1, 17, 3, 7 - l1, 3) poly(g, { { 2, 12 }, { 1, 14 }, { 1, 17 } }, 3, 1) ell(g, 10.5, 15, 8.8, 5.3, 2) for y = 18, 19 do for x = 0, GW - 1 do if g[y * GW + x + 1] == 2 then g[y * GW + x + 1] = 3 end end end ell(g, 19, 13.5, 5, 5.3, 2) ell(g, 15.5, 13, 3.6, 4.8, 3) ell(g, 15.5, 13, 2.6, 3.8, 4) box(g, 7 + s1, 19, 3, 5 - l1, 2) box(g, 16 + s2, 19, 3, 5 - l2, 2) poly(g, TRUNK[(key == "T1" or key == "T2" or key == "T3") and key or "hang"], 2, 2) put(g, 21, 12, 6); put(g, 20, 15, 7); put(g, 21, 15, 7) box(g, 21, 18, 1, 3, 5) local o = {} for y = 0, GH - 1 do for x = 0, GW - 1 do if g[y * GW + x + 1] == 0 and (g[y * GW + x] ~= 0 and x > 0 or g[y * GW + x + 2] ~= 0 and x < GW - 1 or y > 0 and g[(y - 1) * GW + x + 1] ~= 0 or y < GH - 1 and g[(y + 1) * GW + x + 1] ~= 0) then o[#o + 1] = y * GW + x + 1 end end end for i = 1, #o do g[o[i]] = 1 end local ed = {} for y = 0, GH - 1 do for x = 0, GW - 1 do local v = g[y * GW + x + 1] if v ~= 0 and ((x == 0 or g[y * GW + x] == 0) or (x == GW - 1 or g[y * GW + x + 2] == 0)) then ed[#ed + 1] = x; ed[#ed + 1] = y; ed[#ed + 1] = v end end end for i = 1, #ed, 3 do g[ed[i + 1] * GW + ed[i] + 1] = 0 end local rows = {} for y = 0, GH - 1 do rows[y + 1] = string.char(table.unpack(g, y * GW + 1, y * GW + GW)) end return { core = table.concat(rows), ed = ed } end local POSES = {} local function sprite(key, x, y) local P = POSES[key] if not P then P = bake(key); POSES[key] = P end local ix = floor(x) local f = x - ix blit(P.core, PAL, ix, y, GW, GH, GW, GH, true) local ed, a = P.ed, 1 - f for i = 1, #ed, 3 do local c = ed[i + 2] * 3 local ex, ey, r, g, b = ix + ed[i], y + ed[i + 1], PAL[c + 1], PAL[c + 2], PAL[c + 3] blend(ex, ey, r, g, b, a) if f > 0.02 then blend(ex + 1, ey, r, g, b, f) end end end local START, STOP, XEND, RAMP = -29, 18, 64, 16 local V, E1, TS, TL, TP, TX, C = 0.5, 0, 0, 0, 0, 0, 0 local SPRAY = 96 function on_settings() V = cfg.pace == "brisk" and 1.0 or 0.5 BG = nil E1 = (STOP - START - V * RAMP / 2) / V + RAMP TS = E1 + 22 TL = TS + SPRAY TP = TL + 12 TX = TP + 8 C = TX + (XEND - STOP - V * RAMP / 2) / V + RAMP + 60 end local function xat(t) if t < E1 - RAMP then return START + V * t end if t < E1 then local u = t - (E1 - RAMP) return START + V * (E1 - RAMP) + V * u - V * u * u / (2 * RAMP) end if t < TX then return STOP end local u = t - TX if u < RAMP then return STOP + V * u * u / (2 * RAMP) end return STOP + V * RAMP / 2 + V * (u - RAMP) end local WALK = { "A", "N", "B", "N" } local DROP, DROP_L = { 196, 230, 246 }, 20 local function dot(x, y, r, g, b, a) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, r, g, b, a * (1 - fx) * (1 - fy)) blend(ix + 1, iy, r, g, b, a * fx * (1 - fy)) blend(ix, iy + 1, r, g, b, a * (1 - fx) * fy) blend(ix + 1, iy + 1, r, g, b, a * fx * fy) end local CLOUDS = { { 11, 22, 0.035, 8 }, { 24, 15, -0.025, 40 }, { 6, 18, 0.02, 52 } } local function clouds(f) for i = 1, #CLOUDS do local c = CLOUDS[i] local L = c[2] local x = (c[4] + f * c[3]) % (W + L) - L span(c[1], x + 4, x + L - 4, 255, 255, 255, 0.45) span(c[1] + 1, x, x + L, 255, 255, 255, 0.45) span(c[1] + 2, x + 2, x + L - 2, 255, 255, 255, 0.3) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, gfx.H) clouds(frame) local t = frame % C if t >= TX + (XEND - STOP - V * RAMP / 2) / V + RAMP then return end local x = xat(t) if x >= W then return end local key = "N" local ts = t - TS if t < E1 - 6 or (t >= TX and t < TX + RAMP + 40) then key = WALK[floor((x - START) / (8 * V)) % 4 + 1] elseif t >= E1 + 10 and t < TP then if t < E1 + 16 then key = "T1" elseif t < TS then key = "T2" elseif t < TL then key = (ts // 8) % 2 == 0 and "T2" or "T3" elseif t < TL + 6 then key = "T2" else key = "T1" end end blend_rect(floor(x) + 3, GY + 1, 22, 2, 30, 24, 16, 0.3) sprite(key, x, GY - 23) if ts >= 0 and t < TP + 8 then local first = ts // 3 for k = 0, 7 do local s = (first - k) * 3 local age = ts - s if s >= 0 and s <= SPRAY - 3 and age < DROP_L + 3 then local tip = TIP[(s // 8) % 2 == 0 and "T2" or "T3"] local tx, ty = STOP + tip[1], GY - 23 + tip[2] local dx, dy = tx - 0.55 * age, ty - 1.3 * age + 0.0825 * age * age if age < DROP_L then dot(dx, dy, DROP[1], DROP[2], DROP[3], 1) local a1 = age - 1 dot(tx - 0.55 * a1, ty - 1.3 * a1 + 0.0825 * a1 * a1, DROP[1], DROP[2], DROP[3], 0.45) else local sx, sy = floor(dx), floor(dy) blend(sx - 1, sy - 1, DROP[1], DROP[2], DROP[3], 0.8) blend(sx + 1, sy - 1, DROP[1], DROP[2], DROP[3], 0.8) end end end end end