info = { type = "image", title = "Tractor plough", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Tractor ploughs a field, gulls follow", api = 33, } local tr = ({ sk = { ["Tractor"] = "Traktor", ["Red"] = "Červený", ["Green"] = "Zelený", ["Blue"] = "Modrý", ["Time of day"] = "Denná doba", ["Day"] = "Deň", ["Dusk"] = "Súmrak" }, cs = { ["Tractor"] = "Traktor", ["Red"] = "Červený", ["Green"] = "Zelený", ["Blue"] = "Modrý", ["Time of day"] = "Denní doba", ["Day"] = "Den", ["Dusk"] = "Soumrak" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "paint", label = T("Tractor"), type = "int", default = "red", opts = { keys = { "red", "green", "blue" }, T("Red"), T("Green"), T("Blue") } }, { key = "tod", label = T("Time of day"), type = "int", default = "day", opts = { keys = { "day", "dusk" }, T("Day"), T("Dusk") } }, } 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 = math.hash32 local W, H = gfx.W, gfx.H local HZ = 34 local TW, TH = 36, 26 local GROUND = 60 local TY = GROUND - TH + 1 local CYC, DRIVE, OFF = 540, 400, 172 local KEY = { 255, 0, 255 } local TROWS = {} local PAINT = { red = { { 212, 52, 38 }, { 244, 100, 72 }, { 140, 28, 26 } }, green = { { 52, 150, 62 }, { 108, 198, 104 }, { 28, 92, 44 } }, blue = { { 46, 98, 190 }, { 100, 154, 238 }, { 24, 58, 126 } }, } local DAY = { sky = { { 0, 66, 134, 214 }, { 1, 178, 216, 240 } }, hill1 = { 118, 158, 150 }, hill2 = { 84, 134, 96 }, barn = { 176, 62, 48 }, roof = { 96, 70, 64 }, silo = { 196, 200, 204 }, tree = { 52, 104, 64 }, far = { 118, 164, 98 }, crop = { { 108, 168, 66 }, { 92, 150, 56 }, { 80, 134, 50 }, { 92, 150, 56 } }, soil = { { 160, 108, 66 }, { 122, 80, 48 }, { 82, 52, 30 }, { 122, 80, 48 } }, cloud = { 250, 250, 252 }, smoke = { 120, 120, 124 }, glass = { 168, 206, 226 }, } local DUSK = { sky = { { 0, 40, 42, 102 }, { 0.5, 150, 84, 118 }, { 0.82, 236, 140, 90 }, { 1, 252, 196, 112 } }, hill1 = { 96, 70, 104 }, hill2 = { 62, 48, 80 }, barn = { 120, 52, 52 }, roof = { 62, 46, 58 }, silo = { 132, 120, 136 }, tree = { 36, 52, 58 }, far = { 92, 112, 70 }, crop = { { 92, 120, 56 }, { 78, 104, 48 }, { 66, 90, 42 }, { 78, 104, 48 } }, soil = { { 150, 92, 60 }, { 112, 68, 44 }, { 74, 44, 32 }, { 112, 68, 44 } }, cloud = { 255, 190, 170 }, smoke = { 148, 116, 120 }, glass = { 224, 184, 160 }, } local P, PT = DAY, PAINT.red local dusk = false local function cl(v) v = floor(v + 0.5); return v < 0 and 0 or v > 255 and 255 or v end local function grad(st, t) for i = 2, #st do local a, b = st[i - 1], st[i] if t <= b[1] then local k = (t - a[1]) / (b[1] - a[1]) return cl(a[2] + (b[2] - a[2]) * k), cl(a[3] + (b[3] - a[3]) * k), cl(a[4] + (b[4] - a[4]) * k) end end local l = st[#st] return l[2], l[3], l[4] end local function fdisc(cx, cy, r, c, a, ymax) for y = floor(cy - r + 0.5), floor(cy + r - 0.5) do if not ymax or y < ymax then local dy = y + 0.5 - cy local hw = sqrt(r * r - dy * dy) local x0, x1 = floor(cx - hw + 0.5), floor(cx + hw + 0.5) if a then blend_rect(x0, y, x1 - x0, 1, c[1], c[2], c[3], a) else rect(x0, y, x1 - x0, 1, c[1], c[2], c[3]) end end end end local function field_rows(y0, y1, cols, amp, seed) for y = y0, y1 do local c = cols[(y - y0) % #cols + 1] local x, k = 0, 0 while x < W do k = k + 1 local h = hash32(y * 7919 + k * 104729 + seed) & 0x7FFF local len = 4 + h % 9 local m = 1 + ((h >> 4) % 11 - 5) * amp / 5 rect(x, y, len, 1, cl(c[1] * m), cl(c[2] * m), cl(c[3] * m)) x = x + len end end end local A, B, TR0, TQ local stage = 0 local ROWAVG = {} local function build_scene() for y = 0, HZ - 1 do rect(0, y, W, 1, grad(P.sky, y / (HZ - 1))) end if dusk then fdisc(44, HZ - 5, 11, { 244, 150, 100 }, 0.22, HZ) fdisc(44, HZ - 5, 7, { 255, 226, 150 }, nil, HZ) else fdisc(15, 9, 7, { 214, 232, 248 }, 0.4) fdisc(15, 9, 4.5, { 255, 248, 214 }) end for x = 0, W - 1 do local t1 = HZ - 6 + floor(2 * sin(x * 0.09) + 1.5 * sin(x * 0.23 + 1) + 0.5) rect(x, t1, 1, HZ - t1, P.hill1[1], P.hill1[2], P.hill1[3]) local t2 = HZ - 3 + floor(1.5 * sin(x * 0.13 + 2) + 0.5) rect(x, t2, 1, HZ - t2, P.hill2[1], P.hill2[2], P.hill2[3]) end rect(7, HZ - 7, 9, 6, P.barn[1], P.barn[2], P.barn[3]) rect(6, HZ - 8, 11, 1, P.roof[1], P.roof[2], P.roof[3]) rect(8, HZ - 9, 7, 1, P.roof[1], P.roof[2], P.roof[3]) rect(18, HZ - 10, 2, 9, P.silo[1], P.silo[2], P.silo[3]) for _, x in ipairs({ 50, 54, 58 }) do rect(x, HZ - 9, 2, 8, P.tree[1], P.tree[2], P.tree[3]) rect(x - 1, HZ - 7, 4, 5, P.tree[1], P.tree[2], P.tree[3]) end end local function build_field() rect(0, HZ, W, 7, P.far[1], P.far[2], P.far[3]) field_rows(HZ + 1, HZ + 6, { P.far, { P.far[1] * 0.93, P.far[2] * 0.93, P.far[3] * 0.93 } }, 0.05, 11) field_rows(HZ + 7, H - 1, P.crop, 0.07, 23) for y = HZ, H - 1 do local c = P.crop[(y - HZ - 7) % 4 + 1] if y < HZ + 7 then c = P.far end ROWAVG[y] = c end A = gfx.snapshot(0, 0, W, H) end local function build_soil() field_rows(HZ + 1, HZ + 6, { P.soil[1], P.soil[2] }, 0.05, 5) field_rows(HZ + 7, H - 1, P.soil, 0.09, 37) B = gfx.snapshot(0, HZ, W, H - HZ) end local function build_tractor() local k = dusk and 0.8 or 1 local function dim(c) return { c[1] * k, c[2] * k * 0.95, c[3] * k * 0.95 } end local M, HI, LO = dim(PT[1]), dim(PT[2]), dim(PT[3]) local TIRE, STEEL, STEEL_D, DK = { 24, 24, 28 }, { 150, 154, 160 }, { 78, 80, 88 }, { 70, 70, 76 } local function r(x, y, w, h, c) rect(x, y, w, h, c[1], c[2], c[3]) end r(0, 0, TW, TH, KEY) for i = 0, 4 do px(7 - i, 18 + i, DK[1], DK[2], DK[3]) end r(5, 16, 3, 2, DK) r(0, 22, 4, 4, STEEL_D); r(0, 22, 4, 1, STEEL); r(3, 22, 1, 4, STEEL) r(7, 0, 16, 1, HI); r(7, 1, 16, 1, M) r(8, 2, 2, 6, M); r(20, 2, 2, 6, M); r(10, 2, 10, 5, P.glass) px(11, 5, 255, 255, 255); px(12, 4, 255, 255, 255); px(13, 3, 255, 255, 255) r(14, 6, 3, 1, { 60, 60, 72 }) r(21, 11, 13, 9, M); r(21, 11, 13, 1, HI); r(21, 18, 13, 2, LO) r(24, 14, 6, 1, LO); r(24, 16, 6, 1, LO) r(33, 12, 1, 7, DK); r(33, 13, 1, 2, { 255, 236, 170 }) r(29, 5, 2, 6, DK); r(28, 4, 4, 1, { 98, 98, 106 }) fdisc(16.5, 16.5, 9, TIRE); fdisc(16.5, 16.5, 5, M); fdisc(16.5, 16.5, 2, LO) fdisc(30.5, 21.5, 4.5, TIRE); fdisc(30.5, 21.5, 2.2, M) r(8, 7, 18, 2, M); r(9, 7, 16, 1, HI); r(8, 9, 1, 1, LO) TR0 = gfx.snapshot(0, 0, TW, TH) TQ = { [0] = TR0 } TROWS = {} end local function tphase(q, y0, y1) local a, rows = q / 4, TROWS[q] or {} TROWS[q] = rows local k0, k1, k2 = KEY[1], KEY[2], KEY[3] for y = y0, y1 do local b = { TR0:byte(y * TW * 3 + 1, (y + 1) * TW * 3) } local o, n = {}, 0 for x = 0, TW do local i, j = (x - 1) * 3 + 1, x * 3 + 1 local r1, g1, b1, r2, g2, b2 = b[i], b[i + 1], b[i + 2], b[j], b[j + 1], b[j + 2] local lk = x == 0 or (r1 == k0 and g1 == k1 and b1 == k2) local ck = x == TW or (r2 == k0 and g2 == k1 and b2 == k2) if lk and ck then r2, g2, b2 = k0, k1, k2 elseif lk then elseif ck then r2, g2, b2 = r1, g1, b1 elseif r1 ~= r2 or g1 ~= g2 or b1 ~= b2 then r2, g2, b2 = floor(r1 * a + r2 * (1 - a) + 0.5), floor(g1 * a + g2 * (1 - a) + 0.5), floor(b1 * a + b2 * (1 - a) + 0.5) end o[n + 1], o[n + 2], o[n + 3] = r2, g2, b2 n = n + 3 end rows[y + 1] = string.char(table.unpack(o, 1, n)) end if y1 == TH - 1 then TQ[q] = table.concat(rows); TROWS[q] = nil end end function on_settings() dusk = cfg.tod == "dusk" P = dusk and DUSK or DAY PT = PAINT[cfg.paint] or PAINT.red stage = 0 end local GW, GG, GB, GL = { 244, 244, 240 }, { 150, 156, 164 }, { 240, 170, 60 }, { 220, 150, 70 } local function gull(x, y, pose) x, y = floor(x + 0.5), floor(y + 0.5) rect(x + 1, y + 2, 5, 2, GW[1], GW[2], GW[3]) px(x, y + 2, GW[1], GW[2], GW[3]) if pose == 4 then rect(x + 1, y + 2, 4, 1, GG[1], GG[2], GG[3]) px(x + 6, y + 1, GW[1], GW[2], GW[3]); px(x + 7, y + 1, GB[1], GB[2], GB[3]) px(x + 3, y + 4, GL[1], GL[2], GL[3]); px(x + 4, y + 4, GL[1], GL[2], GL[3]) elseif pose == 5 then rect(x + 1, y + 2, 4, 1, GG[1], GG[2], GG[3]) px(x + 6, y + 3, GW[1], GW[2], GW[3]); px(x + 7, y + 4, GB[1], GB[2], GB[3]) px(x + 3, y + 4, GL[1], GL[2], GL[3]); px(x + 4, y + 4, GL[1], GL[2], GL[3]) else px(x + 6, y + 1, GW[1], GW[2], GW[3]); px(x + 7, y + 1, GB[1], GB[2], GB[3]) if pose == 0 then rect(x + 3, y, 1, 2, GW[1], GW[2], GW[3]); rect(x + 2, y - 1, 1, 2, GW[1], GW[2], GW[3]); px(x + 1, y - 2, GG[1], GG[2], GG[3]) elseif pose == 1 or pose == 3 then rect(x + 2, y + 1, 2, 1, GW[1], GW[2], GW[3]); px(x + 1, y, GW[1], GW[2], GW[3]); px(x, y - 1, GG[1], GG[2], GG[3]) else rect(x, y + 4, 4, 1, GW[1], GW[2], GW[3]); px(x - 1, y + 5, GG[1], GG[2], GG[3]) end end end local LX = { 10, 24, 38, 52 } local LY = { 56, 60, 54, 58 } local function trail(i, u) local ix = u / 4 - 36 local fx = ix - 12 - 11 * i if fx > 64 then fx = 64 end return fx, 22 + (i % 3) * 3 + 2.5 * sin(u * 0.07 + i * 1.7) end local function draw_gulls(u) for i = 1, 4 do local lx, ly = LX[i], LY[i] local tl = (lx + 10 + 36) * 4 local te = DRIVE + 50 + 16 * i local flap = ((u // 3) + i) % 4 if u < tl then local fx, fy = trail(i, u) if fx > -8 then gull(fx, fy, flap) end elseif u < tl + 45 then local p = (u - tl) / 45 local sx, sy = trail(i, tl) local e = p * p * (3 - 2 * p) gull(sx + (lx - sx) * e, sy + (ly - sy) * e, p < 0.8 and flap or 4) elseif u < te then gull(lx, ly, ((u + i * 23) % 70 < 4) and 5 or 4) else local p = (u - te) / 60 local x, y = lx + p * 70, ly - p * 20 - p * p * 40 if x < 70 and y > -8 then gull(x, y, flap) end end end end local CLOUDS = { { 40, 8, 16, 0.020 }, { 4, 17, 11, 0.012 }, { 22, 22, 9, 0.016 } } local function draw_clouds(f) local c = P.cloud for _, k in ipairs(CLOUDS) do local w = k[3] local x = floor((k[1] + f * k[4]) % (W + w + 8) - w) blend_rect(x + 2, k[2], w - 4, 1, c[1], c[2], c[3], 0.7) blend_rect(x, k[2] + 1, w, 2, c[1], c[2], c[3], 0.8) blend_rect(x + 3, k[2] + 3, w - 6, 1, c[1], c[2], c[3], 0.5) end end local function draw_smoke(u) local c = P.smoke for k = 0, 7 do local e = u // 14 - k local te = e * 14 local age = u - te if te >= 0 and te < DRIVE and age < 112 then local x = te / 4 - 36 + 30 - age * 0.03 local y = TY + 3 - age * 0.17 local r = 1 + age / 38 local fade = 1 - age / 112 if x < 70 then blend_rect(floor(x - r), floor(y - r), floor(2 * r), floor(2 * r), c[1], c[2], c[3], 0.4 * fade) blend_rect(floor(x - r / 2), floor(y - r / 2), floor(r), floor(r), c[1], c[2], c[3], 0.35 * fade) end end end end local function draw_marks(ix, xf) local th = xf / 8.5 for k = 0, 7 do local a = th + k * 0.7854 px(floor(ix + 16.5 + 7.2 * cos(a)), floor(TY + 16.5 + 7.2 * sin(a)), 70, 70, 76) end th = xf / 4.5 for k = 0, 5 do local a = th + k * 1.0472 px(floor(ix + 30.5 + 3.4 * cos(a)), floor(TY + 21.5 + 3.4 * sin(a)), 70, 70, 76) end end function render() if stage < 10 then stage = stage + 1 if stage == 1 then build_scene() elseif stage == 2 then build_field() elseif stage == 3 then build_soil() elseif stage == 4 then build_tractor() else local k = stage - 5 tphase(k // 2 + 1, (k % 2) * 13, (k % 2) * 13 + 12) end if A then gfx.blit_rgb(A, 0, 0, W, H) end return end local u = (frame + OFF) % CYC gfx.blit_rgb(A, 0, 0, W, H) draw_clouds(frame) local xf = u / 4 - 36 local ix = floor(xf) if u < DRIVE then local cw = ix + 3 if cw > 0 then gfx.clip(0, HZ, cw > W and W or cw, H - HZ) gfx.blit_rgb(B, 0, HZ, W, H - HZ) gfx.clip() end else gfx.blit_rgb(B, 0, HZ, W, H - HZ) if u > DRIVE + 40 then local a = (u - DRIVE - 40) / 100 * 0.85 if a > 0.85 then a = 0.85 end for y = HZ, H - 1 do local c = ROWAVG[y] blend_rect(0, y, W, 1, c[1], c[2], c[3], a) end end end if u < DRIVE then local q = u % 4 gfx.blit_rgb(TQ[q], ix, TY, q == 0 and TW or TW + 1, TH, KEY[1], KEY[2], KEY[3]) draw_marks(ix, xf) end draw_smoke(u) draw_gulls(u) end