info = { type = "image", title = "Soccer ball", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "A spinning ball bounces in front of the goal", api = 33, } local tr = ({ sk = { ["Spin speed"] = "Rýchlosť otáčania", ["Bounce height"] = "Výška skoku" }, cs = { ["Spin speed"] = "Rychlost otáčení", ["Bounce height"] = "Výška skoku" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "spin", label = T("Spin speed"), type = "int", min = 0, max = 4, default = 2 }, { key = "height", label = T("Bounce height"), type = "int", min = 1, max = 3, default = 2 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sqrt, sin, cos, acos = math.floor, math.sqrt, math.sin, math.cos, math.acos local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local N, RR = 23, 11.5 local NPH = 15 local TEXS = table.concat({ "111222222222211", "111111133111111", "111111133111111", "111111122111111", "111112222221111", "333222222222233", "111122222222111", "111112222221111", "111112222221111", "111113211231111", "111133111133111", "111131111113111", "111311111111311", "112311111111321", "222111111111122", "222211111111222", "222221111112222", "222223333332222", "222211111111222", "221111111111112", "311111111111111", "331111111111113", "222221111112222", }) local TEX = {} for i = 1, #TEXS do TEX[i] = TEXS:byte(i) - 48 end local PI, PC, PP, PV, NP = {}, {}, {}, {}, 0 local CELLS = {} do local lx, ly, lz = -0.5, -0.62, 0.6 for y = 0, N - 1 do for x = 0, N - 1 do local i = y * N + x + 1 CELLS[i] = 0 local X, Y = (x - 11) / RR, (y - 11) / RR local d2 = X * X + Y * Y if d2 < 1 then NP = NP + 1 PI[NP], PC[NP] = i, x * NPH + 1 if d2 > (10.4 / RR) ^ 2 then PV[NP] = -1 else local r = sqrt(1 - X * X) local c = Y / r if c > 1 then c = 1 elseif c < -1 then c = -1 end PP[NP] = floor(acos(c) * 5 * NPH / 6.283185 + 0.5) local lit = X * lx + Y * ly + sqrt(1 - d2) * lz PV[NP] = lit < 0.05 and 3 or (lit > 0.9 and 6 or 0) end end end end end local BPAL = { 0, 0, 0, 238, 238, 232, 26, 26, 32, 92, 92, 100, 178, 184, 196, 14, 14, 20, 60, 62, 74, 255, 255, 255, 64, 64, 74, 110, 110, 118, 18, 44, 26 } local BG = nil local SKY, STAND = { 104, 168, 226 }, { 42, 54, 100 } local GL, GD = { 98, 178, 74 }, { 70, 148, 58 } local POST, POST_D, NET = { 244, 244, 244 }, { 176, 184, 190 }, { 196, 210, 206 } local CROWD = { { 226, 80, 80 }, { 244, 204, 80 }, { 220, 224, 236 }, { 90, 150, 230 } } local function build_bg() rect(0, 0, W, H, SKY[1], SKY[2], SKY[3]) rect(0, 9, W, 22, STAND[1], STAND[2], STAND[3]) for y = 11, 29, 3 do for x = (y // 3) % 2, W - 1, 3 do local h = hash32(x * 0x9E3779B1 + y * 0x85EBCA6B) if umod(h, 5) > 0 then local c = CROWD[umod(h >> 4, #CROWD) + 1] px(x, y, c[1], c[2], c[3]) end end end rect(0, 30, W, 1, 230, 230, 230) local bands = { 31, 35, 40, 46, 53, 61, 64 } for i = 1, #bands - 1 do local c = i % 2 == 1 and GL or GD rect(0, bands[i], W, bands[i + 1] - bands[i], c[1], c[2], c[3]) end for y = 14, 36 do for x = 10, 53 do if (x + y) % 4 == 0 or (x - y) % 4 == 0 then px(x, y, NET[1], NET[2], NET[3]) end end end rect(10, 36, 44, 1, NET[1], NET[2], NET[3]) rect(8, 12, 48, 2, POST[1], POST[2], POST[3]) rect(8, 12, 2, 26, POST[1], POST[2], POST[3]) rect(54, 12, 2, 26, POST[1], POST[2], POST[3]) rect(9, 14, 1, 24, POST_D[1], POST_D[2], POST_D[3]) rect(55, 14, 1, 24, POST_D[1], POST_D[2], POST_D[3]) BG = gfx.snapshot(0, 0, W, H) end local GROUND = 57 local PERIOD = 32 function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS local u = (t % PERIOD) / PERIOD local hb = 8 + 6 * (cfg.height or 2) local up = hb * (1 - (2 * u - 1) ^ 2) local by = floor(GROUND - N - up + 0.5) local sw = floor(20 - up * 0.4 + 0.5) blend_rect(32 - sw // 2, GROUND, sw, 2, 20, 60, 24, 0.55 - up * 0.01) blend_rect(32 - sw // 2 + 2, GROUND + 2, sw - 4, 1, 20, 60, 24, 0.35) local rot = (t * (cfg.spin or 2)) // 2 for k = 1, NP do local v = PV[k] if v < 0 then CELLS[PI[k]] = 10 else CELLS[PI[k]] = TEX[PC[k] + (PP[k] + rot) % NPH] + v end end if t % PERIOD == 0 then gfx.blit_indexed(CELLS, BPAL, 20, GROUND - 21, 25, 21, N, N, true) else gfx.blit_indexed(CELLS, BPAL, 21, by, N, N, N, N, true) end end