info = { type = "image", title = "Crane and croc", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A crowned crane rides a crocodile at sunset", api = 33, } local tr = ({ sk = { ["Waves"] = "Vlny", ["Birds"] = "Vtáky" }, cs = { ["Waves"] = "Vlny", ["Birds"] = "Ptáci" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "waves", label = T("Waves"), type = "int", min = 1, max = 3, default = 2 }, { key = "birds", label = T("Birds"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt, abs = math.floor, math.sin, math.sqrt, math.abs local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local WL = 41 local HZ = 33 local DOT = 46 local function build_sprite(rows, pal) local h, w = #rows, 0 for y = 1, h do if #rows[y] > w then w = #rows[y] end end local G = {} for y = 1, h do local r = { rows[y]:byte(1, -1) } for x = #r + 1, w do r[x] = DOT end G[y] = r end local function at(x, y) local r = G[y] return r and r[x] or DOT end local core, top, bot = {}, {}, {} local function push(list, x, y, n, ch) local c = pal[ch] local m = #list list[m + 1], list[m + 2], list[m + 3] = x, y, n list[m + 4], list[m + 5], list[m + 6] = c[1], c[2], c[3] end for y = 1, h do local x = 1 while x <= w do local c = at(x, y) if c == DOT then x = x + 1 else local tc = at(x, y - 1) == DOT local n = 1 while x + n <= w and at(x + n, y) == c and (at(x + n, y - 1) == DOT) == tc do n = n + 1 end push(tc and top or core, x - 1, y - 1, n, c) x = x + n end end x = 1 while x <= w do local c = at(x, y) if c ~= DOT and at(x, y + 1) == DOT then local n = 1 while x + n <= w and at(x + n, y) == c and at(x + n, y + 1) == DOT do n = n + 1 end push(bot, x - 1, y, n, c) x = x + n else x = x + 1 end end end return { core = core, top = top, bot = bot, w = w, h = h } end local function draw_sprite(s, x, y) local iy = floor(y) local f = y - iy local c = s.core for k = 1, #c, 6 do rect(x + c[k], iy + c[k + 1], c[k + 2], 1, c[k + 3], c[k + 4], c[k + 5]) end c = s.top local a = 1 - f for k = 1, #c, 6 do blend_rect(x + c[k], iy + c[k + 1], c[k + 2], 1, c[k + 3], c[k + 4], c[k + 5], a) end if f > 0.03 then c = s.bot for k = 1, #c, 6 do blend_rect(x + c[k], iy + c[k + 1], c[k + 2], 1, c[k + 3], c[k + 4], c[k + 5], f) end end end local function B(s) return s:byte() end local CROC_PAL = { [B"o"] = { 44, 62, 34 }, [B"g"] = { 100, 136, 62 }, [B"d"] = { 70, 100, 46 }, [B"l"] = { 168, 180, 100 }, [B"y"] = { 246, 210, 72 }, [B"k"] = { 22, 22, 22 }, [B"w"] = { 250, 248, 236 }, [B"n"] = { 52, 74, 38 }, } local CROC = build_sprite({ ".............................ooo.........", "............................oyyko........", "...d.d.d.d.d.d.d.d.d.d.....ogggggo....nn.", "..ddddddddddddddddddddddgggggggggggggggggo", ".dggggggggggggggggggggggggggggggggggggggo", "dgggggggggggggggggggggggggggwgwgwgwgwgllo", "llllllllllllllllllllllllllllllllllllllo..", ".lllllllllllllllllllllllllllllllllllo....", }, CROC_PAL) local CRANE_PAL = { [B"g"] = { 170, 174, 190 }, [B"w"] = { 248, 248, 248 }, [B"k"] = { 34, 30, 38 }, [B"r"] = { 226, 46, 50 }, [B"b"] = { 80, 80, 92 }, [B"c"] = { 182, 106, 54 }, [B"B"] = { 60, 50, 54 }, [B"y"] = { 238, 190, 64 }, [B"Y"] = { 255, 232, 150 }, [B"e"] = { 236, 236, 236 }, [B"G"] = { 130, 134, 152 }, } local BODY = build_sprite({ "..gg........", "..ggg.......", ".gggggGG....", ".ggggwwwGG..", "gggggwwwwcG.", "GgggggwwwccB", ".GggggggccBB", "..GGggggcBB.", "....k..k....", "....k..k....", "....k..k....", "....k...k...", "....k...k...", "...kk...kk..", }, CRANE_PAL) local HEAD = build_sprite({ "..kkk.", ".kkekk", "bkwwrk", "..wwgg", "..rgg.", "...gg.", "...gg.", "....gg", "....gg", "...gg.", "...gg.", }, CRANE_PAL) local CREST = build_sprite({ "Y.Y.Y.", ".yYy.y", "..yyy.", }, CRANE_PAL) local WING1 = build_sprite({ "......cc..", "...wwwcc..", "..wwwwc...", }, CRANE_PAL) local WING2 = build_sprite({ ".......cc.", "......ccc.", ".....wcc..", "...wwwc...", "..wwww....", }, CRANE_PAL) local BG = nil local SUNX, SUNY, SUNR = 15, 22, 7 local function sky(y) local t = y / (HZ - 1) return floor(236 + 19 * t), floor(112 + 92 * t), floor(122 - 10 * t) end local function water(y) local t = (y - HZ) / (H - 1 - HZ) return floor(238 - 116 * t), floor(160 - 78 * t), floor(130 + 12 * t) end local function acacia(x, y, s, c) local w = 14 * s rect(x - w // 2, y, w, 2, c[1], c[2], c[3]) rect(x - w // 2 + 2, y - 1, w - 5, 1, c[1], c[2], c[3]) rect(x - w // 2 - 1, y + 2, w // 2, 1, c[1], c[2], c[3]) rect(x + 1, y + 2, w // 2 - 1, 1, c[1], c[2], c[3]) local h = 9 * s rect(x, y + 2, 1, h, c[1], c[2], c[3]) for k = 1, 3 * s do px(x - k, y + 2 + 2 * s - k // 2 + 1, c[1], c[2], c[3]) px(x + k, y + 2 + 2 * s - k // 2, c[1], c[2], c[3]) end end local function build_bg() for y = 0, HZ - 1 do rect(0, y, W, 1, sky(y)) end for y = SUNY - SUNR - 4, SUNY + SUNR + 4 do for x = SUNX - SUNR - 4, SUNX + SUNR + 4 do local dx, dy = x - SUNX, y - SUNY local d = sqrt(dx * dx + dy * dy) if d <= SUNR + 0.4 then px(x, y, 255, 238, 176) elseif d <= SUNR + 2.4 then px(x, y, 255, 214, 150) elseif d <= SUNR + 4.2 then local r, g, b = sky(y) px(x, y, floor((r + 255) / 2), floor((g + 196) / 2), floor((b + 140) / 2)) end end end for x = 0, W - 1 do local h = floor(3 + 2.2 * sin(x * 0.09 + 0.6) + 1.4 * sin(x * 0.23 + 2.0) + 0.5) rect(x, HZ - 2 - h, 1, h + 2, 186, 110, 132) end rect(0, HZ - 2, W, 2, 150, 92, 104) acacia(50, 13, 2, { 92, 48, 70 }) acacia(6, 22, 1, { 120, 64, 86 }) for y = HZ, H - 1 do rect(0, y, W, 1, water(y)) end rect(0, HZ, W, 1, 252, 214, 170) BG = gfx.snapshot(0, 0, W, H) end local function hline(x0, x1, y, r, g, b, a) local i0, i1 = floor(x0), floor(x1) local wl, wr = i0 + 1 - x0, x1 - i1 if i1 - i0 > 1 then blend_rect(i0 + 1, y, i1 - i0 - 1, 1, r, g, b, a) end blend_rect(i0, y, 1, 1, r, g, b, a * wl) if wr > 0.03 then blend_rect(i1, y, 1, 1, r, g, b, a * wr) end end local RIP = {} for k = 1, 18 do local h = hash32(k * 0x9E3779B1) local y = HZ + 2 + umod(h, H - HZ - 3) local depth = (y - HZ) / (H - HZ) RIP[k] = { y = y, len = 3 + umod(h >> 8, 5) + floor(depth * 4), v = (0.15 + depth * 0.45) * (1 + umod(h >> 12, 5) / 10), x0 = umod(h >> 16, 96) } end local NRIP, AMP, BIRDS = 12, 1.2, true local function ripples(t, front) for k = 1, NRIP do local r = RIP[k] if (r.y >= WL) == front then local span = W + r.len + 8 local x = r.x0 - (t * r.v) % span if x < -r.len - 4 then x = x + span end hline(x, x + r.len, r.y, 255, 236, 214, 0.55) end end end local function glitter(t, front) local s = t // 5 for y = front and WL + 1 or HZ + 2, front and 58 or WL - 1, 2 do local h = hash32(y * 0x85EBCA6B + s * 0x9E3779B1) local spread = 3 + (y - HZ) // 5 local x = SUNX - spread + umod(h, 2 * spread) rect(x, y, 2 + umod(h >> 8, 3), 1, 255, 236, 180) end end local function birds(t) local P = 11 * STEP_HZ local u = t % P local x0 = W + 6 - u * 1.1 if x0 < -30 then return end local flap = (t // 4) % 2 == 0 local F = { { 0, 0 }, { 6, -3 }, { 7, 4 }, { 13, -1 } } for k = 1, #F do local x = floor(x0 + F[k][1] + 0.5) local y = 9 + F[k][2] + floor(sin((t + k * 7) * 0.2) * 0.6 + 0.5) local wy = flap and -1 or 1 rect(x, y, 3, 1, 84, 40, 64) px(x - 1, y + wy, 84, 40, 64); px(x + 3, y + wy, 84, 40, 64) end end local CX = 13 local function croc_tail(t, y) for k = 0, 6 do local x = CX - 2 - 2 * k local off = sin(t * TAU / 50 - k * 0.7) * (0.3 + k * 0.22) local yy = floor(y + 4 + off + k * 0.15 + 0.5) rect(x, yy, 2, 2, 100, 136, 62) if k % 2 == 0 then px(x, yy - 1, 70, 100, 46) end end end local function under_water(x, w) for y = WL, WL + 4 do local r, g, b = water(y) blend_rect(x, y, w, 1, r, g, b, 0.62) end end function on_settings() local wv = cfg.waves or 2 AMP = 0.5 + 0.55 * (wv - 1) NRIP = 6 + 6 * (wv - 1) BIRDS = cfg.birds ~= false end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS if BIRDS then birds(t) end glitter(t, false) ripples(t, false) local ph = t * TAU / 78 local bob = AMP * sin(ph) local cy = WL - 6 + bob croc_tail(t, cy) draw_sprite(CROC, CX, cy) under_water(CX - 15, 58) local fo = (t // 6) % 3 for x = CX - 12 + fo, CX + 40, 5 do rect(x, WL, 3, 1, 255, 236, 214) end glitter(t, true) ripples(t, true) local bx = CX + 14 local by = cy + 2 - 13 local dip = -sin(ph + 0.6) local wing = nil if dip > 0.55 and AMP > 0.6 then wing = (dip > 0.8) and WING2 or WING1 end local u = t % (4 * STEP_HZ) local nod = 0 if u < 4 then nod = u // 2 + 1 elseif u < 14 then nod = 2 elseif u < 18 then nod = (18 - u) // 2 end local lag = (t - 3) % (4 * STEP_HZ) local cnod = 0 if lag < 4 then cnod = lag // 2 + 1 elseif lag < 14 then cnod = 2 elseif lag < 18 then cnod = (18 - lag) // 2 end draw_sprite(BODY, bx, by) if wing then draw_sprite(wing, bx + 2, by - wing.h + 3) end local hx, hy = bx - 1 - nod, by - 11 + (nod > 1 and 1 or 0) draw_sprite(HEAD, hx, hy) draw_sprite(CREST, bx - 1 - cnod, by - 14 + (cnod > 1 and 1 or 0)) end