info = { type = "image", title = "Gnome night shift", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A garden gnome waters a tulip when lamp dies", api = 33, } local tr = ({ sk = { ["Hat"] = "Čiapka", ["Lamp light"] = "Svetlo lampy", ["Warm"] = "Teplé", ["Cool"] = "Studené", ["Lime"] = "Limetkové" }, cs = { ["Hat"] = "Čepice", ["Lamp light"] = "Světlo lampy", ["Warm"] = "Teplé", ["Cool"] = "Studené", ["Lime"] = "Limetkové" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "hat", label = T("Hat"), type = "color", default = "#D6222C" }, { key = "lamp", label = T("Lamp light"), type = "int", default = 0, opts = { T("Warm"), T("Cool"), T("Lime") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos = math.floor, math.sin, math.cos local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local BASE = { h = { 214, 34, 44 }, H = { 150, 22, 36 }, s = { 250, 190, 160 }, w = { 244, 244, 236 }, W = { 190, 196, 210 }, b = { 52, 92, 204 }, B = { 36, 36, 70 }, p = { 64, 130, 60 }, t = { 74, 44, 28 }, e = { 30, 20, 24 }, c = { 156, 176, 196 }, C = { 96, 112, 140 }, y = { 250, 214, 90 }, n = { 250, 150, 130 }, } local ROWS = { STAND = { ".........hhh", "........hhhh", "........hhhhh", ".......hhhhhh", "......hhhhhhhh", "......hhhhhhhh", ".....HHHHHHHHHH", ".......ssssss", ".......sesnse", ".......wwwnww", ".......wwwwww", ".......wwwwww", "....bbbwWwwwwbbbb", "....bbbbwwwwWbbbb", "....bbbbbwWbbbbbb", "....bbbBBwwBBBbbb", ".....bbbbbbbbbb", ".....sbbbbbbbbbss", ".......pp..pp", ".......pp..pp", "......ttt..ttt", ".......tt..tt", }, W1 = { ".........hhh", "........hhhh", "........hhhhh", ".......hhhhhh", "......hhhhhhhh", "......hhhhhhhh", ".....HHHHHHHHHH", ".......ssssss", ".......sesnse", ".......wwwnww", ".......wwwwww", ".......wwwwww", "....bbbwWwwwwbbbb", "....bbbbwwwwWbbbb", "....bbbbbwWbbbbbb", "....bbbBBwwBBBbbb", ".....bbbbbbbbbb", ".....sbbbbbbbbbss", ".......pp..pp", "......ttt..pp", "......ttt..ttt", "...........ttt", }, W2 = { ".........hhh", "........hhhh", "........hhhhh", ".......hhhhhh", "......hhhhhhhh", "......hhhhhhhh", ".....HHHHHHHHHH", ".......ssssss", ".......sesnse", ".......wwwnww", ".......wwwwww", ".......wwwwww", "....bbbwWwwwwbbbb", "....bbbbwwwwWbbbb", "....bbbbbwWbbbbbb", "....bbbBBwwBBBbbb", ".....bbbbbbbbbb", ".....sbbbbbbbbbss", ".......pp..pp", ".......pp..ttt", "......ttt..ttt", "......ttt", }, WAVE1 = { ".........hhh", "........hhhh", "........hhhhh", ".......hhhhhh", "......hhhhhhhh.s", "......hhhhhhhh.ss", ".....HHHHHHHHHbb", ".......ssssss.bb", ".......sesnse.bb", ".......wwwnww.bb", ".......wwwwww.bb", ".......wwwwww.bb", "....bbbwWwwwwbb", "....bbbbwwwwWbb", "....bbbbbwWbbbb", "....bbbBBwwBBBB", ".....bbbbbbbbbb", ".....sbbbbbbbbb", ".......pp..pp", ".......pp..pp", "......ttt..ttt", ".......tt..tt", }, WAVE2 = { ".........hhh", "........hhhh", "........hhhhh", ".......hhhhhh", "......hhhhhhhh", "......hhhhhhhh...ss", ".....HHHHHHHHHH.ss", ".......ssssss.bb", ".......sesnse.bb", ".......wwwnww.bb", ".......wwwwww.bb", ".......wwwwww.bb", "....bbbwWwwwwbb", "....bbbbwwwwWbb", "....bbbbbwWbbbb", "....bbbBBwwBBBB", ".....bbbbbbbbbb", ".....sbbbbbbbbb", ".......pp..pp", ".......pp..pp", "......ttt..ttt", ".......tt..tt", }, WATER = { "...............hhh", "..............hhhh", "..............hhhhh", ".............hhhhhh", "............hhhhhhhh", "............hhhhhhhh", "...........HHHHHHHHHH", ".............ssssss", ".............sesnse", ".............wwwnww", ".............wwwwww", ".......CC....wwwwww", "....cccccbbbbwWwwwwbbb", "...cccccc..bbbwwwwWbbb", ".cccccccc..bbbbwWbbbbb", "cc..CCCCC..BBBBwwBBBbb", "C..........bbbbbbbbbb", "...........bbbbbbbbbbs", ".............pp..pp", ".............pp..pp", "............ttt..ttt", ".............tt..tt", }, } local ANCH = { STAND = 10, W1 = 10, W2 = 10, WAVE1 = 10, WAVE2 = 10, WATER = 16 } local LAMPS = { { 255, 214, 120 }, { 206, 232, 255 }, { 204, 255, 140 } } local LAMP = LAMPS[1] local function dk(c) return { floor(c[1] * 0.48 + 6), floor(c[2] * 0.56 + 9), floor(c[3] * 0.78 + 24) } end local PALS = { {}, {} } local BG = {} function on_settings() local h = cfg.hat or { r = 214, g = 34, b = 44 } for k, c in pairs(BASE) do PALS[1][k] = c; PALS[2][k] = dk(c) end PALS[1].h = { h.r, h.g, h.b } PALS[1].H = { h.r * 7 // 10, h.g * 7 // 10, h.b * 7 // 10 } PALS[2].h, PALS[2].H = dk(PALS[1].h), dk(PALS[1].H) LAMP = LAMPS[cfg.lamp + 1] or LAMPS[1] BG = {} end local function runs(rows) local S = { n = 0, x = {}, y = {}, w = {}, c = {}, p = {}, q = {} } for j = 1, #rows do local row = rows[j] local i, prevEnd, prevC = 1, -1, nil while i <= #row do local b = row:byte(i) local k = i while k < #row and row:byte(k + 1) == b do k = k + 1 end if b ~= 46 then local n = S.n + 1 S.n = n local c = string.char(b) S.x[n], S.y[n], S.w[n], S.c[n] = i - 1, j - 1, k - i + 1, c S.p[n] = prevEnd == i - 1 and prevC or false if S.p[n] then S.q[n - 1] = true end prevEnd, prevC = k, c end i = k + 1 end end return S end local SPR = {} local function draw(name, cx, y, pal) local S = SPR[name] if not S then S = runs(ROWS[name]); SPR[name] = S end local x = cx - ANCH[name] local ix = floor(x) local f = x - ix local X, Y, Wd, C, P, Q = S.x, S.y, S.w, S.c, S.p, S.q if f < 0.04 then for i = 1, S.n do local c = pal[C[i]] rect(ix + X[i], y + Y[i], Wd[i], 1, c[1], c[2], c[3]) end return end local g = 1 - f for i = 1, S.n do local c, p = pal[C[i]], P[i] local x0, yy, w = ix + X[i], y + Y[i], Wd[i] if w > 1 then rect(x0 + 1, yy, w - 1, 1, c[1], c[2], c[3]) end if p then p = pal[p] px(x0, yy, floor(p[1] * f + c[1] * g + 0.5), floor(p[2] * f + c[2] * g + 0.5), floor(p[3] * f + c[3] * g + 0.5)) else blend(x0, yy, c[1], c[2], c[3], g) end if not Q[i] then blend(x0 + w, yy, c[1], c[2], c[3], f) end end end local function lerp(a, b, t) return floor(a + (b - a) * t + 0.5) end local FENCE, GRASS, GRASS2, DIRT, IRON = { 120, 84, 56 }, { 58, 112, 58 }, { 44, 88, 46 }, { 84, 56, 40 }, { 30, 30, 40 } local GLASS_OFF = { 52, 58, 84 } local function build_bg(s) local lit = s == 1 local function C(c) if lit then return c[1], c[2], c[3] end local d = dk(c); return d[1], d[2], d[3] end for y = 0, 46 do local t = y / 40 t = t > 1 and 1 or t rect(0, y, W, 1, lerp(14, 44, t), lerp(22, 58, t), lerp(64, 124, t)) end for i = 1, 14 do local h = hash32(i * 0x9E3779B1) px(umod(h, 64), umod(h >> 8, 28), 190, 200, 245) end for y = 5, 18 do for x = 5, 19 do local dx, dy = x - 12, y - 11 local ex, ey = x - 14, y - 10 if dx * dx + dy * dy <= 27 and ex * ex + ey * ey > 22 then px(x, y, 250, 246, 214) end end end for x = 0, W - 1 do local hy = floor(35 + 3 * sin(x * 0.12 + 1) + 2 * sin(x * 0.31)) rect(x, hy, 1, 12, C({ 24, 42, 70 })) end for x = 0, W - 1, 4 do rect(x, 39, 3, 9, C(FENCE)) px(x + 1, 38, C(FENCE)) end rect(0, 41, W, 1, C({ 92, 62, 42 })); rect(0, 45, W, 1, C({ 92, 62, 42 })) rect(0, 47, W, H - 47, C(GRASS)) rect(0, 58, W, 6, C(GRASS2)) for i = 1, 30 do local h = hash32(i * 0x85EBCA6B) local x, y = umod(h, 64), 48 + umod(h >> 8, 14) px(x, y, C({ 78, 138, 70 })); px(x, y - 1, C({ 78, 138, 70 })) end rect(8, 50, 14, 3, C(DIRT)) if lit then for lv = 1, 3 do local rx, ry = 10 + lv * 6, 2 + lv * 2 local m = 0.14 * (4 - lv) local c1, c2, c3 = lerp(GRASS[1], LAMP[1], m), lerp(GRASS[2], LAMP[2], m), lerp(GRASS[3], LAMP[3], m) for dy = -ry, ry do local w = floor(rx * math.sqrt(1 - (dy / (ry + 0.5)) ^ 2) + 0.5) if 56 + dy >= 47 then rect(57 - w, 56 + dy, 2 * w, 1, c1, c2, c3) end end end blend_rect(40, 38, 24, 9, LAMP[1], LAMP[2], LAMP[3], 0.2) end rect(56, 33, 2, 21, C(IRON)) rect(54, 52, 6, 2, C(IRON)) rect(52, 24, 10, 1, C(IRON)); rect(54, 22, 6, 2, C(IRON)); rect(56, 21, 2, 1, C(IRON)) rect(52, 25, 1, 9, C(IRON)); rect(61, 25, 1, 9, C(IRON)); rect(52, 33, 10, 1, C(IRON)) if lit then rect(53, 25, 8, 8, LAMP[1], LAMP[2], LAMP[3]) rect(55, 27, 4, 4, 255, 252, 236) else rect(53, 25, 8, 8, GLASS_OFF[1], GLASS_OFF[2], GLASS_OFF[3]) end BG[s] = gfx.snapshot(0, 0, W, H) end local function tulip(b, lit) local function C(c) if lit then return c[1], c[2], c[3] end local d = dk(c); return d[1], d[2], d[3] end local h = 4 + floor(3 * b + 0.5) rect(14, 51 - h, 1, h, C({ 60, 150, 60 })) px(13, 49, C({ 60, 150, 60 })); px(12, 48, C({ 60, 150, 60 })); px(15, 48, C({ 60, 150, 60 })); px(16, 47, C({ 60, 150, 60 })) local top = 51 - h local P1, P2 = { 230, 60, 90 }, { 255, 130, 150 } if b < 0.3 then rect(13, top - 2, 3, 3, C(P1)); px(14, top - 3, C(P1)) elseif b < 0.65 then rect(12, top - 3, 5, 4, C(P1)); rect(13, top - 3, 3, 1, C(P2)); px(14, top - 4, C(P1)) else rect(11, top - 2, 7, 3, C(P1)); rect(12, top - 4, 5, 2, C(P2)) px(11, top - 3, C(P1)); px(17, top - 3, C(P1)); rect(13, top - 2, 3, 1, C({ 250, 214, 90 })) end end local FROG, FROG_L = { 80, 170, 70 }, { 150, 214, 110 } local function frog(lit, t) local function C(c) if lit then return c[1], c[2], c[3] end local d = dk(c); return d[1], d[2], d[3] end local dy = 0 if t >= 276 and t < 292 then local u = (t - 276) / 16; dy = -floor(4 * 4 * u * (1 - u) + 0.5) end local y = 55 + dy rect(48, y, 8, 3, C(FROG)); rect(49, y - 1, 6, 1, C(FROG)); rect(47, y + 3, 10, 1, C(FROG)) rect(48, y + 1, 8, 2, C(FROG_L)); rect(48, y, 8, 1, C(FROG)) rect(48, y - 2, 2, 2, C(FROG)); rect(54, y - 2, 2, 2, C(FROG)) local shut = t % 100 < 3 if not shut then px(49, y - 2, 20, 20, 30); px(55, y - 2, 20, 20, 30) end if t >= 262 and t < 298 and (t // 6) % 2 == 0 then rect(56, y - 3, 1, 3, C(FROG)); px(57, y - 4, C(FROG)) end if t >= 262 and t < 298 and t % 12 < 6 then rect(50, y + 1, 4, 1, 240, 240, 230) end end local LOOP = 420 local X0, XW, XV = 38, 30, 34 local function sstep(u) u = u < 0 and 0 or u > 1 and 1 or u; return u * u * (3 - 2 * u) end local SX, SY, SP = {}, {}, {} for i = 1, 8 do local h = hash32(i * 0xC2B2AE35) SX[i], SY[i], SP[i] = umod(h, 64), umod(h >> 8, 30), umod(h >> 16, 628) / 100 end function render() local t = frame % LOOP local lit = t < 90 or (t >= 93 and t < 95) or (t >= 300 and t < 303) or t >= 306 local s = lit and 1 or 2 if not BG[s] then build_bg(s) end gfx.blit_rgb(BG[s], 0, 0, W, H) local pal = PALS[s] for i = 1, 8 do local a = 0.35 + 0.35 * sin(frame * 0.05 + SP[i]) blend(SX[i], SY[i], 220, 230, 255, a) end local b = 0 if t >= 150 and t < 225 then b = sstep((t - 150) / 75) elseif t >= 225 then b = 1 - (t - 225) / 195 end tulip(b, lit) frog(lit, t) local pose, cx = "STAND", X0 if t >= 105 and t < 140 then cx = X0 + (XW - X0) * sstep((t - 105) / 35) pose = (t // 5) % 2 == 0 and "W1" or "W2" elseif t >= 140 and t < 150 then cx = XW elseif t >= 150 and t < 225 then cx, pose = XW, "WATER" elseif t >= 225 and t < 235 then cx = XW elseif t >= 235 and t < 250 then cx = XW + (XV - XW) * sstep((t - 235) / 15) pose = (t // 5) % 2 == 0 and "W1" or "W2" elseif t >= 250 and t < 306 then cx = XV pose = (t // 8) % 2 == 0 and "WAVE1" or "WAVE2" end if t >= 300 and t < 306 then pose = "WAVE1" end if t >= 303 and t < 306 then cx, pose = X0, "STAND" end draw(pose, cx, 31, pal) if pose == "WATER" and t >= 160 and t < 220 then local tx = cx - 15 for j = 0, 3 do local ph = (t * 0.12 + j / 4) % 1 px(tx - floor(ph * 1.5), 47 + floor(ph * ph * 5), 170, 215, 255) end if t % 4 < 2 then px(tx - 2, 51, 200, 230, 255); px(tx + 1, 51, 200, 230, 255) end end if lit then for j = 1, 3 do local a = frame * (0.07 + j * 0.015) + j * 2 px(57 + floor(8 * cos(a) + 0.5), 29 + floor(5 * sin(a * 1.3) + 0.5), 255, 240, 200) end end end