info = { type = "image", title = "Big wave boat", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Boat tossed on stormy waves in the rain", api = 33, } local tr = ({ sk = { ["Sea"] = "More", ["Rain"] = "Dážď", ["Boat"] = "Loďka" }, cs = { ["Sea"] = "Moře", ["Rain"] = "Déšť", ["Boat"] = "Loďka" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "sea", label = T("Sea"), type = "int", min = 1, max = 5, default = 3 }, { key = "rain", label = T("Rain"), type = "int", min = 0, max = 5, default = 3 }, { key = "boat", label = T("Boat"), type = "bool", default = true }, } local rect, blend_rect, px, blend = gfx.rect, gfx.blend_rect, gfx.px, gfx.blend local floor, sin, atan, sqrt = math.floor, math.sin, math.atan, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local BASE = 36 local K, NP, NR, BOAT = 1, 10, 18, true local TOP = { 38, 44, 60 } local HOR = { 128, 136, 152 } local FAR = { 88, 108, 142 } local FOAM = { 238, 244, 252 } local LIT, MID, DARK = { 58, 126, 210 }, { 34, 92, 176 }, { 20, 60, 138 } local BODY = { 16, 52, 124 } local DEEP = { 6, 22, 70 } local SKYR = nil local PRE = 0 local function build_sky(stage) if stage == 1 then for y = 0, H - 1 do local t = y / 30 if t > 1 then t = 1 end rect(0, y, W, 1, floor(TOP[1] + (HOR[1] - TOP[1]) * t), floor(TOP[2] + (HOR[2] - TOP[2]) * t), floor(TOP[3] + (HOR[3] - TOP[3]) * t)) end for x = 0, W - 1 do local y = floor(30.5 + 1.2 * sin(x * 0.17) + 0.8 * sin(x * 0.41)) rect(x, y, 1, H - y, FAR[1], FAR[2], FAR[3]) end else gfx.blit_rgb(SKYR, 0, 0, W, H) for i = 1, 12 do local h = hash32(i * 0x9E3779B1) local x, y, w = umod(h, W + 10) - 5, umod(h >> 8, 20), 14 + umod(h >> 16, 18) local dark = i % 3 ~= 0 for j = 0, 3 do local s = (j == 0 or j == 3) and 0.6 or 1 if dark then blend_rect(x + floor(w * (1 - s) / 2), y + j, floor(w * s), 1, 26, 30, 44, 0.3) else blend_rect(x + floor(w * (1 - s) / 2), y + j, floor(w * s), 1, 170, 178, 192, 0.2) end end end end SKYR = gfx.snapshot(0, 0, W, H) end local WV = { { 56, 10, 5.5, 0.18 }, { 22, 16, 2.2, 0.12 }, { 9, 23, 0.8, 0 } } local function surf(x, t) local y = BASE for i = 1, 3 do local w = WV[i] local f = TAU * (x - w[2] * t) / w[1] y = y - K * w[3] * (sin(f) + w[4] * sin(2 * f + 0.9)) end return y end local CR = 0.85 * 9.8 local Y = {} local BCOL = { { 118, 82, 62 }, { 206, 190, 164 }, { 206, 210, 218 }, { 26, 38, 60 }, { 120, 130, 148 }, { 110, 120, 136 }, { 240, 244, 250 } } local GW, GH, CX, CY = 26, 26, 13, 18 local function shape(u, v) if u > 8.5 or u < -8.5 then return 0 end local vt = 2.4 + 0.25 * (u > 0 and u or 0) local vb = -4.2 * (1 - (u / 9) ^ 2) if u >= -3.2 and u <= -0.8 and v >= 3.4 and v <= 5.4 then return 4 end if u >= 4.6 and u <= 7.4 and v >= 11 and v <= 12.8 then return 7 end if u >= 3.4 and u <= 4.6 and v >= 4 and v <= 13 then return 6 end if u >= -5.6 and u <= 1.6 and v >= 6.4 and v <= 7.6 then return 5 end if u >= -5 and u <= 1 and v >= 2.4 and v <= 6.4 then return 3 end if v <= vt and v >= vt - 1 then return 2 end if v < vt - 1 and v >= vb then return 1 end return 0 end local POSE = {} local function pose(k) if POSE[k] then return POSE[k] end local a = k * 0.1 local ca, sa = math.cos(a), sin(a) local runs, open = {}, {} for j = 0, GH - 1 do local V = CY - (j + 0.5) local row = {} for i = 0, GW - 1 do local Uu = i + 0.5 - CX row[i] = shape(Uu * ca - V * sa, Uu * sa + V * ca) end local i = 0 while i < GW do local c, n = row[i], 1 while c ~= 0 and i + n < GW and row[i + n] == c do n = n + 1 end if c ~= 0 then local key = i * 4096 + n * 64 + c local o = open[key] if o and o[2] + o[5] == j then o[5] = o[5] + 1 else o = { i, j, n, c, 1 }; runs[#runs + 1] = o; open[key] = o end end i = i + n end end POSE[k] = runs return runs end local function draw_runs(runs, x0, y0, a) for i = 1, #runs do local r = runs[i] local c = BCOL[r[4]] if a >= 1 then rect(x0 + r[1], y0 + r[2], r[3], r[5], c[1], c[2], c[3]) else blend_rect(x0 + r[1], y0 + r[2], r[3], r[5], c[1], c[2], c[3], a) end end end local function draw_boat(t) local xb = 30 + 3 * sin(t * 0.84) local ix = floor(xb + 0.5) local yl, yc, yr = surf(ix - 6, t), surf(ix, t), surf(ix + 6, t) local yy = (yl + yc + yr) / 3 - 1.5 local ang = atan((surf(ix + 7, t) - surf(ix - 7, t)) / 14) * 0.85 local k = floor(ang / 0.1 + 0.5) if k > 3 then k = 3 elseif k < -3 then k = -3 end local runs = POSE[k] if not runs then return end local y0 = floor(yy) - CY local f = yy - floor(yy) if f < 0.1 then draw_runs(runs, ix - CX, y0, 1) elseif f > 0.9 then draw_runs(runs, ix - CX, y0 + 1, 1) else draw_runs(runs, ix - CX, y0, 1 - f); draw_runs(runs, ix - CX, y0 + 1, f) end end local G = 55 local function spray(t) for i = 1, NP do local h = hash32(i * 0x85EBCA6B) local per = 0.9 + umod(h, 7) / 10 local off = umod(h >> 8, 100) / 100 * per local n = floor((t + off) / per) local tau = (t + off) - n * per local ts = t - tau local hh = hash32(n * 0x9E3779B1 + i * 0x7F4A7C15) local xc = (WV[1][1] / 4 + WV[1][2] * ts) % WV[1][1] + umod(hh, 2) * WV[1][1] + umod(hh >> 4, 5) - 2 if xc >= 0 and xc < W then local y0 = surf(xc, ts) - 1 local vx = WV[1][2] + umod(hh >> 8, 16) - 4 local vy = -(14 + umod(hh >> 12, 14)) local x = xc + vx * tau local y = y0 + vy * tau + 0.5 * G * tau * tau local a = 0.9 - tau / per * 0.7 if y < y0 + 3 then blend(floor(x + 0.5), floor(y + 0.5), 240, 246, 252, a) end end end end local function rain(t) for i = 1, NR do local h = hash32(i * 0xC2B2AE35) local x = (umod(h, W + 24) + t * 34) % (W + 24) - 12 local y = (umod(h >> 8, H + 10) + t * 96) % (H + 10) - 5 local ix, iy = floor(x), floor(y) blend_rect(ix, iy - 1, 1, 2, 196, 208, 228, 0.5) end end function on_settings() local sea = cfg.sea or 3 K = 0.6 + 0.16 * sea NP = 4 + 2 * sea NR = (cfg.rain or 3) * 8 BOAT = cfg.boat ~= false end function render() if PRE < 2 then PRE = PRE + 1; build_sky(PRE) elseif PRE < 9 then PRE = PRE + 1; pose(PRE - 6) end local t = frame / FPS gfx.blit_rgb(SKYR, 0, 0, W, H) if BOAT then draw_boat(t) end for x = 0, W do Y[x] = surf(x, t) end for x = 0, W - 2, 2 do local yb = Y[x] < Y[x + 1] and Y[x] or Y[x + 1] local ys = floor(yb) + 6 rect(x, ys, 2, H - ys, BODY[1], BODY[2], BODY[3]) end for x = 0, W - 1 do local y = Y[x] local ys = floor(y) local fr = y - ys local c = (BASE - y) / (CR * K) local sl = Y[x + 1] - y local face = sl > 0.7 and DARK or (sl < -0.5 and LIT or MID) local top = c > 0.4 and FOAM or face if fr < 0.05 then px(x, ys, top[1], top[2], top[3]) else blend(x, ys, top[1], top[2], top[3], 1 - fr) end rect(x, ys + 1, 1, 5, face[1], face[2], face[3]) if c > 0.4 then rect(x, ys + 1, 1, floor((c - 0.4) * 8) + 1, FOAM[1], FOAM[2], FOAM[3]) if c > 0.75 and umod(hash32(x * 31 + floor(t * 8)), 4) ~= 0 then px(x, ys - 1, FOAM[1], FOAM[2], FOAM[3]) end end end for y = 52, H - 1 do blend_rect(0, y, W, 1, DEEP[1], DEEP[2], DEEP[3], 0.6 * (y - 51) / 12) end spray(t) rain(t) end