info = { type = "image", title = "Brick rally", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Self-playing breakout in shades of one colour", api = 33, } local tr = ({ sk = { ["Brick colour"] = "Farba tehličiek", ["Ball speed"] = "Rýchlosť loptičky", ["slow"] = "pomaly", ["normal"] = "bežne", ["fast"] = "rýchlo" }, cs = { ["Brick colour"] = "Barva cihliček", ["Ball speed"] = "Rychlost míčku", ["slow"] = "pomalu", ["normal"] = "běžně", ["fast"] = "rychle" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tone", label = T("Brick colour"), type = "color", default = "#E8643C" }, { key = "pace", label = T("Ball speed"), type = "int", default = "normal", opts = { keys = { "slow", "normal", "fast" }, T("slow"), T("normal"), T("fast") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, abs, min, max, ceil, sqrt = math.floor, math.sin, math.cos, math.abs, math.min, math.max, math.ceil, math.sqrt local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local COLS, ROWS, BW, BH, PXP, PYP, X0, Y0 = 9, 7, 6, 3, 7, 4, 1, 5 local PW, PAD_Y = 12, 59 local WALL_H = Y0 + ROWS * PYP local function sstep(a, b, t) local u = (t - a) / (b - a) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local function bar(x, y, w, h, r, g, b) local ix = floor(x) local f = x - ix if f < 0.04 then rect(ix, y, w, h, r, g, b) return end if w > 1 then rect(ix + 1, y, w - 1, h, r, g, b) end blend_rect(ix, y, 1, h, r, g, b, 1 - f) blend_rect(ix + w, y, 1, h, r, g, b, f) end local TONE = { 232, 100, 60 } local WALLIMG = nil local SH = {} local SPEED = 1.35 function on_settings() local c = cfg.tone or { r = 232, g = 100, b = 60 } TONE = { c.r, c.g, c.b } for r = 0, ROWS - 1 do local k = 1.35 - r * 0.13 local function sh(v, kk) if kk <= 1 then return floor(v * kk + 0.5) end return floor(v + (255 - v) * (kk - 1) + 0.5) end SH[r] = { { sh(c.r, k), sh(c.g, k), sh(c.b, k) }, { sh(c.r, k + 0.3), sh(c.g, k + 0.3), sh(c.b, k + 0.3) } } end local p = cfg.pace SPEED = p == "slow" and 1.0 or p == "fast" and 1.8 or 1.35 WALLIMG = nil end local alive, nalive, round = {}, 0, 0 local state, timer = "build", 0 local bx, by, vx, vy = 32, 57, 0, 0 local pcx, aim = 32, 0 local hist, trail, PT = {}, {}, {} local rngc = 0 local last = nil local function rnd(m) rngc = rngc + 1 return umod(hash32(rngc * 0x9E3779B1 + round), m) end local function new_wall() round = round + 1 local p = (round - 1) % 4 nalive = 0 for r = 0, ROWS - 1 do for c = 0, COLS - 1 do local on if p == 0 then on = true elseif p == 1 then local m = floor(r * 0.6); on = c >= m and c < COLS - m elseif p == 2 then on = (r + c) % 2 == 0 else on = c ~= 2 and c ~= 6 and r ~= 3 end alive[r * COLS + c + 1] = on if on then nalive = nalive + 1 end end end state, timer, WALLIMG = "build", 0, nil end local function spark(cx, cy, r) for _ = 1, 7 do local a = rnd(628) / 100 local s = 0.35 + rnd(80) / 100 PT[#PT + 1] = { x = cx, y = cy, vx = cos(a) * s, vy = sin(a) * s - 0.3, l = 10 + rnd(8), w = rnd(3) == 0 and 1 or 0, r = r } end end local function brick_at(x, y) local c, r = floor((x - X0) / PXP), floor((y - Y0) / PYP) if c < 0 or c >= COLS or r < 0 or r >= ROWS then return nil end if alive[r * COLS + c + 1] then return r * COLS + c + 1, r, c end end local function hit_brick(i, r, c) alive[i] = false nalive = nalive - 1 WALLIMG = nil spark(X0 + c * PXP + 3, Y0 + r * PYP + 1, r) end local function pick_aim() if nalive <= 0 then aim = 0 return end local k = rnd(nalive) for i = 1, ROWS * COLS do if alive[i] then if k == 0 then local tx = X0 + ((i - 1) % COLS) * PXP + 3 aim = max(-0.8, min(0.8, (tx - bx) / 26)) return end k = k - 1 end end end local function launch() local a = (rnd(70) - 35) * 0.01745 local s = SPEED vx, vy = s * sin(a), -s * cos(a) state = "play" pick_aim() end local function step() for i = #PT, 1, -1 do local p = PT[i] p.x, p.y, p.vy, p.l = p.x + p.vx, p.y + p.vy, p.vy + 0.07, p.l - 1 if p.l <= 0 then PT[i] = PT[#PT]; PT[#PT] = nil end end timer = timer + 1 local tgt = 32 if state == "play" then hist[#hist + 1] = bx if #hist > 5 then table.remove(hist, 1) end tgt = hist[1] - aim * (PW / 2) end tgt = max(PW / 2, min(W - PW / 2, tgt)) pcx = pcx + max(-1.6, min(1.6, (tgt - pcx) * 0.35)) if state == "build" then if timer > 6 * ROWS + 14 then state, timer, hist = "serve", 0, {} end elseif state == "serve" then bx, by = pcx, PAD_Y - 2 if timer > 28 then launch() end elseif state == "clear" then if timer > 45 then new_wall() end else trail[#trail + 1] = { bx, by } if #trail > 4 then table.remove(trail, 1) end local ns = ceil(SPEED / 0.8) for _ = 1, ns do local dx, dy = vx / ns, vy / ns bx = bx + dx if bx < 1 then bx, vx, dx = 1, abs(vx), 0 elseif bx > W - 1 then bx, vx, dx = W - 1, -abs(vx), 0 end local i, r, c = brick_at(bx + (dx > 0 and 0.9 or -0.9), by) if i and dx ~= 0 then hit_brick(i, r, c); vx = -vx; bx = bx - dx * 2 end by = by + dy if by < 1 then by, vy = 1, abs(vy) end i, r, c = brick_at(bx, by + (dy > 0 and 0.9 or -0.9)) if i then hit_brick(i, r, c); vy = -vy; by = by - dy * 2 end if nalive <= 0 then state, timer = "clear", 0 break end if vy > 0 and by >= PAD_Y - 1 and by < PAD_Y + 1 and abs(bx - pcx) <= PW / 2 + 1 then local o = max(-1, min(1, (bx - pcx) / (PW / 2))) local a = o * 1.0 local s = SPEED vx, vy = s * sin(a), -s * cos(a) by = PAD_Y - 1.1 pick_aim() end end if abs(vy) < 0.3 * SPEED then vy = (vy < 0 and -1 or 1) * 0.3 * SPEED vx = (vx < 0 and -1 or 1) * sqrt(SPEED * SPEED - vy * vy) end if state == "play" and by > H + 2 then state, timer, hist, trail = "serve", 0, {}, {} end end end local function brick(x, r) local s = SH[r] bar(x, Y0 + r * PYP, BW, BH, s[1][1], s[1][2], s[1][3]) bar(x, Y0 + r * PYP, BW, 1, s[2][1], s[2][2], s[2][3]) end function render() if not SH[0] then on_settings() end if not alive[1] then new_wall() end local n = last and (frame - last) or 1 if n < 1 then n = 1 elseif n > 8 then n = 8 end last = frame for _ = 1, n do step() end rect(0, 0, W, H, 4, 4, 10) rect(0, 0, W, 1, 22, 22, 40) if state == "build" then for r = 0, ROWS - 1 do local o = (1 - sstep(5 * r, 5 * r + 14, timer)) * ((r % 2 == 0) and -72 or 72) for c = 0, COLS - 1 do if alive[r * COLS + c + 1] then brick(X0 + c * PXP + o, r) end end end else if not WALLIMG then for r = 0, ROWS - 1 do for c = 0, COLS - 1 do if alive[r * COLS + c + 1] then brick(X0 + c * PXP, r) end end end WALLIMG = gfx.snapshot(0, 0, W, WALL_H) else gfx.blit_rgb(WALLIMG, 0, 0, W, WALL_H) end end for i = 1, #PT do local p = PT[i] local s = SH[p.r][2] local a = min(1, p.l / 8) if p.w == 1 then gfx.blend(floor(p.x), floor(p.y), 255, 255, 240, a) else gfx.blend(floor(p.x), floor(p.y), s[1], s[2], s[3], a) end end bar(pcx - PW / 2, PAD_Y, PW, 2, 232, 232, 244) rect(floor(pcx - PW / 2 + 0.5), PAD_Y, 2, 2, TONE[1], TONE[2], TONE[3]) rect(floor(pcx + PW / 2 + 0.5) - 2, PAD_Y, 2, 2, TONE[1], TONE[2], TONE[3]) if state == "play" or state == "serve" then for i = 1, #trail do blend_rect(floor(trail[i][1] - 0.5), floor(trail[i][2] - 0.5), 2, 2, 255, 255, 255, 0.08 * i) end rect(floor(bx - 0.5), floor(by - 0.5), 2, 2, 255, 255, 255) end end