info = { type = "image", title = "Microwave", category = "food", author = "BRRRO", version = "1.0", license = "MIT", desc = "Food spins in a microwave until it dings", api = 33, } local tr = ({ sk = { ["Food"] = "Jedlo", ["mug"] = "hrnček", ["chicken"] = "kura", ["Cook time"] = "Čas varenia", ["Body colour"] = "Farba tela" }, cs = { ["Food"] = "Jídlo", ["mug"] = "hrnek", ["chicken"] = "kuře", ["Cook time"] = "Doba vaření", ["Body colour"] = "Barva těla" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "food", label = T("Food"), type = "int", default = 0, opts = { T("pizza"), T("mug"), T("chicken") } }, { key = "secs", label = T("Cook time"), type = "int", min = 5, max = 30, unit = "s", default = 12 }, { key = "body", label = T("Body colour"), type = "color", default = "#ECE6DA" }, } 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 W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU, PI = 6.28318, 3.14159 local function c3(c) return c[1], c[2], c[3] end local function lerp3(a, b, u) return floor(a[1] + (b[1] - a[1]) * u + 0.5), floor(a[2] + (b[2] - a[2]) * u + 0.5), floor(a[3] + (b[3] - a[3]) * u + 0.5) end local function mul(c, k) return { floor(c[1] * k), floor(c[2] * k), floor(c[3] * k) } end local function tint(c, k) return { floor(c[1] + (255 - c[1]) * k), floor(c[2] + (255 - c[2]) * k), floor(c[3] + (255 - c[3]) * k) } end local function runs(rows, pal) local S = {} for r = 1, #rows do local s, out, k, n = rows[r], {}, 1, #rows[r] while k <= n do local ch, e = s:sub(k, k), k while e < n and s:sub(e + 1, e + 1) == ch do e = e + 1 end if ch ~= "." then out[#out + 1] = { k - 1, e - k + 1, pal[ch] } end k = e + 1 end S[r] = out end return S end local function sprite(S, x, y) local ix = floor(x) local f = x - ix for r = 1, #S do local row, yy, m = S[r], y + r - 1, #S[r] for k = 1, m do local e = row[k] local a, w, c = ix + e[1], e[2], e[3] if k == 1 then blend_rect(a, yy, 1, 1, c[1], c[2], c[3], 1 - f) if w > 1 then rect(a + 1, yy, w - 1, 1, c[1], c[2], c[3]) end else rect(a, yy, w, 1, c[1], c[2], c[3]) end if k == m and f > 0.03 then blend_rect(a + w, yy, 1, 1, c[1], c[2], c[3], f) end end end end local IX, IY, IW, IH = 7, 18, 30, 27 local PCX, PCY = 21.5, 40 local DX, DY, DW, DH = 45, 18, 13, 7 local TILE, GROUT = { 156, 204, 198 }, { 128, 178, 172 } local WOOD, WOOD_L, WOOD_D = { 200, 156, 108 }, { 224, 186, 140 }, { 168, 126, 86 } local CAB, CAB_D, KNOB = { 150, 104, 72 }, { 112, 76, 54 }, { 224, 224, 224 } local FEET, HANDLE, HANDLE_L = { 70, 70, 76 }, { 96, 100, 108 }, { 150, 156, 166 } local DISP, DIG, DIG_D = { 24, 38, 32 }, { 110, 255, 150 }, { 50, 120, 80 } local BTN, BTN_L = { 168, 174, 180 }, { 214, 218, 222 } local START, START_D = { 86, 190, 108 }, { 52, 130, 70 } local LIT0, LIT1, LAMP = { 255, 232, 150 }, { 238, 178, 92 }, { 255, 250, 214 } local OFF = { 78, 88, 96 } local PLATE_ON, RIM_ON = { 246, 242, 228 }, { 206, 196, 176 } local PLATE_OFF, RIM_OFF = { 120, 128, 134 }, { 100, 108, 114 } local SHADOW_ON = { 214, 156, 80 } local DING_C, DING_O = { 255, 222, 70 }, { 150, 70, 30 } local FOODS = { [0] = { crust = { 222, 152, 72 }, crust_d = { 186, 116, 50 }, cheese = { 255, 208, 84 }, pep = { 196, 46, 40 } }, [1] = { l = { 206, 224, 242 }, m = { 70, 130, 210 }, h = { 132, 182, 240 }, hd = { 46, 92, 168 } }, [2] = { b = { 214, 140, 60 }, h = { 246, 196, 110 }, d = { 170, 96, 40 }, leg = { 196, 118, 50 }, bone = { 244, 238, 222 } }, } local MUG_ART = { "lllll", "mhmmm", "mhmmm", "mhmmm", "mhmmm", "mmmmm" } local CHICK_ART = { "...bbbbb...", ".bbhhbbbbb.", "bbhhbbbbbbb", "bbbbbbbbbbb", ".ddddddddd." } local PEP = { { 5.5, 0.0 }, { 5.5, 2.1 }, { 5.0, 4.2 }, { 2.2, 1.2 }, { 2.4, 4.0 } } local FOOD, SECS, COOK, TOT, OMEGA = 0, 12, 360, 522, 0 local BODY, BODY_D, BODY_L, PANEL = { 236, 230, 218 }, nil, nil, nil local FP = { {}, {} } local SPR = { {}, {} } local BG, WIN_ON, WIN_OFF = nil, nil, nil local READY = false local function apply() READY = true local C = cfg or {} FOOD = C.food or 0 if not FOODS[FOOD] then FOOD = 0 end SECS = C.secs or 12 if SECS < 1 then SECS = 1 end COOK = SECS * STEP_HZ TOT = 12 + COOK + 150 OMEGA = TAU * math.max(1, floor(SECS / 5 + 0.5)) / COOK local b = C.body or { r = 236, g = 230, b = 218 } BODY = { b.r, b.g, b.b } BODY_D, BODY_L, PANEL = mul(BODY, 0.58), tint(BODY, 0.5), mul(BODY, 0.9) for i = 1, 2 do local k = i == 1 and 1 or 0.55 local P = {} for name, c in pairs(FOODS[FOOD]) do P[name] = mul(c, k) end FP[i] = P if FOOD == 1 then SPR[i] = runs(MUG_ART, P) elseif FOOD == 2 then SPR[i] = runs(CHICK_ART, P) end end BG = nil end function on_settings() apply() end local function plate(c, rim) for dy = -3, 3 do local hw = floor(11 * sqrt(1 - (dy / 3.6) ^ 2) + 0.5) rect(22 - hw, PCY + dy, 2 * hw, 1, c3(dy == 3 and rim or c)) end end local function pizza(P) for dy = -2, 2 do local hw = floor(8.6 * sqrt(1 - (dy / 2.6) ^ 2) + 0.5) local y = PCY - 1 + dy rect(22 - hw, y, 2 * hw, 1, c3(dy == 2 and P.crust_d or P.crust)) if dy > -2 and dy < 2 then rect(23 - hw, y, 2 * hw - 2, 1, c3(P.cheese)) end end end local function build_bg() rect(0, 0, W, 50, c3(TILE)) for i = 0, 6 do local y0 = i * 7 rect(0, y0 + 6, W, 1, c3(GROUT)) for x = (i % 2) * 5, W - 1, 10 do rect(x, y0, 1, 6, c3(GROUT)) end end rect(0, 50, W, 3, c3(WOOD)); rect(0, 50, W, 1, c3(WOOD_L)); rect(4, 51, 56, 1, c3(WOOD_D)) rect(0, 53, W, 11, c3(CAB)); rect(0, 53, W, 1, c3(CAB_D)); rect(31, 53, 1, 11, c3(CAB_D)) rect(26, 56, 3, 1, c3(KNOB)); rect(34, 56, 3, 1, c3(KNOB)) rect(6, 50, 4, 1, c3(FEET)); rect(54, 50, 4, 1, c3(FEET)) gfx.round_rect(2, 13, 60, 37, BODY[1], BODY[2], BODY[3], BODY_D[1], BODY_D[2], BODY_D[3], 3) rect(5, 14, 54, 1, c3(BODY_L)) gfx.round_rect(4, 15, 38, 33, BODY[1], BODY[2], BODY[3], BODY_D[1], BODY_D[2], BODY_D[3], 2) rect(6, 17, 32, 29, c3(BODY_D)) rect(38, 20, 2, 22, c3(HANDLE)); rect(38, 20, 1, 22, c3(HANDLE_L)) rect(43, 15, 17, 33, c3(PANEL)) rect(44, 17, 15, 9, c3(BODY_D)); rect(DX, DY, DW, DH, c3(DISP)) for r = 0, 2 do for c = 0, 2 do rect(45 + c * 5, 28 + r * 4, 3, 2, c3(BTN)); rect(45 + c * 5, 28 + r * 4, 3, 1, c3(BTN_L)) end end rect(45, 41, 13, 3, c3(START)) for y = 0, IH - 1 do rect(IX, IY + y, IW, 1, lerp3(LIT0, LIT1, y / (IH - 1))) end rect(IX + 9, IY, 12, 1, c3(LAMP)) rect(13, 44, 18, 1, c3(SHADOW_ON)) plate(PLATE_ON, RIM_ON) if FOOD == 0 then pizza(FP[1]) end WIN_ON = gfx.snapshot(IX, IY, IW, IH) rect(IX, IY, IW, IH, c3(OFF)) plate(PLATE_OFF, RIM_OFF) if FOOD == 0 then pizza(FP[2]) end WIN_OFF = gfx.snapshot(IX, IY, IW, IH) BG = gfx.snapshot(0, 0, W, H) end local function dot2(x, y, c) local ix = floor(x) local f = x - ix blend_rect(ix, y, 1, 1, c[1], c[2], c[3], 1 - f) rect(ix + 1, y, 1, 1, c3(c)) if f > 0.03 then blend_rect(ix + 2, y, 1, 1, c[1], c[2], c[3], f) end end local function draw_food(a, li) local P = FP[li] if FOOD == 0 then for i = 1, #PEP do local e = PEP[i] local ang = a + e[2] dot2(PCX - 1 + e[1] * cos(ang), PCY - 1 + floor(e[1] * 0.3 * sin(ang) + 0.5), P.pep) end return PCY - 4, PCX elseif FOOD == 1 then local mx, my = PCX + 5 * cos(a), floor(PCY + 1.5 * sin(a) + 0.5) local top = my - 5 sprite(SPR[li], mx - 2.5, top) local ix = floor(mx - 2.5 + 0.5) local ca = cos(a) if ca > 0.35 then px(ix + 5, top + 1, c3(P.m)); px(ix + 6, top + 2, c3(P.m)); px(ix + 6, top + 3, c3(P.m)); px(ix + 5, top + 4, c3(P.m)) elseif ca < -0.35 then px(ix - 1, top + 1, c3(P.m)); px(ix - 2, top + 2, c3(P.m)); px(ix - 2, top + 3, c3(P.m)); px(ix - 1, top + 4, c3(P.m)) elseif sin(a) > 0 then rect(ix + 2, top + 2, 1, 3, c3(P.hd)) end return top - 1, mx end local cx, by = PCX + 2 * cos(a), floor(PCY - 0.5 + 0.6 * sin(a) + 0.5) local top = by - 4 local back = a + PI for pass = 1, 2 do if pass == 2 then sprite(SPR[li], cx - 5.5, top) end for s = -1, 1, 2 do local ang = back + 0.5 * s local sn = sin(ang) if (pass == 1) == (sn < 0) then local lx, ly = cx - 1 + 4.5 * cos(ang), floor(by - 3 + 0.9 * sn + 0.5) dot2(lx, ly, P.leg) px(floor(lx + 0.5) + (cos(ang) > 0 and 2 or -1), ly - 1, c3(P.bone)) end end end return top - 1, cx end local F3 = { font = "3x5" } local F3C = { font = "3x5", w = DW, align = "center" } local FDING = { font = "5x7", outline = DING_O } local TY, SHORT, DW_DING = nil, false, 0 local function show(s, c) gfx.text(DX + (DW - gfx.text_width(s, F3)) // 2, TY, s, c[1], c[2], c[3], F3) end local function clock(s) if SHORT then return string.format("%d", s) end return string.format("%d:%02d", s // 60, s % 60) end function render() if not READY then apply() end if not BG then build_bg() end if not TY then local top, bot = gfx.text_ink("0:", F3) TY = DY + (DH - (bot - top + 1)) // 2 - top SHORT = gfx.text_width("0:00", F3) > DW DW_DING = gfx.text_width("DING!", FDING) end local t = frame * STEP_HZ // FPS gfx.blit_rgb(BG, 0, 0, W, H) local c = t % TOT local on, el, d = false, 0, -1 if c < 12 then on = c >= 4 elseif c < 12 + COOK then on = true; el = c - 12 else d = c - 12 - COOK el = COOK on = d < 5 or (d >= 9 and d < 14) end local li = on and 1 or 2 gfx.blit_rgb(on and WIN_ON or WIN_OFF, IX, IY, IW, IH) gfx.clip(IX, IY, IW, IH) local top, fx = draw_food(OMEGA * el, li) if (el > COOK * 0.55 and d < 0) or (d >= 0 and d < 90) then for k = 1, 3 do local q = (t * 0.5 + k * 13) % 18 local x = floor(fx + (k - 2) * 3 + 1.2 * sin(q * 0.4 + k) + 0.5) blend_rect(x, floor(top - q), 1, 2, 255, 255, 255, 0.5 * (1 - q / 18)) end end for i = 0, 5 do blend_rect(IX + 20 + i, IY + 2 + i, 2, 1, 255, 255, 255, 0.2) end for i = 0, 2 do blend_rect(IX + 26 + i, IY + 2 + i, 1, 1, 255, 255, 255, 0.2) end if d >= 0 and d < 3 then blend_rect(IX, IY, IW, IH, 255, 255, 240, 0.35) end gfx.clip() if c < 6 then rect(45, 41, 13, 3, c3(START_D)) end if c < 12 then show(clock(SECS), DIG) elseif d < 0 then show(clock(math.ceil((COOK - el) / STEP_HZ)), DIG) elseif d < 60 then if d % 10 < 6 then gfx.text(DX, TY, "END", DIG[1], DIG[2], DIG[3], F3C) end else show(clock(0), DIG_D) end if d >= 0 and d < 45 then gfx.text_center(3, "DING!", DING_C[1], DING_C[2], DING_C[3], FDING) if d % 8 < 4 then local lx, rx = 32 - DW_DING // 2 - 6, 32 + (DW_DING + 1) // 2 + 3 rect(lx, 6, 3, 1, c3(DING_C)); px(lx + 1, 3, c3(DING_C)); px(lx + 1, 9, c3(DING_C)) rect(rx, 6, 3, 1, c3(DING_C)); px(rx + 1, 3, c3(DING_C)); px(rx + 1, 9, c3(DING_C)) end end end