info = { type = "image", title = "Rainy city", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Rain over a city street at day or night", api = 44, } permissions = { "location" } local tr = { ["Intensity"] = { "Intenzita", "Intenzita" }, ["„intense“ = downpour, „random“ = changes by itself"] = { "„silno“ = lejak, „náhodne“ = mení sa samo", "„silně“ = liják, „náhodně“ = mění se samo" }, ["random"] = { "náhodne", "náhodně" }, ["weak"] = { "slabo", "slabě" }, ["medium"] = { "stredne", "středně" }, ["intense"] = { "silno", "silně" }, ["Time of day"] = { "Denná doba", "Denní doba" }, ["night"] = { "noc", "noc" }, ["day"] = { "deň", "den" }, ["by time of day"] = { "podľa dennej doby", "podle denní doby" }, ["Lightning"] = { "Blesky", "Blesky" }, ["flashes over the city now and then"] = { "občas zablyskne nad mestom", "občas zableskne nad městem" }, ["City"] = { "Mesto", "Město" }, ["off = just sky, rain and the street"] = { "vypnuté = len obloha, dážď a ulica", "vypnuto = jen obloha, déšť a ulice" }, ["Car"] = { "Auto", "Auto" }, ["drives past now and then"] = { "občas prejde", "občas projede" }, ["Moon"] = { "Mesiac", "Měsíc" }, ["not visible in heavy rain"] = { "nie je vidieť pri silnom daždi", "není vidět při silném dešti" }, } local LI = ({ sk = 1, cs = 2 })[lang] local function T(s) local v = LI and tr[s]; return v and v[LI] or s end settings = { { key = "power", type = "int", default = "medium", label = T("Intensity"), hint = T("„intense“ = downpour, „random“ = changes by itself"), opts = { keys = { "random", "weak", "medium", "intense" }, T("random"), T("weak"), T("medium"), T("intense") } }, { key = "day", type = "int", default = "auto", label = T("Time of day"), opts = { keys = { "night", "day", "auto" }, T("night"), T("day"), T("by time of day") } }, { key = "lightning", type = "bool", default = true, label = T("Lightning"), hint = T("flashes over the city now and then") }, { key = "city", type = "bool", default = true, label = T("City"), hint = T("off = just sky, rain and the street") }, { key = "car", type = "bool", default = true, label = T("Car"), hint = T("drives past now and then") }, { key = "moon", type = "bool", default = true, label = T("Moon"), hint = T("not visible in heavy rain") }, } local px, blend, rect = gfx.px, gfx.blend, gfx.rect local floor, abs, cos, sqrt = math.floor, math.abs, math.cos, math.sqrt local hash32, umod = math.hash32, math.umod local schar, concat, unpack = string.char, table.concat, table.unpack local W, H, FPS = gfx.W, gfx.H, gfx.FPS local GROUND_Y = H - 13 local CURB_Y = GROUND_Y + 2 local ROAD_Y = GROUND_Y + 3 local GLOW_TOP = GROUND_Y - 16 local CL_BOT = GROUND_Y - 30 local SNAP_Y = CL_BOT + 1 local MOON_X, MOON_Y = W - 14, 9 local BOLT0, BOLT0_SPAN, CAR0, CAR0_SPAN, WET0 = 5 * FPS, 12 * FPS, 7 * FPS, 30 * FPS, 0.6 local SKY = { { 0, 0, 0 }, { 30, 90, 140 } } local GLOW = { { 24, 112, 146 }, { 100, 190, 240 } } local CLOUD = { { 0, 0, 0 }, { 34, 72, 120 } } local CRIM = { { 22, 80, 106 }, { 100, 186, 238 } } local C_FAR = { { 58, 24, 24 }, { 108, 54, 40 } } local C_NEAR = { { 0, 0, 0 }, { 74, 34, 28 } } local ROOF = { { 0, 0, 0 }, { 132, 46, 32 } } local WALK = { { 10, 46, 62 }, { 36, 76, 110 } } local CURB = { { 22, 84, 110 }, { 76, 136, 180 } } local ROAD = { { 8, 40, 56 }, { 20, 52, 84 } } local ROADB = { { 0, 0, 0 }, { 20, 52, 84 } } local C_PUD = { { 16, 78, 104 }, { 54, 124, 172 } } local POLE = { { 0, 0, 0 }, { 16, 34, 44 } } local DROP = { { 140, 210, 250 }, { 190, 240, 255 } } local SPRAY = { { 160, 225, 255 }, { 210, 245, 255 } } local AWN1 = { { 84, 22, 16 }, { 210, 60, 50 } } local AWN2 = { { 92, 84, 74 }, { 235, 225, 205 } } local GLASS_F, GLASS_N = { 46, 84, 124 }, { 32, 62, 98 } local WIN_ON, SHOP, TV = { 255, 205, 110 }, { 255, 190, 120 }, { 120, 170, 255 } local LAMP, LAMP_HOT = { 255, 214, 140 }, { 255, 236, 180 } local BOLT_COL, FLASH, FLASH_HI = { 240, 236, 255 }, { 120, 100, 230 }, { 180, 170, 255 } local FR, FG, FB = 120, 100, 230 local DROPS = { 18, 32, 54 } local SPEED = { 1.8, 2.5, 3.4 } local LVL = { random = 0, weak = 1, medium = 2, intense = 3 } local function mixc(ar, ag, ab, br, bg, bb, t) return floor(ar + (br - ar) * t), floor(ag + (bg - ag) * t), floor(ab + (bb - ab) * t) end local function mixp(p, t) local a, b = p[1], p[2] return floor(a[1] + (b[1] - a[1]) * t), floor(a[2] + (b[2] - a[2]) * t), floor(a[3] + (b[3] - a[3]) * t) end local function flashed(k, r, g, b) if k <= 0 then return r, g, b end return mixc(r, g, b, FR, FG, FB, k) end local rng = 0x1F123BB5 local function rnd() rng = (rng ~ (rng << 13)) & 0xFFFFFFFF rng = rng ~ (rng >> 17) rng = (rng ~ (rng << 5)) & 0xFFFFFFFF return rng end local function frnd(a, b) return a + (b - a) * (umod(rnd(), 1000) / 999.0) end local function amb_lerp(v, n, t) if t <= 0 then return v[1] end if t >= n - 1 then return v[n] end local i = floor(t) return v[i + 1] + (v[i + 2] - v[i + 1]) * (t - i) end local a_cur, a_tgt, a_hold = 1.0, 1.0, 0 local function auto_level(opt, n) if opt > 0 and opt <= n then a_cur, a_tgt, a_hold = opt - 1, opt - 1, 0; return a_cur end if a_hold == 0 then local span = n - 1 for i = 1, 8 do local t = umod(rnd(), 1000) / 999.0 * span if abs(t - a_tgt) >= span * 0.30 then a_tgt = t; break end if i == 8 then a_tgt = span - a_tgt end end a_hold = FPS * 25 + umod(rnd(), FPS * 60) end a_hold = a_hold - 1 a_cur = a_cur + (a_tgt - a_cur) * 0.0032 return a_cur end local FAR = { { 2, 10, 20, 1 }, { 14, 10, 24, 2 }, { 29, 12, 18, 3 }, { 45, 9, 22, 0 }, { 56, 8, 17, 0 } } local NEAR = { { 0, 8, 10, 5 }, { 9, 8, 13, 0 }, { 21, 11, 9, 4 }, { 35, 9, 12, 5 }, { 50, 14, 8, 6 } } for _, L in ipairs({ FAR, NEAR }) do for i = 1, #L do local b = L[i]; b.top = GROUND_Y - b[3] end end local ANT_X, ANT_Y = FAR[2][1] + 4, FAR[2].top - 4 local LP, LY = 44, GROUND_Y - 15 local LXF = LP + 3.5 local function near_covers(x, y) for i = 1, #NEAR do local b = NEAR[i] local x0, x1 = b[1], b[1] + b[2] - 1 if x >= x0 and x <= x1 then if y >= b.top then return true end local k = b.top - 1 - y if b[4] == 4 and k <= 4 and x >= x0 + k and x <= x1 - k then return true end if b[4] == 5 and k <= 2 and x >= x1 - 2 and x <= x1 - 1 then return true end end end return false end local WX, WY, WON, NW, NWF, TVW = {}, {}, {}, 0, 0, 0 for li, L in ipairs({ FAR, NEAR }) do for i = 1, #L do local b = L[i] if b[4] ~= 6 then for y = b.top + 2, GROUND_Y - 3, 3 do for x = b[1] + 2, b[1] + b[2] - 3, 2 do if li == 2 or not near_covers(x, y) then NW = NW + 1 WX[NW], WY[NW] = x, y WON[NW] = umod(hash32(x * 73 + y * 151), 100) < 45 if li == 2 and i == 2 and TVW == 0 and y > b.top + 2 then TVW = NW; WON[NW] = false end end end end end end if li == 1 then NWF = NW end end local SHOP_B = NEAR[5] local SHOP_X0, SHOP_X1, SHOP_Y = SHOP_B[1] + 2, SHOP_B[1] + SHOP_B[2] - 6, SHOP_B.top + 3 local PUD = { { H - 6, 6, 14 }, { H - 5, 4, 16 }, { H - 3, 38, 47 }, { H - 2, 36, 50 } } local function in_puddle(x, y) for i = 1, #PUD do local p = PUD[i] if y == p[1] and x >= p[2] and x <= p[3] then return true end end return false end local MAX_DROP, N_SPLASH = 54, 16 local lvl, wet = 1.0, WET0 local wind, wind_target, wind_next = 0.28, 0.28, 0 local flash = 0 local bolt_on, bolt_until, next_bolt, burst = false, 0, 0, 0 local moon_on, moon_ph = false, 0.0 local day_now, lamp_on = 0.0, false local seeded, last_frame = false, 0 local SCENE, scene_key, PART = nil, nil, nil local N_CL, CL_HW, P = 3, 14, 2 local NC = 2 * CL_HW + 1 local CL_SPAN = W + 2 * CL_HW + 2 local cloud = {} for k = 1, N_CL do cloud[k] = { x = 0, y = 0, sp = 0, top = {}, bot = {}, rim = {}, mb = {}, mr = {} } end local function cloud_shape(c, yw) local n, dm, sm = 3 + umod(rnd(), 3), {}, {} c.top, c.bot, c.rim = {}, {}, {} local pb, pt = frnd(0, 6.28), frnd(0, 6.28) for k = 1, n do local w = frnd(3.0, 5.5) dm[k] = { cx = -8 + 16 * (k - 1) / (n - 1) + frnd(-1.5, 1.5), w = w, h = w * frnd(0.90, 1.60) } end local lo, hi = 99, -99 for i = 1, NC do local u, t, b = i - CL_HW - 1, nil, nil for k = 1, n do local d = dm[k] local q = (u - d.cx) / d.w if q > -1 and q < 1 then local hh = d.h * sqrt(1 - q * q) if not t or -hh * 0.75 < t then t = -hh * 0.75 end if not b or hh > b then b = hh end end end sm[i] = b if t then c.top[i] = floor(t - 0.4 * math.sin(u * 0.7 + pt)) c.bot[i] = floor(b + 0.5 * math.sin(u * 0.9 + pb)) if c.top[i] < lo then lo = c.top[i] end if c.bot[i] > hi then hi = c.bot[i] end else c.top[i], c.bot[i] = 0, -1 end end for i = 1, NC do local a, b = sm[i], sm[i + 1] or sm[i - 1] local s = (a and b) and abs(a - b) or 3.0 c.rim[i] = s > 1.5 and 0.6 or (s > 0.5 and 0.8 or 1.0) end c.y = yw if c.y + hi > CL_BOT then c.y = CL_BOT - hi end c.r0, c.r1 = lo, hi c.mb, c.mr = {}, {} end local function cloud_mask(c, p) local f = p / P local g = 1 - f local top, bot, rim = c.top, c.bot, c.rim local MW = NC + 1 local B, R, rb, rr = {}, {}, {}, {} for r = c.r0, c.r1 do for j = 1, MW do local b, m = 0, 0 for s = 0, 1 do local i, wt = j - s, (s == 0) and g or f if wt > 0 and i >= 1 and i <= NC and r >= top[i] and r <= bot[i] then b = b + wt local d = bot[i] - r if d <= 3 and umod(hash32(i * 131 + r * 17), 100) < 100 - 25 * d then m = m + wt * rim[i] end end end rb[j], rr[j] = floor(b * 255 + 0.5), floor(m * 255 + 0.5) end B[#B + 1] = schar(unpack(rb, 1, MW)) R[#R + 1] = schar(unpack(rr, 1, MW)) end c.mb[p], c.mr[p] = concat(B), concat(R) if c.mb[1 - p] then c.top, c.bot, c.rim = nil, nil, nil end end local function draw_clouds(dq, fl) local br, bg, bb = mixp(CLOUD, dq) local rr, rg, rb = mixp(CRIM, dq) if fl > 0 then br, bg, bb = mixc(br, bg, bb, FLASH_HI[1], FLASH_HI[2], FLASH_HI[3], (0.8 - 0.35 * dq) * fl) rr, rg, rb = mixc(rr, rg, rb, FLASH_HI[1], FLASH_HI[2], FLASH_HI[3], 0.8 * fl) end local MW = NC + 1 for k = 1, N_CL do local c = cloud[k] local ix = floor(c.x) local p = floor((c.x - ix) * P) if p >= P then p = P - 1 end if not c.mb[p] then cloud_mask(c, p) end local MH = c.r1 - c.r0 + 1 gfx.blend_mask(c.mb[p], br, bg, bb, ix - CL_HW, c.y + c.r0, MW, MH, MW, MH, 1.0) gfx.blend_mask(c.mr[p], rr, rg, rb, ix - CL_HW, c.y + c.r0, MW, MH, MW, MH, 1.0) end end local function step_clouds() for k = 1, N_CL do local c = cloud[k] c.x = c.x + c.sp * (0.45 + wind) if c.x >= W + CL_HW + 1 then c.x = c.x - CL_SPAN elseif c.x < -CL_HW - 1 then c.x = c.x + CL_SPAN end end end local CAR_W, CAR_H = 15, 6 local CAR_TOP = H - 8 local CAR_ART = { "......#####....", "....##+++++##..", "..#############", "###############", "###############", ".##oo#####oo##.", } local CAR_TOPS = { 3, 3, 2, 2, 1, 1, 0, 0, 0, 0, 0, 1, 1, 2, 2 } local CAR_BODY = { { 225, 70, 60 }, { 70, 140, 235 }, { 235, 200, 90 }, { 214, 220, 228 } } local CAR_GLASS, CAR_TYRE = { 140, 215, 250 }, { 18, 18, 24 } local CAR_HEAD, CAR_TAIL = { 255, 240, 190 }, { 235, 60, 45 } local CAR_PAL = { 0, 0, 0, 0, 0, 0, 0, 0, 0, CAR_TYRE[1], CAR_TYRE[2], CAR_TYRE[3] } local CAR_IDX = { ["."] = 0, ["#"] = 1, ["+"] = 2, ["o"] = 3 } local function car_cells(mirror) local rows, out = {}, {} for r = 1, CAR_H do local s = CAR_ART[r] for c = 1, CAR_W do local k = mirror and (CAR_W + 1 - c) or c out[c] = CAR_IDX[s:sub(k, k)] end rows[r] = schar(unpack(out, 1, CAR_W)) end return concat(rows) end local CAR_R, CAR_L = car_cells(false), car_cells(true) local car = { on = false, x = 0, sp = 1, right = true, col = 1 } local next_car = 0 local LAND0, LANDN = { GROUND_Y, ROAD_Y, H - 5 }, { 3, 6, 5 } local DX, DY, DS, DL, DLD = {}, {}, {}, {}, {} for i = 1, MAX_DROP do DX[i], DY[i], DS[i], DL[i], DLD[i] = 0, 0, 1, 0, GROUND_Y end local SON, SX, SY, SR, SRM, SPUD, SD, SDX, SDY, SVX, SVY = {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} for i = 1, N_SPLASH do SON[i], SD[i] = false, false end local function reset_drop(i, anywhere) local l = umod(rnd(), 3) DL[i], DS[i] = l, (1.1 + 0.5 * l) * amb_lerp(SPEED, 3, lvl) DLD[i] = LAND0[l + 1] + umod(rnd(), LANDN[l + 1]) if anywhere then DX[i] = frnd(-4, W + 4); DY[i] = frnd(0, DLD[i]); return end if frnd(0, W + abs(wind) * GROUND_Y) < W then DX[i], DY[i] = frnd(-2, W + 2), frnd(-14, -1) else DX[i] = (wind >= 0) and frnd(-9, -1) or frnd(W + 1, W + 9) DY[i] = frnd(-6, GROUND_Y - 8) end end local function hit_y(i) if DL[i] == 1 and car.on then local c = floor(DX[i] + 0.5) - car.x if c >= 0 and c < CAR_W then return CAR_TOP + CAR_TOPS[(car.right and c or (CAR_W - 1 - c)) + 1] end end return DLD[i] end local function add_splash(x, y, layer) for i = 1, N_SPLASH do if not SON[i] then x = floor(x + 0.5) SON[i], SX[i], SY[i], SR[i], SRM[i] = true, x, y, 0, 1.5 + 0.75 * layer SPUD[i] = in_puddle(x, y) SD[i] = layer == 2 and not SPUD[i] if SD[i] then SDX[i], SDY[i], SVX[i], SVY[i] = 0, 0, frnd(0.25, 0.5), -frnd(0.7, 1.0) end return end end end local function drop_count() return floor(amb_lerp(DROPS, 3, lvl) * (0.45 + 0.55 * wet)) end local function step_drops() for i = 1, drop_count() do DY[i] = DY[i] + DS[i] DX[i] = DX[i] + wind * DS[i] local hy = hit_y(i) if DY[i] >= hy then add_splash(DX[i], hy, DL[i]); reset_drop(i, false) elseif DX[i] > W + 6 or DX[i] < -6 then reset_drop(i, false) end end end local function step_splash() for i = 1, N_SPLASH do if SON[i] then SR[i] = SR[i] + (SPUD[i] and 0.35 or 0.7) if SD[i] then SDX[i], SDY[i], SVY[i] = SDX[i] + SVX[i], SDY[i] + SVY[i], SVY[i] + 0.15 if SDY[i] > 0 then SD[i] = false end end if SR[i] > (SPUD[i] and 3.0 or SRM[i]) and not SD[i] then SON[i] = false end end end end local function draw_drops(l0, l1, day) local dr, dg, db = mixp(DROP, day) local lr, lg, lb = mixc(dr, dg, db, LAMP[1], LAMP[2], LAMP[3], 0.65) local lit = lamp_on for i = 1, drop_count() do local l = DL[i] if l >= l0 and l <= l1 then local a, qx, qy = 0.35 + 0.25 * l, DX[i], floor(DY[i] + 0.5) for k = 0, 2 + l do local y = qy - k if y >= 0 and y < H then local x = floor(qx - k * wind + 0.5) local ak = a * (1.0 - k * 0.22) if ak < 0.25 then ak = 0.25 end if lit and y > LY and abs(x - LXF) <= 0.5 + (y - LY) * 0.5 then blend(x, y, lr, lg, lb, ak + 0.3) else blend(x, y, dr, dg, db, ak) end end end end end end local function draw_splash(day) local cr, cg, cb = mixp(SPRAY, day) for i = 1, N_SPLASH do if SON[i] then local x, y, s = SX[i], SY[i], SR[i] local r = floor(s + 0.5) if SPUD[i] then local a = 0.55 * (1.0 - s / 3.0) if a >= 0.25 then if r == 0 then blend(x, y, cr, cg, cb, a) else blend(x - r, y, cr, cg, cb, a); blend(x + r, y, cr, cg, cb, a) end end elseif s <= SRM[i] then local a = 0.25 + 0.35 * (1.0 - s / SRM[i]) blend(x - r, y, cr, cg, cb, a) blend(x + r, y, cr, cg, cb, a) if r <= 1 then blend(x, y - 1, cr, cg, cb, a) end end if SD[i] then local dx, dy = floor(SDX[i] + 0.5), y + floor(SDY[i] + 0.5) blend(x - dx, dy, cr, cg, cb, 0.6) blend(x + dx, dy, cr, cg, cb, 0.6) end end end end local BX, BY, BN, RX, RY, RN = {}, {}, 0, {}, {}, 0 local function make_bolt() local c = cloud[1 + umod(rnd(), N_CL)] local x = floor(c.x + 0.5) + umod(rnd(), 9) - 4 if x < 4 then x = 4 elseif x > W - 5 then x = W - 5 end local y = c.y + 3 local tx, ty if cfg.city and umod(rnd(), 3) == 0 then tx, ty = ANT_X, ANT_Y - 1 else tx = x + umod(rnd(), 17) - 8 ty = cfg.city and (GLOW_TOP + 6) or (GROUND_Y - 1) end BN = 1; BX[1], BY[1] = x, y while y < ty do local ny = y + 2 + umod(rnd(), 3) local nx if ny >= ty then ny, nx = ty, tx else nx = x + floor((tx - x) * (ny - y) / (ty - y) + 0.5) + umod(rnd(), 5) - 2 end BN = BN + 1; BX[BN], BY[BN] = nx, ny x, y = nx, ny end RN = 0 if BN >= 4 then local k = 2 + umod(rnd(), BN - 3) local dir = (rnd() & 1) == 1 and 1 or -1 x, y = BX[k], BY[k] RN = 1; RX[1], RY[1] = x, y for _ = 1, 3 do x, y = x + dir * (2 + umod(rnd(), 3)), y + 2 + umod(rnd(), 2) RN = RN + 1; RX[RN], RY[RN] = x, y end end end local function line(x0, y0, x1, y1, a) local dx, sx = abs(x1 - x0), (x0 < x1) and 1 or -1 local dy, sy = -abs(y1 - y0), (y0 < y1) and 1 or -1 local err = dx + dy while true do blend(x0, y0, BOLT_COL[1], BOLT_COL[2], BOLT_COL[3], a) if x0 == x1 and y0 == y1 then break end local e2 = 2 * err if e2 >= dy then err = err + dy; x0 = x0 + sx end if e2 <= dx then err = err + dx; y0 = y0 + sy end end end local function draw_bolt() for i = 2, BN do line(BX[i - 1], BY[i - 1], BX[i], BY[i], 1.0) end for i = 2, RN do line(RX[i - 1], RY[i - 1], RX[i], RY[i], 0.6) end end local function step_bolt() if bolt_on and frame > bolt_until then bolt_on = false end if frame < next_bolt then return end if burst == 0 then make_bolt() end bolt_on, bolt_until, flash = true, frame + 2, 1.0 if burst > 0 then burst = burst - 1; next_bolt = frame + FPS // 8 + umod(rnd(), FPS // 6) else burst = umod(rnd(), 3); next_bolt = frame + 7 * FPS + umod(rnd(), 27 * FPS) end end local function seed() for i = 1, MAX_DROP do reset_drop(i, true) end for i = 1, N_SPLASH do SON[i], SD[i] = false, false end for k = 1, N_CL do local c = cloud[k] c.x = -CL_HW + (k - 1) * CL_SPAN / N_CL + frnd(-3, 3) c.sp = (1.0 + 0.5 * k) / FPS cloud_shape(c, 4 + (k - 1) * 6 + umod(rnd(), 3)) end next_bolt = frame + BOLT0 + umod(rnd(), BOLT0_SPAN) next_car = frame + CAR0 + umod(rnd(), CAR0_SPAN) car.on, flash, bolt_on, burst = false, 0, false, 0 a_cur, a_tgt, a_hold = 1.0, 1.0, 0 scene_key = nil end local function daylight() local t, s = data.time, data.sun if not (t and t.ok and s) then return 0.0 end if s.polar then return s.up and 1.0 or 0.0 end local h = t.h + t.m / 60.0 local rise, set = s.rise, s.set if h <= rise - 1 or h >= set + 1 then return 0.0 end if h >= rise + 1 and h <= set - 1 then return 1.0 end if h < rise + 1 then return (h - rise + 1) / 2 end return (set + 1 - h) / 2 end local function day_mix() if cfg.day == "night" then return 0.0 end if cfg.day == "day" then return 1.0 end return daylight() end local function step_moon() local m = data.moon moon_ph = m and m.phase or 0.0 local vis = 0.85 * (1.0 - wet) * (1.0 - 2.0 * day_now) if not (cfg.moon and m) then moon_on = false elseif moon_on then if vis < 0.30 then moon_on = false end elseif vis >= 0.40 and (m.lit or 0) >= 0.06 then moon_on = true end end local MARE = { { -3, -3, 3 }, { 2, 1, 3 }, { -2, 4, 2 } } local ML, MM, MK = nil, nil, -1 local function draw_moon(ph) local key = floor(ph * 64) if key ~= MK then MK = key local c, waxing, a, b = cos(ph * 6.28318), ph < 0.5, {}, {} for dy = -5, 5 do local xr = sqrt(25 - dy * dy) for dx = -5, 5 do local i = (dy + 5) * 11 + dx + 6 a[i], b[i] = 0, 0 if dx * dx + dy * dy <= 25 and ((waxing and dx >= c * xr) or (not waxing and dx <= -c * xr)) then local mare = false for k = 1, 3 do local m = MARE[k] local ex, ey = dx - m[1], dy - m[2] if ex * ex + ey * ey <= m[3] * m[3] then mare = true; break end end if mare then b[i] = 217 else a[i] = 217 end end end end ML, MM = schar(unpack(a, 1, 121)), schar(unpack(b, 1, 121)) end gfx.blend_mask(ML, 225, 228, 235, MOON_X - 5, MOON_Y - 5, 11, 11, 11, 11, 1.0) gfx.blend_mask(MM, 186, 190, 200, MOON_X - 5, MOON_Y - 5, 11, 11, 11, 11, 1.0) end local function step_weather() wet = wet + (umod(rnd(), 2001) - 1000) / 1000.0 * 0.0057 if wet < 0.30 then wet = 0.30 elseif wet > 1.00 then wet = 1.00 end if frame >= wind_next then wind_target = frnd(-0.55, 0.55) wind_next = frame + 25 * FPS + umod(rnd(), 40 * FPS) end wind = wind + (wind_target - wind) * 0.0024 end local function step_car() if car.on then car.x = car.x + (car.right and car.sp or -car.sp) if car.x < -CAR_W - 2 or car.x > W + 2 then car.on = false next_car = frame + 35 * FPS + umod(rnd(), 65 * FPS) end return end if frame < next_car then return end car.on = true car.right = (rnd() & 1) == 1 car.sp = umod(rnd(), 3) == 0 and 2 or 1 car.col = umod(rnd(), #CAR_BODY) + 1 car.x = car.right and (-CAR_W - 1) or (W + 1) end local function step_windows() if frame % (FPS * 2 // 3) ~= 0 then return end local i = umod(rnd(), NW) + 1 if i ~= TVW and umod(rnd(), 100) < 35 then WON[i] = not WON[i]; scene_key = nil end end local function win_lit(i, dq) return WON[i] and (dq < 0.65 or WX[i] % 5 == 0) end local COLW = {} local function draw_street(dq, fl, hr, hg, hb) local k = 0.35 * fl rect(0, GROUND_Y, W, 2, flashed(k, mixp(WALK, dq))) rect(0, CURB_Y, W, 1, flashed(k, mixp(CURB, dq))) local tr, tg, tb = flashed(k, mixp(ROAD, dq)) local br, bg, bb = flashed(k, mixp(ROADB, dq)) local n = H - ROAD_Y for y = ROAD_Y, H - 1 do local j = y - ROAD_Y local r, g, b = mixc(tr, tg, tb, br, bg, bb, j / (n - 1)) if j < 6 then r, g, b = mixc(r, g, b, hr, hg, hb, dq * 0.55 * (1 - j / 6)) end rect(0, y, W, 1, r, g, b) end local pr, pg, pb = flashed(k, mixp(C_PUD, dq)) for i = 1, #PUD do local p = PUD[i]; rect(p[2], p[1], p[3] - p[2] + 1, 1, pr, pg, pb) end if not cfg.city then return end local lit = 1.0 - dq if lit > 0.02 then for x = 0, W - 1 do COLW[x] = 0 end for i = 1, NW do if win_lit(i, dq) then COLW[WX[i]] = COLW[WX[i]] + 1 end end for x = SHOP_X0, SHOP_X1 do COLW[x] = COLW[x] + 2 end for x = 0, W - 1 do if COLW[x] >= 2 then for j = 0, 3 do blend(x, ROAD_Y + j, WIN_ON[1], WIN_ON[2], WIN_ON[3], 0.36 * lit * (1.0 - j / 4)) end end end end if lamp_on then for j = 0, 8 do local a = 0.55 * (1.0 - j / 9) blend(LP + 3, ROAD_Y + j, LAMP[1], LAMP[2], LAMP[3], a) blend(LP + 4, ROAD_Y + j, LAMP[1], LAMP[2], LAMP[3], a) end end end local function draw_city(dq, fl) local k = 0.15 * fl local fr, fg, fb = flashed(k, mixp(C_FAR, dq)) local nr, ng, nb = flashed(k, mixp(C_NEAR, dq)) local lit = 1.0 - dq for i = 1, #FAR do local b = FAR[i] local x, w, t = b[1], b[2], b.top rect(x, t, w, b[3], fr, fg, fb) if b[4] == 1 then rect(x + 3, t - 4, 4, 3, fr, fg, fb); px(x + 3, t - 1, fr, fg, fb); px(x + 6, t - 1, fr, fg, fb) elseif b[4] == 2 then rect(x + 3, t - 1, 3, 1, fr, fg, fb); rect(ANT_X, ANT_Y + 1, 1, 3, fr, fg, fb) elseif b[4] == 3 then rect(x + 3, t - 3, w - 6, 3, fr, fg, fb) end end local gfr, gfg, gfb = mixc(fr, fg, fb, GLASS_F[1], GLASS_F[2], GLASS_F[3], dq) local gnr, gng, gnb = mixc(nr, ng, nb, GLASS_N[1], GLASS_N[2], GLASS_N[3], dq) local function windows(far) for i = far and 1 or NWF + 1, far and NWF or NW do do local x, y = WX[i], WY[i] if win_lit(i, dq) then if far then px(x, y, mixc(nr, ng, nb, WIN_ON[1], WIN_ON[2], WIN_ON[3], 0.45 + 0.55 * lit)) else px(x, y, mixc(fr, fg, fb, WIN_ON[1], WIN_ON[2], WIN_ON[3], 0.45 + 0.55 * lit)) end if lit > 0.02 then blend(x, y + 1, WIN_ON[1], WIN_ON[2], WIN_ON[3], 0.25 * lit) end elseif dq > 0.1 then if far then px(x, y, gfr, gfg, gfb) else px(x, y, gnr, gng, gnb) end end end end end windows(true) local rr, rg, rb = flashed(k, mixp(ROOF, dq)) for i = 1, #NEAR do local b = NEAR[i] local x, w, t = b[1], b[2], b.top rect(x, t, w, b[3], nr, ng, nb) if b[4] == 4 then for j = 0, 4 do rect(x + j, t - 1 - j, w - 2 * j, 1, rr, rg, rb) end elseif b[4] == 5 then rect(x + w - 3, t - 3, 2, 3, rr, rg, rb) elseif b[4] == 6 then local a1r, a1g, a1b = mixp(AWN1, dq) local a2r, a2g, a2b = mixp(AWN2, dq) for j = 0, w - 1, 2 do if (j // 2) % 2 == 0 then rect(x + j, t + 1, 2, 1, a1r, a1g, a1b) else rect(x + j, t + 1, 2, 1, a2r, a2g, a2b) end end rect(SHOP_X0, SHOP_Y, SHOP_X1 - SHOP_X0 + 1, 3, mixc(nr, ng, nb, SHOP[1], SHOP[2], SHOP[3], 0.55 + 0.45 * lit)) rect(x + w - 4, SHOP_Y, 2, GROUND_Y - SHOP_Y, mixc(nr, ng, nb, 0, 0, 0, 0.5)) end end windows(false) local pr, pg, pb = mixp(POLE, dq) if lamp_on then for x = LP - 5, LP + 12 do local a = 0.5 * (1.0 - abs(x - LXF) / 8.5) if a > 0 then blend(x, GROUND_Y, LAMP[1], LAMP[2], LAMP[3], a) blend(x, GROUND_Y + 1, LAMP[1], LAMP[2], LAMP[3], a * 0.7) end end end rect(LP, LY - 1, 1, GROUND_Y + 2 - LY + 1, pr, pg, pb) rect(LP + 1, LY - 1, 3, 1, pr, pg, pb) if lamp_on then rect(LP + 3, LY, 2, 1, LAMP_HOT[1], LAMP_HOT[2], LAMP_HOT[3]) blend(LP + 3, LY + 1, LAMP[1], LAMP[2], LAMP[3], 0.5) blend(LP + 4, LY + 1, LAMP[1], LAMP[2], LAMP[3], 0.5) blend(LP + 2, LY, LAMP[1], LAMP[2], LAMP[3], 0.35) blend(LP + 5, LY, LAMP[1], LAMP[2], LAMP[3], 0.35) else rect(LP + 3, LY, 2, 1, mixc(pr, pg, pb, 150, 150, 140, 0.5)) end end local function draw_scene(dq, fl, live, part) FR, FG, FB = mixc(FLASH[1], FLASH[2], FLASH[3], FLASH_HI[1], FLASH_HI[2], FLASH_HI[3], dq) local sr, sg, sb = flashed(0.60 * fl, mixp(SKY, dq)) local hr, hg, hb = flashed(0.60 * fl, mixp(GLOW, dq)) if part ~= 2 then rect(0, 0, W, GLOW_TOP, sr, sg, sb) local n = GROUND_Y - GLOW_TOP for y = GLOW_TOP, GROUND_Y - 1 do rect(0, y, W, 1, mixc(sr, sg, sb, hr, hg, hb, (y - GLOW_TOP + 1) / n)) end if moon_on then draw_moon(moon_ph) end if live then draw_clouds(dq, fl) if bolt_on then draw_bolt() end end end if part == 1 then return end draw_street(dq, fl, hr, hg, hb) if cfg.city then draw_city(dq, fl) end end local function draw_car(day) local x0 = car.x local bc = CAR_BODY[car.col] local nr, ng, nb = mixc(bc[1], bc[2], bc[3], 44, 16, 10, 0.45) local br, bg, bb = mixc(nr, ng, nb, bc[1], bc[2], bc[3], day) local xr, xg, xb = mixc(CAR_GLASS[1], CAR_GLASS[2], CAR_GLASS[3], 18, 56, 72, 0.5) CAR_PAL[4], CAR_PAL[5], CAR_PAL[6] = br, bg, bb CAR_PAL[7], CAR_PAL[8], CAR_PAL[9] = mixc(xr, xg, xb, CAR_GLASS[1], CAR_GLASS[2], CAR_GLASS[3], day) gfx.blit_indexed(car.right and CAR_R or CAR_L, CAR_PAL, x0, CAR_TOP, CAR_W, CAR_H, CAR_W, CAR_H, true) if car.col == 3 then px(x0 + 8, CAR_TOP - 1, 255, 150, 40) end local dir = car.right and 1 or -1 local fx = car.right and (x0 + CAR_W - 1) or x0 local bx = car.right and x0 or (x0 + CAR_W - 1) px(fx, CAR_TOP + 3, CAR_HEAD[1], CAR_HEAD[2], CAR_HEAD[3]) px(bx, CAR_TOP + 3, CAR_TAIL[1], CAR_TAIL[2], CAR_TAIL[3]) local k = 0.45 * (1.0 - 0.5 * day) for i = 1, 6 do blend(fx + dir * i, CAR_TOP + 5, CAR_HEAD[1], CAR_HEAD[2], CAR_HEAD[3], k * (1.0 - i / 7)) end gfx.blend_rect(x0, CAR_TOP + CAR_H, CAR_W, 1, br, bg, bb, 0.22) blend(bx, CAR_TOP + CAR_H, CAR_TAIL[1], CAR_TAIL[2], CAR_TAIL[3], 0.45) blend(fx, CAR_TOP + CAR_H, CAR_HEAD[1], CAR_HEAD[2], CAR_HEAD[3], 0.45) local sr, sg, sb = mixp(SPRAY, day) local wx = car.right and (x0 + 3) or (x0 + CAR_W - 4) for j = 1, 2 do local h = hash32(frame * 0x9E3779B1 + j) local dx, dy = 1 + umod(h, 4), umod(h >> 8, 3) blend(wx - dir * dx, CAR_TOP + 5 - dy, sr, sg, sb, 0.4 - 0.08 * dy) end end function on_settings() scene_key = nil if not cfg.car then car.on = false end end function update() if not seeded or frame < last_frame then seed(); seeded = true end last_frame = frame day_now = day_mix() lvl = auto_level(LVL[cfg.power] or 2, 3) if flash > 0 then flash = flash - 0.20; if flash < 0 then flash = 0 end end step_weather() step_clouds() if cfg.car then step_car() else car.on = false end step_drops() step_splash() if cfg.lightning then step_bolt() else bolt_on = false end step_windows() step_moon() end function render() if not seeded then seed(); seeded = true; day_now = day_mix() end local day = day_now local step = floor(day * 8 + 0.5) local dq = step / 8 lamp_on = cfg.city and dq <= 0.5 if flash > 0 then draw_scene(dq, flash, true) else local key = step + (cfg.city and 16 or 0) if key ~= scene_key or not SCENE then if not PART then draw_scene(dq, 0, false, 1) PART = gfx.snapshot(0, SNAP_Y, W, H - SNAP_Y) if not SCENE then gfx.ready(false) return end else gfx.blit_rgb(PART, 0, SNAP_Y, W, H - SNAP_Y) draw_scene(dq, 0, false, 2) SCENE, PART, scene_key = gfx.snapshot(0, SNAP_Y, W, H - SNAP_Y), nil, key end end rect(0, 0, W, SNAP_Y, mixp(SKY, dq)) if moon_on then draw_moon(moon_ph) end gfx.blit_rgb(SCENE, 0, SNAP_Y, W, H - SNAP_Y) draw_clouds(dq, 0) end if cfg.city then if frame % (FPS * 3 // 2) < FPS // 6 then px(ANT_X, ANT_Y, 255, 40, 30) end if dq <= 0.5 and TVW > 0 then local v = 0.55 + 0.45 * umod(hash32((frame * 15 // (2 * FPS)) * 0x9E3779B1), 100) / 100 px(WX[TVW], WY[TVW], floor(TV[1] * v), floor(TV[2] * v), floor(TV[3] * v)) end end draw_drops(0, 1, day) if car.on then draw_car(day) end draw_drops(2, 2, day) draw_splash(day) end