info = { type = "image", title = "Salmon leap", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Salmon jump a forest cascade, a bear waits", api = 44, } local tr = ({ sk = { ["Salmon"] = "Lososy", ["Time of day"] = "Denná doba", ["Day"] = "Deň", ["Dusk"] = "Súmrak" }, cs = { ["Salmon"] = "Lososi", ["Time of day"] = "Denní doba", ["Day"] = "Den", ["Dusk"] = "Soumrak" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "n", label = T("Salmon"), type = "int", min = 1, max = 5, default = 3 }, { key = "tod", label = T("Time of day"), type = "int", default = "day", opts = { keys = { "day", "dusk" }, T("Day"), T("Dusk") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32 = math.hash32 local W, H = gfx.W, gfx.H local DAY = { sky1 = { 112, 184, 210 }, sky2 = { 176, 220, 214 }, far = { 92, 150, 140 }, mid = { 52, 112, 92 }, near = { 28, 76, 62 }, trunk = { 70, 52, 40 }, water = { 66, 140, 180 }, deep = { 46, 110, 154 }, foam = { 214, 238, 246 }, lite = { 120, 180, 214 }, rock = { 108, 100, 94 }, rockd = { 74, 68, 66 }, moss = { 70, 120, 62 }, wet = { 80, 78, 82 }, } local DUSK = { sky1 = { 74, 62, 128 }, sky2 = { 236, 148, 104 }, far = { 120, 84, 130 }, mid = { 72, 50, 100 }, near = { 40, 30, 72 }, trunk = { 52, 36, 58 }, water = { 84, 80, 146 }, deep = { 58, 52, 112 }, foam = { 255, 210, 170 }, lite = { 170, 128, 170 }, rock = { 96, 78, 96 }, rockd = { 60, 46, 72 }, moss = { 72, 84, 90 }, wet = { 70, 58, 86 }, } local P = DAY local BODY, BACK, BELLY, FIN = { 232, 104, 90 }, { 168, 62, 74 }, { 252, 178, 150 }, { 196, 74, 78 } local function c3(c) return c[1], c[2], c[3] end local POOLS = { { 0, 20, 50 }, { 24, 40, 42 }, { 44, 63, 34 } } local FALLS = { { 21, 42, 50 }, { 41, 34, 42 } } local LIP = { 23, 43 } local DEPTH = 8 local function pine(cx, base, h, wmax, c) for r = 0, h - 1 do local tier = (r * 4 // h) * (h // 4) local w = 1 + floor(wmax * (0.35 * (r - tier) / (h / 4) + 0.65 * r / h)) rect(cx - w, base - h + r, 2 * w + 1, 1, c3(c)) end end 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 BG, STAGE = nil, 1 local STAGES = { function() for y = 0, 35 do rect(0, y, W, 1, lerp3(P.sky1, P.sky2, y / 35)) end for k = 0, 8 do pine(k * 8 + 3, 38 - (k * 5) % 5, 12 + (k * 3) % 5, 3, P.far) end for k = 0, 6 do pine(k * 10 + 7, 41 - (k * 3) % 4, 17 + (k * 5) % 6, 4, P.mid) end rect(0, 37, W, 27, c3(P.mid)) end, function() for _, x in ipairs({ 47, 55, 61 }) do rect(x, 12, 2, 24, c3(P.trunk)) end pine(50, 24, 22, 5, P.near); pine(58, 20, 20, 5, P.near); pine(63, 26, 20, 4, P.near) pine(8, 40, 30, 6, P.near) rect(7, 40, 2, 12, c3(P.trunk)) for i = 1, 3 do local p = POOLS[i] local w = p[2] - p[1] + 1 rect(p[1], p[3], w, 64 - p[3], c3(P.rock)) rect(p[1], p[3], w, DEPTH, c3(P.water)) rect(p[1], p[3] + DEPTH - 3, w, 3, c3(P.deep)) rect(p[1], p[3], w, 1, c3(P.lite)) end for i = 1, 2 do local f = FALLS[i] rect(f[1] - 1, f[2], 1, 24, c3(P.rockd)) rect(f[1] + 3, f[2], 1, 22, c3(P.rockd)) rect(f[1] + 3, f[2] - 2, 3, 2, c3(P.moss)) rect(f[1] - 3, f[3] - 1, 3, 1, c3(P.rockd)) end end, function() for k = 1, 90 do local h = hash32(k * 0x9E3779B1) local x, y = h & 63, 36 + (h >> 6) % 28 local ok = false for i = 1, 3 do local p = POOLS[i] if x >= p[1] and x <= p[2] and y >= p[3] + DEPTH then ok = true end end if ok then px(x, y, c3((h >> 12) % 2 == 0 and P.rockd or P.rock)) end end rect(0, 58, W, 6, c3(P.rockd)) for x = 0, W - 1, 5 do rect(x, 58 + (x * 7) % 3, 3, 1, c3(P.rock)) end rect(0, 44, 7, 20, c3(P.rock)); rect(0, 44, 7, 3, c3(P.moss)); rect(5, 47, 2, 17, c3(P.rockd)) rect(24, 52, 6, 6, c3(P.rock)); rect(24, 52, 6, 2, c3(P.moss)); rect(28, 54, 2, 4, c3(P.rockd)) rect(44, 44, 8, 8, c3(P.rock)); rect(44, 44, 8, 2, c3(P.moss)); rect(50, 46, 2, 6, c3(P.rockd)) rect(58, 60, 6, 4, c3(P.rock)) for _, x in ipairs({ 2, 19, 37, 52, 60 }) do for k = -2, 2 do px(x + k, 58 - 3 + (k * k) // 2, c3(P.mid)) end px(x, 54, c3(P.mid)) end BG = gfx.snapshot(0, 0, W, H) end, } function on_settings() P = cfg.tod == "dusk" and DUSK or DAY BG, STAGE = nil, 1 end local function streak(x, y, l, c, a) local ix = floor(x) local f = x - ix blend_rect(ix + 1, y, l - 1, 1, c[1], c[2], c[3], a) blend_rect(ix, y, 1, 1, c[1], c[2], c[3], a * (1 - f)) blend_rect(ix + l, y, 1, 1, c[1], c[2], c[3], a * f) end local FLOW = { { 3.2, 5, 11 }, { 5.4, 3, 7 }, { 4.1, 4, 3 } } local function draw_pool(p, t) local w = p[2] - p[1] + 1 for k = 1, 3 do local f = FLOW[k] local x = p[2] - ((t * f[1] / 30 + f[3] * 3 + p[3] * k * 5) % (w + f[2])) local y = p[3] + k * 2 if y < p[3] + DEPTH - 1 then local x0 = floor(x) if x0 >= p[1] - 1 and x0 + f[2] <= p[2] + 1 then streak(x, y, f[2], P.foam, k == 1 and 0.5 or 0.28) end end end end local function sparkle(p, t) for k = 1, 4 do local h = hash32(p[3] * 31 + k * 977 + (t // 20) * 7919) & 0x7FFF if h % 3 == 0 then local x = p[1] + h % (p[2] - p[1] - 1) + 1 px(x, p[3] + 1 + (h >> 4) % (DEPTH - 3), c3(P.foam)) end end end local function draw_fall(f, t) local x, top, bot = f[1], f[2], f[3] rect(x, top, 3, bot - top, c3(P.lite)) rect(x + 1, top, 1, bot - top, c3(P.foam)) gfx.clip(x, top, 3, bot - top) for c = 0, 2 do local y = top + ((t * (1.2 + c * 0.25) + c * 5) % 11) - 3 rect(x + c, floor(y), 1, 3, c3(P.foam)) rect(x + c, floor(y) + 5, 1, 2, c3(P.water)) end gfx.clip() local k = (t // 4 + x) % 3 blend_rect(x - 2, bot - 1, 7, 2, P.foam[1], P.foam[2], P.foam[3], 0.7) rect(x - 1 + k, bot - 2, 2, 1, c3(P.foam)) rect(x + 3 - k, bot, 2, 1, c3(P.foam)) end local PERIOD = { 134, 157, 181, 203, 229 } local JT, LEAD = 34, 26 local S = {} for i = 1, 5 do S[i] = {} end local function cycle(i, t) local s = S[i] local per = PERIOD[i] local f = t + i * 61 local n = f // per local h = hash32(i * 7919 + n * 104729) & 0x7FFF s.u = f % per s.k = h % 2 + 1 s.fail = (h >> 3) % 100 < 22 s.pair = (h >> 5) % 4 s.x0 = (s.k == 1 and 12 or 32) + s.pair s.dx = s.fail and 4 or 16 s.caught = s.k == 2 and not s.fail and (h >> 9) % 3 == 0 s.snap = s.k == 2 and not s.fail and (h >> 12) % 2 == 0 s.ya = POOLS[s.k][3] s.yb = s.fail and s.ya or POOLS[s.k + 1][3] return s end local function fish(cx, cy, dx, dy) local n = sqrt(dx * dx + dy * dy) dx, dy = dx / n, dy / n local px_, py_ = -dy, dx local HW = { 1.2, 1.4, 0.7, 0.9, 1.3, 1.7, 1.8, 1.7, 1.4, 0.9 } for i = 1, 10 do local d = i - 8 local x, y = cx + dx * d, cy + dy * d local hw = HW[i] for k = -2, 2 do if k <= hw and k >= -hw then local c = i <= 2 and FIN or (k < 0 and BACK or (k > 0 and BELLY or BODY)) local r, g, b = c3(c) px(floor(x + px_ * k + 0.5), floor(y + py_ * k + 0.5), r, g, b) end end end px(floor(cx + dx * 1.5 - px_ * 0.4 + 0.5), floor(cy + dy * 1.5 - py_ * 0.4 + 0.5), 20, 16, 24) end local function splash(x, y, a) if a < 0 or a > 11 then return end local al = 1 - a / 12 blend_rect(x - 2 - a // 2, y, 5 + a, 1, P.foam[1], P.foam[2], P.foam[3], 0.6 * al) for j = -2, 2 do local vx = j * 0.55 local vy = 2.4 - (j * j) * 0.12 local yy = y - (vy * a - 0.28 * a * a) if yy < y then px(floor(x + vx * a * 0.9 + 0.5), floor(yy + 0.5), c3(P.foam)) end end end local BX, BS = 58, 34 local BEAR, BEAR_L, BEAR_D, NOSE, MOUTH = { 100, 68, 44 }, { 150, 106, 72 }, { 70, 46, 32 }, { 22, 16, 16 }, { 150, 46, 54 } local function bear(pose, t) local x, s = BX, BS rect(x - 5, s - 9, 11, 11, c3(BEAR)) rect(x - 3, s - 7, 6, 7, c3(BEAR_L)) rect(x + 3, s - 9, 3, 9, c3(BEAR_D)) local hy = s - 17 if pose == 2 and (t // 5) % 2 == 0 then hy = hy + 1 end rect(x - 5, hy + 1, 10, 7, c3(BEAR)) rect(x - 4, hy, 8, 1, c3(BEAR)) rect(x - 5, hy - 1, 2, 2, c3(BEAR)); rect(x + 3, hy - 1, 2, 2, c3(BEAR)) rect(x - 9, hy + 3, 5, 4, c3(BEAR_L)) px(x - 10, hy + 3, c3(NOSE)); px(x - 10, hy + 4, c3(NOSE)) rect(x + 3, hy + 1, 2, 7, c3(BEAR_D)) px(x - 3, hy + 2, 12, 10, 8) if pose == 1 then rect(x - 9, hy + 6, 4, 1, c3(MOUTH)) end if pose == 0 then rect(x - 7, s - 8, 2, 8, c3(BEAR)); rect(x + 5, s - 8, 2, 8, c3(BEAR_D)) elseif pose == 1 then rect(x - 13, s - 13, 8, 3, c3(BEAR)); px(x - 14, s - 14, 230, 226, 210); px(x - 14, s - 12, 230, 226, 210) rect(x + 5, s - 8, 2, 8, c3(BEAR_D)) else rect(x - 8, s - 10, 3, 5, c3(BEAR)); rect(x + 5, s - 8, 2, 7, c3(BEAR_D)) rect(x - 12, s - 10, 7, 3, c3(BODY)); rect(x - 12, s - 8, 7, 1, c3(BELLY)) px(x - 13, s - 10, c3(FIN)); px(x - 13, s - 9, c3(FIN)) end end function render() if not BG then STAGES[STAGE]() STAGE = STAGE + 1 if not BG then gfx.ready(false) return end end local t = frame gfx.blit_rgb(BG, 0, 0, W, H) for i = 1, 3 do draw_pool(POOLS[i], t); sparkle(POOLS[i], t) end for i = 1, 2 do draw_fall(FALLS[i], t) end local n = cfg.n or 3 local pose, pt = 0, 0 for i = 1, n do local s = cycle(i, t) local u = s.u - LEAD if s.k == 2 then if s.caught and u >= JT * 0.7 and u < JT * 0.7 + 60 then local a = u - JT * 0.7 pose, pt = a < 10 and 1 or 2, a elseif s.snap and u >= JT * 0.4 and u < JT * 0.4 + 10 then pose = 1 end end end bear(pose, t) blend_rect(BX - 14, BS + 1, 28, 8, P.water[1], P.water[2], P.water[3], 0.6) for i = 1, n do local s = S[i] local u = s.u - LEAD local p = POOLS[s.k] if u < 0 then local x = s.x0 - 10 + (s.u / LEAD) * 10 blend_rect(floor(x) - 4, p[3] + 3, 9, 2, 150, 70, 76, 0.5) elseif u <= JT then local v = u / JT local x = s.x0 + s.dx * v local y = s.ya + (s.yb - s.ya) * v - 4 * 8 * (s.fail and 0.75 or 1) * v * (1 - v) if not (s.caught and v >= 0.7) then local dyv = (s.yb - s.ya) - 32 * (s.fail and 0.75 or 1) * (1 - 2 * v) fish(x, y, s.dx, dyv) end splash(s.x0, p[3], floor(u)) elseif u <= JT + 22 then local a = u - JT local q = POOLS[s.fail and s.k or s.k + 1] local xe = s.x0 + s.dx if not s.caught then blend_rect(floor(xe + a * 0.5) - 4, q[3] + 3, 9, 2, 150, 70, 76, 0.5 * (1 - a / 22)) splash(floor(xe), q[3], floor(a)) end end end end