info = { type = "image", title = "Swamp wader", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A friendly monster wades through a misty bog", api = 44, } local tr = ({ sk = { ["Monster"] = "Príšera", ["Moss"] = "Machová", ["Teal"] = "Tyrkysová", ["Ochre"] = "Okrová", ["Lilac"] = "Orgovánová", ["Fireflies"] = "Svetlušky" }, cs = { ["Monster"] = "Příšera", ["Moss"] = "Mechová", ["Teal"] = "Tyrkysová", ["Ochre"] = "Okrová", ["Lilac"] = "Šeříková", ["Fireflies"] = "Světlušky" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "mon", label = T("Monster"), type = "int", default = "moss", opts = { keys = { "moss", "teal", "ochre", "lilac" }, T("Moss"), T("Teal"), T("Ochre"), T("Lilac") } }, { key = "flies", label = T("Fireflies"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, snapshot, blend_mask = gfx.blit_rgb, gfx.snapshot, gfx.blend_mask local floor, sin, sqrt, min, max = math.floor, math.sin, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HZ, WL = 34, 52 local TP, PC, STEP, DC = 13.1, 17.2, 0.55, 24 local SKY_T, SKY_H = { 30, 54, 84 }, { 120, 158, 148 } local WAT_N, WAT_F = { 26, 58, 62 }, { 96, 140, 136 } local FARC, TRC, MIST = { 74, 110, 110 }, { 52, 90, 92 }, { 196, 222, 210 } local REED, REED_L, CATT = { 18, 44, 40 }, { 44, 88, 64 }, { 96, 60, 40 } local MONS = { moss = { 86, 142, 84 }, teal = { 52, 152, 152 }, ochre = { 178, 150, 72 }, lilac = { 142, 112, 172 } } local ORDER = { "moss", "teal", "ochre", "lilac" } local EYEW, DETAIL = { 240, 238, 176 }, { 26, 38, 42 } local PAL = {} local function T3(c) return c[1], c[2], c[3] end local function BR(x, y, w, h, c, a) blend_rect(x, y, w, h, c[1], c[2], c[3], a) end local function mix(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 sh(c, k) return { floor(c[1] * k), floor(c[2] * k), floor(c[3] * k) } end function on_settings() local i0 = 1 for i = 1, 4 do if ORDER[i] == cfg.mon then i0 = i end end for m = 1, 2 do local b = MONS[ORDER[(i0 + m - 2) % 4 + 1]] PAL[m] = { b, sh(b, 0.6), { mix(b, { 232, 240, 170 }, 0.5) }, EYEW, DETAIL } end end local function hs(x0, x1, y, h, c, a) if x1 <= x0 or x1 < 0 or x0 > W then return end a = a or 1 local r, g, b = c[1], c[2], c[3] local i0, i1 = floor(x0), floor(x1) if i0 == i1 then blend_rect(i0, y, 1, h, r, g, b, (x1 - x0) * a); return end if i1 > i0 + 1 then if a >= 1 then rect(i0 + 1, y, i1 - i0 - 1, h, r, g, b) else blend_rect(i0 + 1, y, i1 - i0 - 1, h, r, g, b, a) end end blend_rect(i0, y, 1, h, r, g, b, (i0 + 1 - x0) * a) if x1 > i1 then blend_rect(i1, y, 1, h, r, g, b, (x1 - i1) * a) end end local function vline(x, y, h, c, a) local ix = floor(x) local f = x - ix blend_rect(ix, y, 1, h, c[1], c[2], c[3], a * (1 - f)) blend_rect(ix + 1, y, 1, h, c[1], c[2], c[3], a * f) end local TOP, WATER, WROW, STAGE = nil, nil, {}, 1 local function st_sky() for y = 0, HZ do rect(0, y, W, 1, mix(SKY_T, SKY_H, (y / HZ) ^ 1.3)) end for y = 24, HZ - 1 do blend_rect(0, y, W, 1, 186, 232, 206, 0.05 + 0.16 * (y - 24) / 10) end for i = 0, 9 do local h = hash32(i * 0x9E3779B1) local x = i * 7 + umod(h, 5) local ht = 16 + umod(h >> 4, 8) rect(x, HZ - ht, 1 + umod(h >> 9, 2), ht + 1, T3(FARC)) rect(x - 3, HZ - ht, 8, 2, T3(FARC)) rect(x - 2, HZ - ht - 2, 6, 2, T3(FARC)) rect(x - 1, HZ - ht - 3, 4, 1, T3(FARC)) end blend_rect(0, HZ - 12, W, 12, 190, 226, 206, 0.3) rect(0, HZ - 1, W, 2, 52, 92, 88) TOP = snapshot(0, 0, W, HZ + 1) end local function st_water(ya, yb) local rows = {} for y = 0, HZ do rows[y] = snapshot(0, y, W, 1) end for y = ya, yb do local d = (y - HZ) / (H - 1 - HZ) gfx.blit_rgb(rows[max(0, 2 * HZ - y)], 0, y, W, 1) local wr, wg, wb = mix(WAT_F, WAT_N, d ^ 0.8) blend_rect(0, y, W, 1, wr, wg, wb, 0.45 + 0.4 * d) WROW[y] = snapshot(0, y, W, 1) end end local GW, GHT = 22, 24 local G, MASK = {}, { {}, {} } local function make_grid(p) local g = {} for y = 1, GHT do g[y] = {} end local function ell(cx, cy, rx, ry, idx) for y = max(0, floor(cy - ry)), min(GHT - 1, floor(cy + ry)) do local dy = (y + 0.5 - cy) / ry if dy * dy < 1 then local w = rx * sqrt(1 - dy * dy) for x = floor(cx - w + 0.5), floor(cx + w - 0.5) do g[y + 1][x + 1] = idx end end end end local function cell(x, y, idx) g[y + 1][x + 1] = idx end ell(9, 15, 8, 9.5, 2) ell(10.6, 14.6, 7, 8.8, 1) ell(14, 7, 6, 6.2, 1) ell(11, 18, 5, 5.5, 3) ell(11, 0.8, 1.3, 1.8, 3); ell(16, 0.8, 1.3, 1.8, 3) ell(15.6, 5.5, 2.5, 2.6, 4); ell(16.6, 5.5, 1, 1, 5) ell(11.3, 6, 1.7, 1.9, 4); ell(11.8, 6, 0.8, 0.9, 5) for x = 14, 19 do cell(x, 10, 5) end cell(13, 9, 5); cell(20, 9, 5) ell(13 + (p == 1 and 2 or -1.5), 16, 2.4, 4.2, 2) ell(6, p == 2 and 20.5 or 22, 2.6, 2.4, 2) ell(14, p == 1 and 20.5 or 22, 2.6, 2.4, 2) G[p] = g end local function make_masks(p, q) if not G[p] then make_grid(p) end local g, f, m = G[p], q / 4, {} local lut = { 0, floor(255 * f + 0.5), floor(255 * (1 - f) + 0.5), 255 } local empty = string.rep("\0", GW + 1) for c = 1, 5 do local rows, row = {}, {} for y = 1, GHT do local gy = g[y] local any = false for x = 1, GW do if gy[x] == c then any = true; break end end if any then local prev = 0 for x = 1, GW + 1 do local cur = gy[x] == c and 2 or 0 row[x] = lut[cur + prev + 1] prev = cur == 2 and 1 or 0 end rows[y] = string.char(table.unpack(row, 1, GW + 1)) else rows[y] = empty end end m[c] = table.concat(rows) end MASK[p][q] = m end local STAGES = { st_sky, function() st_water(HZ + 1, 48) end, function() st_water(49, H - 1) for y = HZ + 1, 48 do blit_rgb(WROW[y], 0, y, W, 1) end WATER = snapshot(0, HZ + 1, W, H - HZ - 1) end } for p = 1, 2 do for q = 0, 3 do STAGES[#STAGES + 1] = function() make_masks(p, q); if q == 3 then G[p] = nil end end end end local function smooth(x) x = min(1, max(0, x)); return x * x * (3 - 2 * x) end local function pass(t) local n = floor(t / PC) local tau = t - n * PC return n, tau, -12 + (W + 24) * tau / TP, (n + smooth(tau / TP)) * DC end local MID = {} for k = 0, 4 do local h = hash32(k * 0x85EBCA6B) MID[k] = { dx = umod(h, 8), h = 14 + umod(h >> 4, 8) } end local FLY = {} for i = 1, 9 do local h = hash32(i * 0xC2B2AE35) FLY[i] = { x = umod(h, W), y = 22 + umod(h >> 8, 34), ph = umod(h >> 16, 628) / 100, w = 0.35 + umod(h >> 20, 8) / 20 } end local RINGC = { 200, 232, 222 } local function ring(x, y, r, a) local ry = max(1, floor(r * 0.22 + 0.5)) local c = RINGC hs(x - r * 0.55, x + r * 0.55, y - ry, 1, c, a) hs(x - r * 0.55, x + r * 0.55, y + ry, 1, c, a) hs(x - r - 0.5, x - r + 0.5, y, 1, c, a) hs(x + r - 0.5, x + r + 0.5, y, 1, c, a) end function render() if STAGE <= #STAGES then local s = STAGE if s >= 2 then blit_rgb(TOP, 0, 0, W, HZ + 1) end STAGES[s]() STAGE = s + 1 if s <= 3 then gfx.ready(false) return end end if not PAL[1] then on_settings() end local t = frame / FPS local n, tau, X, cam = pass(t) blit_rgb(TOP, 0, 0, W, HZ + 1) local c35 = cam * 0.35 for k = floor((c35 - 12) / 26), floor((c35 + W) / 26) + 1 do local m = MID[k % 5] local x = k * 26 + m.dx - c35 local h = m.h hs(x - 1, x + 1, HZ + 2 - h, h, TRC) hs(x - 2, x + 2, HZ - h - 2, 1, TRC) hs(x - 4, x + 4, HZ - h - 1, 2, TRC) hs(x - 3, x + 3, HZ - h + 1, 1, TRC) end for y = 29, 36 do BR(0, y, W, 1, MIST, 0.06 + 0.04 * (y - 29)) end blit_rgb(WATER, 0, HZ + 1, W, H - HZ - 1) for y = HZ + 1, H - 1 do local d = (y - HZ) / (H - 1 - HZ) local dx = floor((0.3 + 1.3 * d) * (sin(y * 0.95 - t * 2.1) + 0.6 * sin(y * 1.9 + t * 1.5 + 1.3)) + 0.5) if dx ~= 0 then blit_rgb(WROW[y], dx, y, W, 1) end end for k = floor((c35 - 12) / 26), floor((c35 + W) / 26) + 1 do local m = MID[k % 5] BR(floor(k * 26 + m.dx - c35) - 1, HZ + 2, 2, 10, TRC, 0.3) end local j1 = floor(t / STEP) for j = max(0, j1 - 6), j1 do local age = t - j * STEP local jn, jt, jx, jc = pass(j * STEP) if age < 3.3 and jt < TP then ring(jx - (cam - jc) + (j % 2 == 0 and -3 or 3), WL + 1 + j % 2, 2 + 4.2 * age, 0.5 * (1 - age / 3.3)) end end if tau < TP then local pose = floor(tau / STEP) % 2 + 1 local xl = X - 10 local ix = floor(xl) local q = floor((xl - ix) * 4 + 0.5) if q == 4 then ix, q = ix + 1, 0 end local ms = MASK[pose][q] local pal = PAL[n % 2 + 1] local body = pal[1] for d = 0, 10 do local dx = (0.5 + 0.09 * d) * sin(d * 1.1 - t * 2.4) local y = WL + 1 + d hs(xl + 2 + dx, xl + 19 + dx, y, 1, body, 0.34 * (1 - d / 12)) end local by = WL - GHT - (pose == 2 and 1 or 0) if ms then for c = 1, 5 do blend_mask(ms[c], pal[c][1], pal[c][2], pal[c][3], ix, by, GW + 1, GHT, GW + 1, GHT, 1) end end blend_rect(ix + 3, WL - 2, 16, 2, 70, 112, 108, 0.45) hs(xl + 1, xl + 20, WL, 1, MIST, 0.3) end local rc = cam * 3 for k = floor((rc - 10) / 34), floor((rc + W) / 34) + 1 do local h0 = hash32(k * 0x27D4EB2F) for s = 0, 6 do local hh = hash32(h0 + s * 0x9E3779B1) local x = k * 34 + s * 3 + umod(hh, 3) - rc + 1.2 * sin(t * 0.9 + s + k) local hgt = 9 + umod(hh >> 4, 9) vline(x, H - hgt, hgt, s % 2 == 0 and REED or REED_L, 1) if s == 2 or s == 5 then hs(x - 0.5, x + 1.5, H - hgt - 3, 3, CATT) end end end if cfg.flies then for i = 1, 9 do local f = FLY[i] local a = sin(t * f.w * 2.4 + f.ph) if a > 0 then local x = (f.x + 5 * sin(t * 0.4 + f.ph) - cam * 0.5) % (W + 8) - 4 local y = f.y + 3 * sin(t * 0.55 + f.ph * 2) local ix, iy = floor(x + 0.5), floor(y + 0.5) blend_rect(ix, iy, 1, 1, 214, 255, 120, 0.4 + 0.6 * a) blend_rect(ix - 1, iy, 3, 1, 190, 255, 110, 0.18 * a) blend_rect(ix, iy - 1, 1, 3, 190, 255, 110, 0.18 * a) end end end end