info = { type = "image", title = "Train coffee", category = "food", author = "BRRRO", version = "1.0", license = "MIT", desc = "Coffee ripples as the train rolls by", api = 33, } local tr = ({ sk = { ["Train speed"] = "Rýchlosť vlaku", ["Tunnels"] = "Tunely" }, cs = { ["Train speed"] = "Rychlost vlaku", ["Tunnels"] = "Tunely" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Train speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "tunnels", label = T("Tunnels"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, sqrt, abs = math.floor, math.sin, math.sqrt, math.abs local hash32 = math.hash32 local W, H = gfx.W, gfx.H local KR, KG, KB = 255, 0, 255 local WX0, WX1, WY0, WY1 = 3, 61, 5, 38 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 box(x, y, w, h, c) rect(x, y, w, h, c[1], c[2], c[3]) end local SKY0, SKY1 = { 84, 154, 232 }, { 178, 216, 242 } local FAR, MID = { 148, 178, 210 }, { 64, 120, 88 } local FIELD, FIELD_D = { 138, 174, 84 }, { 112, 150, 70 } local WOOD, WOOD_D, WOOD_L = { 112, 76, 52 }, { 70, 46, 34 }, { 156, 112, 76 } local TAB0, TAB1 = { 172, 120, 76 }, { 124, 82, 54 } local CER, CER_L, CER_D, BAND = { 228, 230, 240 }, { 252, 252, 255 }, { 170, 176, 196 }, { 70, 108, 186 } local SKIN, SKIN_D, SKIN_L, SLEEVE = { 238, 188, 152 }, { 196, 138, 108 }, { 252, 220, 196 }, { 50, 70, 104 } local COF, RING = { 70, 40, 26 }, { 196, 136, 88 } local SKYBG, INT, MUG local function bake_sky() for y = WY0, WY1 - 1 do if y < 29 then rect(0, y, W, 1, mix(SKY0, SKY1, (y - WY0) / 24)) else rect(0, y, W, 1, mix(FIELD, FIELD_D, (y - 29) / 9)) end end for y = -4, 4 do local hw = floor(sqrt(25 - y * y) + 0.5) rect(50 - hw, 12 + y, 2 * hw + 1, 1, 255, 250, 224) end SKYBG = gfx.snapshot(0, WY0, W, WY1 - WY0) end local function bake_int() rect(0, 0, W, H, KR, KG, KB) box(0, 0, W, 5, WOOD_D); rect(0, 4, W, 1, 50, 32, 24) box(0, 5, 3, 36, WOOD); box(61, 5, 3, 36, WOOD); box(40, 5, 3, 33, WOOD) rect(2, 5, 1, 33, 84, 56, 40); rect(40, 5, 1, 33, 148, 104, 72) rect(0, 38, W, 4, WOOD_L[1], WOOD_L[2], WOOD_L[3]); rect(0, 38, W, 1, 196, 150, 108) for y = 42, 63 do rect(0, y, W, 1, mix(TAB0, TAB1, (y - 42) / 21)) end rect(0, 42, W, 1, 104, 66, 44) for i = 1, 26 do local v = hash32(i * 0x9E3779B1) rect(v % 56, 44 + (v >> 8) % 19, 4 + (v >> 16) % 8, 1, 150, 100, 64) end INT = gfx.snapshot(0, 0, W, H) end local MUG_Y = 44 local function bake_mug() rect(0, 0, W, H, KR, KG, KB) for y = 50, 63 do local hw = 12 - (y - 50) // 5 rect(32 - hw, y, 2 * hw + 1, 1, CER[1], CER[2], CER[3]) rect(32 + hw - 3, y, 4, 1, CER_D[1], CER_D[2], CER_D[3]) rect(32 - hw + 2, y, 1, 1, CER_L[1], CER_L[2], CER_L[3]) end rect(21, 57, 22, 2, BAND[1], BAND[2], BAND[3]) for y = 46, 54 do local dy = (y - 50) / 4.5 local hw = floor(12 * sqrt(1 - dy * dy) + 0.5) rect(32 - hw, y, 2 * hw + 1, 1, CER_L[1], CER_L[2], CER_L[3]) end for y = 47, 53 do local dy = (y - 50) / 3.2 local hw = floor(10 * sqrt(1 - dy * dy) + 0.5) rect(32 - hw, y, 2 * hw + 1, 1, KR, KG, KB) end local function mx(x, w, s) return s < 0 and x or 64 - x - w end for s = -1, 1, 2 do rect(mx(10, 11, s), 55, 11, 9, SKIN[1], SKIN[2], SKIN[3]) rect(mx(11, 9, s), 54, 9, 1, SKIN_L[1], SKIN_L[2], SKIN_L[3]) rect(mx(13, 1, s), 56, 1, 8, SKIN_D[1], SKIN_D[2], SKIN_D[3]) rect(mx(17, 7, s), 51, 7, 3, SKIN[1], SKIN[2], SKIN[3]) rect(mx(17, 7, s), 53, 7, 1, SKIN_D[1], SKIN_D[2], SKIN_D[3]) px(mx(23, 1, s), 51, SKIN_L[1], SKIN_L[2], SKIN_L[3]) for k = 0, 3 do rect(mx(19, 6, s), 54 + k * 3, 6, 2, SKIN[1], SKIN[2], SKIN[3]) rect(mx(19, 6, s), 56 + k * 3, 6, 1, SKIN_D[1], SKIN_D[2], SKIN_D[3]) px(mx(24, 1, s), 54 + k * 3, SKIN_L[1], SKIN_L[2], SKIN_L[3]) end rect(mx(0, 11, s), 58, 11, 6, SLEEVE[1], SLEEVE[2], SLEEVE[3]) rect(mx(0, 11, s), 58, 11, 1, 82, 104, 148) end MUG = gfx.snapshot(0, MUG_Y, W, 20) end local HT = {} for k = 0, 63 do HT[k] = 5 + hash32(k * 0x85EBCA6B) % 5 end local function hills(off) for x = WX0, WX1 - 1, 2 do local xw = x + off local yt = 27 - (4 + 3 * sin(xw * 0.09) + 2 * sin(xw * 0.21 + 1)) local iy = floor(yt) local f = yt - iy rect(x, iy + 1, 2, 29 - iy - 1, FAR[1], FAR[2], FAR[3]) blend_rect(x, iy, 2, 1, FAR[1], FAR[2], FAR[3], 1 - f) end end local function treeline(off) off = off % 448 for x = WX0, WX1 - 1 do local xw = x + off local k = floor(xw / 7) local u = (xw - 7 * k) / 7 local h = 3 + (HT[k % 64] - 3) * (1 - abs(2 * u - 1)) local yt = 30 - h local iy = floor(yt) local f = yt - iy rect(x, iy + 1, 1, 30 - iy - 1, MID[1], MID[2], MID[3]) blend(x, iy, MID[1], MID[2], MID[3], 1 - f) end end local function pole(x) rect(x, 7, 2, 31, 52, 42, 44); rect(x - 2, 10, 6, 1, 52, 42, 44); rect(x - 1, 13, 4, 1, 52, 42, 44) end local function house(x) rect(x, 27, 16, 10, 228, 208, 172) for i = 0, 4 do rect(x - 1 + i, 22 + i, 18 - 2 * i, 1, 178, 66, 54) end rect(x + 3, 31, 3, 6, 110, 70, 46); rect(x + 9, 29, 3, 3, 80, 120, 170) end local function ring(cx, cy, r, c) local ry = floor(r * 0.32 + 0.5) for ady = 0, ry do local t = ady / (ry + 0.5) local xo = floor(r * sqrt(1 - t * t) + 0.5) local u = (ady + 1) / (ry + 0.5) local xi = u >= 1 and 0 or floor(r * sqrt(1 - u * u) + 0.5) local w = xo - xi if w < 1 then w = 1 end for s = -1, 1, 2 do local y = cy + s * ady if s < 0 or ady > 0 then if ady == ry then rect(cx - xo, y, 2 * xo + 1, 1, c[1], c[2], c[3]) else rect(cx - xo, y, w, 1, c[1], c[2], c[3]); rect(cx + xo - w + 1, y, w, 1, c[1], c[2], c[3]) end end end end end local function coffee(dy, tau) for y = 47, 53 do local d = (y - 50) / 3.2 local hw = floor(10 * sqrt(1 - d * d) + 0.5) rect(32 - hw, y + dy, 2 * hw + 1, 1, COF[1], COF[2], COF[3]) end rect(26, 47 + dy, 3, 1, 128, 80, 46) for _, t0 in ipairs({ 0, 7 }) do local a = tau - t0 if a >= 0 and a < 16 then local r = 1 + a * 0.56 local m = a / 16 ring(32, 50 + dy, r, { mix(RING, COF, m * 0.9) }) end end end local SPD = { 0.8, 1, 1.3, 1.7, 2.2 } local PER = { 60, 48, 40, 34, 28 } local TUN = 800 function render() if not MUG then bake_sky(); bake_int(); bake_mug() end local sp = SPD[cfg.speed or 3] local ns = frame * 1.2 * sp gfx.blit_rgb(SKYBG, 0, WY0, W, WY1 - WY0) hills(frame * 0.07 * sp) treeline(frame * 0.3 * sp) for x = -(ns % 44), WX1, 44 do pole(floor(x)) end local hx = floor((100 - ns) % 176) - 40 if hx > -17 and hx < WX1 then house(hx) end for x = -(ns % 6), WX1, 6 do rect(floor(x), 33, 1, 5, 96, 74, 52) end rect(0, 34, W, 1, 96, 74, 52) for i = 1, 2 do local x = (i * 31 + frame * 0.04 * i) % 90 - 14 local ix = floor(x) blend_rect(ix + 1, 8 + i * 6, 11, 1, 255, 255, 255, 0.55) blend_rect(ix, 8 + i * 6, 1, 1, 255, 255, 255, 0.55 * (1 - (x - ix))) blend_rect(ix + 12, 8 + i * 6, 1, 1, 255, 255, 255, 0.55 * (x - ix)) end local cover = 0 if cfg.tunnels ~= false then local v = 1.2 * sp local t = frame % TUN local t1, t2 = 300, 300 + floor(58 / v) + 150 local a = t >= t1 and WX1 - v * (t - t1) or WX1 + 4 local b = t >= t2 and WX1 - v * (t - t2) or WX1 + 4 local x0, x1 = a < WX0 and WX0 or a, b > WX1 and WX1 or b if x1 > x0 and a < WX1 then x0, x1 = floor(x0), floor(x1) rect(x0, WY0, x1 - x0, WY1 - WY0, 8, 10, 18) rect(x0, 33, x1 - x0, 5, 22, 26, 38) for lx = -(ns % 22), WX1, 22 do local l = floor(lx) if l >= x0 and l + 3 <= x1 then rect(l, 14, 3, 1, 255, 214, 120); blend_rect(l - 1, 14, 5, 1, 255, 190, 90, 0.35) end end if a >= WX0 and a < WX1 then rect(floor(a), WY0, 4, WY1 - WY0, 98, 92, 88); rect(floor(a), WY0, 1, WY1 - WY0, 138, 130, 122) end if b > WX0 and b <= WX1 then rect(floor(b) - 4, WY0, 4, WY1 - WY0, 98, 92, 88) end cover = (x1 - x0) / (WX1 - WX0) end end gfx.blit_rgb(INT, 0, 0, W, H, KR, KG, KB) local P = PER[cfg.speed or 3] local tau = frame % P local dy = 0 if tau < 4 then dy = ({ 1, 2, 1, 0 })[tau + 1] elseif tau >= 7 and tau < 10 then dy = ({ 1, 1, 0 })[tau - 6] end coffee(dy, tau) gfx.blit_rgb(MUG, 0, MUG_Y + dy, W, 20, KR, KG, KB) if cover > 0 then local a = 0.4 * cover blend_rect(0, 0, W, 5, 0, 0, 0, a); blend_rect(0, 5, 3, 33, 0, 0, 0, a); blend_rect(61, 5, 3, 33, 0, 0, 0, a) blend_rect(40, 5, 3, 33, 0, 0, 0, a); blend_rect(0, 38, W, 26, 0, 0, 0, a) end end