info = { type = "image", title = "Croc river", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A crocodile surfaces by the bank, a bird visits", api = 33, } local tr = ({ sk = { ["Bird"] = "Vtáčik", ["Pace"] = "Tempo" }, cs = { ["Bird"] = "Ptáček", ["Pace"] = "Tempo" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bird", label = T("Bird"), type = "bool", default = true }, { key = "pace", label = T("Pace"), type = "int", min = 1, max = 3, default = 2 }, } local rect, blend, blend_rect, px = gfx.rect, gfx.blend, gfx.blend_rect, gfx.px local floor, sin, sqrt = math.floor, math.sin, math.sqrt local W, H = gfx.W, gfx.H local function rn(a, b) return math.hash32(a * 7919 + b * 104729) & 0x7FFF end local YW, XE, C = 46, 46, 840 local PACE = { 0.7, 1, 1.4 } local BG local function build_bg() for y = 0, 26 do rect(0, y, W, 1, 150 + y * 2, 206 + y, 236 + y // 9) end for x = 0, W - 1 do local top = 22 + rn(x // 3, 1) % 6 rect(x, top, 1, 34 - top, 30, 86, 58) if rn(x, 2) % 3 == 0 then px(x, top, 62, 132, 76) end end for y = 33, H - 1 do local t = (y - 33) / 30 rect(0, y, W, 1, floor(62 - 36 * t), floor(152 - 52 * t), floor(186 - 36 * t)) end for i = 1, 14 do blend_rect(rn(i, 3) % 56, 36 + rn(i, 4) % 27, 3 + rn(i, 5) % 5, 1, 224, 246, 250, 0.2) end for i = 1, 10 do blend_rect(rn(i, 6) % 56, 36 + rn(i, 7) % 27, 3 + rn(i, 8) % 6, 1, 20, 80, 120, 0.25) end BG = gfx.snapshot(0, 0, W, H) end local function bank() for x = 50, W - 1 do local top = floor(47 - (x - 50) * 0.8) rect(x, top, 1, 1, 158, 120, 76) rect(x, top + 1, 1, 6, 118, 84, 54) rect(x, top + 7, 1, H - top - 7, 88, 62, 44) if x % 3 == 1 then local L = 6 + rn(x, 9) % 4 rect(x, top - L, 1, L, 46, 120, 50) px(x, top - L, 96, 170, 80) end end end local G1, G2, G3 = { 46, 138, 52 }, { 24, 92, 40 }, { 86, 176, 72 } local BELLY, TEETH, EYE, PUPIL = { 176, 204, 112 }, { 240, 240, 222 }, { 240, 206, 60 }, { 18, 18, 18 } local NOSTRIL, INNER, TONGUE = { 16, 50, 24 }, { 150, 48, 60 }, { 214, 100, 110 } local OPS = { { -34, 6, 14, 40, G1 }, { -33, 5, 2, 1, G2 }, { -29, 5, 2, 1, G2 }, { -25, 4, 2, 1, G2 }, { -26, 2, 12, 10, G1 }, { -26, 2, 12, 1, G3 }, { -15, 4, 15, 4, G1 }, { -15, 4, 15, 1, G3 }, { -4, 3, 3, 1, G1 }, { -3, 3, 1, 1, NOSTRIL }, { -21, 0, 5, 4, G1 }, { -21, 0, 5, 1, G2 }, { -15, 8, 15, 1, G2 }, } local EYEOPS = { { -19, 1, 3, 2, EYE }, { -18, 1, 1, 2, PUPIL } } local function frect(x, y, w, h, c) local ix = floor(x) local f = x - ix rect(ix + 1, y, w - 1, h, c[1], c[2], c[3]) blend_rect(ix, y, 1, h, c[1], c[2], c[3], 1 - f) blend_rect(ix + w, y, 1, h, c[1], c[2], c[3], f) end local function croc(xs, yt, open, blink) for i = 1, #OPS do local o = OPS[i]; frect(xs + o[1], yt + o[2], o[3], o[4], o[5]) end for i = 1, #EYEOPS do local o = EYEOPS[i] frect(xs + o[1], yt + o[2], o[3], o[4], blink and G2 or o[5]) end for k = 0, 6 do px(floor(xs) - 13 + 2 * k, yt + 8, TEETH[1], TEETH[2], TEETH[3]) end local X = floor(xs + 0.5) for c = 0, 19 do local x = X - 20 + c local drop = floor(c * 0.4 * open + 0.5) if c >= 5 and drop > 0 then rect(x, yt + 8, 1, drop + 1, INNER[1], INNER[2], INNER[3]) if c >= 6 and c <= 16 and drop > 1 then px(x, yt + 7 + drop, TONGUE[1], TONGUE[2], TONGUE[3]) end if c % 3 == 0 then px(x, yt + 8 + drop, TEETH[1], TEETH[2], TEETH[3]) end end rect(x, yt + 9 + drop, 1, 1, G1[1], G1[2], G1[3]) rect(x, yt + 10 + drop, 1, 2, BELLY[1], BELLY[2], BELLY[3]) end end local BR, BB, BO, BW = { 150, 100, 60 }, { 232, 204, 150 }, { 240, 150, 40 }, { 110, 70, 40 } local function bird(bx, by, flying, ph) bx, by = floor(bx + 0.5), floor(by + 0.5) rect(bx + 2, by + 1, 4, 3, BR[1], BR[2], BR[3]) rect(bx + 2, by + 3, 3, 1, BB[1], BB[2], BB[3]) rect(bx, by + 1, 2, 2, BR[1], BR[2], BR[3]) px(bx - 1, by + 2, BO[1], BO[2], BO[3]) px(bx, by + 1, 20, 20, 20) rect(bx + 6, by + 2, 2, 1, BW[1], BW[2], BW[3]) if flying then if ph == 0 then rect(bx + 3, by - 2, 2, 3, BW[1], BW[2], BW[3]) else rect(bx + 3, by + 3, 3, 2, BW[1], BW[2], BW[3]) end else rect(bx + 3, by + 1, 3, 2, BW[1], BW[2], BW[3]) end end local function sm(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local function xs_at(t) return 20 + (XE - 20) * sm(t / 260) end local EV = {} for n = 0, 8 do EV[#EV + 1] = { n * 28, xs_at(n * 28) - (n % 2 == 0 and 3 or 19), 1 } end for _, e in ipairs({ { 262, XE - 12, 2 }, { 285, XE - 4, 2 }, { 705, XE - 14, 2 }, { 735, XE - 8, 2 }, { 765, XE - 12, 2 } }) do EV[#EV + 1] = { e[1], e[2], e[3] } end local LIFE = 66 local function ring(cx, cy, r, a) local ry = r * 0.36 local nr = floor(ry + 0.5) for dy = 0, nr do local v0, v1 = dy - 0.5, dy + 0.5 if v0 < 0 then v0 = 0 end if v1 > ry then v1 = ry end local x1 = floor(r * sqrt(1 - (v0 / ry) ^ 2) + 0.5) local x0 = floor(r * sqrt(1 - (v1 / ry) ^ 2) + 0.5) for s = 0, (dy > 0 and 1 or 0) do local y = s == 0 and cy + dy or cy - dy if x0 == 0 then blend_rect(cx - x1, y, 2 * x1 + 1, 1, 222, 244, 250, a) else blend_rect(cx - x1, y, x1 - x0 + 1, 1, 222, 244, 250, a) blend_rect(cx + x0, y, x1 - x0 + 1, 1, 222, 244, 250, a) end end end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = floor(frame * PACE[cfg.pace or 2]) % C for i = 1, 6 do blend_rect(floor((rn(i, 11) % 64 + frame * 0.05 * (i % 3 + 1)) % 70) - 6, 38 + rn(i, 12) % 24, 5, 1, 224, 246, 250, 0.22) end for i = 1, #EV do local e = EV[i] local age = t - e[1] if age >= 0 and age < LIFE then local a = 0.5 * (1 - age / LIFE) ring(floor(e[2] + 0.5), YW + 1, 1 + age * 0.16 * e[3], a) end end bank() local yt, open, xs = YW - 4, 0, xs_at(t) if t < 30 then yt = YW - 4 * sm(t / 30) elseif t >= 260 and t < 320 then yt = YW - 4 - 13 * sm((t - 260) / 60) elseif t >= 320 and t < 700 then yt = YW - 17 elseif t >= 700 and t < 780 then yt = YW - 17 + 23 * sm((t - 700) / 80) elseif t >= 780 then yt = YW + 6 end yt = floor(yt + 0.5) if t >= 320 and t < 596 then open = sm((t - 320) / 60) elseif t >= 596 and t < 604 then open = 1 - (t - 596) / 8 end if yt < YW then gfx.clip(0, 0, W, YW) croc(xs, yt, open, t >= 320 and t < 700 and t % 50 < 3) gfx.clip() local xl = floor(xs) - 34 blend_rect(xl, YW, 35, 1, 222, 244, 250, 0.4) blend_rect(xl + 4, YW + 1, 26, 2, 10, 60, 50, 0.22) end if cfg.bird ~= false and t >= 380 and t < 650 then local lx, ly = XE - 24, YW - 17 - 4 if t < 440 then local s = (t - 380) / 60 local e = 1 - (1 - s) * (1 - s) bird(lx + (76 - lx) * (1 - e), ly + (4 - ly) * (1 - e) - 5 * sin(3.14159 * e) + sin(t * 0.7), true, (t // 3) % 2) elseif t < 580 then bird(lx, ly + ((t % 50) < 6 and 1 or 0), false, 0) else local s = (t - 580) / 70 bird(lx - (lx + 10) * s * s, ly - 34 * s * s, true, (t // 3) % 2) end end end