info = { type = "image", title = "Rail gate", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "A train passes a level crossing, a car waits", api = 33, } local tr = ({ sk = { ["Train"] = "Vlak", ["Time of day"] = "Denná doba", ["Passenger"] = "Osobný", ["Freight"] = "Nákladný", ["Day"] = "Deň", ["Dusk"] = "Súmrak" }, cs = { ["Train"] = "Vlak", ["Time of day"] = "Denní doba", ["Passenger"] = "Osobní", ["Freight"] = "Nákladní", ["Day"] = "Den", ["Dusk"] = "Soumrak" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "train", label = T("Train"), type = "int", default = "passenger", opts = { keys = { "passenger", "freight" }, T("Passenger"), T("Freight") } }, { key = "time", label = T("Time of day"), type = "int", default = "dusk", opts = { keys = { "day", "dusk" }, T("Day"), T("Dusk") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, min, max = math.floor, math.sin, math.cos, math.min, math.max local W, H = gfx.W, gfx.H local HORIZON = 34 local PIVX, PIVY, ARM = 21, 51, 21 local UP = 1.4 local MODES = { dusk = { top = { 34, 32, 98 }, hor = { 250, 150, 86 }, trees = { 26, 30, 58 }, f1 = { 36, 52, 52 }, f2 = { 24, 38, 40 }, road = { 66, 66, 84 }, dash = { 214, 196, 120 }, ballast = { 92, 86, 92 }, post = { 84, 84, 98 }, win = { 255, 224, 120 }, loco = { 190, 52, 52 }, coach = { 200, 204, 220 }, band = { 56, 88, 160 }, wag = { { 134, 76, 48 }, { 98, 100, 116 }, { 152, 112, 62 }, { 74, 90, 98 } }, diesel = { 206, 122, 40 }, car = { 70, 150, 204 }, night = true, }, day = { top = { 74, 150, 240 }, hor = { 176, 220, 250 }, trees = { 60, 120, 70 }, f1 = { 96, 170, 76 }, f2 = { 70, 140, 60 }, road = { 110, 112, 122 }, dash = { 244, 234, 184 }, ballast = { 140, 132, 124 }, post = { 120, 120, 134 }, win = { 150, 200, 240 }, loco = { 214, 66, 60 }, coach = { 232, 234, 244 }, band = { 70, 110, 190 }, wag = { { 176, 100, 60 }, { 140, 144, 160 }, { 196, 150, 80 }, { 100, 124, 136 } }, diesel = { 232, 150, 50 }, car = { 70, 150, 204 }, night = false, }, } local M = MODES.dusk local RAIL, SLEEP, DARK = { 178, 184, 198 }, { 58, 42, 36 }, { 30, 32, 44 } local LAMP_ON, LAMP_OFF = { 255, 70, 56 }, { 112, 32, 36 } local V, TLEN = 1.5, 86 local T_LIGHT, T_GD, T_TR, T_TE, T_GU, T_LOFF, T_CD, CYC local BG local CDUR = 140 function on_settings() M = MODES[cfg.time] or MODES.dusk local freight = cfg.train == "freight" V, TLEN = freight and 1.0 or 1.5, freight and 100 or 86 T_LIGHT, T_GD, T_TR = 70, 100, 165 T_TE = T_TR + floor((W + TLEN) / V) + 1 T_GU = T_TE + 40 T_LOFF = T_GU + 70 T_CD = T_LOFF + 25 CYC = T_CD + CDUR + 40 BG = nil end local function hw(y) return 2 + (y - HORIZON) * 0.43 end local function build_bg() local top, hor = M.top, M.hor for y = 0, HORIZON do local t = y / HORIZON rect(0, y, W, 1, floor(top[1] + (hor[1] - top[1]) * t + 0.5), floor(top[2] + (hor[2] - top[2]) * t + 0.5), floor(top[3] + (hor[3] - top[3]) * t + 0.5)) end for y = HORIZON + 1, H - 1 do local t = (y - HORIZON) / (H - HORIZON) rect(0, y, W, 1, floor(M.f1[1] + (M.f2[1] - M.f1[1]) * t + 0.5), floor(M.f1[2] + (M.f2[2] - M.f1[2]) * t + 0.5), floor(M.f1[3] + (M.f2[3] - M.f1[3]) * t + 0.5)) end for x = 0, W - 1 do local h = 2 + floor(2 * sin(x * 0.45) + 1.5 * sin(x * 1.1 + 1) + 1.5) rect(x, HORIZON - h + 1, 1, h, M.trees[1], M.trees[2], M.trees[3]) end if M.night then for _, p in ipairs({ { 6, 33 }, { 11, 33 }, { 50, 32 }, { 57, 33 } }) do px(p[1], p[2], 255, 214, 110) end end for y = HORIZON + 1, H - 1 do local w = hw(y) rect(floor(32 - w), y, floor(2 * w), 1, M.road[1], M.road[2], M.road[3]) if y % 6 < 3 then px(32, y, M.dash[1], M.dash[2], M.dash[3]) end end rect(0, 45, W, 6, M.ballast[1], M.ballast[2], M.ballast[3]) for x = 0, W - 1, 4 do rect(x, 46, 1, 3, SLEEP[1], SLEEP[2], SLEEP[3]) end local rw = hw(47) rect(floor(32 - rw), 45, floor(2 * rw), 6, M.road[1], M.road[2], M.road[3]) rect(0, 46, W, 1, RAIL[1], RAIL[2], RAIL[3]) rect(0, 48, W, 1, RAIL[1], RAIL[2], RAIL[3]) rect(19, 42, 1, 14, M.post[1], M.post[2], M.post[3]) rect(16, 44, 7, 1, M.post[1], M.post[2], M.post[3]) rect(45, 47, 1, 9, M.post[1], M.post[2], M.post[3]) rect(42, 46, 7, 1, M.post[1], M.post[2], M.post[3]) for _, p in ipairs({ { 17, 39 }, { 18, 40 }, { 19, 41 }, { 20, 42 }, { 21, 43 }, { 21, 39 }, { 20, 40 }, { 18, 42 }, { 17, 43 } }) do px(p[1], p[2], 232, 232, 240) end rect(20, 51, 2, 5, M.post[1] * 0.8 // 1, M.post[2] * 0.8 // 1, M.post[3] * 0.8 // 1) BG = gfx.snapshot(0, 0, W, H) end local function ease(q) if q <= 0 then return 0 elseif q >= 1 then return 1 end return q * q * (3 - 2 * q) end local function soft(x, y, w, h, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local ax, ay = 1 - fx, 1 - fy if w > 1 and h > 1 then rect(ix + 1, iy + 1, w - 1, h - 1, r, g, b) end if h > 1 then if ax > 0.03 then blend_rect(ix, iy + 1, 1, h - 1, r, g, b, ax) end if fx > 0.03 then blend_rect(ix + w, iy + 1, 1, h - 1, r, g, b, fx) end end if w > 1 then if ay > 0.03 then blend_rect(ix + 1, iy, w - 1, 1, r, g, b, ay) end if fy > 0.03 then blend_rect(ix + 1, iy + h, w - 1, 1, r, g, b, fy) end end if ax * ay > 0.03 then blend(ix, iy, r, g, b, ax * ay) end if fx * ay > 0.03 then blend(ix + w, iy, r, g, b, fx * ay) end if ax * fy > 0.03 then blend(ix, iy + h, r, g, b, ax * fy) end if fx * fy > 0.03 then blend(ix + w, iy + h, r, g, b, fx * fy) end end local function shade(c, k) return c[1] * k // 1, c[2] * k // 1, c[3] * k // 1 end local function wheels(x, len) rect(x + 1, 47, 3, 1, DARK[1], DARK[2], DARK[3]) rect(x + len - 4, 47, 3, 1, DARK[1], DARK[2], DARK[3]) end local function passenger_loco(x2) local x = x2 - 15 local c = M.loco rect(x, 40, 16, 7, c[1], c[2], c[3]) rect(x + 1, 39, 13, 1, shade(c, 0.7)) rect(x + 12, 40, 4, 7, shade(c, 0.8)) rect(x + 13, 41, 2, 2, M.win[1], M.win[2], M.win[3]) rect(x, 45, 16, 1, 244, 232, 200) wheels(x, 16) if M.night then px(x2, 44, 255, 244, 190) blend_rect(x2 + 1, 46, 9, 3, 255, 236, 160, 0.18) end end local function coach(x) local c = M.coach rect(x, 40, 14, 7, c[1], c[2], c[3]) rect(x, 45, 14, 1, M.band[1], M.band[2], M.band[3]) rect(x + 1, 41, 12, 2, M.win[1], M.win[2], M.win[3]) for k = 4, 12, 4 do rect(x + k, 41, 1, 2, c[1] * 0.6 // 1, c[2] * 0.6 // 1, c[3] * 0.6 // 1) end wheels(x, 14) end local function diesel(x2) local x = x2 - 13 local c = M.diesel rect(x, 41, 14, 6, c[1], c[2], c[3]) rect(x + 8, 39, 6, 2, shade(c, 0.8)) rect(x + 9, 40, 3, 1, M.win[1], M.win[2], M.win[3]) rect(x, 41, 8, 1, shade(c, 1.15)) rect(x, 45, 14, 1, shade(c, 0.6)) wheels(x, 14) if M.night then px(x2, 44, 255, 244, 190) blend_rect(x2 + 1, 46, 8, 3, 255, 236, 160, 0.18) end end local function wagon(x, i) local k = (i - 1) % 3 local c = M.wag[(i * 3 % 4) + 1] if k == 0 then rect(x, 40, 12, 7, c[1], c[2], c[3]) rect(x, 39, 12, 1, shade(c, 0.6)) rect(x + 5, 40, 1, 7, shade(c, 0.7)) elseif k == 1 then rect(x, 42, 12, 3, c[1], c[2], c[3]) rect(x + 1, 41, 10, 5, c[1], c[2], c[3]) rect(x + 1, 41, 10, 1, shade(c, 1.25)) rect(x, 46, 12, 1, DARK[1], DARK[2], DARK[3]) else rect(x, 41, 12, 5, c[1] * 0.7 // 1, c[2] * 0.7 // 1, c[3] * 0.7 // 1) rect(x + 2, 40, 8, 1, 22, 22, 26) rect(x + 3, 39, 6, 1, 22, 22, 26) rect(x, 46, 12, 1, DARK[1], DARK[2], DARK[3]) end wheels(x, 12) end local function draw_train(xf) if cfg.train == "freight" then diesel(xf) local x = xf - 14 - 12 local i = 1 while x > -12 do if x < W then wagon(x, i) end x = x - 13 i = i + 1 if i > 7 then break end end else passenger_loco(xf) local x = xf - 16 - 14 for i = 1, 5 do if x < W and x > -14 then coach(x) end x = x - 15 end end end local function draw_arm(a, lamp) local c, s = cos(a), sin(a) for r = -3, ARM do local x, y = PIVX + r * c, PIVY - r * s local ix, iy = floor(x + 0.5), floor(y + 0.5) local px2, py2 = floor(x + s + 0.5), floor(y + c + 0.5) local rr, gg, bb if r < 0 then rr, gg, bb = 40, 40, 52 elseif (r // 3) % 2 == 0 then rr, gg, bb = 224, 52, 48 else rr, gg, bb = 244, 244, 248 end px(ix, iy, rr, gg, bb) px(px2, py2, rr, gg, bb) end local tx, ty = floor(PIVX + ARM * c + 0.5), floor(PIVY - ARM * s + 0.5) local l = lamp and LAMP_ON or LAMP_OFF rect(tx, ty - 1, 2, 2, l[1], l[2], l[3]) end local function lamp(x, y, on) local l = on and LAMP_ON or LAMP_OFF rect(x, y, 2, 2, l[1], l[2], l[3]) if on then blend_rect(x - 1, y - 1, 4, 4, 255, 70, 56, 0.28) end end local function draw_car(y, brake) local x = 32 + hw(y) * 0.45 local c = M.car local tl = brake and 255 or 170 if y >= 52 then soft(x - 4, y, 8, 3, 40, 52, 80) soft(x - 6, y + 3, 12, 5, c[1], c[2], c[3]) soft(x - 6, y + 3, 12, 1, shade(c, 1.2)) soft(x - 6, y + 4, 2, 1, tl, 40, 36) soft(x + 4, y + 4, 2, 1, tl, 40, 36) soft(x - 5, y + 8, 2, 1, 20, 20, 26) soft(x + 3, y + 8, 2, 1, 20, 20, 26) elseif y >= 41 then soft(x - 3, y, 6, 2, 40, 52, 80) soft(x - 4, y + 2, 8, 3, c[1], c[2], c[3]) soft(x - 4, y + 3, 1, 1, tl, 40, 36) soft(x + 3, y + 3, 1, 1, tl, 40, 36) soft(x - 3, y + 5, 1, 1, 20, 20, 26) soft(x + 2, y + 5, 1, 1, 20, 20, 26) elseif y >= 35 then soft(x - 2, y, 4, 1, 40, 52, 80) soft(x - 2, y + 1, 4, 2, c[1], c[2], c[3]) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame local t = (f + 130) % CYC local cy if t < 50 then cy = 54 + 14 * (1 - ease(t / 50)) elseif t < T_CD then cy = 54 elseif t < T_CD + CDUR then cy = 54 - 19 * ease((t - T_CD) / CDUR) end if cy and cy < 70 then draw_car(cy, t < T_CD + 12) end if t >= T_TR and t <= T_TE then draw_train(floor(V * (t - T_TR) + 0.5)) end local a = UP if t >= T_GD and t < T_GD + 60 then a = UP * (1 - ease((t - T_GD) / 60)) elseif t >= T_GD + 60 and t < T_GU then local q = (t - T_GD - 60) / 25 a = q < 1 and 0.12 * sin(q * 9.4) * (1 - q) or 0 elseif t >= T_GU and t < T_GU + 60 then a = UP * ease((t - T_GU) / 60) end local lit = t >= T_LIGHT and t < T_LOFF local ph = (f // 14) % 2 == 0 draw_arm(a, lit and ph) lamp(16, 45, lit and ph) lamp(21, 45, lit and not ph) lamp(42, 47, lit and ph) lamp(47, 47, lit and not ph) end