info = { type = "image", title = "Aquarium", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Fish, plants and bubbles in a sunlit tank", api = 39, } permissions = { "tilt", "location" } local tr = { ["Fish count"] = { "Počet rýb", "Počet ryb" }, ["big ones: at most three"] = { "veľké: najviac tri", "velké: nejvýše tři" }, ["Bubbles"] = { "Bubliny", "Bubliny" }, ["from decoration, plants, shell"] = { "z výzdoby, rastlín a mušle", "z výzdoby, rostlin a mušle" }, ["Plants"] = { "Rastliny", "Rostliny" }, ["Speed"] = { "Rýchlosť", "Rychlost" }, ["„random“ = changes by itself"] = { "„náhodne“ = mení sa samo", "„náhodně“ = mění se samo" }, ["random"] = { "náhodne", "náhodně" }, ["slow"] = { "pomaly", "pomalu" }, ["medium"] = { "stredne", "středně" }, ["fast"] = { "rýchlo", "rychle" }, ["Life on the bottom"] = { "Život na dne", "Život na dně" }, ["crab, snail and shell"] = { "krab, slimák a mušľa", "krab, šnek a mušle" }, ["Aquarium type"] = { "Typ akvária", "Typ akvária" }, ["water, sand, plants and fish"] = { "voda, piesok, rastliny a ryby", "voda, písek, rostliny a ryby" }, ["freshwater"] = { "sladká voda", "sladká voda" }, ["reef"] = { "útes", "útes" }, ["blackwater"] = { "čierna voda", "černá voda" }, ["Fish size"] = { "Veľkosť rýb", "Velikost ryb" }, ["species follow the aquarium type"] = { "druhy podľa typu akvária", "druhy podle typu akvária" }, ["big"] = { "veľké", "velké" }, ["small"] = { "malé", "malé" }, ["Decoration"] = { "Výzdoba", "Výzdoba" }, ["somewhere for the fish to hide"] = { "úkryt pre ryby", "úkryt pro ryby" }, ["none"] = { "žiadne", "žádné" }, ["castle"] = { "hrad", "hrad" }, ["amphora"] = { "amfora", "amfora" }, ["wreck"] = { "vrak", "vrak" }, ["Night mode"] = { "Nočný režim", "Noční režim" }, ["the water deepens after dusk"] = { "voda po súmraku tmavne", "voda po soumraku tmavne" }, ["after dusk"] = { "po súmraku", "po soumraku" }, ["always"] = { "vždy", "vždy" }, ["never"] = { "nikdy", "nikdy" }, } local LI = ({ sk = 1, cs = 2 })[lang] local function T(s) local v = LI and tr[s]; return v and v[LI] or s end settings = { { key = "fish", type = "int", default = 5, min = 2, max = 8, label = T("Fish count"), hint = T("big ones: at most three") }, { key = "bubbles", type = "bool", default = true, label = T("Bubbles"), hint = T("from decoration, plants, shell") }, { key = "plants", type = "bool", default = true, label = T("Plants") }, { key = "speed", type = "int", default = "slow", label = T("Speed"), hint = T("„random“ = changes by itself"), opts = { keys = { "random", "slow", "medium", "fast" }, T("random"), T("slow"), T("medium"), T("fast") } }, { key = "bottom", type = "bool", default = true, label = T("Life on the bottom"), hint = T("crab, snail and shell") }, { key = "kind", type = "int", default = "freshwater", label = T("Aquarium type"), hint = T("water, sand, plants and fish"), opts = { keys = { "freshwater", "reef", "blackwater" }, T("freshwater"), T("reef"), T("blackwater") } }, { key = "size", type = "int", default = "big", label = T("Fish size"), hint = T("species follow the aquarium type"), opts = { keys = { "big", "small" }, T("big"), T("small") } }, { key = "decor", type = "int", default = "castle", label = T("Decoration"), hint = T("somewhere for the fish to hide"), opts = { keys = { "none", "castle", "amphora", "wreck" }, T("none"), T("castle"), T("amphora"), T("wreck") } }, { key = "light", type = "int", default = "auto", label = T("Night mode"), hint = T("the water deepens after dusk"), opts = { keys = { "auto", "always", "never" }, T("after dusk"), T("always"), T("never") } }, } local px, blend, rect, blit = gfx.px, gfx.blend, gfx.rect, gfx.blit_indexed local floor, abs, sin = math.floor, math.abs, math.sin local hash32, umod = math.hash32, math.umod local schar, byte, concat, unpack = string.char, string.byte, table.concat, table.unpack local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local SAND_Y, MAX_FISH, TURN_F, NB = 54, 8, 14, 14 local KINDS = { freshwater = { day = { { 44, 132, 238 }, { 14, 62, 132 } }, night = { { 30, 100, 214 }, { 12, 58, 124 } }, back = { 16, 110, 92 }, sand = { { 220, 154, 62 }, { 120, 72, 22 } }, pebble = { 246, 192, 92 }, pa = { 130, 240, 90 }, pb = { 20, 128, 52 }, shape = 1, ray = 1.0, fish = { { 255, 150, 40 }, { 255, 226, 70 }, { 240, 90, 160 }, { 120, 240, 200 } }, big = { "angel", "gourami" }, }, reef = { day = { { 44, 200, 214 }, { 8, 90, 144 } }, night = { { 26, 150, 196 }, { 6, 66, 126 } }, back = { 20, 60, 130 }, sand = { { 236, 226, 200 }, { 150, 140, 116 } }, pebble = { 255, 248, 228 }, pa = { 255, 206, 234 }, pb = { 214, 52, 140 }, shape = 2, ray = 1.25, fish = { { 255, 165, 30 }, { 255, 240, 90 }, { 255, 90, 120 }, { 180, 120, 255 } }, big = { "clown", "tang" }, }, blackwater = { day = { { 180, 122, 48 }, { 96, 58, 18 } }, night = { { 150, 104, 46 }, { 88, 52, 18 } }, back = { 44, 30, 12 }, sand = { { 120, 90, 58 }, { 60, 42, 24 } }, pebble = { 150, 118, 76 }, pa = { 190, 210, 70 }, pb = { 80, 104, 30 }, shape = 3, ray = 0.8, fish = { { 255, 90, 40 }, { 80, 224, 255 }, { 236, 40, 60 }, { 255, 232, 140 } }, big = { "gourami", "angel" }, }, } local K = KINDS.freshwater local SPR = { clown = { keys = "kodwe", tail = 4, pal = { 0, 0, 0, 8, 8, 12, 255, 110, 20, 200, 66, 8, 250, 250, 255, 8, 8, 10 }, ".......kddddddk...", "kk......kkkokkk...", "ddkk..kkwdoowdokk.", "ddddkkoowdoowdoook", "ddddddoowdoowdowek", "dddddooowdoowdoooo", "ddddddoowdoowdoook", "ddddkkoowdoowdoook", "ddkk..kkwdoowdokk.", "kk......kkkokkk...", "........kddddk....", }, tang = { keys = "kbnywe", tail = 4, pal = { 0, 0, 0, 8, 8, 12, 55, 95, 240, 18, 22, 86, 255, 205, 40, 250, 250, 255, 8, 8, 10 }, ".......kbbbbbk....", "kk....kkkbbbkkk...", "yyk..knnnbbbbbbk..", "yyykkbnnnnnnbbbbk.", "yyyybbbbbnnnnnwebk", "yyyybbbbbbbbnnbbbk", "yyyybbbbbbbbbbbbbk", "yyykkbbbbbbbbbbbk.", "yyk..kbbbbbbbbbk..", "kk....kkkbbbkkk...", ".......kbbbbk.....", }, angel = { keys = "kyYe", tail = 3, pal = { 0, 0, 0, 24, 18, 14, 255, 196, 60, 255, 238, 150, 12, 10, 10 }, "........k.....", ".......kk.....", "......kyk.....", ".....kyyk.....", "kk..kyykyyk...", "kyk.kyykyyyYk.", ".kykyykyyyeYYk", ".kykyykyyyyYYk", "kyk.kyykyyyYk.", "kk..kyykyyk...", ".....kyyk.....", "......kyk.....", ".......kk.....", "........k.....", }, gourami = { keys = "krbe", tail = 3, pal = { 0, 0, 0, 40, 14, 10, 248, 96, 36, 150, 34, 18, 10, 10, 10 }, "........kkk.....", "kk....kkrrrkkk..", "krk.kkrrrrrrrrk.", "krrkrrrrrrrrrrek", "krrrrrbbbbbbrrrk", "krrkrrrrrrrrrrk.", "krk.kkrrrrrrkk..", "kk....kkkrrk....", "........kk......", }, } local DECOR = { castle = { x = 40, src = { 6, 6 }, flag = true, body = { 132, 124, 150 }, hi = { 184, 176, 204 }, lo = { 74, 68, 92 }, "+ # + #", "+## +##", "+o# +o#", "+## +##", "+##+.+.+.++##", "+############", "+###.ooo.####", "+##.ooooo.###", "+##.ooooo.###", "+##.ooooo.###", "+############", }, amphora = { x = 38, src = { 1, 3 }, body = { 190, 104, 56 }, hi = { 232, 152, 92 }, lo = { 110, 54, 28 }, " ...... ", " .. .####+. ", ".oo..######+. ", ".oo.########+.", ".oo.########+.", " .. .######. ", " .####. ", " .... ", }, wreck = { x = 36, src = { 3, 6 }, body = { 118, 128, 106 }, hi = { 178, 188, 152 }, lo = { 62, 70, 58 }, " + ", " + + ", " .+. + ", " + .+. ", " + + ", " ..............+", " .#o##o##o####+.", " .###########. ", " .#########. ", " ........ ", }, } local CORAL = { " # # ", "# # # #", "# # ## #", "## # # #", " ## ## ", " # ## ", " ## # ", " ### ", " # ", " # ", } local function mixc(ar, ag, ab, br, bg, bb, t) return floor(ar + (br - ar) * t + 0.5), floor(ag + (bg - ag) * t + 0.5), floor(ab + (bb - ab) * t + 0.5) end local function mixp(a, b, t) return mixc(a[1], a[2], a[3], b[1], b[2], b[3], t) end local lit, dk = 1.0, 1.0 local function C(r, g, b) return floor(r * lit), floor(g * lit), floor(b * lit) end local rng = 0x9E3779B9 local function rnd() rng = (rng ~ (rng << 13)) & 0xFFFFFFFF rng = rng ~ (rng >> 17) rng = (rng ~ (rng << 5)) & 0xFFFFFFFF return rng end local function frnd(a, b) return a + (b - a) * (umod(rnd(), 1000) / 999.0) end local LVL = { random = 0, slow = 1, medium = 2, fast = 3 } local a_cur, a_tgt, a_hold = 1.0, 1.0, 0 local function auto_level(opt, n) if opt > 0 and opt <= n then a_cur, a_tgt, a_hold = opt - 1, opt - 1, 0; return a_cur end if a_hold == 0 then local span = n - 1 for i = 1, 8 do local t = umod(rnd(), 1000) / 999.0 * span if abs(t - a_tgt) >= span * 0.30 then a_tgt = t; break end if i == 8 then a_tgt = span - a_tgt end end a_hold = FPS * 25 + umod(rnd(), FPS * 60) end a_hold = a_hold - 1 a_cur = a_cur + (a_tgt - a_cur) * 0.0032 return a_cur end local SY, BY = {}, {} for x = -12, W + 12 do SY[x] = SAND_Y + floor(1.3 * sin(x * 0.13 + 0.8) + 0.8 * sin(x * 0.41 + 2.0) + 0.5) BY[x] = 45 + floor(2.6 * sin(x * 0.23 + 1.0) + 1.8 * sin(x * 0.61) + 0.5) end local function sy(x) if x < -12 then x = -12 elseif x > W + 12 then x = W + 12 end return SY[x] end local SMIN = SAND_Y for x = 0, W - 1 do if SY[x] < SMIN then SMIN = SY[x] end end local SNR, SPAL = H - SMIN, { 0, 0, 0 } local SANDC do local rows, line = {}, {} for y = SMIN, H - 1 do for x = 0, W - 1 do local v = 0 if y >= SY[x] then local k = umod(hash32((y >> 1) * 0x9E3779B1 + (x >> 1) * 0x85EBCA6B), 5) v = 1 + ((y - SMIN) * 3 + (k == 0 and 0 or (k == 4 and 2 or 1))) * 2 + (y == SY[x] and 1 or 0) end line[x + 1] = v end rows[#rows + 1] = schar(unpack(line, 1, W)) end SANDC = concat(rows) end local WR, WG, WB = {}, {}, {} local PR, PG, PB = {}, {}, {} local SC = {} local function day_tables(dq) dk, lit = dq, 0.80 + 0.20 * dq local d, n = K.day, K.night for y = 0, H - 1 do local t = y / (SAND_Y + 2) if t > 1 then t = 1 end local r1, g1, b1 = mixp(n[1], n[2], t) local r2, g2, b2 = mixp(d[1], d[2], t) WR[y], WG[y], WB[y] = mixc(r1, g1, b1, r2, g2, b2, dq) end local s0, s1, pb = K.sand[1], K.sand[2], K.pebble for r = 0, SNR - 1 do local t = (SMIN + r - SAND_Y + 2) / (H - SAND_Y + 2) for k = 0, 2 do local cr, cg, cb = mixp(k == 0 and pb or s0, s1, (k == 2 and 0.45 or 0) + t * 0.5) local i = (1 + (r * 3 + k) * 2) * 3 + 1 SPAL[i], SPAL[i + 1], SPAL[i + 2] = C(cr, cg, cb) SPAL[i + 3], SPAL[i + 4], SPAL[i + 5] = C(mixc(cr, cg, cb, 255, 250, 230, 0.22)) end end for i = 0, 15 do local r, g, b = mixp(K.pb, K.pa, i / 15) PR[i], PG[i], PB[i] = C(r, g, b) end local wr, wg, wb = WR[28], WG[28], WB[28] for l = 0, 2 do for c = 1, 4 do local f, k = K.fish[c], ({ 0.30, 0.14, 0 })[l + 1] local r, g, b = mixc(f[1] * lit, f[2] * lit, f[3] * lit, wr, wg, wb, k) local tr, tg, tb = mixc(r, g, b, wr, wg, wb, 0.35) SC[l * 4 + c] = { r, g, b, tr, tg, tb } end end end local BAKED, baked_now = {}, false local function bake(name) local S = SPR[name] local w, h, keys, g, rows = #S[1], #S, S.keys, {}, {} for r = 1, h do local row = S[r] for c = 1, w do g[c] = keys:find(row:sub(c, c), 1, true) or 0 end rows[r] = schar(unpack(g, 1, w)) end BAKED[name] = { w = w, h = h, tail = S.tail, g = concat(rows), pal = {}, src = S.pal, {}, {} } end local function frame_of(B, dir, wag) local F = B[dir][wag] if F then return F end local w, h, tail, g = B.w, B.h, B.tail, B.g local sh = wag == 2 and 1 or (wag == 3 and -1 or 0) local rows, L, R, RC, T, Bt, BC, line = {}, {}, {}, {}, {}, {}, {}, {} for c = 1, w do T[c], Bt[c], BC[c] = 255, 255, 0 end for r = 1, h do for c = 1, w do local sc = dir == 1 and c or (w + 1 - c) local sr = sc <= tail and r + sh or r local v = (sr >= 1 and sr <= h) and byte(g, (sr - 1) * w + sc) or 0 line[c] = v if v > 0 then if not L[r] then L[r] = c - 1 end R[r], RC[r] = c - 1, v if T[c] == 255 then T[c] = r - 1 end Bt[c], BC[c] = r - 1, v end end rows[r] = schar(unpack(line, 1, w)) end local e = {} for r = 1, h do e[#e + 1] = schar(L[r] or 255, R[r] or 255, RC[r] or 0) end for c = 1, w do e[#e + 1] = schar(T[c], Bt[c], BC[c]) end F = { s = concat(rows), e = concat(e) } B[dir][wag] = F return F end local function sprite_pals() for _, B in pairs(BAKED) do for i = 1, #B.src do B.pal[i] = floor(B.src[i] * lit) end end end local fish = {} local BX, BYY, BV, BW, BL, BON = {}, {}, {}, {}, {}, {} local crab_x, crab_dir, crab_leg, crab_wait, crab_step = 20, 1, 0, 0, 0 local snail_x = 8.0 local SHELL_X = 31 local shell_open, shell_next = false, FPS * 7 local dec_next, dec_burst = FPS * 2, 0 local seeded, last_frame = false, 0 local SCENE, scene_key = nil, nil local CA = {} local function seed() for i = 1, MAX_FISH do local layer = (i - 1) % 3 fish[i] = { layer = layer, sp = (i - 1) % 2 + 1, v = (0.16 + 0.09 * layer) * frnd(0.72, 1.32), dir = (rnd() & 1) == 1 and 1 or -1, x = 8 + ((i - 1) * 23) % 52 + frnd(-3, 3), y = 15 + ((i - 1) * 13) % 26 + frnd(-2, 2), ph = frnd(0, TAU), sph = frnd(0, TAU), wsp = frnd(0.8, 1.25), col = umod(rnd(), 4) + 1, turn = 0, } fish[i].ty = fish[i].y end for i = 1, NB do BON[i] = false end for i = 1, 7 do CA[i] = { x = frnd(0, W), v = frnd(-0.07, 0.07), ph = frnd(0, TAU), sp = frnd(0.02, 0.05) } end a_cur, a_tgt, a_hold = 1.0, 1.0, 0 scene_key = nil end local function fish_n() local n = cfg.fish or 5 if n > MAX_FISH then n = MAX_FISH end if cfg.size ~= "small" and n > 3 then n = 3 end return n end local function line(x0, y0, x1, y1, thick, r, g, b) local n = math.max(abs(x1 - x0), abs(y1 - y0)) for i = 0, n do local x, y = floor(x0 + (x1 - x0) * i / n + 0.5), floor(y0 + (y1 - y0) * i / n + 0.5) rect(x, y, thick, 1, r, g, b) end end local function draw_static() for y = 0, H - 1 do rect(0, y, W, 1, WR[y], WG[y], WB[y]) end rect(0, 0, W, 1, mixc(WR[0], WG[0], WB[0], 220, 250, 255, 0.40 * (0.4 + 0.6 * dk))) rect(0, 1, W, 1, mixc(WR[1], WG[1], WB[1], 220, 250, 255, 0.16 * (0.4 + 0.6 * dk))) local bk = K.back for y = 38, SAND_Y + 2 do local r, g, b = mixc(WR[y], WG[y], WB[y], bk[1], bk[2], bk[3], 0.42) local x0 = nil for x = 0, W do local on = x < W and BY[x] <= y and y < SY[x] if on and not x0 then x0 = x elseif not on and x0 then rect(x0, y, x - x0, 1, r, g, b); x0 = nil end end end blit(SANDC, SPAL, 0, SMIN, W, SNR, W, SNR, true) if cfg.plants then if K.shape == 2 then local CC = { { 255, 136, 70 }, { 160, 50, 40 }, { 200, 120, 255 }, { 100, 46, 170 } } for k = 0, 1 do local x0 = k == 0 and 2 or 21 local base = sy(x0 + 4) for j = 1, #CORAL do local r, g, b = mixp(CC[k * 2 + 1], CC[k * 2 + 2], (j - 1) / (#CORAL - 1)) local row = CORAL[j] for i = 1, #row do if row:sub(i, i) == "#" then px(x0 + i - 1, base - #CORAL + j, C(r, g, b)) end end end end elseif K.shape == 3 then line(-1, sy(0) + 1, 9, 47, 2, C(150, 104, 58)) line(9, 47, 13, 36, 1, C(150, 104, 58)) line(9, 47, 21, 43, 1, C(150, 104, 58)) line(4, 52, 1, 42, 1, C(150, 104, 58)) line(10, 46, 14, 35, 1, C(204, 156, 96)) end end local d = DECOR[cfg.decor] if d then local y0 = sy(d.x + #d[1] // 2) + 1 - #d for j = 1, #d do local row = d[j] for i = 1, #row do local ch = row:sub(i, i) local c = (ch == "#" and d.body) or (ch == "+" and d.hi) or (ch == "." and d.lo) or nil if ch == "o" then px(d.x + i - 1, y0 + j - 1, 14, 16, 30) elseif c then px(d.x + i - 1, y0 + j - 1, C(c[1], c[2], c[3])) end end end d.y0 = y0 end end local function daylight() if cfg.light == "always" then return 0.0 end if cfg.light == "never" then return 1.0 end local t, s = data.time, data.sun if not (t and t.ok and s) then return 1.0 end if s.polar then return s.up and 1.0 or 0.0 end local h = t.h + t.m / 60.0 local rise, set = s.rise, s.set if h <= rise - 1 or h >= set + 1 then return 0.0 end if h >= rise + 1 and h <= set - 1 then return 1.0 end if h < rise + 1 then return (h - rise + 1) / 2 end return (set + 1 - h) / 2 end local function turn(f) if f.turn == 0 then f.dir, f.turn = -f.dir, TURN_F end end local function step_fish(f, m, big) local hh = big and 8 or 3 f.ph = (f.ph + 0.20 * f.wsp * (0.5 + m)) % TAU f.sph = (f.sph + 0.03 * f.wsp) % TAU if f.turn > 0 then f.turn = f.turn - 1 else f.x = f.x + f.dir * f.v * m * (0.75 + 0.45 * sin(f.sph)) if (f.dir > 0 and f.x > W + 2) or (f.dir < 0 and f.x < -2) then turn(f) elseif f.x > 16 and f.x < W - 16 and umod(rnd(), 900) == 0 then turn(f) end end if umod(rnd(), 500) == 0 then f.ty = frnd(hh + 6, SAND_Y - hh - 4) end local dy = (f.ty - f.y) * 0.03 if dy > 0.05 then dy = 0.05 elseif dy < -0.05 then dy = -0.05 end f.y = f.y + dy * m end local SPOOK_F = floor(FPS * 1.2) local spook, last_tx, last_ty = 0, 0.0, 0.0 local function spook_step(n) local t = data.tilt if t and t.ok then local dx, dy = (t.x or 0) - last_tx, (t.y or 0) - last_ty last_tx, last_ty = t.x or 0, t.y or 0 if (t.tap or t.bump or t.shake or (dx * dx + dy * dy) > 0.045) and spook < SPOOK_F * 0.4 then spook = SPOOK_F for i = 1, n do local f = fish[i] if (f.x < W * 0.5 and 1 or -1) == f.dir then turn(f) end end end end if spook > 0 then spook = spook - 1 end local k = spook / SPOOK_F return 1.0 + 2.4 * k * k end local function spawn(x, y, big) for i = 1, NB do if not BON[i] then BON[i], BX[i], BYY[i], BV[i], BW[i], BL[i] = true, x, y, 0.12, frnd(0, TAU), big return end end end local function step_bubbles() for i = 1, NB do if BON[i] then local vmax = BL[i] and 0.9 or 0.45 if BV[i] < vmax then BV[i] = BV[i] + 0.02 end BYY[i] = BYY[i] - BV[i] BW[i] = (BW[i] + 0.22) % TAU if BYY[i] < 2 then BON[i] = false end end end local d = DECOR[cfg.decor] if d and d.y0 then if frame >= dec_next then spawn(d.x + d.src[1], d.y0 + d.src[2] - 1, umod(rnd(), 3) > 0) if dec_burst > 0 then dec_burst = dec_burst - 1; dec_next = frame + FPS // 6 + umod(rnd(), FPS // 5) else dec_burst = 2 + umod(rnd(), 5); dec_next = frame + FPS * 2 + umod(rnd(), FPS * 5) end end elseif umod(rnd(), 50) == 0 then spawn(frnd(4, W - 4), SAND_Y, (rnd() & 1) == 1) end if cfg.plants and umod(rnd(), 70) == 0 then spawn(frnd(3, 28), frnd(32, 46), false) end end local function step_bottom(m) if crab_wait > 0 then crab_wait = crab_wait - 1 else crab_step = crab_step + 0.30 * m if crab_step >= 1 then crab_step = crab_step - 1 crab_x, crab_leg = crab_x + crab_dir, crab_leg + 1 if crab_x < 2 then crab_x, crab_dir = 2, 1 elseif crab_x > W - 10 then crab_x, crab_dir = W - 10, -1 end if umod(rnd(), 40) == 0 then crab_wait = FPS + umod(rnd(), FPS * 4) if (rnd() & 1) == 1 then crab_dir = -crab_dir end end end end snail_x = snail_x + 0.014 * m if snail_x > W + 6 then snail_x = -8 end if frame >= shell_next then shell_open = not shell_open shell_next = frame + (shell_open and (FPS * 2 + umod(rnd(), FPS * 2)) or (FPS * 5 + umod(rnd(), FPS * 10))) if shell_open and cfg.bubbles then spawn(SHELL_X + 2, sy(SHELL_X + 2) - 2, true) end end end function update() if not seeded or frame < last_frame then seed(); seeded = true end last_frame = frame local n = fish_n() local s = auto_level(LVL[cfg.speed] or 1, 3) local m = (s < 1 and 0.6 + 0.4 * s or 0.4 + 0.6 * s) * spook_step(n) m = m * (0.6 + 0.4 * dk) local big = cfg.size ~= "small" for i = 1, n do step_fish(fish[i], m, big) end if cfg.bubbles then step_bubbles() end if cfg.bottom then step_bottom(m) end for i = 1, #CA do local c = CA[i] c.x = (c.x + c.v) % (W + 8) c.ph = (c.ph + c.sp) % TAU end end local RAYM, RAY_W = nil, 24 local RAY_X, RAY_F = { 4, 24, 43 }, { 0.021, 0.016, 0.026 } local function draw_light(f) if dk <= 0 then return end if not RAYM then local rows, row = {}, {} for y = 0, SAND_Y - 1 do local c, a = 2.5 + y * 0.36, 255 * (1 - y / SAND_Y) for x = 1, RAY_W do local v = a * (1 - abs(x - 1 - c) / 2.4) row[x] = v >= 100 and floor(v) or 0 end rows[y + 1] = schar(unpack(row, 1, RAY_W)) end RAYM = concat(rows) end for i = 1, 3 do local s = K.ray * dk * (0.23 + 0.10 * sin(f * RAY_F[i] + i * 2.1)) if s > 0.11 then gfx.blend_mask(RAYM, 210, 244, 255, RAY_X[i], 0, RAY_W, SAND_Y, RAY_W, SAND_Y, s) end end for i = 1, #CA do local c = CA[i] local a = 0.55 * K.ray * dk * sin(c.ph) if a > 0.15 then local x = c.x - 4 local ix = floor(x) local fx = x - ix if fx < 0.7 then blend(ix, sy(ix), 255, 250, 220, a * (1 - fx)) end blend(ix + 1, sy(ix + 1), 255, 250, 220, a) blend(ix + 2, sy(ix + 2), 255, 250, 220, a) if fx > 0.3 then blend(ix + 3, sy(ix + 3), 255, 250, 220, a * fx) end blend(ix + 1, sy(ix + 1) + 1, 255, 250, 220, a * 0.5) end end end local function aa(x, y, r, g, b, a) local ix = floor(x) local f = x - ix if a then if a * (1 - f) > 0.1 then blend(ix, y, r, g, b, a * (1 - f)) end if a * f > 0.1 then blend(ix + 1, y, r, g, b, a * f) end elseif f < 0.1 then px(ix, y, r, g, b) elseif f > 0.9 then px(ix + 1, y, r, g, b) else blend(ix, y, r, g, b, 1 - f); blend(ix + 1, y, r, g, b, f) end end local function strand(bx, by, hh, amp, ph, wide, leaf, lean, st) local j0 = floor(hh * 0.35) if st then amp = 0 end for j = st and 0 or j0, (st and j0 or hh) - 1 do local t = j / hh local x = bx + lean * t + amp * t * t * sin(ph + t * 2.2) local ix = floor(x) local f = x - ix local ci = floor(t * 15.99) local r, g, b = PR[ci], PG[ci], PB[ci] local y = by - j if wide and t < 0.5 then px(ix + 1, y, r, g, b) if f < 0.9 then blend(ix, y, r, g, b, 1 - f) end if f > 0.1 then blend(ix + 2, y, r, g, b, f) end elseif f < 0.1 then px(ix, y, r, g, b) elseif f > 0.9 then px(ix + 1, y, r, g, b) else blend(ix, y, r, g, b, 1 - f); blend(ix + 1, y, r, g, b, f) end if leaf and j > 3 and j % 6 == 0 then local d = (j % 12 == 0) and 1 or -1 local lx = x + (d > 0 and 1.5 or -0.5) aa(lx, y, PR[15], PG[15], PB[15]) aa(lx + d * 1.2, y - 1, PR[15], PG[15], PB[15]) end end end local PLANTS = { { 3, 28, 0.2 }, { 10, 34, -0.5 }, { 19, 22, 0.4 }, { 58, 26, 0.3 }, } local GRASS = { 5, 17, 57 } local function draw_plants(f, st) local sh = K.shape if sh == 1 then for i = 1, #PLANTS do local p = PLANTS[i] strand(p[1], sy(p[1] + 1), p[2], 3.0, (f * 0.045 + i * 1.9) % TAU, true, true, p[3] * 4, st) end elseif sh == 3 then for i = 1, #GRASS do local x = GRASS[i] for k = -1, 1 do strand(x + k * 2, sy(x), 12 + ((i + k) % 3) * 3, 2.2, (f * 1.5 / FPS + i * 1.3 + k * 0.6) % TAU, false, false, k * 2.5, st) end end else for a = 0, 1 do local ax = a == 0 and 13 or 55 local ay = sy(ax + 2) - 1 if st then rect(ax - 1, ay, 6, 2, PR[4], PG[4], PB[4]) end for k = 0, st and -1 or 4 do local dx = (k - 2) * 0.8 local ph = (f * 0.06 + k * 0.9 + a * 2) % TAU for j = 1, 6 do local t = j / 6 local x = ax + 0.5 + k + dx * j * 0.4 + 1.6 * t * t * sin(ph) local ix = floor(x) local ci = 4 + j * 2 - (j == 6 and 1 or 0) px(ix, ay - j, PR[ci], PG[ci], PB[ci]) if x - ix > 0.1 then blend(ix + 1, ay - j, PR[ci], PG[ci], PB[ci], x - ix) end end end end end end local function wag_of(f) local s = sin(f.ph) return s > 0.5 and 2 or (s < -0.5 and 3 or 1) end local function draw_big(f) local B = BAKED[K.big[f.sp]] if not B then if baked_now then return end bake(K.big[f.sp]); sprite_pals(); baked_now = true B = BAKED[K.big[f.sp]] end local w, h, pal = B.w, B.h, B.pal local iy = floor(f.y - h * 0.5) if f.turn > 0 then local k = f.turn / TURN_F local d = k > 0.5 and -f.dir or f.dir local ww = floor(w * (0.22 + 0.78 * abs(k * 2 - 1)) + 0.5) blit(frame_of(B, d > 0 and 1 or 2, 1).s, pal, floor(f.x - ww * 0.5 + 0.5), iy, ww, h, w, h, true) return end local F = frame_of(B, f.dir > 0 and 1 or 2, wag_of(f)) local x = f.x - w * 0.5 local ix = floor(x) local fx = x - ix blit(F.s, pal, ix, iy, w, h, w, h, true) local e = F.e if fx > 0.1 then for r = 1, h do local L, R, c = byte(e, r * 3 - 2, r * 3) if L < 255 then local y = iy + r - 1 c = c * 3 blend(ix + L, y, WR[y] or 0, WG[y] or 0, WB[y] or 0, fx) blend(ix + R + 1, y, pal[c + 1], pal[c + 2], pal[c + 3], fx) end end end local fy = (f.y - h * 0.5) - iy if fy > 0.1 and abs(f.ty - f.y) > 0.05 then for c = 1, w do local o = (h + c) * 3 local T, Bo, k = byte(e, o - 2, o) if T < 255 then local y = iy + T k = k * 3 blend(ix + c - 1, y, WR[y] or 0, WG[y] or 0, WB[y] or 0, fy) blend(ix + c - 1, iy + Bo + 1, pal[k + 1], pal[k + 2], pal[k + 3], fy) end end end end local function draw_small(f) local c = SC[f.layer * 4 + f.col] local r, g, b, tr, tg, tb = c[1], c[2], c[3], c[4], c[5], c[6] local d = f.dir local iy = floor(f.y + 0.5) if f.turn > 0 then if f.turn > TURN_F // 2 then d = -d end if abs(f.turn - TURN_F // 2) < 3 then local ix = floor(f.x + 0.5) rect(ix - 1, iy - 1, 2, 3, r, g, b) return end end local ix = floor(f.x) local fx = (f.turn > 0) and 0 or (f.x - ix) local s = sin(f.ph) local w = s > 0.5 and 1 or (s < -0.5 and -1 or 0) rect(ix - 1, iy - 1, 3, 3, r, g, b) px(ix + d * 2, iy, r, g, b) px(ix - d * 2, iy, tr, tg, tb) px(ix - d * 3, iy - 1 + w, tr, tg, tb) px(ix - d * 3, iy + 1 + w, tr, tg, tb) if f.layer == 2 then px(ix, iy - 2, tr, tg, tb) px(ix + d, iy, mixc(r, g, b, 255, 255, 255, 0.35)) end px(ix + d, iy - 1, 10, 14, 24) if fx > 0.1 then local wr, wg, wb = WR[iy], WG[iy], WB[iy] if d > 0 then blend(ix + 3, iy, r, g, b, fx) blend(ix + 2, iy - 1, r, g, b, fx); blend(ix + 2, iy + 1, r, g, b, fx) blend(ix - 3, iy - 1 + w, wr, wg, wb, fx); blend(ix - 3, iy + 1 + w, wr, wg, wb, fx) else blend(ix - 2, iy, wr, wg, wb, fx) blend(ix - 1, iy - 1, wr, wg, wb, fx); blend(ix - 1, iy + 1, wr, wg, wb, fx) blend(ix + 4, iy - 1 + w, tr, tg, tb, fx); blend(ix + 4, iy + 1 + w, tr, tg, tb, fx) end end end local function draw_bottom() local cx = crab_x local cy = sy(cx + 3) - 3 local snap = crab_wait > 0 and (frame * 15 // (4 * FPS)) % 2 == 0 local cr, cg, cb = C(240, 92, 56) local dr, dg, db = C(156, 46, 30) px(cx - 1, cy - (snap and 2 or 1), cr, cg, cb); px(cx + 7, cy - (snap and 1 or 2), cr, cg, cb) px(cx, cy, dr, dg, db); px(cx + 6, cy, dr, dg, db) rect(cx + 1, cy, 5, 2, cr, cg, cb) px(cx + 2, cy - 1, 15, 15, 20); px(cx + 4, cy - 1, 15, 15, 20) for i = 0, 2 do px(cx + 1 + i * 2, cy + 2 + ((i + crab_leg) & 1), dr, dg, db) end local sx0 = snail_x local sx = floor(sx0) local fx = sx0 - sx local y = sy(sx + 2) - 2 local hr, hg, hb = C(200, 146, 80) local lr, lg, lb = C(120, 80, 42) local fr, fg, fb = C(232, 214, 176) rect(sx, y + 2, 5, 1, fr, fg, fb) px(sx + 1, y + 1, lr, lg, lb); px(sx + 2, y + 1, hr, hg, hb); px(sx + 3, y + 1, lr, lg, lb) px(sx + 2, y, hr, hg, hb) px(sx + 5, y + 1, fr, fg, fb); px(sx + 5, y, fr, fg, fb) if fx > 0.1 then blend(sx + 5, y + 2, fr, fg, fb, fx); blend(sx + 6, y + 1, fr, fg, fb, fx) blend(sx, y + 2, WR[y + 2] or 0, WG[y + 2] or 0, WB[y + 2] or 0, fx) end local mx, my = SHELL_X, sy(SHELL_X + 2) local ar, ag, ab = C(210, 176, 222) local br, bg, bb = C(132, 96, 156) if shell_open then px(mx, my - 1, ar, ag, ab); px(mx + 1, my - 2, ar, ag, ab) px(mx + 2, my - 2, ar, ag, ab); px(mx + 3, my - 1, ar, ag, ab) rect(mx, my, 4, 1, br, bg, bb) px(mx + 1, my - 1, C(255, 246, 250)) else rect(mx, my - 1, 4, 1, ar, ag, ab) rect(mx, my, 4, 1, br, bg, bb) end end local function draw_flag(f) local d = DECOR.castle if cfg.decor ~= "castle" or not d.y0 then return end local x, y = d.x + 1, d.y0 - 4 local r, g, b = C(150, 140, 150) rect(x, y, 1, 4, r, g, b) local fr, fg, fb = C(240, 60, 70) local up = (f * 5 // (2 * FPS)) % 2 rect(x + 1, y, 2, 2, fr, fg, fb) px(x + 3, y + up, fr, fg, fb) end local function draw_bubbles() for i = 1, NB do if BON[i] then local y = BYY[i] if BL[i] and y < SAND_Y - 8 then local x = floor(BX[i] + 1.0 * sin(BW[i]) + 0.5) local iy = floor(y + 0.5) blend(x, iy - 1, 210, 240, 255, 0.85); blend(x, iy + 1, 210, 240, 255, 0.85) blend(x - 1, iy, 210, 240, 255, 0.85); blend(x + 1, iy, 210, 240, 255, 0.85) blend(x + 1, iy - 1, 250, 252, 255, 0.5) else local x = BX[i] + 0.7 * sin(BW[i]) local iy = floor(y) local fy = y - iy local a, b = 0.9 * (1 - fy), 0.9 * fy aa(x, iy, 225, 246, 255, a) aa(x, iy + 1, 225, 246, 255, b) end end end end function on_settings() K = KINDS[cfg.kind] or KINDS.freshwater scene_key, SCENE = nil, nil if not cfg.bubbles then for i = 1, NB do BON[i] = false end end end function render() if not seeded then seed(); seeded = true end local step = floor(daylight() * 8 + 0.5) if step ~= scene_key or not SCENE then SCENE = nil day_tables(step / 8) draw_static() if cfg.plants then draw_plants(0, true) end SCENE = gfx.snapshot(0, 0, W, H) sprite_pals() scene_key = step else gfx.blit_rgb(SCENE, 0, 0, W, H) end baked_now = false draw_light(frame) draw_flag(frame) local n = fish_n() local small = cfg.size == "small" if small then for i = 1, n do if fish[i].layer == 0 then draw_small(fish[i]) end end end if cfg.bottom then draw_bottom() end if cfg.plants then draw_plants(frame) end if small then for l = 1, 2 do for i = 1, n do if fish[i].layer == l then draw_small(fish[i]) end end end else for i = 1, n do draw_big(fish[i]) end end if cfg.bubbles then draw_bubbles() end end