info = { type = "image", title = "Crane at sunset", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Crane swings a steel beam over a dusk site", api = 44, } local tr = ({ sk = { ["Sky"] = "Obloha", ["Amber"] = "Jantárová", ["Rose"] = "Ružová", ["Violet"] = "Fialová", ["Warning lights"] = "Výstražné svetlá" }, cs = { ["Sky"] = "Obloha", ["Amber"] = "Jantarová", ["Rose"] = "Růžová", ["Violet"] = "Fialová", ["Warning lights"] = "Výstražná světla" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "sky", label = T("Sky"), type = "int", default = 0, opts = { T("Amber"), T("Rose"), T("Violet") } }, { key = "lights", label = T("Warning lights"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, sqrt, abs, min = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.min local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local SKIES = { { { 96, 54, 120 }, { 214, 98, 102 }, { 255, 168, 84 } }, { { 110, 56, 128 }, { 230, 104, 128 }, { 255, 176, 120 } }, { { 54, 56, 126 }, { 150, 80, 150 }, { 250, 140, 110 } }, } local HAZE, NEAR, FG, GROUND = { 168, 84, 100 }, { 96, 46, 88 }, { 40, 24, 50 }, { 18, 8, 26 } local CRANE_FAR, RIM = { 128, 64, 96 }, { 176, 92, 84 } local STEEL, STEEL_LIT = { 96, 66, 84 }, { 236, 150, 84 } local PAL, LIGHTS = SKIES[1], true local HALF, G, JY, TRAVEL_L, HOOK_UP = 420, 100, 16, 12, 8 local SPOT = { { x = 22, L = 32, left = 16, top = 52 }, { x = 49, L = 16, left = 43, top = 36 } } local function ease(u) return u * u * (3 - 2 * u) end local function plan(s, e) local tx, L, th = {}, {}, {} for f = 0, HALF - 1 do local l if f < 54 then l = HOOK_UP + (s.L - HOOK_UP) * ease(f / 54) elseif f < 75 then l = s.L elseif f < 150 then l = s.L + (TRAVEL_L - s.L) * ease((f - 75) / 75) elseif f < 285 then l = TRAVEL_L elseif f < 360 then l = TRAVEL_L + (e.L - TRAVEL_L) * ease((f - 285) / 75) elseif f < 378 then l = e.L else l = e.L + (HOOK_UP - e.L) * ease((f - 378) / 42) end local u = (f - 138) / 84 u = u < 0 and 0 or u > 1 and 1 or u tx[f], L[f], th[f] = s.x + (e.x - s.x) * ease(u), l, 0 end local a, w, dt = 0, 0, 1 / 30 for f = 75, 359 do a = (tx[f + 1] - 2 * tx[f] + tx[f - 1]) / (dt * dt) local c = 0.35 + (f > 285 and 7 * (f - 285) / 75 or 0) local t0 = th[f - 1] local acc = -(G / L[f]) * sin(t0) - a / L[f] * cos(t0) - 2 * (L[f] - L[f - 1]) / dt / L[f] * w - c * w w = w + acc * dt th[f] = t0 + w * dt end return { tx = tx, L = L, th = th, s = s, e = e } end local HALVES = {} local function lerp(a, b, t) return floor(a + (b - a) * t + 0.5) end local function sky() local top, mid, low = PAL[1], PAL[2], PAL[3] for y = 0, 43 do local a, b, t = top, mid, y / 24 if y >= 24 then a, b, t = mid, low, (y - 24) / 19 end rect(0, y, W, 1, lerp(a[1], b[1], t), lerp(a[2], b[2], t), lerp(a[3], b[3], t)) end rect(0, 44, W, H - 44, low[1], low[2], low[3]) for _, d in ipairs({ { 9, 255, 184, 104 }, { 7, 255, 214, 140 }, { 5, 255, 240, 190 } }) do for dy = -d[1], d[1] do local hw = floor(sqrt(d[1] * d[1] - dy * dy) + 0.5) rect(30 - hw, 44 + dy, 2 * hw + 1, 1, d[2], d[3], d[4]) end end blend_rect(0, 17, 26, 2, 255, 206, 170, 0.32); blend_rect(6, 19, 18, 1, 255, 206, 170, 0.22) blend_rect(40, 9, 22, 2, 255, 190, 170, 0.28); blend_rect(46, 11, 14, 1, 255, 190, 170, 0.2) end local function skylines() local x, i = 0, 0 while x < W do i = i + 1 local h = hash32(i * 0x9E3779B1) local w, top = 4 + umod(h, 5), 45 + umod(h >> 8, 7) rect(x, top, w, H - top, HAZE[1], HAZE[2], HAZE[3]) x = x + w end local c = CRANE_FAR rect(46, 24, 2, 26, c[1], c[2], c[3]); rect(36, 24, 28, 2, c[1], c[2], c[3]) rect(46, 17, 2, 7, c[1], c[2], c[3]); rect(38, 26, 3, 2, c[1], c[2], c[3]) for k = 0, 7 do px(47 - k, 17 + k, c[1], c[2], c[3]); px(47 + k, 17 + k * 7 // 8, c[1], c[2], c[3]) end x, i = 0, 40 while x < 40 do i = i + 1 local h = hash32(i * 0x9E3779B1) local w, top = 5 + umod(h, 5), 50 + umod(h >> 8, 5) rect(x, top, w, H - top, NEAR[1], NEAR[2], NEAR[3]) for k = 1, 2 do px(x + umod(h >> (8 + 4 * k), w), top + 2 + umod(h >> (16 + 4 * k), 5), 255, 190, 100) end x = x + w end end local function foreground() local d = FG rect(8, 3, 1, 55, d[1], d[2], d[3]); rect(11, 3, 1, 55, d[1], d[2], d[3]) for y = 4, 57, 2 do px(9 + (y // 2) % 2, y, d[1], d[2], d[3]) end rect(9, 1, 2, 3, d[1], d[2], d[3]) rect(1, 11, 62, 1, d[1], d[2], d[3]); rect(1, 13, 62, 1, d[1], d[2], d[3]) for x = 9, 61, 2 do px(x, 12, d[1], d[2], d[3]) end rect(1, 14, 4, 3, d[1], d[2], d[3]); rect(12, 14, 4, 3, d[1], d[2], d[3]); px(14, 15, 255, 200, 100) for k = 0, 8 do px(8 - k * 7 // 8, 2 + k, d[1], d[2], d[3]) end for k = 0, 24 do px(11 + k, 2 + k * 9 // 24, d[1], d[2], d[3]) end for _, cx in ipairs({ 43, 53, 60 }) do rect(cx, 39, 2, 19, d[1], d[2], d[3]) end for _, sy in ipairs({ 47, 55 }) do rect(40, sy, 23, 2, d[1], d[2], d[3]); rect(40, sy - 1, 23, 1, RIM[1], RIM[2], RIM[3]) end rect(0, 58, W, H - 58, GROUND[1], GROUND[2], GROUND[3]) rect(13, 55, 17, 3, STEEL[1], STEEL[2], STEEL[3]); rect(13, 55, 17, 1, STEEL_LIT[1], STEEL_LIT[2], STEEL_LIT[3]) rect(17, 54, 2, 1, d[1], d[2], d[3]); rect(25, 54, 2, 1, d[1], d[2], d[3]) end local STAGES = { sky, skylines, foreground, function() HALVES[1] = plan(SPOT[1], SPOT[2]) end, function() HALVES[2] = plan(SPOT[2], SPOT[1]) end } local BG, STAGE = nil, 0 function on_settings() PAL = SKIES[(cfg.sky or 0) + 1] or SKIES[1] LIGHTS = cfg.lights ~= false BG, STAGE = nil, 0 end local function bar(x, y, w, h, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy if w > 1 and h > 1 then rect(ix + 1, iy + 1, w - 1, h - 1, r, g, b) end if fy < 0.97 and w > 1 then blend_rect(ix + 1, iy, w - 1, 1, r, g, b, 1 - fy) end if fy > 0.03 and w > 1 then blend_rect(ix + 1, iy + h, w - 1, 1, r, g, b, fy) end if fx < 0.97 and h > 1 then blend_rect(ix, iy + 1, 1, h - 1, r, g, b, 1 - fx) end if fx > 0.03 and h > 1 then blend_rect(ix + w, iy + 1, 1, h - 1, r, g, b, fx) end blend_rect(ix, iy, 1, 1, r, g, b, (1 - fx) * (1 - fy)) if fx > 0.03 then blend_rect(ix + w, iy, 1, 1, r, g, b, fx * (1 - fy)) end if fy > 0.03 then blend_rect(ix, iy + h, 1, 1, r, g, b, (1 - fx) * fy) end if fx > 0.03 and fy > 0.03 then blend_rect(ix + w, iy + h, 1, 1, r, g, b, fx * fy) end end local function line(x0, y0, x1, y1) local dx, dy = x1 - x0, y1 - y0 local steep = abs(dy) >= abs(dx) local a0, b0, a1, b1 = x0, y0, x1, y1 if steep then a0, b0, a1, b1 = y0, x0, y1, x1 end if a1 < a0 then a0, b0, a1, b1 = a1, b1, a0, b0 end local n = a1 - a0 if n < 0.01 then return end local k = (b1 - b0) / n local i, last = floor(a0), floor(a1 - 0.0001) local ci, cq, start = nil, nil, i local function flush(upto) if ci then local len, al = upto - start, cq / 4 if steep then if al < 1 then blend_rect(ci, start, 1, len, FG[1], FG[2], FG[3], min(1, 1.5 - 1.5 * al)) end if al > 0 then blend_rect(ci + 1, start, 1, len, FG[1], FG[2], FG[3], min(1, 1.5 * al)) end else if al < 1 then blend_rect(start, ci, len, 1, FG[1], FG[2], FG[3], min(1, 1.5 - 1.5 * al)) end if al > 0 then blend_rect(start, ci + 1, len, 1, FG[1], FG[2], FG[3], min(1, 1.5 * al)) end end end end for s = i, last do local v = b0 + k * (s + 0.5 - a0) - 0.5 local bi = floor(v) local q = floor((v - bi) * 4 + 0.5) if q == 4 then bi, q = bi + 1, 0 end if bi ~= ci or q ~= cq then flush(s); ci, cq, start = bi, q, s end end flush(last + 1) end local function beam(x, y) bar(x, y, 12, 3, STEEL[1], STEEL[2], STEEL[3]) bar(x, y, 12, 1, STEEL_LIT[1], STEEL_LIT[2], STEEL_LIT[3]) end local function lamp(x, y, f, ph) if (f + ph) % 40 < 7 then blend_rect(x - 1, y - 1, 3, 3, 255, 70, 50, 0.3) px(x, y, 255, 90, 70) end end local function build() if BG then gfx.blit_rgb(BG, 0, 0, W, H) end STAGES[STAGE + 1]() STAGE = STAGE + 1 BG = gfx.snapshot(0, 0, W, H) end function render() if STAGE < #STAGES then build() gfx.ready(false) return end gfx.blit_rgb(BG, 0, 0, W, H) local F = frame % (2 * HALF) local hp = HALVES[F // HALF + 1] local f = F % HALF local x, L, th = hp.tx[f], hp.L[f], hp.th[f] local hx, hy = x + L * sin(th), JY + L * cos(th) local carry = f >= 75 and f < 360 if not carry then local spot = f < 75 and hp.s or hp.e beam(spot.left, spot.top) end rect(floor(x + 0.5) - 2, 14, 5, 2, FG[1], FG[2], FG[3]) line(x + 0.5, JY, hx + 0.5, hy) if carry then line(hx + 0.5, hy, hx - 5, hy + 4); line(hx + 0.5, hy, hx + 6, hy + 4) beam(hx - 6, hy + 4) end bar(hx - 1, hy - 1, 2, 3, FG[1], FG[2], FG[3]) if LIGHTS then lamp(10, 0, frame, 0); lamp(62, 10, frame, 13); lamp(2, 10, frame, 27); lamp(46, 16, frame, 5) end end