info = { type = "image", title = "Sakura fox", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A fox wags its tail under falling blossoms", api = 44, } local tr = ({ sk = { ["Petals"] = "Lupene", ["Wind"] = "Vietor" }, cs = { ["Petals"] = "Okvětní lístky", ["Wind"] = "Vítr" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "petals", label = T("Petals"), type = "int", min = 1, max = 6, default = 3 }, { key = "wind", label = T("Wind"), type = "int", min = 0, max = 4, default = 2 }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local function c3(c) return c[1], c[2], c[3] end local SKY_T, SKY_B = { 150, 196, 232 }, { 240, 214, 226 } local HILL, HILL_L = { 184, 166, 206 }, { 212, 196, 228 } local GRASS, GRASS_L = { 100, 160, 72 }, { 140, 196, 96 } local GRASS_D = { 80, 138, 60 } local BLADE, BLADE_L = { 70, 132, 54 }, { 150, 206, 100 } local TRUNK, TRUNK_L = { 96, 58, 54 }, { 136, 88, 76 } local TRUNK_D = { 70, 42, 44 } local BLOS, BLOS_L = { 242, 168, 194 }, { 255, 214, 228 } local BLOS_D = { 212, 122, 158 } local PET, PET_L = { 248, 140, 176 }, { 255, 200, 218 } local OUT, FOX = { 72, 36, 28 }, { 226, 112, 44 } local FOX_D, WHITE = { 180, 80, 36 }, { 248, 242, 232 } local BLACK = { 48, 32, 32 } local HL, GL = {}, {} for x = 0, W - 1 do HL[x] = floor(36 + 2.5 * sin(x * 0.07 + 1) + sin(x * 0.19) + 0.5) GL[x] = floor(46 + 1.5 * sin(x * 0.09 + 0.3) + 0.5) end local BG, STAGE = nil, 0 local function art(x0, y0, rows, pal) for r = 1, #rows do local s = rows[r] local n, i = #s, 1 while i <= n do local ch = s:byte(i) local j = i while j < n and s:byte(j + 1) == ch do j = j + 1 end local c = pal[ch] if c then rect(x0 + i - 1, y0 + r - 1, j - i + 1, 1, c[1], c[2], c[3]) end i = j + 1 end end end local FOXA = { "............ooooo.....", "...........oOOOOOo....", "..........oOOOOOOOo...", "..........oOOOOKOOOo..", "..........oOOOOOOOOOoo", "..........oDOOOOOOOOOK", "..........oDOWWWWWWWoo", "...........oWWWWWWoo..", "...........oWWWWWo....", "..........oOWWWWWo....", ".........oOOWWWWWo....", "........oOOOOWWWWo....", ".......oOOOOOWWWWo....", "......oOOOOOOWWWWo....", ".....oDOOOOOOOWWWo....", "....oDOOOOOOOOWWWo....", "....oDOOOOOOOOOWWo....", "...oDDOOOOOOOOOKKo....", "...oDDOOOOOOOOOKKo....", "...oDDOOOOOOOOoKKo....", "...oDDDOOOOOOooKKo....", "...oDDDOOOOOOoKKKKo...", "....oDDDDDDDooKKKKo...", ".....ooooooo..oooo....", } local FOX_PAL = { [("o"):byte()] = OUT, [("O"):byte()] = FOX, [("D"):byte()] = FOX_D, [("W"):byte()] = WHITE, [("K"):byte()] = BLACK } local FX0, FY0 = 14, 26 local CROWN = { { 36, 6, 7 }, { 46, 3, 8 }, { 57, 5, 8 }, { 62, 15, 6 }, { 50, 14, 7 }, { 40, 15, 5 }, { 28, 2, 5 } } local function in_crown(x, y) for i = 1, #CROWN do local c = CROWN[i] local dx, dy = x - c[1], y - c[2] if dx * dx + dy * dy <= c[3] * c[3] then return c end end return nil end local function disc(cx, cy, r, col) for dy = -r, r do local w = floor(sqrt(r * r - dy * dy) + 0.5) rect(cx - w, cy + dy, 2 * w + 1, 1, col[1], col[2], col[3]) end end local function build_bg() for y = 0, 44 do local t = y / 44 rect(0, y, W, 1, floor(SKY_T[1] + (SKY_B[1] - SKY_T[1]) * t + 0.5), floor(SKY_T[2] + (SKY_B[2] - SKY_T[2]) * t + 0.5), floor(SKY_T[3] + (SKY_B[3] - SKY_T[3]) * t + 0.5)) end for x = 0, W - 1 do rect(x, HL[x], 1, H - HL[x], c3(HILL)); px(x, HL[x], c3(HILL_L)) end for x = 0, W - 1 do rect(x, GL[x], 1, H - GL[x], c3(GRASS)); px(x, GL[x], c3(GRASS_L)) rect(x, 56 + (x * 7 % 3 == 0 and 1 or 0), 1, H - 56, c3(GRASS_D)) end for i = 1, 30 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), 48 + umod(h >> 8, 16) px(x, y, c3((y < 56) and GRASS_D or GRASS)) end rect(47, 14, 5, 34, c3(TRUNK)) rect(47, 14, 1, 34, c3(TRUNK_L)); rect(51, 14, 1, 34, c3(TRUNK_D)) rect(46, 45, 7, 2, c3(TRUNK)); rect(45, 47, 9, 1, c3(TRUNK)) rect(49, 24, 1, 2, c3(TRUNK_D)); rect(48, 33, 1, 2, c3(TRUNK_D)); rect(50, 40, 1, 2, c3(TRUNK_D)) for k = 0, 10 do rect(47 - k, 22 - k, 2, 1, c3(TRUNK)) end for k = 0, 7 do rect(51 + k, 19 - k, 2, 1, c3(TRUNK)) end BG = gfx.snapshot(0, 0, W, H) end local function build_bg2() for i = 1, #CROWN do local c = CROWN[i]; disc(c[1] + 1, c[2] + 2, c[3], BLOS_D) end for i = 1, #CROWN do local c = CROWN[i]; disc(c[1], c[2], c[3], BLOS) end for i = 1, 90 do local h = hash32(i * 0x85EBCA6B) local x, y = 20 + umod(h, 44), umod(h >> 8, 22) local c = in_crown(x, y) if c then if y < c[2] then px(x, y, c3(BLOS_L)); px(x + 1, y, c3(BLOS_L)) else px(x, y, c3(BLOS_D)) end end end px(41, 20, c3(TRUNK)); px(42, 21, c3(TRUNK)); px(58, 20, c3(TRUNK)); px(59, 21, c3(TRUNK)) art(FX0, FY0, FOXA, FOX_PAL) BG = gfx.snapshot(0, 0, W, H) end local EAR_F = { { { -4, 14, 14 }, { -3, 14, 15 }, { -2, 14, 16 }, { -1, 13, 17 } }, { { -5, 16, 16 }, { -4, 15, 16 }, { -3, 15, 17 }, { -2, 14, 17 }, { -1, 13, 17 } } } local EAR_FI = { { { -2, 15, 15 }, { -1, 15, 16 } }, { { -3, 16, 16 }, { -2, 15, 16 }, { -1, 15, 16 } } } local EAR_B = { { { -3, 10, 10 }, { -2, 10, 11 }, { -1, 10, 12 } }, { { -4, 11, 11 }, { -3, 10, 11 }, { -2, 10, 12 }, { -1, 10, 12 } } } local function ear(rows, col) for i = 1, #rows do local s = rows[i] rect(FX0 + s[2] - 1, FY0 + s[1] - 1, s[3] - s[2] + 3, 2, c3(OUT)) end for i = 1, #rows do local s = rows[i] rect(FX0 + s[2], FY0 + s[1], s[3] - s[2] + 1, 1, c3(col)) end end local function ears(t) local p = t % 240 local perk = p >= 40 and p < 130 local fp = (perk and not (p >= 90 and p < 95)) and 2 or 1 ear(EAR_B[perk and 2 or 1], FOX_D) ear(EAR_F[fp], FOX) rect(FX0 + 12, FY0, 5, 1, c3(FOX)) local inn = EAR_FI[fp] for i = 1, #inn do local s = inn[i] rect(FX0 + s[2], FY0 + s[1], s[3] - s[2] + 1, 1, c3(WHITE)) end end local TSZ = { 4, 5, 5, 6, 6, 6, 6, 5, 5, 4, 4, 3 } local TX, TY = {}, {} local function wag(u) if u < 0 or u >= 54 then return 0 end local env = u / 6 if env > 1 then env = 1 end local e2 = (54 - u) / 14 if e2 < env then env = e2 end return 0.5 * env * sin(u * TAU / 16) end local function tail(t) local u = (t - 175) % 160 local x, y = FX0 + 4.0, FY0 + 20.0 local a = 3.14159 + 0.25 for s = 1, #TSZ do a = a + 0.12 local d = wag(u - 2 * (s - 1)) * (1 + 0.08 * s) x = x + 1.5 * cos(a + d) y = y + 1.5 * sin(a + d) TX[s], TY[s] = x, y end for s = 1, #TSZ do local z = TSZ[s] local l, tp = floor(TX[s] - z / 2 + 0.5), floor(TY[s] - z / 2 + 0.5) rect(l - 1, tp - 1, z + 2, z + 2, c3(OUT)) end for s = 1, #TSZ do local z = TSZ[s] local l, tp = floor(TX[s] - z / 2 + 0.5), floor(TY[s] - z / 2 + 0.5) rect(l, tp, z, z, c3(s > #TSZ - 3 and WHITE or FOX)) if s <= #TSZ - 3 and z >= 4 then rect(l, tp + z - 1, z, 1, c3(FOX_D)) end end end local NP = 48 local PX0, PY0, PV, PA, PW, PD, POFF = {}, {}, {}, {}, {}, {}, {} for i = 1, NP do local h = hash32(i * 0x9E3779B1) PX0[i] = 24 + umod(h, 46) PY0[i] = 4 + umod(h >> 6, 16) PV[i] = (18 + umod(h >> 11, 15)) / 100 PA[i] = (15 + umod(h >> 16, 20)) / 10 PW[i] = (4 + umod(h >> 21, 5)) / 100 PD[i] = 50 - PY0[i] + umod(h >> 25, 10) POFF[i] = umod(h >> 3, 997) end local NB = 34 local BXs, BYs, BHs = {}, {}, {} for i = 1, NB do local h = hash32(i * 0xC2B2AE35) BXs[i] = umod(h, W) BYs[i] = 52 + umod(h >> 8, 12) BHs[i] = 3 + umod(h >> 16, 3) end local function petals(t, n, wind) local vx = -(0.04 + 0.05 * wind) for i = 1, n do local p = (t * PV[i] + POFF[i]) % PD[i] local steps = p / PV[i] local y = PY0[i] + p local x = (PX0[i] + vx * steps + PA[i] * sin(steps * PW[i] + i)) % 70 - 3 local a = 1 if PD[i] - p < 3 then a = (PD[i] - p) / 3 end local ix, iy = floor(x + 0.5), floor(y) local f = y - iy local c = (i % 3 == 0) and PET_L or PET blend(ix, iy, c[1], c[2], c[3], a * (1 - f)) blend(ix, iy + 1, c[1], c[2], c[3], a * f) if (t // 7 + i) % 3 == 0 then blend(ix + 1, iy, c[1], c[2], c[3], a * 0.6) end end end local function grass(t, wind) local amp = 0.4 + 0.45 * wind for i = 1, NB do local x, b, h = BXs[i], BYs[i], BHs[i] local c = (i % 3 == 0) and BLADE_L or BLADE rect(x, b - h + 3, 1, h - 2, c3(c)) local tx = x + amp * sin(x * 0.22 - t * 0.08) local ix = floor(tx) local f = tx - ix blend_rect(ix, b - h + 1, 1, 2, c[1], c[2], c[3], 1 - f) blend_rect(ix + 1, b - h + 1, 1, 2, c[1], c[2], c[3], f) end end function render() if STAGE == 0 then build_bg(); STAGE = 1; gfx.ready(false) elseif STAGE == 1 then gfx.blit_rgb(BG, 0, 0, W, H); build_bg2(); STAGE = 2 end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS local wind = cfg.wind or 2 ears(t) tail(t) grass(t, wind) petals(t, (cfg.petals or 3) * 8, wind) end