info = { type = "image", title = "Shoreline", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Waves wash the sand, a fish jumps, a crab runs", api = 41, } local tr = ({ sk = { ["Wave speed"] = "Rýchlosť vĺn", ["calm"] = "pokojne", ["normal"] = "normálne", ["rough"] = "drsné", ["Fish and crab"] = "Ryba a krab" }, cs = { ["Wave speed"] = "Rychlost vln", ["calm"] = "klidně", ["normal"] = "normálně", ["rough"] = "drsné", ["Fish and crab"] = "Ryba a krab" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Wave speed"), type = "int", default = 1, opts = { T("calm"), T("normal"), T("rough") } }, { key = "life", label = T("Fish and crab"), type = "bool", default = true }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local floor, sin, cos, PI = math.floor, math.sin, math.cos, math.pi local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local DRY, DOT, WET = { 238, 208, 142 }, { 210, 174, 104 }, { 180, 138, 84 } local SHAL, TURQ = { 72, 202, 200 }, { 24, 158, 184 } local MID, DEEP = { 18, 106, 168 }, { 12, 62, 130 } local RUN, FOAM = { 128, 224, 212 }, { 246, 252, 255 } local SWELL = { 44, 106, 172 } local STAR, STAR_C = { 238, 112, 58 }, { 255, 170, 98 } local SHELL = { 250, 178, 186 } local FISH, FISH_L, FISH_D = { 240, 72, 44 }, { 255, 152, 98 }, { 170, 34, 26 } local SHADE = { 8, 44, 98 } local CRAB, CRAB_D = { 224, 66, 40 }, { 160, 40, 28 } local CLAW, CEYE = { 252, 120, 74 }, { 46, 20, 16 } local function c3(c) return c[1], c[2], c[3] end local S, B1, B2, B3 = {}, {}, {}, {} for y = 0, H - 1 do S[y] = floor(16 + 0.44 * y + 2.2 * sin(0.19 * y + 0.7) + 1.3 * sin(0.47 * y + 2.1) + 0.5) B1[y] = floor(5 + 1.5 * sin(0.31 * y + 0.4) + 0.5) B2[y] = floor(12 + 2 * sin(0.23 * y + 1.0) + 0.5) B3[y] = floor(22 + 3 * sin(0.17 * y + 2.5) + 0.5) end local BG = nil local STARFISH = { { 0, -2 }, { 0, -1 }, { -2, 0 }, { -1, 0 }, { 1, 0 }, { 2, 0 }, { -1, 1 }, { -2, 2 }, { 1, 1 }, { 2, 2 } } local function build_bg() for y = 0, H - 1 do local s = S[y] rect(0, y, s, 1, c3(DRY)) rect(s, y, B1[y], 1, c3(SHAL)) rect(s + B1[y], y, B2[y] - B1[y], 1, c3(TURQ)) rect(s + B2[y], y, B3[y] - B2[y], 1, c3(MID)) rect(s + B3[y], y, W, 1, c3(DEEP)) end for i = 1, 90 do local h = hash32(i * 0x9E3779B1) local y, x = umod(h, H), umod(h >> 8, W) if x < S[y] - 2 then px(x, y, c3(DOT)) end end for i = 1, #STARFISH do px(7 + STARFISH[i][1], 51 + STARFISH[i][2], c3(STAR)) end px(7, 51, c3(STAR_C)) px(4, 22, c3(SHELL)); px(5, 22, c3(SHELL)); px(4, 23, c3(FOAM)) px(13, 58, c3(SHELL)); px(14, 57, c3(SHELL)) BG = gfx.snapshot(0, 0, W, H) end local PERIODS = { 6, 4.5, 3.33 } local P, LIFE = 4.5, true function on_settings() P = PERIODS[(cfg.speed or 1) + 1] or 4.5 LIFE = cfg.life ~= false end local AC, AP, cur_c = {}, {}, nil local function shape(c, A) local p1, p2 = c * 1.7, c * 2.9 for y = 0, H - 1 do A[y] = 1 + 0.26 * sin(0.27 * y + p1) + 0.12 * sin(0.71 * y + p2) end end local function peak(c) return 7 + umod(hash32(c * 0x9E3779B1 + 0x51ED), 6) end local function draw_water(t) local c = floor(t / P) if c ~= cur_c then shape(c, AC); shape(c - 1, AP); cur_c = c end local u = (t % P) / P local R, Rp = peak(c), peak(c - 1) local run, back if u < 0.35 then run = R * sin(u / 0.35 * PI * 0.5); back = false else run = R * 0.5 * (cos((u - 0.35) / 0.65 * PI) + 1); back = true end local wc = back and R * (1 - (u - 0.35) / 1.4) or 0 local wp = Rp * (1 - (u + 0.65) / 1.4) local fr, fg, fb = FOAM[1], FOAM[2], FOAM[3] local rr, rg, rb = RUN[1], RUN[2], RUN[3] local wr_, wg_, wb_ = WET[1], WET[2], WET[3] for y = 0, H - 1 do local s, a = S[y], AC[y] local er = run * a local wr = wc * a local w2 = wp * AP[y] if w2 > wr then wr = w2 end local e = s - er local ei = floor(e) if wr > er then local wx = floor(s - wr + 0.5) if wx <= ei then rect(wx, y, ei - wx + 1, 1, wr_, wg_, wb_) end end if s - ei - 1 > 0 then rect(ei + 1, y, s - ei - 1, 1, rr, rg, rb) end local cov = 1 - (e - ei) if back and (y * 5 + c * 3) % 7 < 3 then blend(ei, y, rr, rg, rb, cov) else px(ei + 1, y, fr, fg, fb) blend(ei, y, fr, fg, fb, cov) end end if u > 0.55 then local k = (u - 0.55) / 0.45 local d = 22 * (1 - k) local al = k < 0.2 and k * 5 or 1 for y = 0, H - 1 do if (y + c * 5) % 6 ~= 0 then local x = S[y] + 1 + d * AC[y] local xi = floor(x) local f = x - xi blend(xi, y, fr, fg, fb, al * (1 - f)) blend(xi + 1, y, fr, fg, fb, al * f) end end end end local SWY, SWP = {}, {} for i = 1, 9 do local h = hash32(i * 0xC2B2AE35 + 11) SWY[i], SWP[i] = umod(h, 34), umod(h >> 8, 30) end local function draw_swell(t) local r_, g_, b_ = SWELL[1], SWELL[2], SWELL[3] for i = 1, #SWY do local d = 22 + (SWP[i] - t * 3) % 30 for r = 0, 3 do local y = SWY[i] + r local over = d - B3[y] - 1 if over > 0 then local x = S[y] + d if x < W then local al = over < 3 and over / 3 or 1 local xi = floor(x) local f = x - xi blend(xi, y, r_, g_, b_, al * (1 - f)) blend(xi + 1, y, r_, g_, b_, al * f) end end end end end local EV = 7 local AIR, DIST = 1, 12 local function window(len, t) local c = data.time if c.ok then return c.unix // len, c.unix % len + c.ms / 1000 end return floor(t / len), t % len end local RING = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } } local function ring(x, y, q) if q < 0 or q >= 0.8 then return end local r = 1 + floor(q * 6) local a = 1 - q / 0.8 local d = floor(r * 0.7 + 0.5) local fr, fg, fb = FOAM[1], FOAM[2], FOAM[3] for i = 1, 4 do blend(x + RING[i][1] * r, y + RING[i][2] * r, fr, fg, fb, a) end if r > 1 then blend(x + d, y + d, fr, fg, fb, a); blend(x - d, y + d, fr, fg, fb, a) blend(x + d, y - d, fr, fg, fb, a); blend(x - d, y - d, fr, fg, fb, a) end end local function draw_fish(h, le) local y0 = 14 + umod(h >> 4, 18) local x0 = S[y0] + B2[y0] + 1 + umod(h >> 10, 5) local dir = ((h >> 15) & 1) == 1 and 1 or -1 local x1 = floor(x0 + 0.44 * dir * DIST + 0.5) local y1 = y0 + dir * DIST ring(x0, y0, le) ring(x1, y1, le - AIR) if le >= AIR then return end local k = le / AIR local p = DIST * k local pi_ = floor(p) local fp = p - pi_ local hy = y0 + dir * pi_ local hx = floor(x0 + 0.44 * dir * p + 0.5) local lift = sin(k * PI) local so = floor(lift * 3 + 0.5) px(hx + so, hy + so, c3(SHADE)); px(hx + so, hy + so - dir, c3(SHADE)) px(hx + so, hy + so - 2 * dir, c3(SHADE)) px(hx, hy, c3(FISH)) px(hx, hy - dir, c3(FISH)) if lift > 0.6 then px(hx + 1, hy - dir, c3(FISH_D)); px(hx - 1, hy - dir, c3(FISH_D)) end px(hx, hy - 2 * dir, c3(FISH_D)) px(hx - 1, hy - 3 * dir, c3(FISH_L)); px(hx + 1, hy - 3 * dir, c3(FISH_L)) blend(hx, hy + dir, FISH[1], FISH[2], FISH[3], fp) end local function walk(tm) local w = tm % 0.6 local mv = w < 0.33 local px_ = floor(w * 30) if px_ > 10 then px_ = 10 end return floor(tm / 0.6) * 10 + px_, mv end local BACK = 3.67 local function draw_crab(h, le) local yc = 38 + umod(h >> 4, 18) local dist = S[yc] - 10 local x, mv if le < BACK then local g g, mv = walk(le) if g >= dist then g, mv = dist, false end x = -7 + g else local g g, mv = walk(le - BACK) if g > dist then return end x = -7 + dist - g end local leg = mv and floor(le * 15) % 2 or 0 px(x + 1, yc - 1, c3(CEYE)); px(x + 3, yc - 1, c3(CEYE)) px(x - 1, yc - 1, c3(CLAW)); px(x + 5, yc - 1, c3(CLAW)) rect(x - 1, yc, 7, 1, c3(CRAB)) rect(x, yc + 1, 5, 1, c3(CRAB)) rect(x + 1, yc + 1, 3, 1, c3(CRAB_D)) if leg == 0 then px(x - 1, yc + 2, c3(CRAB_D)); px(x + 1, yc + 2, c3(CRAB_D)) px(x + 3, yc + 2, c3(CRAB_D)); px(x + 5, yc + 2, c3(CRAB_D)) else px(x, yc + 2, c3(CRAB_D)); px(x + 2, yc + 2, c3(CRAB_D)); px(x + 4, yc + 2, c3(CRAB_D)) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS draw_swell(t) draw_water(t) if LIFE then local n, le = window(EV, t) local h = hash32(n * 0x85EBCA6B + 0x2F) local kind = umod(h, 5) if kind < 2 then if le < AIR + 0.8 then draw_fish(h, le) end elseif kind < 4 then draw_crab(h, le) end end end