info = { type = "image", title = "Ferris night", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Slow Ferris wheel and stalls on a fair night", api = 33, } local tr = ({ sk = { ["Speed"] = "Rýchlosť", ["Lights"] = "Svetlá", ["Warm"] = "Teplé", ["Cool"] = "Studené", ["Pink"] = "Ružové" }, cs = { ["Speed"] = "Rychlost", ["Lights"] = "Světla", ["Warm"] = "Teplá", ["Cool"] = "Studená", ["Pink"] = "Růžová" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "lights", label = T("Lights"), type = "int", default = "warm", opts = { keys = { "warm", "cool", "pink" }, T("Warm"), T("Cool"), T("Pink") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, min, max = math.floor, math.sin, math.cos, math.min, math.max local W, H = gfx.W, gfx.H local TAU = 6.2831853 local CX, CY, R = 9, 27, 24 local GROUND = 57 local LOOKS = { warm = { a = { 255, 196, 70 }, b = { 255, 240, 205 }, cab = { { 210, 70, 46 }, { 236, 140, 44 }, { 172, 52, 84 } } }, cool = { a = { 120, 200, 255 }, b = { 230, 246, 255 }, cab = { { 46, 110, 210 }, { 40, 170, 190 }, { 110, 90, 210 } } }, pink = { a = { 255, 112, 196 }, b = { 255, 226, 242 }, cab = { { 210, 52, 142 }, { 150, 70, 210 }, { 236, 110, 120 } } }, } local L = LOOKS.warm local N, TH, P = 1, { 0 }, { 0 } local function build_motion() local sp = cfg.speed or 3 local S, A = 150, 90 local C = ({ 1500, 1000, 700, 500, 350 })[sp] or 700 N = S + 2 * A + C local wc = 1.0 local om = {} for i = 1, N do local k = i - 1 local w if k < S then w = 0 elseif k < S + A then local u = (k - S) / A; w = wc * u * u * (3 - 2 * u) elseif k < S + A + C then w = wc else local u = (k - S - A - C) / A; w = wc * (1 - u * u * (3 - 2 * u)) end om[i] = w end local tot = 0 for i = 1, N do tot = tot + om[i] end TH = {} local acc = 0 for i = 1, N do acc = acc + om[i] * TAU / tot; TH[i] = acc end local ph, v, mx = 0, 0, 0.0001 local w0, z = 0.11, 0.1 P = {} for pass = 1, 2 do for i = 1, N do local da = om[i] - om[i == 1 and N or i - 1] v = v + (-w0 * w0 * ph - 2 * z * w0 * v + da * 40 * wc / (om[1] + 1e-9 + wc)) ph = ph + v if pass == 2 then P[i] = ph; mx = max(mx, ph > 0 and ph or -ph) end end end local amp = 0.7 + 0.15 * sp for i = 1, N do P[i] = P[i] / mx * amp end 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 local ax, ay = 1 - fx, 1 - fy if w > 1 and h > 1 then rect(ix + 1, iy + 1, w - 1, h - 1, r, g, b) end if h > 1 then if ax > 0.03 then blend_rect(ix, iy + 1, 1, h - 1, r, g, b, ax) end if fx > 0.03 then blend_rect(ix + w, iy + 1, 1, h - 1, r, g, b, fx) end end if w > 1 then if ay > 0.03 then blend_rect(ix + 1, iy, w - 1, 1, r, g, b, ay) end if fy > 0.03 then blend_rect(ix + 1, iy + h, w - 1, 1, r, g, b, fy) end end if ax * ay > 0.03 then blend(ix, iy, r, g, b, ax * ay) end if fx * ay > 0.03 then blend(ix + w, iy, r, g, b, fx * ay) end if ax * fy > 0.03 then blend(ix, iy + h, r, g, b, ax * fy) end if fx * fy > 0.03 then blend(ix + w, iy + h, r, g, b, fx * fy) end end local function bulb(x, y, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local a = min(1, (1 - fx) * (1 - fy) * 1.5) if a > 0.05 then blend(ix, iy, r, g, b, a) end a = min(1, fx * (1 - fy) * 1.5) if a > 0.05 then blend(ix + 1, iy, r, g, b, a) end a = min(1, (1 - fx) * fy * 1.5) if a > 0.05 then blend(ix, iy + 1, r, g, b, a) end a = min(1, fx * fy * 1.5) if a > 0.05 then blend(ix + 1, iy + 1, r, g, b, a) end end local BG local STARS = { 3, 5, 14, 2, 22, 9, 30, 4, 38, 11, 45, 3, 52, 19, 60, 14, 28, 17, 41, 24, 58, 30, 34, 8 } local FESTOON = {} for k = 0, 7 do local x = 35 + k * 4 FESTOON[#FESTOON + 1] = { x, 43 + floor(3 * sin(3.14159 * (x - 33) / 29) + 0.5) } end local function build_bg() for y = 0, H - 1 do local t = min(1, y / 56) local hz = max(0, min(1, (y - 44) / 12)) rect(0, y, W, 1, floor(9 + 24 * hz + 0.5), floor(8 + 10 * hz + 0.5), floor(40 + 40 * t + 0.5)) end rect(0, GROUND, W, H - GROUND, 24, 16, 50) for dy = -4, 4 do for dx = -4, 4 do if dx * dx + dy * dy <= 16 and (dx + 2) ^ 2 + (dy - 1) ^ 2 >= 13 then px(55 + dx, 9 + dy, 236, 232, 206) end end end for i = 1, #STARS, 2 do px(STARS[i], STARS[i + 1], 110, 118, 186) end for x = 34, 63 do local y = 36 + floor(6 * sin((x - 34) * 0.3) + 0.5) px(x, y, 52, 50, 130) if x % 5 == 0 then rect(x, y + 1, 1, 48 - y, 30, 26, 80) end end for k = 0, 179 do local a = k * TAU / 180 local x, y = floor(CX + R * cos(a) + 0.5), floor(CY + R * sin(a) + 0.5) if x >= 0 then px(x, y, 72, 94, 190) end if k % 2 == 0 then x, y = floor(CX + 10 * cos(a) + 0.5), floor(CY + 10 * sin(a) + 0.5) if x >= 0 then px(x, y, 40, 54, 124) end end end for y = CY, GROUND - 1 do local u = (y - CY) / (GROUND - CY) rect(floor(CX - 12 * u), y, 2, 1, 38, 50, 116) rect(floor(CX + 12 * u) - 1, y, 2, 1, 38, 50, 116) end rect(0, 44, 20, 1, 38, 50, 116) rect(0, 55, 24, 2, 58, 50, 100) rect(CX - 2, CY - 2, 5, 5, 64, 82, 170) for _, px0 in ipairs({ 33, 62 }) do rect(px0, 43, 1, GROUND - 43, 52, 44, 92) end for x = 34, 61 do px(x, 43 + floor(3 * sin(3.14159 * (x - 33) / 29) + 0.5), 78, 66, 112) end local function stall(x0, c1, c2) rect(x0, 51, 12, 6, 56, 40, 90) rect(x0 + 2, 52, 8, 3, 214, 164, 74) rect(x0, 50, 12, 1, 36, 28, 70) for i = 0, 5 do local c = i % 2 == 0 and c1 or c2 rect(x0 + i * 2, 47, 2, 3, c[1], c[2], c[3]) end rect(x0 - 1, 46, 14, 1, 70, 56, 108) end stall(36, { 200, 60, 70 }, { 226, 216, 226 }) stall(50, { 56, 170, 170 }, { 226, 216, 226 }) BG = gfx.snapshot(0, 0, W, H) end local FOLK = { { 6, 0.11, { 160, 70, 180 }, { 70, 60, 120 } }, { 30, -0.08, { 70, 150, 190 }, { 60, 52, 100 } }, { 44, 0.14, { 210, 130, 60 }, { 70, 60, 120 } }, { 58, -0.12, { 190, 70, 90 }, { 50, 56, 110 } }, } local SKIN = { 226, 178, 140 } function on_settings() L = LOOKS[cfg.lights] or LOOKS.warm build_motion() end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame local idx = f % N + 1 local th = TH[idx] - 1.2 local pend = P[idx] local a, b = L.a, L.b for i = 1, 8, 2 do local k = (f // 24 + i) % 3 if k == 0 then px(STARS[i], STARS[i + 1], 190, 198, 255) end end for i = 0, 11 do local ang = th + i * 0.5235988 local c, s = cos(ang), sin(ang) local rmax = R - 1 if c < 0 then rmax = min(rmax, floor(CX / -c)) end for r = 3, rmax, 2 do px(floor(CX + r * c + 0.5), floor(CY + r * s + 0.5), 46, 62, 140) end end local chase = f // 7 for i = 0, 11 do local ang = th + i * 0.5235988 local c, s = cos(ang), sin(ang) local k = 0.8 + 0.2 * sin(f * 0.07 + i) local x2, y2 = CX + 16 * c, CY + 16 * s if x2 > -1 then bulb(x2, y2, a[1] * k, a[2] * k, a[3] * k) end local x3, y3 = CX + R * c, CY + R * s if x3 > -1 then local m = (i * 2 - chase) % 3 == 0 and 1 or 0.55 bulb(x3, y3, b[1] * m, b[2] * m, b[3] * m) end local am = ang + 0.2617994 local x4, y4 = CX + R * cos(am), CY + R * sin(am) if x4 > -1 then local m = (i * 2 + 1 - chase) % 3 == 0 and 1 or 0.55 bulb(x4, y4, a[1] * m, a[2] * m, a[3] * m) end end local hk = 0.75 + 0.25 * sin(f * 0.09) rect(CX - 1, CY - 1, 3, 3, a[1] * hk // 1, a[2] * hk // 1, a[3] * hk // 1) for i = 0, 11 do local ang = th + i * 0.5235988 local hx, hy = CX + R * cos(ang), CY + R * sin(ang) if hx > -4 then local sway = pend * sin(ang) + 0.25 * sin(f * 0.045 + i * 1.7) local cc = L.cab[i % 3 + 1] local x, y = hx - 2 + sway, hy + 1.5 soft(x, y, 5, 4, cc[1], cc[2], cc[3]) soft(x + 1, y + 1, 3, 1, b[1] * 0.9 // 1, b[2] * 0.9 // 1, b[3] * 0.9 // 1) end end for k = 1, #FESTOON do local p = FESTOON[k] local on = ((f // 15 + k) % 2 == 0) local c = on and a or b local m = on and 1 or 0.5 px(p[1], p[2], c[1] * m // 1, c[2] * m // 1, c[3] * m // 1) end for i = 1, #FOLK do local p = FOLK[i] local x = (p[1] + p[2] * f) % (W + 8) - 4 soft(x, GROUND, 2, 2, SKIN[1], SKIN[2], SKIN[3]) soft(x, GROUND + 2, 2, 2, p[3][1], p[3][2], p[3][3]) soft(x, GROUND + 4, 2, 2, p[4][1], p[4][2], p[4][3]) end end