info = { type = "image", title = "Storm", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Lightning over the city", api = 41, } local px, rect, blend, blend_rect = gfx.px, gfx.rect, gfx.blend, gfx.blend_rect local floor, sin, fmod = math.floor, math.sin, math.fmod local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local FPS = gfx.FPS local STEP_HZ = 30 local SKY = { 26, 20, 52 } local CLOUD, CLOUD_D = { 92, 76, 132 }, { 62, 50, 98 } local TOWER, TOWER_D = { 20, 16, 44 }, { 12, 10, 28 } local WINDOW = { 255, 200, 90 } local RAIN = { 150, 170, 230 } local BOLT, BOLT_H = { 235, 240, 255 }, { 170, 200, 255 } local TOWERS = { { x = 0, w = 9, h = 18 }, { x = 9, w = 7, h = 26 }, { x = 16, w = 11, h = 14 }, { x = 27, w = 8, h = 30 }, { x = 35, w = 10, h = 20 }, { x = 45, w = 7, h = 25 }, { x = 52, w = 12, h = 16 }, } local GROUND = H - 2 local function draw_city(f) for i = 1, #TOWERS do local t = TOWERS[i] local y = GROUND - t.h rect(t.x, y, t.w, t.h, TOWER[1], TOWER[2], TOWER[3]) rect(t.x + t.w - 1, y, 1, t.h, TOWER_D[1], TOWER_D[2], TOWER_D[3]) for wy = y + 2, GROUND - 3, 4 do for wx = t.x + 1, t.x + t.w - 3, 3 do local h = hash32(wx * 131 + wy * 17 + i) local on = (h % 5) < 2 if (h >> 9) % 7 == 0 then on = ((f // (STEP_HZ * 3) + (h >> 12)) % 2) == 0 end if on then px(wx, wy, WINDOW[1], WINDOW[2], WINDOW[3]) end end end end rect(0, GROUND, W, H - GROUND, TOWER_D[1], TOWER_D[2], TOWER_D[3]) end local CLOUDS = { { y = 1, w = 30, hgt = 8, sp = 0.020, seed = 3 }, { y = 3, w = 26, hgt = 7, sp = 0.020, seed = 61 }, { y = 8, w = 34, hgt = 7, sp = 0.013, seed = 17 }, { y = 10, w = 28, hgt = 6, sp = 0.013, seed = 83 }, { y = 15, w = 22, hgt = 5, sp = 0.028, seed = 41 }, } local function draw_cloud(c, f, lit) local span = W + c.w local x0 = fmod(f * c.sp + c.seed, span) - c.w for rep_i = 0, 1 do local bx = floor(x0) + rep_i * span for i = 0, c.w - 1, 2 do local h = hash32(c.seed * 131 + i) local d = h % 3 local col = d == 0 and CLOUD or CLOUD_D if lit then col = CLOUD end rect(bx + i, c.y + d, 2, c.hgt - d, col[1], col[2], col[3]) end end end local N_RAIN = 46 local function draw_rain(f) for i = 1, N_RAIN do local h = hash32(i * 0x9E3779B1) local sp = 1.6 + (h >> 4) % 10 / 5.0 local x = (h % W + floor(f * 0.35)) % W local y = fmod(f * sp + h % 97, H + 8) - 4 local a = 0.35 + (h >> 8) % 40 / 100.0 blend(x, floor(y), RAIN[1], RAIN[2], RAIN[3], a) blend(x - 1, floor(y) - 1, RAIN[1], RAIN[2], RAIN[3], a * 0.6) end end local STRIKE = 6 local BOLT_F = 4 local function draw_bolt(n, age) local h = hash32(n * 0x85EBCA6B) local x = 10 + h % (W - 20) local y = 16 + (h >> 8) % 6 local ty = GROUND - 8 - (h >> 12) % 14 local seg = 0 while y < ty do local hh = hash32(n * 977 + seg) local dx = hh % 5 - 2 local dy = 3 + (hh >> 4) % 3 for k = 0, dy - 1 do local xx = x + floor(dx * k / dy + 0.5) rect(xx, y + k, 2, 1, BOLT[1], BOLT[2], BOLT[3]) px(xx - 1, y + k, BOLT_H[1], BOLT_H[2], BOLT_H[3]) px(xx + 2, y + k, BOLT_H[1], BOLT_H[2], BOLT_H[3]) end if (hh >> 8) % 2 == 0 then local bd = dx < 0 and -1 or 1 for k = 0, 4 do rect(x + bd * (k + 1), y + k + 1, 2, 1, BOLT_H[1], BOLT_H[2], BOLT_H[3]) end end x, y, seg = x + dx, y + dy, seg + 1 end local a = age < 2 and 0.22 or 0.09 blend_rect(0, 0, W, H, 190, 190, 255, a) end function render() gfx.clear(SKY[1], SKY[2], SKY[3]) local f = frame * STEP_HZ // FPS local c = data.time local n, age if c.ok then n, age = c.unix // STRIKE, floor((c.unix % STRIKE + c.ms / 1000) * STEP_HZ) else n = f // (STRIKE * STEP_HZ); age = f - n * STRIKE * STEP_HZ end local lit = age < BOLT_F and umod(hash32(n * 31), 3) ~= 0 for i = 1, #CLOUDS do draw_cloud(CLOUDS[i], f, lit and age < 2) end draw_city(f) draw_rain(f) if lit then draw_bolt(n, age) end end