info = { type = "image", title = "Sandcastle tide", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A sandcastle grows, a wave takes it, repeat", api = 33, } local tr = ({ sk = { ["Flag"] = "Vlajka", ["Speed"] = "Rýchlosť" }, cs = { ["Flag"] = "Vlajka", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "flag", label = T("Flag"), type = "color", default = "#E53A32" }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend, blend_rect, blit_rgb = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blit_rgb local floor, sin = math.floor, math.sin local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HOR = 22 local SKY0, SKY1 = { 58, 132, 230 }, { 178, 226, 248 } local SEA0, SEA1 = { 30, 104, 176 }, { 96, 214, 214 } local SAND0, SAND1 = { 214, 186, 128 }, { 246, 226, 172 } local FOAM = { 244, 250, 250 } local SL, SM, SD, SW = { 236, 192, 104 }, { 212, 160, 76 }, { 170, 120, 52 }, { 110, 76, 36 } local WET = { 150, 112, 64 } local CYC = 36 local PT = { 1.5, 4.0, 6.5, 9.0, 11.5, 14.0, 16.0 } local RISE = 1.4 local CX, GY = 26, 58 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 function smooth(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local SAND, SEA, SEAC = nil, nil, {} local function rows(y0, y1, a, b) for y = y0, y1 do rect(0, y, W, 1, lerp3(a, b, (y - y0) / (y1 - y0))) end end local function build() rows(0, 63, SAND0, SAND1) for i = 1, 46 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), 27 + umod(h >> 8, 37) local k = ((h >> 20) & 1) == 0 and -14 or 10 local r, g, b = lerp3(SAND0, SAND1, (y - 27) / 36) px(x, y, r + k, g + k, b + k * 7 // 10) end SAND = gfx.snapshot(0, 0, W, H) rows(0, HOR - 1, SKY0, SKY1) rows(HOR, 63, SEA0, SEA1) for y = HOR, 63 do SEAC[y] = { lerp3(SEA0, SEA1, (y - HOR) / (63 - HOR)) } end local R = { 2, 3, 4, 4, 4, 3, 2 } for i = 1, 7 do rect(50 - R[i], 4 + i, 2 * R[i] + 1, 1, 255, 246, 196) end SEA = gfx.snapshot(0, 0, W, H) end local function sea(yf, ph) local yi = floor(yf) blit_rgb(SEA, 0, 0, W, yi - 2) for x0 = 0, W - 1, 4 do local yb = floor(yf + 1.3 * sin(x0 * 0.23 + ph)) if yb > yi - 2 then local c = SEAC[yb - 1 > 63 and 63 or yb - 1] rect(x0, yi - 2, 4, yb - yi + 2, c[1], c[2], c[3]) end rect(x0, yb, 4, 1, FOAM[1], FOAM[2], FOAM[3]) blend_rect(x0, yb + 1, 4, 1, FOAM[1], FOAM[2], FOAM[3], 0.4) end end local function cloud(xf, y, w, a) local x = floor(xf) local f = xf - x blend_rect(x + 5, y, w - 10, 1, 255, 255, 255, a) blend(x + 4, y, 255, 255, 255, a * (1 - f)) blend(x + w - 5, y, 255, 255, 255, a * f) blend_rect(x + 1, y + 1, w - 2, 1, 255, 255, 255, a) blend(x, y + 1, 255, 255, 255, a * (1 - f)) blend(x + w - 1, y + 1, 255, 255, 255, a * f) end local SPK = {} for i = 1, 10 do local h = hash32(i * 0x85EBCA6B) SPK[i] = { umod(h, W), HOR + 1 + umod(h >> 8, 30), umod(h >> 16, 628) / 100 } end local function body(x, y, w, h, s) rect(x, y + s, w, h, SM[1], SM[2], SM[3]) rect(x, y + s, 1, h, SL[1], SL[2], SL[3]) rect(x + w - 1, y + s, 1, h, SD[1], SD[2], SD[3]) end local function merl(x, y, w, s) for i = 0, w - 1, 2 do rect(x + i, y + s, 1, 1, SL[1], SL[2], SL[3]) end end local function win(x, y, s) rect(x, y + s, 1, 2, SW[1], SW[2], SW[3]) end local PIECES = { { CX - 16, 55, 32, 4, function(s) rect(CX - 12, 55 + s, 24, 1, SL[1], SL[2], SL[3]) rect(CX - 14, 56 + s, 28, 1, SM[1], SM[2], SM[3]) rect(CX - 16, 57 + s, 32, 1, SM[1], SM[2], SM[3]) rect(CX - 16, 58 + s, 32, 1, SD[1], SD[2], SD[3]) end }, { CX - 6, 49, 13, 6, function(s) body(CX - 6, 50, 13, 5, s); merl(CX - 6, 49, 13, s) rect(CX - 1, 52 + s, 3, 3, SW[1], SW[2], SW[3]); rect(CX, 51 + s, 1, 1, SW[1], SW[2], SW[3]) end }, { CX - 13, 44, 7, 11, function(s) body(CX - 13, 45, 7, 10, s); merl(CX - 13, 44, 7, s); win(CX - 10, 48, s) end }, { CX + 7, 46, 7, 9, function(s) body(CX + 7, 47, 7, 8, s); merl(CX + 7, 46, 7, s); win(CX + 10, 50, s) end }, { CX - 4, 37, 9, 13, function(s) body(CX - 4, 38, 9, 12, s); merl(CX - 4, 37, 9, s); win(CX - 2, 41, s); win(CX + 2, 41, s) end }, { CX - 2, 32, 5, 5, function(s) body(CX - 2, 33, 5, 4, s); merl(CX - 2, 32, 5, s) end }, } local function castle(t, d, fc, fr, f) for i = 1, 7 do local u = (t - PT[i]) / RISE if u > 0 then local e = u >= 1 and 1 or 1 - (1 - u) * (1 - u) local bx, by, bw, bh, fn if i <= 6 then bx, by, bw, bh, fn = PIECES[i][1], PIECES[i][2], PIECES[i][3], PIECES[i][4], PIECES[i][5] else bx, by, bw, bh = CX, 26, 6, 6 end local s = floor((1 - e) * bh + 0.5) + d if d > 0 then gfx.clip(0, 0, W, GY + 1) else gfx.clip(bx, by, bw, bh) end if fn then fn(s) else rect(CX, 26 + s, 1, 6, SW[1], SW[2], SW[3]) local up = (f // 12) % 2 rect(CX + 1, 26 + s, 4, 2, fc, fr[1], fr[2]) px(CX + 5, 26 + up + s, fc, fr[1], fr[2]) end end end gfx.clip() end function render() if not SAND then build() end local ts = frame * (cfg.speed or 3) / 3 / FPS + 13 local t = ts % CYC blit_rgb(SAND, 0, 0, W, H) local yf if t < 20 then yf = 30 + 14 * t / 20 + 0.5 * sin(ts * 1.1) elseif t < 24.5 then yf = 44 + 18 * smooth((t - 20) / 4.5) elseif t < 27 then yf = 62 elseif t < 34 then yf = 62 - 32 * smooth((t - 27) / 7) else yf = 30 end if t >= 27 then local a = 0.5 * (1 - (t - 27) / 9) local y0 = floor(yf) + 3 blend_rect(0, y0, W, 63 - y0 + 1, WET[1], WET[2], WET[3], a) end sea(yf, ts * 1.6) cloud(6 + ts * 4 % 90 - 16, 4, 17, 0.7) cloud(34 + ts * 2.6 % 90 - 16, 11, 13, 0.5) for i = 1, 10 do local s = SPK[i] if s[2] < yf - 2 then local a = sin(ts * 2.2 + s[3]) * 0.5 + 0.1 if a > 0 then blend(s[1], s[2], 255, 255, 255, a) end end end local fc = cfg.flag or { r = 229, g = 58, b = 50 } local d = t > 22.8 and floor(34 * (smooth((t - 22.8) / 3.8) ^ 1.5) + 0.5) or 0 castle(t, d, fc.r, { fc.g, fc.b }, frame) local sw = smooth((t - 22.8) / 1.6) * (1 - smooth((t - 26.5) / 3)) if sw > 0.02 then local L = floor(6 * sw + 0.5) local c = SEAC[60] for x0 = 0, W - 1, 4 do local top = GY - L + floor(sin(x0 * 0.3 + ts * 2) + 0.5) rect(x0, top, 4, 64 - top, c[1], c[2], c[3]) rect(x0, top, 4, 1, FOAM[1], FOAM[2], FOAM[3]) end end rect(50, 56, 7, 1, 120, 170, 255); rect(51, 57, 5, 3, 52, 100, 210) rect(51, 57, 1, 3, 90, 140, 240); rect(52, 60, 3, 1, 36, 70, 160) rect(59, 53, 1, 7, 150, 100, 56); rect(58, 59, 3, 2, 220, 70, 60) end