info = { type = "image", title = "Pumpkin patch crow", category = "holidays", author = "BRRRO", version = "1.0", license = "MIT", desc = "Pumpkins puff up, a crow lands on the fence", api = 33, } local tr = ({ sk = { ["Pumpkin colour"] = "Farba tekvice", ["Crow"] = "Vrana", ["Breeze"] = "Vánok" }, cs = { ["Pumpkin colour"] = "Barva dýně", ["Crow"] = "Vrána", ["Breeze"] = "Vánek" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "pumpkin", label = T("Pumpkin colour"), type = "color", default = "#F07818" }, { key = "crow", label = T("Crow"), 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, abs = math.floor, math.sin, math.cos, math.sqrt, math.abs local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local SKY, SKY_L = { 126, 196, 240 }, { 156, 214, 246 } local HILL, GROUND, FURROW = { 96, 176, 92 }, { 72, 156, 64 }, { 50, 124, 50 } local TRUNK, FENCE, FENCE_D = { 110, 70, 40 }, { 168, 120, 72 }, { 112, 76, 46 } local CAN, CAN_D, CAN_L = { 46, 134, 60 }, { 30, 100, 50 }, { 98, 180, 84 } local APPLE = { 222, 48, 40 } local SUNC, RAY = { 255, 226, 70 }, { 255, 238, 110 } local CLOUD = { 248, 252, 255 } local STEMC = { 54, 112, 40 } local CROW, BEAK = { 26, 24, 38 }, { 244, 182, 44 } local SUNX, SUNY, SUNR = 52, 10, 5 local PUMP = { r = 240, g = 120, b = 24 } local RIB, LIGHT = { 190, 80, 14 }, { 255, 170, 70 } local function half(r, dy) return floor(sqrt((r + 0.5) * (r + 0.5) - dy * dy)) end local PK = { { cx = 32, cy = 47, rx = 8, ry = 7, ph = 0.0 }, { cx = 14, cy = 55, rx = 9, ry = 7, ph = 2.2 }, { cx = 51, cy = 56, rx = 8, ry = 6, ph = 4.1 }, } local function ring(p) local rx, ry = p.rx, p.ry local list = {} for dy = -(ry + 1), ry + 1 do local e0 = abs(dy) <= ry local h0 = e0 and floor(rx * sqrt(1 - (dy / (ry + 0.5)) ^ 2) + 0.5) or -1 local u = dy / (ry + 1.5) local h1 = floor((rx + 1) * sqrt(1 - u * u) + 0.5) if h0 < 0 then list[#list + 1] = { p.cx - h1, p.cy + dy, 2 * h1 + 1 } elseif h1 > h0 then list[#list + 1] = { p.cx - h1, p.cy + dy, h1 - h0 } list[#list + 1] = { p.cx + h0 + 1, p.cy + dy, h1 - h0 } end end return list end for i = 1, #PK do PK[i].ring = ring(PK[i]) end local BG = nil local function build_bg() rect(0, 0, W, 32, SKY[1], SKY[2], SKY[3]) rect(0, 32, W, 8, SKY_L[1], SKY_L[2], SKY_L[3]) for dy = -SUNR, SUNR do local h = half(SUNR, dy) rect(SUNX - h, SUNY + dy, 2 * h + 1, 1, SUNC[1], SUNC[2], SUNC[3]) end rect(0, 36, 30, 4, HILL[1], HILL[2], HILL[3]); rect(38, 35, 26, 5, HILL[1], HILL[2], HILL[3]) rect(0, 40, W, H - 40, GROUND[1], GROUND[2], GROUND[3]) rect(0, 42, 24, 1, FURROW[1], FURROW[2], FURROW[3]); rect(40, 43, 24, 1, FURROW[1], FURROW[2], FURROW[3]) rect(4, 45, 28, 1, FURROW[1], FURROW[2], FURROW[3]); rect(38, 48, 20, 1, FURROW[1], FURROW[2], FURROW[3]) rect(9, 27, 3, 14, TRUNK[1], TRUNK[2], TRUNK[3]); rect(8, 39, 5, 2, TRUNK[1], TRUNK[2], TRUNK[3]) rect(26, 36, 2, 5, TRUNK[1], TRUNK[2], TRUNK[3]) for _, x in ipairs({ 46, 54, 61 }) do rect(x, 34, 3, 10, FENCE[1], FENCE[2], FENCE[3]); rect(x + 2, 34, 1, 10, FENCE_D[1], FENCE_D[2], FENCE_D[3]) end rect(44, 37, 20, 1, FENCE_D[1], FENCE_D[2], FENCE_D[3]); rect(44, 41, 20, 1, FENCE_D[1], FENCE_D[2], FENCE_D[3]) local P = PUMP for i = 1, #PK do local p = PK[i] for dy = -p.ry, p.ry do local h = floor(p.rx * sqrt(1 - (dy / (p.ry + 0.5)) ^ 2) + 0.5) rect(p.cx - h, p.cy + dy, 2 * h + 1, 1, P.r, P.g, P.b) end for _, k in ipairs({ -0.58, 0, 0.58 }) do local x = p.cx + floor(k * p.rx + 0.5) local hh = floor(p.ry * sqrt(1 - (k * p.rx / (p.rx + 0.5)) ^ 2) + 0.5) rect(x, p.cy - hh + 1, 1, 2 * hh - 1, RIB[1], RIB[2], RIB[3]) end rect(p.cx - floor(p.rx * 0.6), p.cy - floor(p.ry * 0.5), 3, 2, LIGHT[1], LIGHT[2], LIGHT[3]) rect(p.cx - 1, p.cy - p.ry - 3, 2, 3, STEMC[1], STEMC[2], STEMC[3]) px(p.cx + 1, p.cy - p.ry - 3, STEMC[1], STEMC[2], STEMC[3]) end BG = gfx.snapshot(0, 0, W, H) end function on_settings() local c = cfg.pumpkin or { r = 240, g = 120, b = 24 } PUMP = c RIB = { floor(c.r * 0.78), floor(c.g * 0.62), floor(c.b * 0.6) } LIGHT = { 255, floor(c.g + (255 - c.g) * 0.4), floor(c.b + (255 - c.b) * 0.45) } BG = nil end local function hrect(x, y, w, h, r, g, b) local ix = floor(x) local f = x - ix if w > 1 then rect(ix + 1, y, w - 1, h, r, g, b) end blend_rect(ix, y, 1, h, r, g, b, 1 - f) blend_rect(ix + w, y, 1, h, r, g, b, f) 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 canopy(cx, cy, r, amp, t, ph, apples) for dy = -r, r do local h = half(r, dy) local dx = amp * (0.25 + 0.75 * (r - dy) / (2 * r)) * sin(t * 0.04 + ph) local c = dy > r * 0.3 and CAN_D or CAN hrect(cx + dx - h, cy + dy, 2 * h + 1, 1, c[1], c[2], c[3]) end local dxt = amp * 0.75 * sin(t * 0.04 + ph) hrect(cx - r // 2 + dxt, cy - r // 2, 3, 1, CAN_L[1], CAN_L[2], CAN_L[3]) hrect(cx + dxt * 0.9 + 1, cy - r // 2 + 3, 2, 1, CAN_L[1], CAN_L[2], CAN_L[3]) if apples then local dxm = amp * 0.5 * sin(t * 0.04 + ph) for _, a in ipairs(apples) do hrect(cx + a[1] + dxm, cy + a[2], 2, 2, APPLE[1], APPLE[2], APPLE[3]) end end end local RAYS = {} for k = 0, 11 do local a = k * 0.5236 local ca, sa = cos(a), sin(a) RAYS[k + 1] = { floor(SUNX + ca * 7.5 + 0.5), floor(SUNY + sa * 7.5 + 0.5), floor(SUNX + ca * 9.5 + 0.5), floor(SUNY + sa * 9.5 + 0.5), floor(SUNX + ca * 11.5 + 0.5), floor(SUNY + sa * 11.5 + 0.5), } 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 CYC = 540 local PERCH_X, PERCH_Y = 55, 34 local function crow_state(p) if p < 110 then local u = sstep(p / 110) local x = -10 + (PERCH_X + 1 - -10) * u local y = 6 + (PERCH_Y - 6) * u * u + 3 * sin(u * 7) * (1 - u) return x, y, true, false elseif p < 330 then local y = PERCH_Y local hop = 0 if p >= 290 and p < 310 then hop = 3 * sin((p - 290) / 20 * 3.14159) end local open = (p >= 190 and p < 204) or (p >= 232 and p < 246) return PERCH_X, y - hop, hop > 0.4, open elseif p < 420 then local u = sstep((p - 330) / 90) return PERCH_X + 20 * u, PERCH_Y - 40 * u * u - 4 * u, true, false end return nil end local function draw_crow(x, y, fly, open, t, p) local r, g, b = CROW[1], CROW[2], CROW[3] if fly then local up = (t // 3) % 2 == 0 soft(x - 3, y - 3, 5, 2, r, g, b) soft(x + 1, y - 4, 3, 2, r, g, b) rect(floor(x - 6), floor(y - 2), 3, 1, r, g, b) hrect(x + 4, floor(y - 3), 2, 1, BEAK[1], BEAK[2], BEAK[3]) if up then soft(x - 3, y - 8, 3, 5, r, g, b) else soft(x - 3, y - 2, 3, 4, r, g, b) end else local bob = (p >= 150 and p < 190 and (p // 12) % 2 == 0) and 1 or 0 soft(x - 3, y - 4 + bob, 5, 3, r, g, b) soft(x + 1, y - 6 + bob, 3, 3, r, g, b) rect(floor(x - 6), floor(y - 3 + bob), 3, 1, r, g, b) px(floor(x + 2), floor(y - 5 + bob), 255, 255, 255) if open then rect(floor(x + 4), floor(y - 6 + bob), 2, 1, BEAK[1], BEAK[2], BEAK[3]) rect(floor(x + 4), floor(y - 4 + bob), 2, 1, BEAK[1], BEAK[2], BEAK[3]) else rect(floor(x + 4), floor(y - 5 + bob), 2, 1, BEAK[1], BEAK[2], BEAK[3]) end px(floor(x - 1), floor(y - 1 + bob), 214, 170, 110); px(floor(x + 1), floor(y - 1 + bob), 214, 170, 110) end end local CL = { { y = 6, L = 16, v = 0.07, x0 = 6 }, { y = 21, L = 12, v = 0.045, x0 = 36 } } function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS local wind = (cfg.wind or 3) / 3 for i = 1, #PK do local p = PK[i] local a = 0.5 + 0.5 * sin(t * 0.045 + p.ph) a = a * a * 0.85 local list = p.ring for k = 1, #list do local q = list[k] blend_rect(q[1], q[2], q[3], 1, PUMP.r, PUMP.g, PUMP.b, a) end end for k = 1, 12 do local R = RAYS[k] local a = 0.5 + 0.5 * sin(t * 0.09 + k * 1.1) gfx.blend(R[1], R[2], RAY[1], RAY[2], RAY[3], 0.9) gfx.blend(R[3], R[4], RAY[1], RAY[2], RAY[3], 0.25 + 0.65 * a) gfx.blend(R[5], R[6], RAY[1], RAY[2], RAY[3], 0.7 * (1 - a)) end for i = 1, #CL do local c = CL[i] local x = (c.x0 + t * c.v) % (W + c.L) - c.L hrect(x + 3, c.y, c.L - 7, 1, CLOUD[1], CLOUD[2], CLOUD[3]) hrect(x, c.y + 1, c.L, 2, CLOUD[1], CLOUD[2], CLOUD[3]) hrect(x + 4, c.y + 3, c.L - 9, 1, CLOUD[1], CLOUD[2], CLOUD[3]) end canopy(10, 19, 9, 1.6 * wind, t, 0.0, { { -5, -3 }, { 2, -5 }, { 4, 2 }, { -2, 3 } }) canopy(27, 31, 5, 1.0 * wind, t, 1.7) if cfg.crow ~= false then local p = t % CYC local x, y, fly, open = crow_state(p) if x then draw_crow(x, y, fly, open, t, p) end end end