info = { type = "image", title = "Cliff angler", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A boy fishes off a sea cliff, float bobbing", api = 33, } local ltr = ({ sk = { ["Time of day"] = "Denná doba", ["day"] = "deň", ["sunset"] = "západ slnka", ["Bites"] = "Hryzenie", ["rare"] = "zriedka", ["normal"] = "normálne", ["often"] = "často" }, cs = { ["Time of day"] = "Denní doba", ["day"] = "den", ["sunset"] = "západ slunce", ["Bites"] = "Kousání", ["rare"] = "zřídka", ["normal"] = "normálně", ["often"] = "často" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "tod", label = LT("Time of day"), type = "int", default = 0, opts = { LT("day"), LT("sunset") } }, { key = "bite", label = LT("Bites"), type = "int", default = 1, opts = { LT("rare"), LT("normal"), LT("often") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, sqrt, abs, max = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.max local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU, PI = 6.28318, 3.14159 local HZ = 36 local WY = 44 local FX0 = 11 local HX, HY = 39, 25 local RL = 27 local A0, A1 = 0.52, 0.86 local KR, KG, KB = 255, 0, 255 local function c3(c) return c[1], c[2], c[3] end local function ease(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local function lerp3(a, b, u) return floor(a[1] + (b[1] - a[1]) * u + 0.5), floor(a[2] + (b[2] - a[2]) * u + 0.5), floor(a[3] + (b[3] - a[3]) * u + 0.5) end local function runs(rows, pal) local S = {} for r = 1, #rows do local s, out, k, n = rows[r], {}, 1, #rows[r] while k <= n do local ch, e = s:sub(k, k), k while e < n and s:sub(e + 1, e + 1) == ch do e = e + 1 end if ch ~= "." then out[#out + 1] = { k - 1, e - k + 1, pal[ch] } end k = e + 1 end S[r] = out end return S end local function sprite(S, x, y) local ix = floor(x) local f = x - ix for r = 1, #S do local row, yy, m = S[r], y + r - 1, #S[r] for k = 1, m do local e = row[k] local a, w, c = ix + e[1], e[2], e[3] if k == 1 then blend_rect(a, yy, 1, 1, c[1], c[2], c[3], 1 - f) if w > 1 then rect(a + 1, yy, w - 1, 1, c[1], c[2], c[3]) end else rect(a, yy, w, 1, c[1], c[2], c[3]) end if k == m and f > 0.03 then blend_rect(a + w, yy, 1, 1, c[1], c[2], c[3], f) end end end end local function line(x0, y0, x1, y1, r, g, b) local dx, dy = x1 - x0, y1 - y0 local n = floor(max(abs(dx), abs(dy)) + 0.5) if n < 1 then px(floor(x0 + 0.5), floor(y0 + 0.5), r, g, b) return end for i = 0, n do local u = i / n px(floor(x0 + dx * u + 0.5), floor(y0 + dy * u + 0.5), r, g, b) end end local THEMES = { [0] = { sky = { { 0, { 84, 150, 228 } }, { HZ - 1, { 180, 218, 246 } } }, hz = { 150, 198, 238 }, sea0 = { 86, 152, 216 }, sea1 = { 34, 92, 178 }, sun = { 255, 248, 204 }, halo = { 196, 226, 250 }, sx = 12, sy = 9, sr = 4, wave = { 176, 218, 248 }, glit = { 255, 255, 255 }, gx = 12, cw = { 250, 252, 255 }, cs = { 204, 220, 240 }, top = { 186, 132, 84 }, face = { 146, 96, 60 }, shade = { 104, 68, 46 }, crack = { 80, 52, 36 }, edge = { 206, 158, 106 }, grass = { 96, 176, 64 }, grassd = { 60, 130, 50 }, bloom = { 255, 236, 90 }, foam = { 236, 246, 255 }, line = { 56, 58, 72 }, }, [1] = { sky = { { 0, { 96, 66, 156 } }, { 17, { 214, 104, 146 } }, { HZ - 1, { 255, 186, 112 } } }, hz = { 255, 206, 140 }, sea0 = { 196, 118, 138 }, sea1 = { 64, 50, 120 }, sun = { 255, 222, 130 }, sx = 22, sy = HZ, sr = 8, refl = { 250, 176, 120 }, wave = { 255, 184, 156 }, glit = { 255, 240, 176 }, gx = 22, cw = { 255, 216, 204 }, cs = { 226, 148, 162 }, top = { 196, 122, 96 }, face = { 150, 84, 74 }, shade = { 106, 58, 60 }, crack = { 78, 40, 46 }, edge = { 228, 152, 112 }, grass = { 134, 150, 72 }, grassd = { 88, 104, 60 }, bloom = { 255, 214, 120 }, foam = { 255, 228, 214 }, line = { 72, 40, 66 }, }, } local SKIN, SKIN_D, EYE = { 250, 204, 160 }, { 222, 164, 122 }, { 40, 30, 34 } local BLUSH, MOUTH = { 248, 150, 140 }, { 176, 92, 80 } local CAP, CAP_D, HAIR = { 212, 52, 48 }, { 156, 32, 38 }, { 112, 70, 42 } local SHIRT, SHIRT_D = { 244, 244, 240 }, { 198, 206, 218 } local STRIPE, SHORTS = { 58, 102, 192 }, { 150, 106, 62 } local SHOE, SOLE = { 220, 60, 50 }, { 240, 240, 240 } local PAIL, PAIL_D, PAIL_L, HANDLE = { 150, 162, 174 }, { 110, 122, 136 }, { 210, 218, 224 }, { 88, 94, 106 } local SILVER = { 200, 214, 226 } local FISH, BELLY = { 84, 108, 142 }, { 176, 196, 214 } local CORK, ROD, ROD_T = { 196, 150, 96 }, { 70, 54, 46 }, { 118, 100, 92 } local FLOAT = { { 0, 1, { 150, 20, 30 } }, { 0, 2, { 232, 40, 40 } }, { 0, 2, { 232, 40, 40 } }, { 0, 2, { 250, 250, 250 } } } local function cliff_x(y) if y < 36 then return 37 elseif y < 41 then return 39 elseif y < 47 then return 38 elseif y < 55 then return 36 end return 34 end local CLOUD_ART = { { ".....wwww.......", "...wwwwwwww.www.", ".wwwwwwwwwwwwwww", "wwwwwwwwwwwwwwww", ".ssssssssssssss." }, { "..www...", ".wwwwww.", "wwwwwwww", ".ssssss." }, { "...wwww.....", ".wwwwwwwww..", "wwwwwwwwwwww", ".ssssssssss." }, } local CLOUDS = { { 1, 5, 0.060, 20 }, { 2, 16, 0.095, 52 }, { 3, 25, 0.045, 2 } } local WAVES = { { 38, 0.05, 2, 14, 3 }, { 40, 0.07, 3, 15, 9 }, { 43, 0.09, 3, 16, 5 }, { 47, 0.12, 4, 18, 12 }, { 51, 0.15, 5, 20, 2 }, { 56, 0.19, 5, 21, 14 }, { 61, 0.23, 6, 23, 7 } } local T, P = THEMES[0], 300 local CS = {} local BG, FG = nil, nil local READY = false local function apply() READY = true local C = cfg or {} T = THEMES[C.tod or 0] or THEMES[0] P = ({ 450, 300, 200 })[(C.bite or 1) + 1] or 300 for i = 1, #CLOUD_ART do CS[i] = runs(CLOUD_ART[i], { w = T.cw, s = T.cs }) end BG, FG = nil, nil end function on_settings() apply() end local function disc(cx, cy, r, c) for dy = -r, r do local y = cy + dy if y < HZ then local hw = floor(sqrt((r + 0.4) * (r + 0.4) - dy * dy)) rect(cx - hw, y, 2 * hw + 1, 1, c3(c)) end end end local function build_bg() local st = T.sky for i = 1, #st - 1 do local a, b = st[i], st[i + 1] for y = a[1], b[1] do rect(0, y, W, 1, lerp3(a[2], b[2], (y - a[1]) / (b[1] - a[1]))) end end if T.halo then disc(T.sx, T.sy, T.sr + 2, T.halo) end disc(T.sx, T.sy, T.sr, T.sun) rect(0, HZ, W, 1, c3(T.hz)) for y = HZ + 1, H - 1 do rect(0, y, W, 1, lerp3(T.sea0, T.sea1, (y - HZ - 1) / (H - HZ - 2))) end if T.refl then for i = 0, 6 do local w = 15 - 2 * i rect(T.sx - w // 2, HZ + 1 + 2 * i, w, 1, c3(T.refl)) end end BG = gfx.snapshot(0, 0, W, H) end local function build_fg() rect(0, 0, W, H, KR, KG, KB) for y = 33, H - 1 do local xl = cliff_x(y) rect(xl, y, W - xl, 1, c3(y < 35 and T.top or T.face)) px(xl, y, c3(T.edge)) end rect(39, 36, 9, 1, c3(T.shade)); rect(40, 37, 5, 1, c3(T.shade)) rect(38, 41, 6, 1, c3(T.top)); rect(36, 47, 8, 1, c3(T.top)); rect(34, 55, 9, 1, c3(T.top)) local CR = T.crack rect(46, 38, 1, 3, c3(CR)); rect(58, 39, 1, 3, c3(CR)); rect(52, 43, 1, 4, c3(CR)) rect(44, 49, 1, 4, c3(CR)); rect(48, 52, 8, 1, c3(CR)); rect(56, 57, 1, 5, c3(CR)) rect(40, 59, 6, 1, c3(CR)) for k = 1, 22 do local h = hash32(k * 0x27D4EB2F) local y = 37 + (h & 0x7FFF) % 27 local x = cliff_x(y) + 2 + ((h >> 15) & 0x7FFF) % 24 if x < W then px(x, y, c3(T.shade)) end end rect(36, 31, W - 36, 1, c3(T.grass)); rect(36, 32, W - 36, 1, c3(T.grassd)) px(36, 33, c3(T.grassd)); px(36, 34, c3(T.grassd)) for x = 36, W - 1 do if (hash32(x * 0x9E3779B1) & 3) == 0 then px(x, 30, c3(T.grass)) end end px(37, 30, c3(T.bloom)); px(49, 30, c3(T.bloom)); px(61, 30, c3(T.bloom)) rect(51, 26, 8, 1, c3(PAIL_L)) rect(52, 27, 6, 4, c3(PAIL)); rect(56, 27, 2, 4, c3(PAIL_D)) px(51, 25, c3(HANDLE)); px(52, 24, c3(HANDLE)); px(53, 23, c3(HANDLE)) rect(54, 23, 3, 1, c3(HANDLE)); px(57, 24, c3(HANDLE)); px(58, 25, c3(HANDLE)) px(54, 24, c3(SILVER)); px(55, 25, c3(SILVER)); px(56, 24, c3(SILVER)) rect(38, 30, 5, 2, c3(SHORTS)) rect(37, 32, 2, 5, c3(SKIN)) rect(36, 37, 3, 1, c3(SHOE)); rect(36, 38, 3, 1, c3(SOLE)) rect(42, 29, 6, 3, c3(SHORTS)) rect(43, 22, 5, 7, c3(SHIRT)); rect(47, 22, 1, 7, c3(SHIRT_D)) rect(43, 24, 5, 1, c3(STRIPE)); rect(43, 27, 5, 1, c3(STRIPE)) rect(40, 14, 7, 7, c3(SKIN)); rect(41, 21, 5, 1, c3(SKIN)); px(39, 17, c3(SKIN)) rect(45, 14, 2, 5, c3(HAIR)); px(44, 14, c3(HAIR)) px(44, 17, c3(SKIN_D)) rect(41, 16, 1, 2, c3(EYE)); px(42, 19, c3(BLUSH)); px(40, 20, c3(MOUTH)) rect(41, 10, 5, 1, c3(CAP)); rect(40, 11, 7, 3, c3(CAP)); rect(46, 12, 1, 2, c3(CAP_D)) rect(36, 13, 4, 1, c3(CAP_D)); px(43, 9, c3(CAP_D)) FG = gfx.snapshot(0, 0, W, H) end local function draw_waves(t) local r, g, b = c3(T.wave) for i = 1, #WAVES do local wv = WAVES[i] local y, L = wv[1], wv[3] local per = L + wv[4] local x0 = (wv[5] + t * wv[2]) % per - per local ix = floor(x0) local f = x0 - ix for x = ix, cliff_x(y) - 1, per do blend_rect(x, y, 1, 1, r, g, b, 1 - f) rect(x + 1, y, L - 1, 1, r, g, b) blend_rect(x + L, y, 1, 1, r, g, b, f) end end end local function draw_glitter(t) local slot = t // 5 local r, g, b = c3(T.glit) for k = 1, 16 do local h = hash32(k * 0x9E3779B1 + slot * 0x85EBCA6B) if (h & 1) == 0 then local y = HZ + 1 + ((h >> 4) & 0x7FFF) % (H - HZ - 1) local spread = 2 + (y - HZ) // 2 local x = T.gx - spread + ((h >> 12) & 0x7FFF) % (2 * spread + 1) if x < cliff_x(y) - 1 then px(x, y, r, g, b) end end end end local function draw_foam(t) local r, g, b = c3(T.foam) for y = 58, H - 1 do local xl = cliff_x(y) local s = sin(t * 0.11 + y * 1.7) if s > -0.2 then px(xl - 1, y, r, g, b) end if s > 0.6 then px(xl - 2, y, r, g, b) end end end local function air_pos(d) local u = d / 30 return FX0 - 2 * (1 - u) + 5 * sin(PI * u), (WY + 2) - 6 * u - 96 * u * (1 - u) end local function float_state(p, t) local E = P - 100 local bob = 0.8 * sin(t * TAU / 66) local rest = WY - 4 if p < E then return FX0, rest + bob, 0, 0, false end local d = p - E if d < 30 then local dip = 1.4 * max(0, sin(d * TAU / 15)) return FX0, rest + bob * (1 - d / 30) + dip, dip * 0.6, 0, false end d = d - 30 if d < 20 then return FX0 - 2 * ease(d / 20), rest + 6 * ease(d / 8), 0.6 + 4.4 * ease(d / 8), 0, false end d = d - 20 if d < 30 then local x, y = air_pos(d) return x, y, 5 * (1 - ease(d / 5)), ease(d / 6), true end d = d - 30 return FX0, rest + bob * ease(d / 20), 0, 1 - ease(d / 20), false end local function draw_float(x, y) local iy = floor(y) local f = y - iy local base = iy if f >= 0.05 then base = iy + 1 local r = FLOAT[1] if iy < WY then blend_rect(x + r[1], iy, r[2], 1, r[3][1], r[3][2], r[3][3], 1 - f) end end for i = 1, #FLOAT do local yy = base + i - 1 if yy < WY then local r = FLOAT[i] rect(x + r[1], yy, r[2], 1, c3(r[3])) end end end local function splash(x, d) if d < 0 or d > 11 then return end local r, g, b = c3(T.foam) for k = -2, 2 do if k ~= 0 then local yy = WY - (1.3 - abs(k) * 0.25) * d + 0.12 * d * d if yy <= WY then px(floor(x + k * 0.35 * d + 0.5), floor(yy + 0.5), r, g, b) end end end if d < 8 then local rr = 1 + d // 2 blend_rect(floor(x) - rr - 1, WY, 2, 1, r, g, b, 0.6) blend_rect(floor(x) + rr, WY, 2, 1, r, g, b, 0.6) end end local function fish(x, y, wig) x = x + wig rect(x, y, 2, 4, c3(FISH)) rect(x + 1, y + 1, 1, 3, c3(BELLY)) px(x, y + 1, c3(EYE)) if wig == 0 then px(x - 1, y + 4, c3(FISH)); px(x + 2, y + 4, c3(FISH)) else rect(x, y + 4, 2, 1, c3(FISH)) end end local function draw_fish(p, t) local d = p - (P - 100 + 50) if d < 0 then return end local wig = (t // 3) % 2 if d < 14 then local fx, fy = air_pos(d) local x, y = floor(fx + 0.5), floor(fy + 0.5) + 4 if y + 1 < WY then fish(x, y, wig) end return end local fx0, fy0 = air_pos(14) local dd = d - 14 local y = fy0 + 4 + 0.1 * dd * dd if y < WY - 1 then fish(floor(fx0 - 0.2 * dd + 0.5), floor(y + 0.5), wig) else local dl = sqrt((WY - 1 - fy0 - 4) / 0.1) splash(fx0 - 0.2 * dl, dd - dl) end end local function draw_rod(hx, hy, a, bend) local ca, sa = cos(a), sin(a) for s = -5, RL do local u = s > 0 and s / RL or 0 local x = floor(hx + 0.5 - ca * s + 0.5) local y = floor(hy + 0.5 - sa * s + bend * u * u + 0.5) if s < 2 then px(x, y, c3(CORK)) elseif s < 15 then px(x, y, c3(ROD)) else px(x, y, c3(ROD_T)) end end return hx + 0.5 - ca * RL, hy + 0.5 - sa * RL + bend end function render() if not READY then apply() end if not BG then build_bg(); build_fg() end local t = frame * STEP_HZ // FPS gfx.blit_rgb(BG, 0, 0, W, H) for i = 1, #CLOUDS do local c = CLOUDS[i] local w = #CLOUD_ART[c[1]][1] sprite(CS[c[1]], (c[4] + t * c[3]) % (W + w) - w, c[2]) end draw_waves(t) draw_glitter(t) local q = t + P - 190 local p, n = q % P, q // P local E = P - 100 local fx, fy, bend, lift, air = float_state(p, t) local fxi = floor(fx + 0.5) if not air then blend_rect(fxi - 2, WY, 6, 1, T.wave[1], T.wave[2], T.wave[3], 0.6) local d = p - E if d >= 0 and d < 50 then local r = 2 + (d % 12) // 3 blend_rect(fxi - r - 1, WY + 1, 2, 1, T.foam[1], T.foam[2], T.foam[3], 0.5) blend_rect(fxi + r + 1, WY + 1, 2, 1, T.foam[1], T.foam[2], T.foam[3], 0.5) end end draw_float(fxi, fy) if n % 2 == 1 then draw_fish(p, t) end splash(FX0 + 1, p - (E + 80)) local a = A0 + (A1 - A0) * lift local hx, hy = HX + floor(2 * lift + 0.5), HY - floor(3 * lift + 0.5) local ca, sa = cos(a), sin(a) local tx, ty = hx + 0.5 - ca * RL, hy + 0.5 - sa * RL + bend line(tx, ty, fx + 0.5, fy < WY and fy or WY, c3(T.line)) gfx.blit_rgb(FG, 0, 0, W, H, KR, KG, KB) draw_foam(t) draw_rod(hx, hy, a, bend) line(43, 25, hx + 2, hy, c3(SKIN)) rect(hx, hy, 2, 2, c3(SKIN)) if (t + 17) % 130 < 3 then rect(41, 16, 1, 2, c3(SKIN)); px(41, 17, c3(EYE)) end end