info = { type = "image", title = "Track loop", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Three race cars lap a circuit seen from above", api = 44, } local tr = ({ sk = { ["Cars"] = "Autá", ["Speed"] = "Rýchlosť", ["Scenery"] = "Scenéria", ["Summer"] = "Leto", ["Autumn"] = "Jeseň" }, cs = { ["Cars"] = "Auta", ["Speed"] = "Rychlost", ["Scenery"] = "Scenérie", ["Summer"] = "Léto", ["Autumn"] = "Podzim" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "cars", label = T("Cars"), type = "int", min = 1, max = 3, default = 3 }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "scene", label = T("Scenery"), type = "int", default = "summer", opts = { keys = { "summer", "autumn" }, T("Summer"), T("Autumn") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, ceil, sin, cos, sqrt, abs, max, min, atan = math.floor, math.ceil, math.sin, math.cos, math.sqrt, math.abs, math.max, math.min, math.atan local hash32, umod = math.hash32, math.umod local char, concat = string.char, table.concat local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI, TAU = 3.14159265, 6.2831853 local CX, CY, HX, HY, R = 32, 37, 22, 15, 9 local HALF = 6.5 local A = R * PI / 2 local S1 = 2 * (HX - R) local S2 = S1 + A local S3 = S2 + 2 * (HY - R) local S4 = S3 + A local S5 = S4 + 2 * (HX - R) local S6 = S5 + A local S7 = S6 + 2 * (HY - R) local LAP = S7 + A local X0, X1, Y0, Y1 = CX - HX + R, CX + HX - R, CY - HY + R, CY + HY - R local function pos(s) s = s % LAP if s < S1 then return X0 + s, CY - HY, 0 elseif s < S2 then local a = (s - S1) / R - PI / 2 return X1 + R * cos(a), Y0 + R * sin(a), a + PI / 2 elseif s < S3 then return CX + HX, Y0 + (s - S2), PI / 2 elseif s < S4 then local a = (s - S3) / R return X1 + R * cos(a), Y1 + R * sin(a), a + PI / 2 elseif s < S5 then return X1 - (s - S4), CY + HY, PI elseif s < S6 then local a = (s - S5) / R + PI / 2 return X0 + R * cos(a), Y1 + R * sin(a), a + PI / 2 elseif s < S7 then return CX - HX, Y1 - (s - S6), 3 * PI / 2 else local a = (s - S7) / R + PI return X0 + R * cos(a), Y0 + R * sin(a), a + PI / 2 end end local function sparam(x, y) if y < Y0 then if x < X0 then return S7 + R * (atan(y - Y0, x - X0) + 2 * PI - PI) elseif x > X1 then return S1 + R * (atan(y - Y0, x - X1) + PI / 2) end return x - X0 elseif y > Y1 then if x < X0 then return S5 + R * (atan(y - Y1, x - X0) - PI / 2) elseif x > X1 then return S3 + R * atan(y - Y1, x - X1) end return S4 + (X1 - x) end if x > CX then return S2 + (y - Y0) end return S6 + (Y1 - y) end local function slow(s) local d = 99 for _, c in ipairs({ { S1, S2 }, { S3, S4 }, { S5, S6 }, { S7, LAP } }) do local dd = s < c[1] and c[1] - s or (s > c[2] and s - c[2] or 0) if dd < d then d = dd end end local k = 1 - d / 6 if k < 0 then k = 0 end return 1 - 0.5 * k * k * (3 - 2 * k) end local TS, NS = {}, 0 local function init_speed() local t = 0 TS[0] = 0 NS = floor(LAP) for k = 1, NS do t = t + 1 / slow(k - 0.5); TS[k] = t end TS[NS + 1] = t + (LAP - NS) / slow(NS + (LAP - NS) / 2) for k = 0, NS + 1 do TS[k] = TS[k] / TS[NS + 1] end end local function phase_s(u) local lo, hi = 0, NS + 1 while hi - lo > 1 do local m = (lo + hi) // 2 if TS[m] <= u then lo = m else hi = m end end local a, b = TS[lo], TS[hi] local sa = lo local sb = hi > NS and LAP or hi return sa + (sb - sa) * (u - a) / (b - a) end local CARS = { { cols = { { 214, 48, 44 }, { 250, 244, 236 }, { 30, 30, 36 }, { 250, 220, 60 } }, off = 0.00, a = 0.00, ph = 0.0, lane = 2.4 }, { cols = { { 52, 98, 214 }, { 250, 250, 250 }, { 30, 30, 36 }, { 250, 120, 40 } }, off = -0.15, a = 0.30, ph = 0.0, lane = 0.6 }, { cols = { { 240, 196, 40 }, { 40, 40, 48 }, { 30, 30, 36 }, { 230, 60, 60 } }, off = -0.55, a = 0.05, ph = 1.5, lane = 2.4 }, } local ASPH = { 76, 80, 92 } local NCARS, LOOP, SPEED = 3, 510, 1 local function model(u, v) local av = abs(v) if u >= -3.6 and u <= -2.6 and av <= 1.7 then return 2 end if av >= 1.15 and av <= 1.85 and ((u >= 1.2 and u <= 2.8) or (u >= -2.4 and u <= -1.0)) then return 3 end if u >= -0.5 and u <= 0.5 and av <= 0.55 then return 4 end local hw = (u > 1.8) and 0.55 or ((u > -2.6) and 1.0 or 0.0) if u >= -2.8 and u <= 3.4 and av <= hw then return (u > 2.4) and 2 or 1 end return 0 end local CB = 9 local HI = {} local function hi_dir(d) local th = d * PI / 4 local cs, sn = cos(th), sin(th) local g = {} for hy = 0, 2 * CB - 1 do local py = (hy + 0.5) / 2 - CB / 2 for hx = 0, 2 * CB - 1 do local pxx = (hx + 0.5) / 2 - CB / 2 g[hy * 2 * CB + hx + 1] = model(pxx * cs + py * sn, -pxx * sn + py * cs) end end HI[d] = g end local SPR = {} local function sprite(ci, d, ph) local key = (ci * 8 + d) * 4 + ph local s = SPR[key] if s then return s end if not HI[d] then hi_dir(d) end local g, cols = HI[d], CARS[ci].cols local fx, fy = ph % 2, ph // 2 local out, n = {}, 0 local N2 = 2 * CB for y = 0, CB - 1 do for x = 0, CB - 1 do local r, gg, b = 0, 0, 0 for sy = 0, 1 do local hy = 2 * y - fy + sy for sx = 0, 1 do local hx = 2 * x - fx + sx local i = (hx >= 0 and hx < N2 and hy >= 0 and hy < N2) and g[hy * N2 + hx + 1] or 0 local c = i == 0 and ASPH or cols[i] r, gg, b = r + c[1], gg + c[2], b + c[3] end end out[n + 1], out[n + 2], out[n + 3] = (r + 2) // 4, (gg + 2) // 4, (b + 2) // 4 n = n + 3 end end s = char(table.unpack(out, 1, n)) SPR[key] = s return s end local BG, PART, STAGE = nil, nil, 1 local SCN = { summer = { g1 = { 66, 152, 74 }, g2 = { 58, 140, 66 }, tree = { 34, 108, 52 }, tree2 = { 70, 160, 84 } }, autumn = { g1 = { 148, 142, 62 }, g2 = { 134, 128, 54 }, tree = { 196, 84, 34 }, tree2 = { 238, 168, 56 } }, } local G = SCN.summer local function rr(y, d) local x0, x1, y0, y1, r = CX - HX - d, CX + HX + d, CY - HY - d, CY + HY + d, R + d local yy = y + 0.5 if yy < y0 or yy > y1 then return nil end local dy = 0 if yy < y0 + r then dy = y0 + r - yy elseif yy > y1 - r then dy = yy - (y1 - r) end local inset = r - sqrt(max(0, r * r - dy * dy)) local a, b = ceil(x0 + inset - 0.5), floor(x1 - inset - 0.5) if b < a then return nil end return a, b end local function kerb(x, y, w) for i = x, x + w - 1 do local red = floor(sparam(i + 0.5, y + 0.5) / 3) % 2 == 0 if red then rect(i, y, 1, 1, 214, 56, 50) else rect(i, y, 1, 1, 240, 240, 240) end end end local function disc(cx, cy, r, c1, c2, c3) for y = -r, r do local w = floor(sqrt(r * r + 0.25 - y * y) + 0.5) rect(cx - w, cy + y, 2 * w + 1, 1, c1, c2, c3) end end local CROWD = {} local function bake_a() for x = 0, W - 1, 4 do local c = (x // 4) % 2 == 0 and G.g1 or G.g2 rect(x, 0, 4, H, c[1], c[2], c[3]) end for y = 0, H - 1 do local a, b = rr(y, HALF) if a then local c, d = rr(y, -HALF) if c then rect(a, y, c - a, 1, ASPH[1], ASPH[2], ASPH[3]); rect(d + 1, y, b - d, 1, ASPH[1], ASPH[2], ASPH[3]) else rect(a, y, b - a + 1, 1, ASPH[1], ASPH[2], ASPH[3]) end end end end local function bake_b() for y = 0, H - 1 do local a, b = rr(y, HALF) if a then local a1, b1 = rr(y, HALF - 1) if a1 then kerb(a, y, a1 - a); kerb(b1 + 1, y, b - b1) else kerb(a, y, b - a + 1) end end local c1, d1 = rr(y, -HALF + 1) if c1 then local c, d = rr(y, -HALF) if c then kerb(c1, y, c - c1); kerb(d + 1, y, d1 - d) else kerb(c1, y, d1 - c1 + 1) end end end for y = 16, 27 do px(31 + y % 2, y, 245, 245, 245); px(32 - y % 2, y, 30, 30, 36) end end local function bake_c() for y = -4, 4 do local w = floor(9 * sqrt(max(0, 1 - (y / 4.6) ^ 2)) + 0.5) rect(32 - w, 37 + y, 2 * w + 1, 1, 58, 128, 190) end rect(27, 36, 5, 1, 150, 200, 236); rect(34, 38, 4, 1, 120, 174, 220) for _, t in ipairs({ { 22, 28 }, { 42, 28 }, { 22, 47 }, { 42, 47 } }) do disc(t[1], t[2], 3, G.tree[1], G.tree[2], G.tree[3]) px(t[1] - 1, t[2] - 1, G.tree2[1], G.tree2[2], G.tree2[3]); px(t[1], t[2] - 2, G.tree2[1], G.tree2[2], G.tree2[3]) end for _, t in ipairs({ { 1, 26 }, { 1, 40 }, { 62, 30 }, { 62, 46 }, { 10, 62 }, { 30, 62 }, { 52, 62 }, { 2, 56 } }) do disc(t[1], t[2], 2, G.tree[1], G.tree[2], G.tree[3]) px(t[1] - 1, t[2] - 1, G.tree2[1], G.tree2[2], G.tree2[3]) end rect(5, 0, 54, 4, 120, 114, 124) for x = 5, 58, 3 do rect(x, 0, 3, 2, (x // 3) % 2 == 0 and 214 or 240, (x // 3) % 2 == 0 and 56 or 240, (x // 3) % 2 == 0 and 50 or 240) end rect(5, 2, 54, 1, 60, 54, 70) rect(5, 3, 54, 1, 90, 84, 98) rect(5, 4, 54, 9, 150, 146, 152) local SHIRT = { { 214, 56, 50 }, { 52, 98, 214 }, { 240, 200, 50 }, { 240, 240, 240 }, { 60, 170, 90 }, { 236, 130, 40 }, { 150, 70, 180 } } local SKIN = { { 246, 206, 170 }, { 214, 160, 120 }, { 150, 104, 72 }, { 60, 44, 36 }, { 224, 180, 80 } } local n = 0 for tier = 0, 3 do local yb = 4 + tier * 2 + 1 for x = 6, 56, 2 do local h = hash32((tier * 40 + x) * 0x9E3779B1) local sk, sh = SKIN[1 + umod(h, 5)], SHIRT[1 + umod(h >> 8, 7)] px(x, yb, sk[1], sk[2], sk[3]) px(x, yb + 1, sh[1], sh[2], sh[3]) if umod(h >> 16, 100) < 22 and tier > 0 then n = n + 1; CROWD[n] = { x + 1, yb, sk, umod(h >> 20, 40) } end end end rect(5, 13, 54, 1, 236, 236, 240) for x = 5, 57, 9 do rect(x, 13, 5, 1, 40, 70, 160) end rect(5, 14, 54, 1, 54, 120, 62) BG = snapshot(0, 0, W, H) end function on_settings() G = SCN[cfg.scene] or SCN.summer NCARS = cfg.cars or 3 SPEED = cfg.speed or 3 LOOP = floor(900 - 100 * SPEED) BG, PART, STAGE = nil, nil, 1 end local LANE_OUT = -3.2 function render() if STAGE < 4 then if STAGE == 1 then init_speed(); bake_a(); PART = snapshot(0, 0, W, H) elseif STAGE == 2 then blit_rgb(PART, 0, 0, W, H); bake_b(); PART = snapshot(0, 0, W, H) else blit_rgb(PART, 0, 0, W, H); bake_c() end STAGE = STAGE + 1 if STAGE < 4 then gfx.ready(false) end return end blit_rgb(BG, 0, 0, W, H) local fi = frame % LOOP local w = TAU * fi / LOOP local r, dr = {}, {} for i = 1, NCARS do local c = CARS[i] r[i] = 3 * fi / LOOP + c.off + c.a * sin(w + c.ph) dr[i] = 3 + c.a * TAU * cos(w + c.ph) end local tf = frame // 6 for i = 1, #CROWD do local q = CROWD[i] if (tf + q[4]) % 40 < 8 then px(q[1], q[2] - (tf % 2), q[3][1], q[3][2], q[3][3]) end end for i = 1, NCARS do local c = CARS[i] local lap = r[i] % 1 local s = phase_s(lap) local lat = c.lane for j = 1, NCARS do if j ~= i and dr[i] > dr[j] then local g = ((r[j] - r[i]) % 1) * LAP if g > LAP / 2 then g = g - LAP end local k if g >= 0 then k = (16 - g) / 6 else k = (g + 15) / 6 end if k > 1 then k = 1 elseif k < 0 then k = 0 end k = k * k * (3 - 2 * k) if k > 0 then lat = lat + (LANE_OUT - c.lane) * k end end end local x, y, th = pos(s) x, y = x - lat * sin(th), y + lat * cos(th) local d = floor(th / (PI / 4) + 0.5) % 8 local nx, ny = floor((x - CB / 2) * 2 + 0.5), floor((y - CB / 2) * 2 + 0.5) local ix, iy = nx // 2, ny // 2 blit_rgb(sprite(i, d, (ny % 2) * 2 + nx % 2), ix, iy, CB, CB, ASPH[1], ASPH[2], ASPH[3]) end end