info = { type = "image", title = "Blossom bush", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Pink blossoms sway, a bee lands on them", api = 33, } local tr = ({ sk = { ["Blossom colour"] = "Farba kvetov", ["Bee"] = "Včela", ["Breeze"] = "Vánok" }, cs = { ["Blossom colour"] = "Barva květů", ["Bee"] = "Včela", ["Breeze"] = "Vánek" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "bloom", label = T("Blossom colour"), type = "color", default = "#F46FA8" }, { key = "bee", label = T("Bee"), type = "bool", default = true }, { key = "wind", label = T("Breeze"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, 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 = gfx.W, gfx.H local SKY0, SKY1 = { 120, 156, 216 }, { 252, 208, 188 } local SUN, SUN_R = { 255, 244, 206 }, { 255, 224, 176 } local G_DARK, G_MID, G_LIGHT = { 20, 54, 46 }, { 36, 84, 62 }, { 78, 138, 86 } local TRUNK = { 70, 48, 36 } local TOPY = {} for x = 0, W - 1 do TOPY[x] = floor(33 + 3.5 * sin(x * 0.19 + 0.6) + 2.5 * sin(x * 0.43 + 1.1) + 0.5) end local BG = nil local function build_bg() local HZ = 44 for y = 0, HZ - 1 do local t = y / (HZ - 1) rect(0, y, W, 1, floor(SKY0[1] + (SKY1[1] - SKY0[1]) * t + 0.5), floor(SKY0[2] + (SKY1[2] - SKY0[2]) * t + 0.5), floor(SKY0[3] + (SKY1[3] - SKY0[3]) * t + 0.5)) end rect(0, HZ, W, H - HZ, SKY1[1], SKY1[2], SKY1[3]) for dy = -8, 8 do local h = floor(sqrt(8.5 * 8.5 - dy * dy)) rect(51 - h, 14 + dy, 2 * h + 1, 1, SUN_R[1], SUN_R[2], SUN_R[3]) end for dy = -5, 5 do local h = floor(sqrt(5.5 * 5.5 - dy * dy)) rect(51 - h, 14 + dy, 2 * h + 1, 1, SUN[1], SUN[2], SUN[3]) end for x = 0, W - 1 do rect(x, TOPY[x], 1, H - TOPY[x], G_DARK[1], G_DARK[2], G_DARK[3]) end for i = 1, 70 do local h = hash32(i * 0x9E3779B1) local x = umod(h, W - 3) local y = TOPY[x] + 1 + umod(h >> 8, H - TOPY[x] - 2) local c = (h >> 20) % 3 == 0 and G_LIGHT or G_MID if y < 40 and (h >> 24) % 2 == 0 then c = G_LIGHT end rect(x, y, 3, 1, c[1], c[2], c[3]) rect(x + 1, y + 1, 2, 1, c[1], c[2], c[3]) end rect(6, 52, 20, 1, TRUNK[1], TRUNK[2], TRUNK[3]); rect(30, 55, 26, 1, TRUNK[1], TRUNK[2], TRUNK[3]) BG = gfx.snapshot(0, 0, W, H) end local BL = { { x = 12, y = 33, n = 7, ph = 0.0 }, { x = 28, y = 30, n = 7, ph = 1.3 }, { x = 45, y = 36, n = 7, ph = 2.2 }, { x = 21, y = 45, n = 5, ph = 3.0 }, { x = 38, y = 46, n = 5, ph = 4.1 }, { x = 57, y = 33, n = 5, ph = 5.0 }, { x = 6, y = 47, n = 5, ph = 0.8 }, { x = 52, y = 51, n = 7, ph = 2.8 }, { x = 30, y = 56, n = 5, ph = 3.7 }, { x = 62, y = 45, n = 5, ph = 1.9 }, } local SPR = {} local function make_spr(n) local c = (n - 1) / 2 local cells, mask = {}, {} local pr, pd = n == 7 and 1.9 or 1.4, n == 7 and 2.2 or 1.5 for y = 0, n - 1 do for x = 0, n - 1 do local dx, dy = x - c, y - c local v = 0 for k = 0, 4 do local a = 1.5708 + k * 1.25664 local ex, ey = dx - cos(a) * pd, dy - sin(a) * pd if ex * ex + ey * ey <= pr * pr then v = 1 end end if dx * dx + dy * dy <= (n == 7 and 1.2 or 0.7) then v = 3 end if v == 1 and dy < 0 and dx <= 0.5 then v = 2 end cells[#cells + 1] = string.char(v) mask[#mask + 1] = string.char(v > 0 and 255 or 0) end end return { cells = table.concat(cells), mask = table.concat(mask), n = n, c = c } end local PAL, EDGE = { 0, 0, 0, 244, 111, 168, 252, 168, 204, 250, 216, 92 }, { 200, 80, 130 } function on_settings() local c = cfg.bloom or { r = 244, g = 111, b = 168 } local function mix(a, b, t) return floor(a + (b - a) * t) end PAL = { 0, 0, 0, c.r, c.g, c.b, mix(c.r, 255, 0.5), mix(c.g, 255, 0.5), mix(c.b, 255, 0.5), 250, 216, 92 } EDGE = { floor(c.r * 0.78), floor(c.g * 0.78), floor(c.b * 0.78) } end local function put(s, fx, fy) local ix, iy = floor(fx), floor(fy) local ax, ay = fx - ix, fy - iy local n = s.n if ax > 0.05 then gfx.blend_mask(s.mask, EDGE[1], EDGE[2], EDGE[3], ix + 1, iy, n, n, n, n, ax) end if ay > 0.05 then gfx.blend_mask(s.mask, EDGE[1], EDGE[2], EDGE[3], ix, iy + 1, n, n, n, n, ay) end gfx.blit_indexed(s.cells, PAL, ix, iy, n, n, n, n, true) end local function soft(x, y, w, h, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy rect(ix + 1, iy + 1, w - 1, h - 1, r, g, b) blend_rect(ix, iy + 1, 1, h - 1, r, g, b, 1 - fx) blend_rect(ix + w, iy + 1, 1, h - 1, r, g, b, fx) blend_rect(ix + 1, iy, w - 1, 1, r, g, b, 1 - fy) blend_rect(ix + 1, iy + h, w - 1, 1, r, g, b, fy) blend_rect(ix, iy, 1, 1, r, g, b, (1 - fx) * (1 - fy)) blend_rect(ix + w, iy, 1, 1, r, g, b, fx * (1 - fy)) blend_rect(ix, iy + h, 1, 1, r, g, b, (1 - fx) * fy) blend_rect(ix + w, iy + h, 1, 1, r, g, b, fx * fy) end local function sstep(t) if t <= 0 then return 0 elseif t >= 1 then return 1 end return t * t * (3 - 2 * t) end local function bloom_pos(i, t, wind, dip) local b = BL[i] local x = b.x + 0.8 * wind * sin(t * 1.35 + b.ph) local y = b.y + 0.45 * wind * sin(t * 1.89 + b.ph * 1.7) + 0.9 * dip return x, y end local A_ORDER = { 2, 1, 3, 8, 6 } local function bee_pos(p, t, wind, cyc) local A = A_ORDER[cyc % #A_ORDER + 1] local B = A_ORDER[(cyc + 2) % #A_ORDER + 1] local ax, ay = bloom_pos(A, t, wind, 0) local bx, by = bloom_pos(B, t, wind, 0) if p < 2.67 then local u = sstep(p / 2.67) return 72 + (ax + 7 - 72) * u, 6 + (ay - 6) * u + 2 * sin(p * 6), false, 0 elseif p < 5.67 then local u = (p - 2.67) / 3 local r = 7 - 5 * u return ax + r * cos(u * 9), ay - 2 + r * 0.7 * sin(u * 9), false, 0 elseif p < 7.67 then local u = sstep((p - 5.67) / 0.33) * (1 - sstep((p - 7.33) / 0.33)) local px_, py_ = bloom_pos(A, t, wind, u) return px_, py_ - 3.4, true, u elseif p < 10 then local u = sstep((p - 7.67) / 2.33) return ax + (bx - ax) * u, ay - 3 + (by - ay) * u - 6 * sin(u * 3.14159), false, 0 elseif p < 11.67 then local u = sstep((p - 10) / 0.33) * (1 - sstep((p - 11.33) / 0.33)) local px_, py_ = bloom_pos(B, t, wind, u) return px_, py_ - 3.4, true, u elseif p < 14.67 then local u = sstep((p - 11.67) / 3) return bx + (-10 - bx) * u, by - 3 - 24 * u + 2 * sin(p * 6), false, 0 end return nil end local PET = { { 13, 0 }, { 19, 5.67 } } local function petal(slot, t, wind) local per, off = PET[slot][1], PET[slot][2] local q = t + off local cyc, p = floor(q / per), q % per if p >= 6.67 then return end local b = BL[umod(hash32(cyc * 0x9E3779B1 + slot * 977), 6) + 1] local x = b.x + 3 * sin(p * 1.8 + slot) + (p * 1.5) * wind * 0.4 + 0.5 * sin(p * 9) local y = b.y + 2 + p * 8.1 if y > 62 then return end soft(x, y, 2, 1, PAL[4], PAL[5], PAL[6]) end local GLX, GLY = {}, {} for i = 1, 9 do local h = hash32(i * 0x85EBCA6B) GLX[i] = umod(h, W - 3) GLY[i] = 36 + umod(h >> 8, 24) end function render() if not BG then build_bg(); SPR[7] = make_spr(7); SPR[5] = make_spr(5) end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / gfx.FPS local wind = (cfg.wind or 3) / 3 for i = 1, 9 do local a = sin(t * 1.2 * wind + i * 1.9) if a > 0 then blend_rect(GLX[i], GLY[i], 3, 1, G_LIGHT[1], G_LIGHT[2], G_LIGHT[3], 0.55 * a) end end local use_bee = cfg.bee ~= false local p, cyc = t % 20, floor(t / 20) local bx, by, sit, dip, bi if use_bee then bx, by, sit, dip = bee_pos(p, t, wind, cyc) end local dipi, dipv = 0, 0 if bx and sit then dipi = (p < 10) and A_ORDER[cyc % #A_ORDER + 1] or A_ORDER[(cyc + 2) % #A_ORDER + 1] dipv = dip end for i = 1, #BL do local b = BL[i] local x, y = bloom_pos(i, t, wind, i == dipi and dipv or 0) local s = SPR[b.n] put(s, x - s.c, y - s.c) end petal(1, t, wind); petal(2, t, wind) if bx then soft(bx - 1, by, 3, 2, 252, 208, 56) rect(floor(bx + 0.5), floor(by), 1, 2, 38, 28, 24) local wy = (sit and (t * 10) % 1 < 0.2 or (not sit and (t * 15) % 1 < 0.25)) and -2 or -1 blend_rect(floor(bx - 1), floor(by) + wy, 3, 1, 255, 255, 255, 0.75) end end