info = { type = "image", title = "Hill drive", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Driver's view along a winding country road", api = 33, } local tr = ({ sk = { ["Season"] = "Ročné obdobie", ["Summer"] = "Leto", ["Autumn"] = "Jeseň", ["Winter"] = "Zima", ["Speed"] = "Rýchlosť", ["Bends"] = "Zákruty" }, cs = { ["Season"] = "Roční období", ["Summer"] = "Léto", ["Autumn"] = "Podzim", ["Winter"] = "Zima", ["Speed"] = "Rychlost", ["Bends"] = "Zatáčky" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "season", label = T("Season"), type = "int", default = "summer", opts = { keys = { "summer", "autumn", "winter" }, T("Summer"), T("Autumn"), T("Winter") } }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "bends", label = T("Bends"), type = "int", min = 1, max = 3, default = 2 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, cos, sqrt, max, min = math.floor, math.sin, math.cos, math.sqrt, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HZ, DASH, ROWS = 24, 46, 21 local ZK, C1 = 36, 0.085 local KEY = 255 local SEASONS = { summer = { skyT = { 62, 132, 230 }, skyH = { 184, 222, 250 }, far = { 132, 172, 210 }, mid = { 84, 148, 100 }, gN = { 66, 150, 62 }, gF = { 120, 182, 96 }, road = { 92, 94, 102 }, trunk = { 86, 58, 34 }, tier = { { 24, 94, 58 }, { 30, 108, 66 }, { 42, 126, 74 } }, leaf = { 58, 142, 62 }, leafL = { 96, 178, 80 }, cloud = { 252, 252, 255 }, sun = { 255, 244, 200 } }, autumn = { skyT = { 92, 150, 214 }, skyH = { 250, 214, 170 }, far = { 172, 160, 176 }, mid = { 176, 112, 60 }, gN = { 150, 142, 58 }, gF = { 190, 170, 86 }, road = { 96, 92, 98 }, trunk = { 80, 54, 34 }, tier = { { 26, 84, 56 }, { 32, 98, 62 }, { 40, 112, 68 } }, leaf = { 214, 110, 28 }, leafL = { 246, 170, 52 }, cloud = { 255, 240, 226 }, sun = { 255, 230, 170 } }, winter = { skyT = { 120, 150, 196 }, skyH = { 224, 232, 244 }, far = { 190, 204, 226 }, mid = { 214, 226, 238 }, gN = { 226, 234, 244 }, gF = { 242, 246, 252 }, road = { 118, 122, 132 }, trunk = { 70, 56, 50 }, tier = { { 36, 84, 70 }, { 196, 212, 220 }, { 244, 248, 252 } }, leaf = { 196, 208, 220 }, leafL = { 244, 248, 252 }, cloud = { 206, 214, 226 }, sun = { 250, 244, 230 } }, } local P, GR, RD = SEASONS.summer, {}, {} local SKY = nil local function mixc(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 function on_settings() P = SEASONS[cfg.season] or SEASONS.summer for r = 1, ROWS do GR[r] = mixc(P.gF, P.gN, r / ROWS) RD[r] = mixc(P.skyH, P.road, 0.5 + 0.5 * r / ROWS) end SKY = nil end local function ridge(base, a1, a2, a3, p) local t = {} for i = 0, 255 do local x = i * 6.2831853 / 256 t[i + 1] = base + a1 * sin(3 * x + p) + a2 * sin(7 * x + p * 2.1) + a3 * sin(17 * x + p * 0.7) end return t end local FAR, MID = ridge(7, 3.4, 1.8, 0.7, 0.6), ridge(3.5, 1.8, 1.4, 0.8, 2.3) local function hills(HF, shift, c) for x = 0, W - 2, 2 do local p = (x + shift) % 256 local i = floor(p) local f = p - i i = i % 256 local top = HZ - (HF[i + 1] * (1 - f) + HF[(i + 1) % 256 + 1] * f) local iy = floor(top) rect(x, iy + 1, 2, HZ - iy, c[1], c[2], c[3]) blend_rect(x, iy, 2, 1, c[1], c[2], c[3], 1 - (top - iy)) end end local function kap(s) return 0.50 * sin(0.085 * s + 0.7) + 0.30 * sin(0.037 * s + 2.1) + 0.28 * sin(0.19 * s + 4.0) end local function heading(s) return -(5.88 * cos(0.085 * s + 0.7) + 8.11 * cos(0.037 * s + 2.1) + 1.47 * cos(0.19 * s + 4.0)) end local OFF = {} local function offat(rf) local i = floor(rf) if i < 1 then return OFF[1] end if i >= ROWS then return OFF[ROWS] end return OFF[i] + (OFF[i + 1] - OFF[i]) * (rf - i) end local WX, WY, WRX, WRY = 32, 60, 22, 14 local OVL = nil local function disc(x, y, r, cr, cg, cb) for j = -r, r do local w = floor(sqrt(r * r - j * j) + 0.5) rect(x - w, y + j, 2 * w + 1, 1, cr, cg, cb) end end local function bake_cockpit() gfx.clear(KEY, 0, KEY) rect(0, 0, W, 3, 30, 32, 40) rect(31, 3, 2, 2, 30, 32, 40) rect(25, 5, 14, 6, 26, 28, 36) rect(26, 6, 12, 4, 126, 156, 188) rect(26, 6, 12, 1, 176, 204, 228) for y = 0, DASH - 1 do local w = floor(8.5 - y * 0.12) rect(0, y, w, 1, 38, 40, 50) rect(W - w, y, w, 1, 38, 40, 50) px(w, y, 92, 96, 110) px(W - w - 1, y, 92, 96, 110) end rect(0, DASH, W, H - DASH, 36, 38, 48) rect(0, DASH, W, 1, 112, 116, 132) rect(0, DASH + 1, W, 1, 64, 68, 82) for _, x in ipairs({ 2, 53 }) do rect(x, 50, 9, 4, 24, 26, 32) rect(x + 1, 51, 7, 1, 66, 70, 84) end rect(25, 48, 14, 8, 70, 74, 88) rect(26, 49, 12, 6, 8, 10, 16) px(22, 52, 70, 220, 110); px(42, 52, 240, 180, 40); px(22, 54, 230, 60, 50) for a = 0, 357, 3 do local r = a * 0.0174533 local f = 0.5 - 0.5 * sin(r) rect(floor(WX + WRX * cos(r) + 0.5) - 1, floor(WY + WRY * sin(r) + 0.5) - 1, 3, 3, floor(58 + 92 * f), floor(60 + 92 * f), floor(70 + 96 * f)) end disc(WX, WY, 5, 56, 58, 70) disc(WX, WY, 3, 38, 40, 50) px(WX, WY, 190, 194, 206); px(WX - 1, WY, 130, 134, 148); px(WX + 1, WY, 130, 134, 148) OVL = snapshot(0, 0, W, H) end local function rim_dot(a, c1, c2, c3) rect(floor(WX + WRX * cos(a) + 0.5) - 1, floor(WY + WRY * sin(a) + 0.5) - 1, 3, 3, c1, c2, c3) end local function wheel(deg) local a = deg * 0.0174533 for k = 0, 2 do local ak = a + (k == 0 and 3.14159 or (k == 1 and 0 or 1.5708)) local ca, sa = cos(ak), sin(ak) for i = 0, 5 do local t = 0.3 + i * 0.12 local d = i < 3 and 3 or 2 rect(floor(WX + ca * WRX * t + 0.5) - 1, floor(WY + sa * WRY * t + 0.5) - 1, d, d, 124, 128, 142) end end for _, g in ipairs({ 0, 3.14159 }) do for d = -0.2, 0.2, 0.2 do rim_dot(a + g + d, 30, 32, 40) end end for d = -0.03, 0.03, 0.06 do rim_dot(a - 1.5708 + d, 236, 196, 40) end end local KMH, KMH_S = 0, "" local function readout(v) v = floor(v) if v ~= KMH then KMH, KMH_S = v, tostring(v) end gfx.text(28, 50, KMH_S, 110, 240, 140, { font = "3x5" }) end local function bake_sky() for y = 0, HZ do local t = y / HZ rect(0, y, W, 1, floor(P.skyT[1] + (P.skyH[1] - P.skyT[1]) * t + 0.5), floor(P.skyT[2] + (P.skyH[2] - P.skyT[2]) * t + 0.5), floor(P.skyT[3] + (P.skyH[3] - P.skyT[3]) * t + 0.5)) end for j = -6, 6 do local w = floor(sqrt(36 - j * j) + 0.5) blend_rect(49 - w, 9 + j, 2 * w + 1, 1, P.sun[1], P.sun[2], P.sun[3], 0.25) end disc(49, 9, 3, P.sun[1], P.sun[2], P.sun[3]) SKY = snapshot(0, 0, W, HZ + 1) end local function cloud_row(xl, xr, y, a) local il, ir = floor(xl), floor(xr) if ir <= il then return end local c = P.cloud blend_rect(il + 1, y, ir - il - 1, 1, c[1], c[2], c[3], a) blend_rect(il, y, 1, 1, c[1], c[2], c[3], a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, c[1], c[2], c[3], a * (xr - ir)) end local CLOUDS = { { 3, 18, 1.4, 5 }, { 11, 12, -0.9, 40 } } local function pine(x, y, ht, ph) local th = max(1, floor(ht * 0.2)) local t, c = P.tier, P.trunk rect(x, y - th, ph > 8 and 2 or 1, th, c[1], c[2], c[3]) local w1, w2, w3 = max(2, floor(ht * 0.55)), max(2, floor(ht * 0.4)), max(1, floor(ht * 0.2)) local h3 = max(1, floor((ht - th) / 3)) local yb = y - th rect(x - w1 // 2, yb - h3, w1, h3, t[1][1], t[1][2], t[1][3]) rect(x - w2 // 2, yb - 2 * h3, w2, h3, t[2][1], t[2][2], t[2][3]) rect(x - w3 // 2, yb - 3 * h3, w3, h3, t[3][1], t[3][2], t[3][3]) end local function round_tree(x, y, ht) local th = max(1, floor(ht * 0.3)) local c, l, k = P.leaf, P.leafL, P.trunk rect(x, y - th, ht > 8 and 2 or 1, th, k[1], k[2], k[3]) local wc, hc = max(2, floor(ht * 0.7)), max(2, floor(ht * 0.6)) rect(x - wc // 2, y - th - hc, wc, hc, c[1], c[2], c[3]) rect(x - wc // 2 + 1, y - th - hc - 1, max(1, wc - 2), 1, c[1], c[2], c[3]) rect(x - wc // 2 + 1, y - th - hc, max(1, wc // 3), max(1, hc // 2), l[1], l[2], l[3]) end local TG, PG = 3.2, 7 local function roadside(s) local k0 = floor(s / TG) for i = floor(34 / TG) + 1, 0, -1 do local id = k0 + i local z = id * TG - s if z > 1.6 and z < 33 then local r = ZK / z local h = hash32(id * 0x9E3779B1) local side = (h & 1) == 0 and -1 or 1 local x = floor(32 + offat(r) + side * (0.95 + 1.25 + umod(h >> 4, 100) / 62) * r + 0.5) local ht = floor(r * (0.8 + umod(h >> 12, 50) / 100) + 0.5) if ht >= 2 and x > -12 and x < W + 12 then local kind = umod(h >> 20, 3) local y = HZ + floor(r + 0.5) if kind == 2 and P ~= SEASONS.winter then round_tree(x, y, ht) else pine(x, y, ht, ht) end end end end k0 = floor(s / PG) for i = floor(34 / PG) + 1, 0, -1 do local id = k0 + i local z = id * PG - s if z > 1.6 and z < 30 then local r = ZK / z local y = HZ + floor(r + 0.5) local ph = max(1, floor(r * 0.28 + 0.5)) local w = r > 10 and 2 or 1 for side = -1, 1, 2 do local x = floor(32 + offat(r) + side * (0.95 + 0.14) * r + 0.5 - w * 0.5) rect(x, y - ph, w, ph, 236, 236, 230) rect(x, y - ph, w, max(1, ph // 4), 210, 40, 40) end end end end local VEL = { 0.16, 0.22, 0.28, 0.36, 0.46 } local BEND = { 0.6, 1.0, 1.4 } function render() if not SKY then bake_sky() end if not OVL then bake_cockpit() end local s = frame * VEL[cfg.speed or 3] local bend = BEND[cfg.bends or 2] local t = frame / FPS blit_rgb(SKY, 0, 0, W, HZ + 1) for i = 1, #CLOUDS do local c = CLOUDS[i] local x = (c[4] + t * c[3]) % (W + c[2]) - c[2] cloud_row(x + 2, x + c[2] - 2, c[1], 0.8) cloud_row(x, x + c[2], c[1] + 1, 0.8) end local hd = heading(s) * bend hills(FAR, hd * 0.8, P.far) hills(MID, hd * 1.5, P.mid) local dx, xo = 0, 0 for r = ROWS, 1, -1 do dx = dx + kap(s + ZK / r) * bend * C1 xo = xo + dx OFF[r] = xo end for r = 1, ROWS do local y = HZ + r local c = RD[r] if r % 2 == 1 then local g = GR[r] rect(0, y, W, 2, g[1], g[2], g[3]) end local cx = 32 + OFF[r] local hw = r * 0.95 + 0.5 local xl, xr = floor(cx - hw + 0.5), floor(cx + hw + 0.5) local lw = max(1, floor(r * 0.07 + 0.5)) rect(xl, y, xr - xl, 1, 236, 236, 230) rect(xl + lw, y, xr - xl - 2 * lw, 1, c[1], c[2], c[3]) if r >= 3 and (s + ZK / r) % 8 < 3.5 then local dw = max(1, floor(r * 0.09 + 0.5)) rect(floor(cx - dw * 0.5 + 0.5), y, dw, 1, 240, 232, 150) end end roadside(s) blit_rgb(OVL, 0, 0, W, H, KEY, 0, KEY) local deg = 75 * bend * kap(s + 3) + 2 * sin(s * 0.55) wheel(max(-105, min(105, deg))) readout(40 + 12 * (cfg.speed or 3) + 3 * sin(t * 0.9)) end