info = { type = "image", title = "Cuckoo clock", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "A cuckoo pops out, the pendulum swings", api = 33, } local tr = ({ sk = { ["Wood colour"] = "Farba dreva", ["Cuckoo"] = "Kukučka", ["off"] = "vypnuté", ["every 10 s"] = "každých 10 s", ["every 20 s"] = "každých 20 s", ["Hands speed"] = "Rýchlosť ručičiek", ["Pendulum"] = "Kyvadlo" }, cs = { ["Wood colour"] = "Barva dřeva", ["Cuckoo"] = "Kukačka", ["off"] = "vypnuto", ["every 10 s"] = "každých 10 s", ["every 20 s"] = "každých 20 s", ["Hands speed"] = "Rychlost ručiček", ["Pendulum"] = "Kyvadlo" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "wood", label = T("Wood colour"), type = "color", default = "#BE8442" }, { key = "cuckoo", label = T("Cuckoo"), type = "int", default = 1, opts = { T("off"), T("every 10 s"), T("every 20 s") } }, { key = "hands", label = T("Hands speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "pendulum", label = T("Pendulum"), type = "bool", default = true }, } local rect, px, blend, brect = gfx.rect, gfx.px, gfx.blend, 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 TAU = 6.28318 local WALL, STRIPE = { 34, 84, 92 }, { 42, 100, 110 } local FACE = { 246, 232, 190 } local BRASS, BRASS_L, BRASS_D = { 226, 178, 58 }, { 255, 232, 134 }, { 168, 124, 36 } local HAND = { 62, 34, 18 } local DOORIN = { 24, 14, 8 } local function c3(c) return c[1], c[2], c[3] end local WD, WM, WL, ROOF, ROOFL = { 99, 69, 34 }, { 190, 132, 66 }, { 212, 166, 100 }, { 72, 50, 25 }, { 118, 82, 41 } local PERIOD, HSPD, SWING = 300, 3, true local BG = nil function on_settings() local c = cfg.wood or { r = 190, g = 132, b = 66 } WM = { c.r, c.g, c.b } WD = { floor(c.r * 0.52), floor(c.g * 0.52), floor(c.b * 0.52) } WL = { floor(c.r + (255 - c.r) * 0.22), floor(c.g + (255 - c.g) * 0.22), floor(c.b + (255 - c.b) * 0.22) } ROOF = { floor(c.r * 0.38), floor(c.g * 0.38), floor(c.b * 0.38) } ROOFL = { floor(c.r * 0.62), floor(c.g * 0.62), floor(c.b * 0.62) } local k = cfg.cuckoo or 1 PERIOD = k == 0 and 0 or (k == 1 and 300 or 600) HSPD = cfg.hands or 3 SWING = cfg.pendulum ~= false BG = nil end local function build_bg() rect(0, 0, W, H, c3(WALL)) for x = 0, W - 1, 8 do rect(x, 0, 2, H, c3(STRIPE)) end rect(0, 62, W, 2, 88, 52, 28); rect(0, 62, W, 1, 124, 78, 42) brect(19, 21, 30, 25, 0, 0, 0, 0.28) for y = 4, 17 do local hw = floor(2 + (y - 4) * 1.25) rect(32 - hw, y, 2 * hw, 1, c3(ROOF)) if y >= 7 then local h2 = hw - 3; rect(32 - h2, y, 2 * h2, 1, c3(WL)) end if y % 2 == 0 then px(32 - hw, y, c3(ROOFL)); px(31 + hw, y, c3(ROOFL)) end end px(31, 3, c3(ROOFL)); px(32, 3, c3(ROOFL)) rect(17, 18, 30, 26, c3(WM)) rect(17, 18, 30, 1, c3(WD)); rect(17, 43, 30, 1, c3(WD)) rect(17, 18, 1, 26, c3(WD)); rect(46, 18, 1, 26, c3(WD)) rect(18, 19, 28, 1, c3(WL)); rect(18, 19, 1, 24, c3(WL)) for y = 22, 40, 6 do px(19, y, c3(WD)); px(20, y + 1, c3(WD)); px(44, y, c3(WD)); px(43, y + 1, c3(WD)) end rect(27, 9, 10, 1, c3(WD)); rect(27, 10, 1, 8, c3(WD)); rect(36, 10, 1, 8, c3(WD)) for y = 22, 41 do for x = 22, 41 do local dx, dy = x + 0.5 - 32, y + 0.5 - 32 local d = sqrt(dx * dx + dy * dy) if d <= 9.6 then if d >= 8.4 then px(x, y, c3(WD)) else px(x, y, c3(FACE)) end end end end for h = 0, 11 do local a = h * 0.5236 px(floor(32 + sin(a) * 7), floor(32 - cos(a) * 7), c3(WD)) if h % 3 == 0 then px(floor(32 + sin(a) * 6), floor(32 - cos(a) * 6), c3(WD)) end end rect(15, 44, 34, 2, c3(WD)); rect(16, 46, 32, 1, c3(ROOF)); rect(15, 44, 34, 1, c3(WM)) BG = gfx.snapshot(0, 0, W, H) end local function line(cx, cy, dx, dy, len, r, g, b) if abs(dy) >= abs(dx) then local n = floor(abs(dy) * len + 0.5) local st = dy > 0 and 1 or -1 local sx = dx / dy for k = 0, n - 1 do local Y = cy + st * (k + 0.5) local X = cx + sx * (Y - cy) - 0.5 local ix = floor(X) local f = X - ix local yi = floor(Y) if f < 0.97 then blend(ix, yi, r, g, b, 1 - f) end if f > 0.03 then blend(ix + 1, yi, r, g, b, f) end end else local n = floor(abs(dx) * len + 0.5) local st = dx > 0 and 1 or -1 local sy = dy / dx for k = 0, n - 1 do local X = cx + st * (k + 0.5) local Y = cy + sy * (X - cx) - 0.5 local iy = floor(Y) local f = Y - iy local xi = floor(X) if f < 0.97 then blend(xi, iy, r, g, b, 1 - f) end if f > 0.03 then blend(xi, iy + 1, r, g, b, f) end end end end local function draw_hands(f) local am = 0.9 + f * TAU / (STEP_HZ * 60 / HSPD) local ah = 4.1 + f * TAU / (STEP_HZ * 60 / HSPD * 12) line(32, 32, sin(ah), -cos(ah), 5, c3(HAND)) line(32, 32, sin(am), -cos(am), 7.5, c3(HAND)) rect(31, 31, 2, 2, c3(HAND)) end local PIVX, PIVY, ROD = 32, 47, 11 local BOBW = { 1.5, 2.5, 3, 3, 3, 2.5, 1.5 } local function span(xa, xb, y, r, g, b) local ia, ib = floor(xa), floor(xb) if ia == ib then blend(ia, y, r, g, b, xb - xa) return end blend(ia, y, r, g, b, ia + 1 - xa) if ib > ia + 1 then rect(ia + 1, y, ib - ia - 1, 1, r, g, b) end if xb > ib then blend(ib, y, r, g, b, xb - ib) end end local function draw_pendulum(f) local th = SWING and 0.32 * sin(f * TAU / (2 * STEP_HZ)) or 0 local s, c = sin(th), cos(th) line(PIVX, PIVY, s, c, ROD, c3(BRASS_D)) local bx, by = PIVX + s * (ROD + 0.5), PIVY + c * (ROD + 0.5) local yc = floor(by + 0.5) for r = 1, 7 do local hw = BOBW[r] span(bx - hw, bx + hw, yc - 4 + r, c3(BRASS)) end blend(floor(bx - 1), yc - 2, BRASS_L[1], BRASS_L[2], BRASS_L[3], 0.9) blend(floor(bx - 1) + 1, yc - 2, BRASS_L[1], BRASS_L[2], BRASS_L[3], 0.6) span(bx - 1.5, bx + 1.5, yc + 3, c3(BRASS_D)) end local CONE_W = { 2, 4, 5, 5, 5, 4, 2 } local CONE_C = { { 118, 78, 40 }, { 152, 106, 56 } } local function weight_top(f, off) local p = (f + off) % 600 if p < 555 then return 48 + 7 * p / 555 end local u = (p - 555) / 45 return 55 - 7 * u * u * (3 - 2 * u) end local function draw_weight(cx, top) local y0 = floor(top) local fr = top - y0 for y = 47, y0 - 1 do px(cx, y, y % 2 == 0 and 214 or 142, y % 2 == 0 and 180 or 112, y % 2 == 0 and 72 or 42) end for r = 1, 7 do local w = CONE_W[r] local c = CONE_C[r % 2 + 1] local x = cx - w // 2 local y = y0 + r - 1 if r == 1 then brect(x, y, w, 1, c[1], c[2], c[3], 1 - fr) else rect(x, y, w, 1, c3(c)) end end brect(cx - 1, y0 + 7, 2, 1, CONE_C[2][1], CONE_C[2][2], CONE_C[2][3], fr) end local BIRD = { "..HH..", ".HHHH.", "HEHHEH", "HHYYHH", ".HHHH.", "GWWWWG", "GWWWWG", ".GWWG.", } local BIRD_OPEN = ".HYYH." local BC = { H = { 250, 242, 214 }, E = { 30, 22, 22 }, Y = { 255, 200, 40 }, G = { 140, 108, 76 }, W = { 232, 218, 186 } } local function draw_bird(y0, open) gfx.clip(28, 10, 8, 8) for r = 1, 8 do local row = (r == 5 and open) and BIRD_OPEN or BIRD[r] for c = 1, 6 do local ch = row:sub(c, c) if ch ~= "." then local col = BC[ch] px(28 + c, y0 + r - 1, col[1], col[2], col[3]) end end end gfx.clip() end local function smooth(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local function draw_door(f) local p = PERIOD > 0 and f >= 24 and (f - 24) % PERIOD or -1 local open = 0 if p >= 0 and p < 120 then open = smooth(p / 10) - smooth((p - 110) / 10) end if open > 0 then rect(28, 10, 8, 8, c3(DOORIN)) local rs = 8 - 8 * smooth((p - 8) / 12) + 8 * smooth((p - 100) / 10) if rs < 8 then local open_beak, yo = false, 0 for k = 0, 2 do local q = p - (26 + 30 * k) if q >= 0 and q < 14 then open_beak = q < 8 if q < 10 then yo = -floor(1.2 * sin(q * 3.1416 / 10) + 0.5) end if q < 10 then local a = 1 - q / 10 local d = q // 3 brect(25 - d, 11, 1, 3, WD[1], WD[2], WD[3], a); brect(38 + d, 11, 1, 3, WD[1], WD[2], WD[3], a) end end end draw_bird(floor(10 + rs + yo + 0.5), open_beak) end end local wl = floor(4 * (1 - open) + 0.5) if wl > 0 then rect(28, 10, wl, 8, c3(WM)); rect(36 - wl, 10, wl, 8, c3(WM)) if wl == 4 then rect(31, 10, 2, 8, c3(WD)) px(30, 14, c3(WL)); px(33, 14, c3(WL)) end end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame * STEP_HZ // FPS draw_hands(f) draw_door(f) draw_weight(21, weight_top(f, 120)) draw_weight(43, weight_top(f, 420)) draw_pendulum(f) end