info = { type = "image", title = "Castle ascent", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Camera climbs a winding road up to a castle", api = 44, } local tr = ({ sk = { ["Rider"] = "Jazdec", ["Climb pace"] = "Tempo stúpania" }, cs = { ["Rider"] = "Jezdec", ["Climb pace"] = "Tempo stoupání" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "rider", label = T("Rider"), type = "bool", default = true }, { key = "pace", label = T("Climb pace"), type = "int", min = 1, max = 5, default = 3 }, } 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 WH, TOP = 192, 128 local PI = 3.14159 local SKY0, SKY1 = { 98, 158, 214 }, { 196, 226, 234 } local MTN, MTN_D = { 158, 186, 190 }, { 132, 164, 176 } local VAL0, VAL1 = { 178, 208, 204 }, { 112, 160, 118 } local LAKE = { 150, 190, 208 } local GRASS, FORST = { 124, 164, 74 }, { 24, 62, 46 } local MEAD, MEAD_L = { 150, 186, 88 }, { 176, 204, 104 } local STONE, LIGHT, DARK, DEEP = { 150, 142, 160 }, { 190, 184, 200 }, { 104, 98, 124 }, { 66, 60, 88 } local ROOF, ROOF_L = { 92, 104, 150 }, { 132, 146, 190 } local ROAD, ROAD_L, ROAD_D = { 176, 164, 152 }, { 214, 204, 190 }, { 118, 108, 106 } local TREE = { { 26, 66, 46 }, { 32, 78, 52 }, { 40, 92, 56 } } local TREE_L = { { 70, 128, 68 }, { 88, 148, 72 }, { 112, 166, 78 } } local TREE_D = { 18, 48, 38 } local FLAG, FLAG_D = { 226, 94, 56 }, { 172, 58, 44 } 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 WP = { { 56, 37 }, { 70, 40 }, { 82, 24 }, { 98, 46 }, { 114, 20 }, { 130, 45 }, { 146, 16 }, { 162, 42 }, { 178, 22 }, { 192, 34 } } local RX = {} for i = 1, #WP - 1 do local y1, x1, y2, x2 = WP[i][1], WP[i][2], WP[i + 1][1], WP[i + 1][2] for y = y1, y2 do RX[y] = x1 + (x2 - x1) * (1 - cos(PI * (y - y1) / (y2 - y1))) / 2 end end for y = 193, 230 do RX[y] = RX[192] end local function roadw(y) return 3 + (y - 56) // 34 end local function edge(y) local e = 46 + (y - 58) * 0.3 + 2 * sin(y * 0.3) return e > W and W or e end local OY = 0 local function R(x, y, w, h, c) local y0 = y - OY if y0 >= H or y0 + h <= 0 or w <= 0 then return end rect(x, y0, w, h, c[1], c[2], c[3]) end local function patch(cx, cy, rx, ry, c) for dy = -ry, ry do local hw = floor(rx * sqrt(1 - (dy / ry) * (dy / ry))) R(cx - hw, cy + dy, 2 * hw + 1, 1, c) end end local function cloud(x, y, w) R(x + 3, y, w - 6, 1, { 250, 252, 252 }) R(x, y + 1, w, 2, { 250, 252, 252 }) R(x + 2, y + 3, w - 5, 1, { 196, 214, 230 }) R(x + 6, y + 1, w // 3, 1, { 226, 238, 246 }) end local function wall(x, y, w, h) R(x, y, w, h, STONE) R(x, y, 2, h, LIGHT) R(x + w - 2, y, 2, h, DARK) for i = 1, w * h // 8 do local v = hash32(x * 131 + y * 7 + i * 977) R(x + v % w, y + (v >> 8) % h, 1, 1, (v >> 16) & 1 == 0 and LIGHT or DARK) end end local function merlons(x, y, w) for cx = x, x + w - 2, 4 do R(cx, y - 2, 2, 2, STONE); R(cx, y - 2, 1, 2, LIGHT) end end local function roof(cx, yb, hw, h) for i = 0, h - 1 do local half = floor(hw * (i + 0.6) / h) R(cx - half, yb - h + i, 2 * half + 1, 1, ROOF) R(cx - half, yb - h + i, half // 2 + 1, 1, ROOF_L) end R(cx, yb - h - 4, 1, 4, DEEP) end local function tree(x, y, L, k) local ti = k % 3 + 1 for i = 0, 2 do local w, h = 2 * L * (i + 1) + 1, L + 1 local ty = y - (3 - i) * h R(x - w // 2, ty, w, h, TREE[ti]) R(x - w // 2, ty, w // 3 + 1, 1, TREE_L[ti]) R(x - w // 2 + 1, ty + h - 1, w - 1, 1, TREE_D) end R(x, y, 1, 1, { 86, 60, 40 }) end local function stage_base() for y = 0, 70 do R(0, y, W, 1, mix(SKY0, SKY1, y / 70)) end cloud(4, 6, 20); cloud(40, 12, 18); cloud(2, 30, 14); cloud(46, 34, 16) for y = 48, 66 do local h = (y - 48) * 1.4 R(floor(52 - h), y, floor(h) + 1, 1, MTN) R(52, y, floor(h) + 1, 1, MTN_D) end for y = 62, 150 do local x0 = floor(edge(y)) R(x0, y, W - x0, 1, mix(VAL0, VAL1, (y - 62) / 88)) end patch(56, 100, 12, 4, LAKE) for i = 1, 70 do local v = hash32(i * 0x9E3779B1) local x, y = 28 + v % 36, 62 + (v >> 8) % 88 if x >= edge(y) + 1 then R(x, y, 1 + (v >> 16) % 3, 1, { 96, 148, 108 }) end end for y = 58, WH - 1 do local t = (y - 58) / 72 R(0, y, floor(edge(y)), 1, mix(GRASS, FORST, t > 1 and 1 or t)) end patch(22, 72, 14, 5, MEAD); patch(36, 92, 10, 4, MEAD); patch(12, 104, 10, 4, MEAD); patch(30, 118, 8, 3, MEAD) for i = 1, 60 do local v = hash32(i * 0x85EBCA6B) local x, y = v % 44, 66 + (v >> 8) % 52 if x < edge(y) - 2 then R(x, y, 1 + (v >> 16) % 2, 1, MEAD_L) end end end local function stage_trees() for y = 60, WH - 1, 2 do local L = y < 100 and 1 or (y < 150 and 2 or 3) for j = 1, 1 + (y - 60) // 40 do local v = hash32(y * 7919 + j * 104729) local x = v % (W + 4) - 2 if x < edge(y) + 1 and math.abs(x - RX[y]) > roadw(y) + 3 and (y > 70 or x < 24 or x > 46) then tree(x, y, L, v >> 12) end end end end local function stage_castle() for y = 56, WH - 1 do local x0, x1 = RX[y], RX[y + 1] if x1 < x0 then x0, x1 = x1, x0 end local w = roadw(y) local a = floor(x0) local len = floor(x1) - a + w R(a, y, len, 1, ROAD) R(a, y, 1, 1, ROAD_L); R(a + len - 1, y, 1, 1, ROAD_D) local v = hash32(y * 31337) if v % 5 == 0 then R(a + 1 + (v >> 8) % (len - 1), y, 1, 1, ROAD_D) end end R(0, 55, 47, 3, DARK) wall(0, 42, 47, 14); merlons(0, 42, 47) wall(12, 32, 9, 12); merlons(12, 32, 9) wall(21, 14, 7, 32); roof(24, 14, 5, 8) wall(30, 12, 11, 32); roof(35, 12, 7, 8) wall(42, 24, 6, 26); roof(45, 24, 4, 7) wall(2, 22, 9, 34); roof(6, 22, 6, 9) R(34, 46, 6, 10, DEEP); R(35, 45, 4, 1, DEEP) for i = 1, 40, 1 do R(2 + (i * 5) % 44, 46, 1, 1, DEEP) end for _, w in ipairs({ { 5, 30 }, { 7, 38 }, { 23, 24 }, { 24, 36 }, { 34, 20 }, { 36, 30 }, { 38, 38 }, { 44, 32 }, { 15, 36 }, { 18, 38 }, { 9, 48 }, { 28, 49 }, { 43, 46 }, { 5, 47 } }) do R(w[1], w[2], 1, 2, DEEP) end end local FLAGS = { { 36, 4 }, { 25, 2 }, { 46, 13 }, { 7, 9 }, } local POSE = { { { 0, 0, 5, 3 } }, { { 0, 0, 2, 3 }, { 2, 0, 3, 2 } }, { { 0, 0, 5, 2 }, { 0, 2, 3, 1 } }, { { 0, 0, 2, 3 }, { 2, 1, 3, 2 } }, } local DYN = { { 6, 6, 24, 0.045 }, { 30, 20, 18, -0.035 }, { 50, 40, 16, 0.03 } } local function dyn_cloud(c, t, cam) local L = c[3] local x = (c[1] + t * c[4]) % (W + L) - L local y = c[2] - cam if y < -4 or y > H then return end local ix = floor(x) local f = x - ix local function row(dx, dy, len, a) blend_rect(ix + dx + 1, y + dy, len - 1, 1, 255, 255, 255, a) blend_rect(ix + dx, y + dy, 1, 1, 255, 255, 255, a * (1 - f)) blend_rect(ix + dx + len, y + dy, 1, 1, 255, 255, 255, a * f) end row(3, 0, L - 6, 0.35); row(0, 1, L, 0.4); row(2, 2, L - 4, 0.3) end local function rider(wy, cam, dir, fade, step) local cx = floor(RX[wy] + roadw(wy) / 2) local sy = wy - cam local function bx(dx, w) return dir > 0 and cx + dx or cx - dx - w + 1 end rect(bx(-2, 5), sy - 3, 5, 2, 122, 78, 48) rect(bx(-2, 1) + (step and 0 or dir), sy - 1, 1, 1, 90, 56, 36) rect(bx(2, 1) - (step and 0 or dir), sy - 1, 1, 1, 90, 56, 36) rect(bx(3, 2), sy - 5, 2, 3, 122, 78, 48) rect(bx(3, 1), sy - 5, 1, 2, 66, 40, 30) rect(bx(-1, 2), sy - 6, 2, 3, 206, 60, 44) rect(bx(-1, 2), sy - 8, 2, 2, 214, 184, 150) px(bx(-1, 1), sy - 9, 80, 84, 110) if fade > 0 then blend_rect(cx - 5, sy - 10, 11, 11, 66, 60, 88, fade) end end local TILE, TMP, step = {}, nil, 0 local ASC, CYC = 720, 0 local WAIT, HOLD, DESC = 45, 120, 240 local STAGES = { stage_base, stage_trees, stage_castle } function on_settings() ASC = ({ 40, 32, 24, 18, 12 })[cfg.pace or 3] * 30 CYC = WAIT + ASC + HOLD + DESC end local function ease(p) return (1 - cos(PI * p)) / 2 end function render() if step < 9 then local k, s = 2 - step // 3, step % 3 OY = k * 64 if s > 0 then gfx.blit_rgb(TMP, 0, 0, W, H) end STAGES[s + 1]() TMP = gfx.snapshot(0, 0, W, H) if s == 2 then TILE[k] = TMP end step = step + 1 if step < 9 then gfx.ready(false) end if not TILE[2] then return end end local t = (frame + WAIT + ASC - 40) % CYC local cam, wy = TOP, nil if t < WAIT then wy = 200 - 26 * t // WAIT elseif t < WAIT + ASC then local e = ease((t - WAIT) / ASC) cam, wy = floor(TOP * (1 - e) + 0.5), floor(174 - 118 * e + 0.5) elseif t < WAIT + ASC + HOLD then cam, wy = 0, 56 else cam = floor(TOP * ease((t - WAIT - ASC - HOLD) / DESC) + 0.5) end for k = 0, 2 do local ty = k * 64 - cam if TILE[k] and ty > -64 and ty < H then gfx.blit_rgb(TILE[k], 0, ty, W, H) end end for i = 1, #DYN do dyn_cloud(DYN[i], frame, cam) end for i = 1, #FLAGS do local f = FLAGS[i] local fy = f[2] - cam if fy > -4 and fy < H then local P = POSE[(frame // 5 + i) % 4 + 1] for j = 1, #P do rect(f[1] + 1 + P[j][1], fy + P[j][2], P[j][3], P[j][4], FLAG[1], FLAG[2], FLAG[3]) end rect(f[1] + 1, fy + 2, 4, 1, FLAG_D[1], FLAG_D[2], FLAG_D[3]) end end if cfg.rider ~= false and wy and t < WAIT + ASC + 40 then local fade = t > WAIT + ASC and (t - WAIT - ASC) / 40 or 0 local d = RX[wy < 60 and 56 or wy - 4] < RX[wy + 4] and -1 or 1 rider(wy, cam, d, fade, (frame // 6) % 2 == 0) end end