info = { type = "image", title = "Rail town", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "A train rolls through an isometric town", api = 33, } local tr = ({ sk = { ["Night"] = "Noc", ["Train speed"] = "Rýchlosť vlaku", ["Carriages"] = "Vagóny" }, cs = { ["Night"] = "Noc", ["Train speed"] = "Rychlost vlaku", ["Carriages"] = "Vagóny" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "night", label = T("Night"), type = "bool", default = true }, { key = "speed", label = T("Train speed"), type = "int", min = 1, max = 3, default = 1 }, { key = "cars", label = T("Carriages"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin = math.floor, math.sin local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local OX, OY = 32, 34 local function P(i, j, z) return OX + 2 * (i - j), OY + i + j - z end local DAY = { grass = { 118, 170, 74 }, grass_d = { 92, 146, 60 }, pave = { 186, 182, 172 }, asph = { 96, 98, 108 }, dash = { 236, 226, 160 }, ballast= { 152, 140, 126 }, sleeper = { 106, 80, 60 }, rail = { 214, 218, 226 }, a_top = { 200, 194, 186 }, a_l = { 232, 220, 200 }, a_r = { 180, 164, 150 }, b_top = { 168, 96, 76 }, b_l = { 204, 118, 86 }, b_r = { 156, 84, 66 }, f_top = { 146, 152, 158 }, f_l = { 190, 194, 198 }, f_r = { 146, 150, 158 }, c_top = { 70, 60, 58 }, c_l = { 188, 96, 70 }, c_r = { 146, 72, 56 }, h_top = { 120, 150, 196 }, h_l = { 236, 210, 160 }, h_r = { 192, 166, 124 }, r1 = { 214, 98, 70 }, r2 = { 96, 140, 200 }, r3 = { 132, 170, 92 }, wall2 = { 226, 226, 214 }, wall2_r = { 180, 178, 166 }, rim = { 246, 242, 232 }, glass = { 104, 140, 186 }, door = { 80, 76, 84 }, trunk = { 118, 82, 56 }, leaf = { 70, 150, 72 }, leaf_d = { 50, 118, 58 }, leaf_l = { 120, 192, 96 }, pole = { 120, 124, 134 }, lamp = { 240, 236, 200 }, smoke = { 236, 236, 240 }, loco_l = { 222, 64, 52 }, loco_r = { 176, 44, 42 }, roof = { 96, 98, 108 }, car_l = { 238, 234, 222 }, car_r = { 192, 188, 178 }, car_t = { 126, 128, 136 }, stripe = { 222, 64, 52 }, under = { 60, 60, 68 }, taxi_l = { 236, 194, 62 }, taxi_r = { 190, 150, 40 }, taxi_t = { 250, 222, 120 }, blue_l = { 74, 126, 206 }, blue_r = { 54, 96, 164 }, blue_t = { 120, 166, 232 }, cab = { 70, 92, 130 }, } local function to_night(c) return { floor(c[1] * 0.52 + 6), floor(c[2] * 0.56 + 10), floor(c[3] * 0.62 + 40) } end local NIGHT = {} for k, c in pairs(DAY) do NIGHT[k] = to_night(c) end NIGHT.lamp = { 255, 226, 150 } NIGHT.glass = { 40, 50, 92 } NIGHT.smoke = { 150, 156, 186 } NIGHT.loco_l, NIGHT.loco_r, NIGHT.stripe = { 200, 60, 56 }, { 152, 44, 52 }, { 200, 60, 56 } NIGHT.car_l, NIGHT.car_r, NIGHT.car_t = { 156, 156, 176 }, { 116, 116, 140 }, { 86, 88, 112 } local C = DAY local LIT = { 255, 210, 116 } local SIG = { 255, 44, 32 } local SIGO = { 74, 30, 34 } local function c3(c) return c[1], c[2], c[3] end local function box(xT, yT, a, b, h, ct, cl, cr, rim) local x0 = xT - 2 * b for k = 0, a + b - 1 do local x = x0 + 2 * k if x > -2 and x < W then local top = k < b and (yT + b - 1 - k) or (yT + k - b) local bot = k < a and (yT + b + k) or (yT + 2 * a + b - 1 - k) rect(x, top, 2, bot - top + 1, ct[1], ct[2], ct[3]) if h > 0 then local c = k < a and cl or cr rect(x, bot + 1, 2, h, c[1], c[2], c[3]) end if rim then rect(x, bot, 2, 1, rim[1], rim[2], rim[3]) end end end end local function boxP(i0, j0, a, b, h, ct, cl, cr, rim) local x, y = P(i0, j0, h) box(x, y, a, b, h, ct, cl, cr, rim) end local function face_y(yT, a, b, h, k, z) local bot = k < a and (yT + b + k) or (yT + 2 * a + b - 1 - k) return bot + h - z end local WX, WY, WPER, WOFF, NWIN = {}, {}, {}, {}, 0 local function windows(i0, j0, a, b, h, zs) local xT, yT = P(i0, j0, h) local x0 = xT - 2 * b for _, z in ipairs(zs) do for k = 1, a + b - 2, 2 do if k ~= a - 1 and k ~= a then local x = x0 + 2 * k if x > -2 and x < W then NWIN = NWIN + 1 WX[NWIN], WY[NWIN] = x, face_y(yT, a, b, h, k, z) - 1 local hh = hash32(NWIN * 0x9E3779B1) WPER[NWIN] = 90 + (hh & 0xFF) WOFF[NWIN] = (hh >> 8) & 0x3FF end end end end end local BLD = { { -9, -21, 5, 4, 12, "h_top", "h_l", "h_r", { 3, 7 } }, { -20, -7, 4, 4, 6, "r1", "wall2", "wall2_r", { 2 } }, { -14, -10, 6, 6, 16, "a_top", "a_l", "a_r", { 3, 7, 11 } }, { -1, -9, 7, 5, 7, "f_top", "f_l", "f_r", nil }, { 7, -6, 2, 2, 12, "c_top", "c_l", "c_r", nil }, } local FRONT = { { -6, 11, 6, 5, 7, "r2", "wall2", "wall2_r", { 2 } }, { 4, 11, 6, 6, 6, "r1", "b_l", "b_r", { 2 } }, { 14, 11, 7, 5, 8, "r3", "wall2", "wall2_r", { 2, 5 } }, } for _, B in ipairs(BLD) do if B[9] then windows(B[1], B[2], B[3], B[4], B[5], B[9]) end end local NBACK = NWIN for _, B in ipairs(FRONT) do windows(B[1], B[2], B[3], B[4], B[5], B[9]) end local SMX, SMY = P(8, -5, 12) SMY = SMY + 1 local BG = nil local function plate(i0, j0, a, b, c) boxP(i0, j0, a, b, 0, c, c, c) end local function tree(i, j, s) local x, y = P(i, j, 0) rect(x, y - 3, 1, 3, c3(C.trunk)) rect(x - 2, y - 7 - s, 5, 4 + s, c3(C.leaf)) rect(x - 1, y - 8 - s, 3, 6 + s, c3(C.leaf)) rect(x + 1, y - 6 - s, 2, 3 + s, c3(C.leaf_d)) px(x - 1, y - 7 - s, c3(C.leaf_l)); px(x - 2, y - 6 - s, c3(C.leaf_l)) end local function lamp(i, j) local x, y = P(i, j, 0) if C == NIGHT then blend_rect(x - 3, y - 1, 7, 2, 255, 220, 140, 0.22) blend_rect(x - 2, y - 11, 5, 3, 255, 220, 140, 0.35) end rect(x, y - 9, 1, 9, c3(C.pole)) rect(x - 1, y - 10, 2, 1, c3(C.lamp)) end local function signal_pole(x, y) rect(x, y - 9, 1, 9, c3(C.pole)) rect(x - 1, y - 11, 3, 2, 30, 30, 36) px(x - 1, y - 6, 236, 236, 236); px(x + 1, y - 6, 236, 236, 236) px(x, y - 5, 220, 40, 40) px(x - 1, y - 4, 236, 236, 236); px(x + 1, y - 4, 236, 236, 236) end local function build_bg() rect(0, 0, W, H, c3(C.grass)) plate(-40, -40, 80, 37, C.pave) plate(-40, 4, 80, 1, C.pave) plate(-40, 5, 80, 4, C.asph) plate(-40, 9, 80, 1, C.pave) for i = -40, 40, 3 do local x, y = P(i, 7, 0) if x > -2 and x < W then rect(x, y, 2, 1, c3(C.dash)) end end plate(-40, -2, 80, 5, C.ballast) plate(0, -4, 4, 9, C.asph) for i = -40, 40, 2 do if i < -1 or i > 4 then for j = -1, 2 do local x, y = P(i, j, 0) if x > -2 and x < W then rect(x, y, 2, 1, c3(C.sleeper)) end end end end for _, r in ipairs({ -1, 2 }) do for i = -40, 40 do local x, y = P(i, r, 0) if x > -2 and x < W then rect(x, y, 2, 1, c3(C.rail)) end end end tree(-24, -4, 0); tree(-6, -4, 1); tree(13, -4, 0); tree(15, -8, 1) lamp(-11, -3); lamp(8, -3) local bx, by = P(-1, -3, 0); signal_pole(bx, by) for _, B in ipairs(BLD) do boxP(B[1], B[2], B[3], B[4], B[5], C[B[6]], C[B[7]], C[B[8]], C.rim) end do local i0, j0, a, b, h = -1, -9, 7, 5, 7 local xT, yT = P(i0, j0, h) local x0 = xT - 2 * b for k = 1, 2 do rect(x0 + 2 * k, face_y(yT, a, b, h, k, 4), 2, 5, c3(C.door)) end for k = 4, 6 do rect(x0 + 2 * k, face_y(yT, a, b, h, k, 4), 2, 2, c3(C.glass)) end for k = a + 1, a + b - 2 do rect(x0 + 2 * k, face_y(yT, a, b, h, k, 4), 2, 2, c3(C.glass)) end end for _, B in ipairs(FRONT) do boxP(B[1], B[2], B[3], B[4], B[5], C[B[6]], C[B[7]], C[B[8]], C.rim) end tree(-10, 10, 0); tree(12, 10, 1); tree(24, 10, 0) for k = 1, NWIN do rect(WX[k], WY[k], 2, 2, c3(C.glass)) end BG = gfx.snapshot(0, 0, W, H) end local LA, LB, LH = 8, 3, 7 local CA, CH = 9, 6 local GAP = 1 local function car_xy(q, j0, h) return OX + q - 2 * j0, OY + q // 2 + j0 - h end local function draw_loco(q, night) local x, y = car_xy(q, -1, LH + 1) box(x, y, LA, LB, LH, C.roof, C.loco_l, C.loco_r) local x0 = x - 2 * LB for k = 0, LA - 1 do rect(x0 + 2 * k, face_y(y, LA, LB, LH, k, 0), 2, 1, c3(C.under)) rect(x0 + 2 * k, face_y(y, LA, LB, LH, k, 3), 2, 1, 250, 236, 200) end local g = night and LIT or C.cab rect(x0 + 2 * (LA - 2), face_y(y, LA, LB, LH, LA - 2, 6), 2, 2, c3(g)) for k = LA, LA + LB - 1 do rect(x0 + 2 * k, face_y(y, LA, LB, LH, k, 6), 2, 2, c3(g)) end local fx, fy = x0 + 2 * (LA + 1), face_y(y, LA, LB, LH, LA + 1, 2) px(fx, fy, 255, 246, 210); px(fx + 1, fy, 255, 246, 210) if night then for k = 1, 6 do blend_rect(fx + 2 * k, fy + k - 1, 3, 3, 255, 236, 170, 0.32 - k * 0.045) end end end local function draw_car(q, night) local x, y = car_xy(q, -1, CH + 1) box(x, y, CA, LB, CH, C.car_t, C.car_l, C.car_r) local x0 = x - 2 * LB for k = 0, CA - 1 do rect(x0 + 2 * k, face_y(y, CA, LB, CH, k, 0), 2, 1, c3(C.under)) rect(x0 + 2 * k, face_y(y, CA, LB, CH, k, 2), 2, 1, c3(C.stripe)) end local g = night and LIT or C.cab for k = 1, CA - 2, 2 do rect(x0 + 2 * k, face_y(y, CA, LB, CH, k, 4), 2, 2, c3(g)) end end local function draw_auto(q, j0, cl, cr, ct, fwd, night) local x, y = car_xy(q, j0, 2) box(x, y, 4, 2, 2, ct, cl, cr) local x2, y2 = car_xy(q + 2, j0, 4) box(x2, y2, 2, 2, 1, C.cab, C.cab, C.cab) if night then local x0 = x - 4 local lx, ly = x0 + 2 * 4, face_y(y, 4, 2, 2, 4, 1) if fwd then px(lx, ly, 255, 246, 200); px(lx + 2, ly - 1, 255, 246, 200) else px(lx, ly, 255, 40, 30); px(lx + 2, ly - 1, 255, 40, 30) end end end local function soft(x0, y0, x1, y1, r, g, b, a) local ix0, iy0, ix1, iy1 = floor(x0), floor(y0), floor(x1), floor(y1) local mw, mh = ix1 - ix0 - 1, iy1 - iy0 - 1 local wl, wr, wt, wb = ix0 + 1 - x0, x1 - ix1, iy0 + 1 - y0, y1 - iy1 if mw > 0 and mh > 0 then blend_rect(ix0 + 1, iy0 + 1, mw, mh, r, g, b, a) end if mh > 0 then if wl > 0.03 then blend_rect(ix0, iy0 + 1, 1, mh, r, g, b, a * wl) end if wr > 0.03 then blend_rect(ix1, iy0 + 1, 1, mh, r, g, b, a * wr) end end if mw > 0 then if wt > 0.03 then blend_rect(ix0 + 1, iy0, mw, 1, r, g, b, a * wt) end if wb > 0.03 then blend_rect(ix0 + 1, iy1, mw, 1, r, g, b, a * wb) end end end local NPUFF, LIFE = 7, 4 * STEP_HZ local function draw_smoke(t) local s = C.smoke for k = 0, NPUFF - 1 do local u = ((t + k * LIFE // NPUFF) % LIFE) / LIFE local x = SMX + 0.5 - u * 14 + sin(u * 6 + k * 1.7) * 1.2 local y = SMY - u * 18 local r = 1.2 + u * 2.6 soft(x - r, y - r, x + r, y + r, s[1], s[2], s[3], 0.75 * (1 - u)) end end local NIGHT_ON, SPEED, NCARS = true, 1, 3 local Q0, QEND, PERIOD = 0, 0, 1 function on_settings() NIGHT_ON = cfg.night ~= false SPEED = cfg.speed or 1 NCARS = cfg.cars or 3 C = NIGHT_ON and NIGHT or DAY BG = nil local len = 2 * (LA + GAP) + NCARS * 2 * (CA + GAP) Q0 = -2 * LA - 40 QEND = 66 + len PERIOD = (QEND - Q0) // SPEED + 3 * STEP_HZ end local function light(x, y, on) local c = on and SIG or SIGO px(x, y, c[1], c[2], c[3]) if on then blend_rect(x - 1, y - 1, 3, 3, 255, 60, 40, 0.35) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS local night = NIGHT_ON if night then for k = 1, NBACK do local sl = (t + WOFF[k]) // WPER[k] if (hash32(k * 0x9E3779B1 + sl * 0x85EBCA6B) & 0x7FFF) % 100 < 62 then rect(WX[k], WY[k], 2, 2, LIT[1], LIT[2], LIT[3]) end end end draw_smoke(t) local tl = t % PERIOD local q = Q0 + tl * SPEED local qtail = q - NCARS * 2 * (CA + GAP) local active = q > -46 and qtail < 12 local on = active and (t // 12) % 2 == 0 local bx, by = P(-1, -3, 0) light(bx - 1, by - 10, on); light(bx + 1, by - 10, active and not on) if q < QEND then for c = NCARS, 1, -1 do draw_car(q - c * 2 * (CA + GAP), night) end draw_loco(q, night) end local fx, fy = P(5, 4, 0) signal_pole(fx, fy) light(fx - 1, fy - 10, on); light(fx + 1, fy - 10, active and not on) local qa = (t * 1 + 40) % 200 - 90 draw_auto(qa, 5, C.taxi_l, C.taxi_r, C.taxi_t, true, night) local qb = 110 - (t + 120) % 230 draw_auto(qb, 7, C.blue_l, C.blue_r, C.blue_t, false, night) if night then for k = NBACK + 1, NWIN do local sl = (t + WOFF[k]) // WPER[k] if (hash32(k * 0x9E3779B1 + sl * 0x85EBCA6B) & 0x7FFF) % 100 < 62 then rect(WX[k], WY[k], 2, 2, LIT[1], LIT[2], LIT[3]) end end end end