info = { type = "image", title = "Bench at dusk", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "Someone on a bench watches the sun set", api = 33, } local tr = ({ sk = { ["Time of day"] = "Denná doba", ["Day to night"] = "Deň a noc", ["Dusk"] = "Súmrak", ["Night"] = "Noc", ["Cycle speed"] = "Rýchlosť cyklu", ["Hat colour"] = "Farba klobúka" }, cs = { ["Time of day"] = "Denní doba", ["Day to night"] = "Den a noc", ["Dusk"] = "Soumrak", ["Night"] = "Noc", ["Cycle speed"] = "Rychlost cyklu", ["Hat colour"] = "Barva klobouku" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "mode", label = T("Time of day"), type = "int", default = 0, opts = { T("Day to night"), T("Dusk"), T("Night") } }, { key = "speed", label = T("Cycle speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "hat", label = T("Hat colour"), type = "color", default = "#E8584E" }, } 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 hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local PI, TAU = 3.14159, 6.28318 local function mix(a, b, t) return { floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5) } end local function c3(c) return c[1], c[2], c[3] end local WHITE = { 255, 255, 255 } local KEY = { 255, 0, 255 } local DT, DH = { 112, 120, 214 }, { 255, 180, 114 } local NT, NH = { 48, 48, 132 }, { 152, 94, 168 } local SKY_ROWS = 45 local DAYC, NIGHTC = {}, {} for y = 0, SKY_ROWS - 1 do local t = y / (SKY_ROWS - 1) DAYC[y], NIGHTC[y] = mix(DT, DH, t), mix(NT, NH, t) end local FAR, FAR_H = { 150, 106, 164 }, { 176, 130, 182 } local MID, MID_H = { 112, 82, 128 }, { 140, 104, 150 } local GROUND, GROUND_L, PATH = { 98, 112, 76 }, { 124, 140, 90 }, { 178, 148, 116 } local HOUSE = { 84, 58, 108 } local WOOD, WOOD2, WOOD_L, WOOD_D = { 172, 112, 70 }, { 150, 96, 62 }, { 204, 144, 92 }, { 108, 70, 48 } local IRON = { 60, 52, 70 } local COAT, COAT_D = { 64, 118, 158 }, { 44, 88, 126 } local HAIR, SKIN = { 124, 78, 58 }, { 244, 198, 160 } local CLOUD_D, CLOUD_N = { 255, 214, 184 }, { 132, 116, 200 } local CLOUD_DU, CLOUD_NU = { 236, 156, 150 }, { 100, 86, 170 } local HAT, HAT_L, SCARF local BG, FG = nil, nil function on_settings() local h = cfg.hat or { r = 232, g = 88, b = 78 } HAT = { h.r, h.g, h.b } HAT_L = mix(HAT, WHITE, 0.4) SCARF = mix(HAT, { 255, 214, 140 }, 0.3) end local FAR_Y, MID_Y = {}, {} for x = 0, W - 1 do FAR_Y[x] = floor(40 + 2.2 * sin(x * 0.09 + 1) + 1.4 * sin(x * 0.21) + 0.5) MID_Y[x] = floor(46 + 2.4 * sin(x * 0.07 + 2) + 1.4 * sin(x * 0.17 + 0.5) + 0.5) end local HOUSES = { 6, 12, 19, 25 } local WIN_X, WIN_Y = {}, {} local function build_bg() gfx.clear(c3(KEY)) for x = 0, W - 1 do local top = FAR_Y[x] rect(x, top, 1, H - top, c3(FAR)) px(x, top, c3(FAR_H)) end for i = 1, #HOUSES do local x = HOUSES[i] local yb = FAR_Y[x + 1] rect(x, yb, 3, 2, c3(HOUSE)) px(x + 1, yb - 1, c3(HOUSE)) WIN_X[i], WIN_Y[i] = x + 1, yb end for x = 0, W - 1 do local top = MID_Y[x] rect(x, top, 1, H - top, c3(MID)) px(x, top, c3(MID_H)) end for x = 0, W - 1 do local top = 50 + floor(1.2 * sin(x * 0.12 + 0.6) + 0.5) rect(x, top, 1, H - top, c3(GROUND)) px(x, top, c3(GROUND_L)) end for x = 0, W - 1 do local top = 59 + floor(sin(x * 0.2) + 0.5) rect(x, top, 1, H - top, c3(PATH)) end BG = gfx.snapshot(0, 0, W, H) gfx.clear(c3(KEY)) rect(14, 56, 31, 1, 80, 98, 66) rect(14, 43, 2, 14, c3(WOOD2)); rect(42, 43, 2, 14, c3(WOOD2)) rect(13, 45, 32, 2, c3(WOOD)); rect(13, 45, 32, 1, c3(WOOD_L)) rect(13, 48, 32, 2, c3(WOOD2)); rect(13, 48, 32, 1, c3(WOOD)) rect(12, 51, 34, 2, c3(WOOD_L)); rect(12, 53, 34, 1, c3(WOOD_D)) rect(50, 26, 2, 31, c3(IRON)); rect(48, 55, 6, 2, c3(IRON)); rect(49, 32, 4, 1, c3(IRON)) rect(48, 19, 6, 1, c3(IRON)); rect(48, 25, 6, 1, c3(IRON)); rect(50, 17, 2, 2, c3(IRON)) rect(48, 20, 1, 5, c3(IRON)); rect(53, 20, 1, 5, c3(IRON)) for _, p in ipairs({ { 2, 57 }, { 4, 56 }, { 6, 58 }, { 58, 59 }, { 60, 57 }, { 62, 58 }, { 36, 60 } }) do rect(p[1], p[2], 1, 3, 70, 96, 64) px(p[1], p[2] - 1, 150, 190, 100) end FG = gfx.snapshot(0, 0, W, H) end local CLOUDS = { { 7, 22, 1.8, 6 }, { 15, 26, -1.3, 38 }, { 23, 16, 1.0, 20 } } local function span(xl, y, w, c, a) local xi = floor(xl) local fx = xl - xi if w > 1 then blend_rect(xi + 1, y, w - 1, 1, c[1], c[2], c[3], a) end blend_rect(xi, y, 1, 1, c[1], c[2], c[3], a * (1 - fx)) if fx > 0.02 then blend_rect(xi + w, y, 1, 1, c[1], c[2], c[3], a * fx) end end local function clouds(t, d) local lit, under = mix(CLOUD_N, CLOUD_D, d), mix(CLOUD_NU, CLOUD_DU, d) for i = 1, #CLOUDS do local c = CLOUDS[i] local y, L = c[1], c[2] local x = (c[4] + t * c[3] / STEP_HZ) % (W + L) - L span(x + 4, y, L - 8, lit, 0.9) span(x, y + 1, L, lit, 0.9) span(x + 2, y + 2, L - 4, under, 0.8) end end local SUN_W = { 2, 3, 4, 5, 5, 5, 5, 4, 3, 2 } local STX, STY, STP = {}, {}, {} for i = 1, 14 do local h = hash32(i * 0x9E3779B1) STX[i], STY[i], STP[i] = umod(h, W), umod(h >> 8, 30), umod(h >> 16, 628) / 100.0 end local function sun(d) if d < 0.12 then return end local sy = 28 + (1 - d) * 22 if sy > 46 then return end local yi = floor(sy) local f = sy - yi local a = d > 0.3 and 1 or (d - 0.12) / 0.18 for k = -8, 8 do local w = floor(sqrt(81 - k * k) + 0.5) blend_rect(46 - w, yi + k, 2 * w + 1, 1, 255, 190, 120, 0.13 * a) end for k = -6, 6 do local w = floor(sqrt(42 - k * k) + 0.5) blend_rect(46 - w, yi + k, 2 * w + 1, 1, 255, 206, 130, 0.17 * a) end for k = 1, 10 do local w = SUN_W[k] local al = (k == 1) and (1 - f) or 1 blend_rect(46 - w, yi - 5 + k, 2 * w, 1, 255, 240, 176, al) end blend_rect(46 - 2, yi + 6, 4, 1, 255, 240, 176, f) end local function stars(t, d) if d > 0.4 then return end local a = (0.4 - d) / 0.3 if a > 1 then a = 1 end for i = 1, 14 do local tw = 0.55 + 0.45 * sin(t * 0.09 + STP[i]) blend_rect(STX[i], STY[i], 1, 1, 240, 240, 255, a * tw) end end local function birds(t, d) if d < 0.35 then return end local per, dur = 14 * STEP_HZ, 7 * STEP_HZ local k = (t + 150) % per if k > dur then return end local lead = -14 + (W + 40) * k / dur local col = mix({ 170, 150, 200 }, { 80, 52, 96 }, (d - 0.35) / 0.65) for i = 0, 2 do local bx = lead - i * 6 local by = 12 + i * 3 + 1.4 * sin(t * 0.05 + i) if bx > -3 and bx < W + 3 then local xi = floor(bx) local fx = bx - xi local yi = floor(by + 0.5) local up = ((t + i * 3) // 5) % 2 == 0 blend_rect(xi, yi, 1, 1, col[1], col[2], col[3], 1 - fx) blend_rect(xi + 1, yi, 1, 1, col[1], col[2], col[3], fx) local wy = up and yi - 1 or yi + 1 px(xi - 1, wy, c3(col)); px(xi + 2, wy, c3(col)) end end end local CXP = 29 local PERIODS = { 70, 52, 40, 28, 18 } local HEADW = { 5, 8, 9, 9, 9, 9, 8, 7 } local function hrow(xl, y, w, c, a) local xi = floor(xl) local fx = xl - xi if a >= 0.999 then rect(xi + 1, y, w - 1, 1, c3(c)) blend_rect(xi, y, 1, 1, c[1], c[2], c[3], 1 - fx) if fx > 0.02 then blend_rect(xi + w, y, 1, 1, c[1], c[2], c[3], fx) end else blend_rect(xi + 1, y, w - 1, 1, c[1], c[2], c[3], a) blend_rect(xi, y, 1, 1, c[1], c[2], c[3], a * (1 - fx)) if fx > 0.02 then blend_rect(xi + w, y, 1, 1, c[1], c[2], c[3], a * fx) end end end local function person(t, lamp) local lam = 0.5 - 0.5 * cos(t * TAU / (4 * STEP_HZ)) local u = (t % (11 * STEP_HZ)) / (3 * STEP_HZ) local turn = 0 if u < 1 then turn = sin(PI * u) * 2.2 if turn > 1 then turn = 1 end turn = -1.6 * turn * turn * (3 - 2 * turn) end local yT = 42 - 0.9 * lam local yi = floor(yT) local f = yT - yi local TW = { 9, 11, 12, 12, 12, 12, 12 } for i = 1, 7 do local w = TW[i] local x0 = CXP - w // 2 if i == 1 then blend_rect(x0, yi, w, 1, COAT[1], COAT[2], COAT[3], 1 - f) else rect(x0, yi + i - 1, w, 1, c3(COAT)) end end rect(CXP - 6, yi + 2, 2, 5, c3(COAT_D)); rect(CXP + 4, yi + 2, 2, 5, c3(COAT_D)) rect(CXP, yi + 3, 1, 4, c3(COAT_D)) if lamp > 0.05 then blend_rect(CXP + 5, yi + 1, 1, 6, 255, 214, 130, 0.5 * lamp) end local yH = 33 - 0.6 * lam local hy = floor(yH) local hf = yH - hy local hx = CXP + turn for i = 1, 8 do local w = HEADW[i] local c = (i <= 4) and HAT or ((i == 5) and HAT_L or HAIR) hrow(hx - w / 2, hy + i - 1, w, c, (i == 1) and (1 - hf) or 1) end blend_rect(floor(hx), hy - 1, 2, 1, HAT_L[1], HAT_L[2], HAT_L[3], 1 - hf) rect(floor(hx), hy, 2, 1, c3(HAT_L)) if turn < -0.5 then px(floor(hx - 5.5), hy + 6, c3(SKIN)); px(floor(hx - 5.5), hy + 7, c3(SKIN)) elseif turn > 0.5 then px(floor(hx + 4.5), hy + 6, c3(SKIN)); px(floor(hx + 4.5), hy + 7, c3(SKIN)) end local sy = hy + 8 rect(CXP - 4, sy, 9, 2, c3(SCARF)) rect(CXP - 5, sy + 1, 11, 1, c3(SCARF)) px(CXP - 2, sy, c3(HAT)); px(CXP + 1, sy, c3(HAT)); px(CXP + 3, sy + 1, c3(HAT)) for k = 0, 6 do local oy = floor(1.2 * (0.3 + k / 6) * sin(t * 0.17 - k * 0.8) + 0.5) rect(CXP + 6 + k, sy + 1 + oy, 1, 2, c3(k % 2 == 0 and SCARF or HAT)) end end local FFX, FFY, FFP = { 8, 22, 40, 57 }, { 54, 57, 53, 56 }, { 0, 1.9, 3.4, 5.1 } local function lamp_glass(t, lamp) local fl = 1 - 0.05 * sin(t * 0.7) * lamp local c = mix({ 176, 158, 142 }, { 255, 228, 132 }, lamp) rect(49, 20, 4, 5, c3(c)) if lamp < 0.05 then return end for k = -9, 9 do local w = floor(sqrt(90 - k * k) + 0.5) blend_rect(51 - w, 22 + k, 2 * w + 1, 1, 255, 220, 130, 0.06 * lamp * fl) end for k = -5, 5 do local w = floor(sqrt(30 - k * k) + 0.5) blend_rect(51 - w, 22 + k, 2 * w + 1, 1, 255, 226, 140, 0.11 * lamp * fl) end blend_rect(40, 57, 24, 1, 255, 220, 130, 0.10 * lamp) blend_rect(37, 58, 27, 1, 255, 220, 130, 0.10 * lamp) blend_rect(41, 59, 23, 1, 255, 220, 130, 0.08 * lamp) end local function windows(night) if night < 0.05 then return end for i = 1, #WIN_X do local on = (night * 1.4 - (i - 1) * 0.1) if on > 1 then on = 1 end if on > 0.05 then blend_rect(WIN_X[i], WIN_Y[i], 1, 1, 255, 214, 120, on) end end end local function fireflies(t, night) if night < 0.4 then return end for i = 1, 4 do local x = FFX[i] + 3 * sin(t * 0.021 + FFP[i]) local y = FFY[i] + 2.2 * sin(t * 0.033 + FFP[i] * 2) local a = (0.5 + 0.5 * sin(t * 0.11 + FFP[i])) * (night - 0.4) / 0.6 if a > 0.05 then local xi, yi = floor(x), floor(y) local fx, fy = x - xi, y - yi blend_rect(xi, yi, 1, 1, 220, 255, 120, a * (1 - fx) * (1 - fy)) blend_rect(xi + 1, yi, 1, 1, 220, 255, 120, a * fx * (1 - fy)) blend_rect(xi, yi + 1, 1, 1, 220, 255, 120, a * (1 - fx) * fy) blend_rect(xi + 1, yi + 1, 1, 1, 220, 255, 120, a * fx * fy) end end end function render() if not BG then build_bg() end local t = frame * STEP_HZ // FPS local mode = cfg.mode or 0 local d if mode == 1 then d = 0.5 elseif mode == 2 then d = 0.0 else local P = PERIODS[cfg.speed or 3] or 40 d = 0.5 + 0.5 * cos(TAU * (t / (P * STEP_HZ) + 0.215)) end local night = 1 - d local lamp = (0.58 - d) / 0.26 if lamp < 0 then lamp = 0 elseif lamp > 1 then lamp = 1 end lamp = lamp * lamp * (3 - 2 * lamp) for y = 0, SKY_ROWS - 1 do local a, b = DAYC[y], NIGHTC[y] rect(0, y, W, 1, floor(b[1] + (a[1] - b[1]) * d + 0.5), floor(b[2] + (a[2] - b[2]) * d + 0.5), floor(b[3] + (a[3] - b[3]) * d + 0.5)) end stars(t, d) sun(d) clouds(t, d) gfx.blit_rgb(BG, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) windows(night) birds(t, d) person(t, lamp) gfx.blit_rgb(FG, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) lamp_glass(t, lamp) fireflies(t, night) end