info = { type = "image", title = "Dusk kaiju", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Giant dino strides behind a dusk skyline", api = 44, } local tr = ({ sk = { ["Pace"] = "Tempo", ["Birds"] = "Vtáky", ["Shake"] = "Otrasy" }, cs = { ["Pace"] = "Tempo", ["Birds"] = "Ptáci", ["Shake"] = "Otřesy" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "pace", label = T("Pace"), type = "int", min = 1, max = 5, default = 3 }, { key = "birds", label = T("Birds"), type = "int", min = 0, max = 8, default = 6 }, { key = "shake", label = T("Shake"), type = "bool", default = true }, } 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, cos, sqrt, max, min = math.floor, math.sin, math.cos, math.sqrt, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local function lerp(a, b, t) return floor(a + (b - a) * t + 0.5) end local KEY = { 255, 0, 255 } local NEAR = { 14, 10, 30 } local ROWS = 52 local SPAN = { {}, {} } local SPIKES = { {}, {} } local function ell(S, cx, cy, rx, ry) for y = max(0, floor(cy - ry)), min(ROWS - 1, floor(cy + ry)) do local t = (y + 0.5 - cy) / ry if t * t < 1 then local hw = rx * sqrt(1 - t * t) local s = S[y] if not s then S[y] = { cx - hw, cx + hw } else if cx - hw < s[1] then s[1] = cx - hw end if cx + hw > s[2] then s[2] = cx + hw end end end end end local function build_dino() for p = 0, 1 do local S = {} ell(S, 0, 32, 19, 13) for i = 0, 8 do ell(S, 14 + i * 1.3, 27 - i * 1.6 + p * i / 8, 6 - i * 0.15, 6 - i * 0.15) end ell(S, 29, 12 + p, 8.5, 5.5); ell(S, 35, 14 + p, 6, 3.5); ell(S, 33, 17 + p, 5, 2) for i = 0, 14 do local r = max(0.8, 6.5 - i * 0.42); ell(S, -14 - i * 2.4, 33 - i * 0.55 - p * i / 14, r, r) end SPAN[p + 1] = S local sp = {} for x = -44, 8, 6 do for y = 0, ROWS - 1 do local s = S[y] if s and x >= s[1] and x <= s[2] then sp[#sp + 1] = { x, y } break end end end SPIKES[p + 1] = sp end end local SKY, CITY = nil, nil local ROOF = {} local STARS = {} local function build_sky() local stops = { { 0, 36, 22, 84 }, { 24, 96, 38, 120 }, { 44, 214, 92, 96 }, { 63, 252, 160, 96 } } for y = 0, H - 1 do local s = y < 24 and 1 or (y < 44 and 2 or 3) local a, b = stops[s], stops[s + 1] local t = (y - a[1]) / (b[1] - a[1]) rect(0, y, W, 1, lerp(a[2], b[2], t), lerp(a[3], b[3], t), lerp(a[4], b[4], t)) end for i = 1, 14 do local h = hash32(i * 0x9E3779B1) blend(umod(h, W), umod(h >> 8, 20), 226, 216, 255, 0.5 + umod(h >> 16, 4) / 10) end local x = 0 while x < W do local w = 4 + umod(hash32(x * 31 + 5), 6) rect(x, 42 + umod(hash32(x * 17 + 3), 9), w, 22, 112, 46, 100) x = x + w end SKY = snapshot(0, 0, W, H) end local FLICK = {} local function build_city() rect(0, 0, W, H, KEY[1], KEY[2], KEY[3]) local x, n = 0, 0 while x < W do n = n + 1 local h = hash32(n * 0x85EBCA6B) local w = 6 + umod(h, 5) local top = 40 + umod(h >> 6, 12) if n % 5 == 3 then top = 31 + umod(h >> 6, 4) end rect(x, top, w, H - top, NEAR[1], NEAR[2], NEAR[3]) rect(x, top, w, 1, 30, 20, 58) for cx = x, x + w - 1 do ROOF[cx] = top end if umod(h >> 12, 3) == 0 then rect(x + w // 2, top - 4, 1, 4, NEAR[1], NEAR[2], NEAR[3]) end for wy = top + 3, H - 4, 4 do for wx = x + 1, x + w - 3, 3 do local q = umod(hash32(wx * 7 + wy * 131), 100) if q < 28 then rect(wx, wy, 1, 2, 255, 206, 96) elseif q < 32 and #FLICK < 6 then FLICK[#FLICK + 1] = { wx, wy } end end end x = x + w + (umod(h >> 20, 4) == 0 and 1 or 0) end CITY = snapshot(0, 0, W, H) end local BIRDS = {} local WING = { { { -2, -1 }, { -1, 0 }, { 0, 0 }, { 1, 0 }, { 2, -1 } }, { { -2, 0 }, { -1, 0 }, { 0, 0 }, { 1, 0 }, { 2, 0 } }, { { -2, 1 }, { -1, 0 }, { 0, 0 }, { 1, 0 }, { 2, 1 } } } local WSEQ = { 1, 2, 3, 2 } local function splat(x, y, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local gx, gy = 1 - fx, 1 - fy blend(ix, iy, r, g, b, gx * gy) if fx > 0.04 then blend(ix + 1, iy, r, g, b, fx * gy) end if fy > 0.04 then blend(ix, iy + 1, r, g, b, gx * fy) end if fx > 0.04 and fy > 0.04 then blend(ix + 1, iy + 1, r, g, b, fx * fy) end end local V, SP, TW, AW = 5.5, 33, 30, 20 local OX0, TRAVEL = -42, 156 function on_settings() local v = 2.5 + (cfg.pace or 3) V, SP = v, floor(6 * FPS / v) TW = (TRAVEL / v) + 2 AW = 108 / v + 1 BIRDS = {} local n = 0 for i = 1, 60 do if n >= (cfg.birds or 6) then break end local x = 3 + umod(hash32(i * 0x27D4EB2F), W - 8) if ROOF[x] and ROOF[x + 2] == ROOF[x] then local ok = true for j = 1, n do if math.abs(BIRDS[j].x - x) < 8 then ok = false end end if ok then n = n + 1 BIRDS[n] = { x = x, y = ROOF[x], dir = n % 2 == 0 and -1 or 1, t0 = (x - 14) / v } end end end end local FD = 2.4 local function flight(b, tau, dir) return b.x + 1 + dir * (9 * tau + 2 * tau * tau), b.y - 2 - 12 * tau - 3 * tau * tau end local function draw_birds(tc, oy) for i = 1, #BIRDS do local b = BIRDS[i] local ph = (tc - b.t0) % TW if ph >= AW then rect(b.x, b.y - 2 + oy, 3, 1, NEAR[1], NEAR[2], NEAR[3]); px(b.x + 3, b.y - 3 + oy, NEAR[1], NEAR[2], NEAR[3]) else local tau, dir if ph < FD then tau, dir = ph, b.dir elseif ph > AW - FD then tau, dir = AW - ph, -b.dir end if tau then local x, y = flight(b, tau, dir) local W5 = WING[WSEQ[(frame // 3 + i) % 4 + 1]] for k = 1, #W5 do splat(x + W5[k][1], y + W5[k][2] + oy, 20, 12, 40) end end end end end local RIM, BELLY = { 96, 168, 146 }, { 34, 80, 90 } local function frow(xa, xb, y, r, g, b) local ia, ib = floor(xa), floor(xb) if ib < ia then return end if ib == ia then blend_rect(ia, y, 1, 1, r, g, b, xb - xa) return end if ib - ia > 1 then rect(ia + 1, y, ib - ia - 1, 1, r, g, b) end blend_rect(ia, y, 1, 1, r, g, b, 1 - (xa - ia)) blend_rect(ib, y, 1, 1, r, g, b, xb - ib) end local function draw_dino(ox, oy, pose) local S = SPAN[pose] for y = 0, ROWS - 1 do local s = S[y] if s then local t = min(1, max(0, (y - 6) / 36)) frow(ox + s[1], ox + s[2], y + oy, lerp(RIM[1], BELLY[1], t), lerp(RIM[2], BELLY[2], t), lerp(RIM[3], BELLY[3], t)) end end local sp = SPIKES[pose] for i = 1, #sp do local x, y = ox + sp[i][1], sp[i][2] + oy frow(x - 1, x + 2, y - 1, 226, 88, 150) frow(x, x + 1, y - 2, 226, 88, 150) end local hy = 11 + (pose - 1) + oy splat(ox + 31, hy, 255, 196, 70); splat(ox + 32, hy, 240, 60, 40) frow(ox + 32, ox + 40, hy + 4, 22, 44, 56) splat(ox + 34, hy + 5, 238, 238, 232); splat(ox + 37, hy + 5, 238, 238, 232) end local DINO = false function render() if not DINO then build_dino() DINO = true gfx.ready(false) return end if not SKY then build_sky(); gfx.ready(false) return end if not CITY then build_city() on_settings() end local t = frame / FPS local tc = (t + 7) % TW local fs = frame % SP local oy = (cfg.shake ~= false and fs < 2) and 1 or 0 blit_rgb(SKY, 0, oy, W, H) if oy == 1 then rect(0, 0, W, 1, 36, 22, 84) end local ox = OX0 + V * tc if ox < W + 50 then local bob = fs < 3 and 2 or (fs < 7 and 1 or 0) draw_dino(ox, bob + oy, (frame // SP) % 2 + 1) end blit_rgb(CITY, 0, oy, W, H, KEY[1], KEY[2], KEY[3]) for i = 1, #FLICK do local f = FLICK[i] if (frame // 20 + i * 7) % 5 < 3 then rect(f[1], f[2] + oy, 1, 2, 255, 206, 96) end end draw_birds(tc, oy) end