info = { type = "image", title = "GB Quest", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A tiny hero runs a four-shade platformer", api = 33, } local tr = ({ sk = { ["Palette"] = "Paleta", ["DMG green"] = "Zelená DMG", ["Pocket grey"] = "Vreckovo sivá", ["Teal"] = "Smaragdová", ["Coins and level"] = "Mince a úroveň" }, cs = { ["Palette"] = "Paleta", ["DMG green"] = "Zelená DMG", ["Pocket grey"] = "Kapesní šeď", ["Teal"] = "Smaragdová", ["Coins and level"] = "Mince a úroveň" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "pal", label = T("Palette"), type = "int", default = 0, opts = { T("DMG green"), T("Pocket grey"), T("Teal") } }, { key = "hud", label = T("Coins and level"), type = "bool", default = true }, } local rect, px = gfx.rect, gfx.px local blit_indexed = gfx.blit_indexed local floor, max = math.floor, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local PALS = { { { 196, 222, 96 }, { 122, 168, 50 }, { 50, 102, 52 }, { 18, 54, 30 } }, { { 214, 214, 196 }, { 148, 148, 136 }, { 84, 84, 82 }, { 30, 30, 36 } }, { { 168, 232, 210 }, { 86, 170, 160 }, { 38, 98, 110 }, { 14, 44, 60 } }, } local C = PALS[1] local PAL = {} local MAP = { ["."] = "\0", a = "\1", b = "\2", c = "\3", d = "\4" } local function cells(rows) local out = {} for i = 1, #rows do out[i] = rows[i]:gsub(".", MAP) end return table.concat(out) end local SEG, LC = 32, 32 local TOP = { 52, 44, 36 } local HX = 20 local function seg_h(k) local j = k % LC if j == 0 or j >= LC - 2 then return 0 end return umod(hash32((k // 3) * 0x9E3779B1), 3) end local function seg_pit(k) local j = k % LC if j <= 1 or j >= LC - 2 then return false end return umod(hash32(k * 0x85EBCA6B), 5) == 0 end local function jump_at(k) return seg_h(k) ~= seg_h(k - 1) or seg_pit(k) end local function seg_bug(k) local j = k % LC if j <= 1 or j >= LC - 3 or jump_at(k) or jump_at(k + 1) then return false end return umod(hash32(k * 0xC2B2AE35), 3) ~= 0 end local function seg_plat(k) if seg_bug(k) then return false end return umod(hash32(k * 0x27D4EB2F), 3) == 0 end local function arc(x, len, ya, yb, A) local s = x / len return ya + (yb - ya) * s - 4 * A * s * (1 - s) end local function up_a(ya, yb) return 9 + 0.6 * max(0, ya - yb) end local function hero_y(hx) local k = hx // SEG local lx = hx - k * SEG if lx >= 22 and jump_at(k + 1) then local ya, yb = TOP[seg_h(k) + 1], TOP[seg_h(k + 1) + 1] return arc(lx - 22, 24, ya, yb, up_a(ya, yb)), true elseif lx < 14 and jump_at(k) then local ya, yb = TOP[seg_h(k - 1) + 1], TOP[seg_h(k) + 1] return arc(lx + 10, 24, ya, yb, up_a(ya, yb)), true elseif seg_bug(k) then local y = TOP[seg_h(k) + 1] return arc(lx, SEG, y, y, 16), lx > 0 end return TOP[seg_h(k) + 1], false end local function seg_coins(k) local n, x1, y1, x2, y2 = 0 if jump_at(k) then local ya, yb = TOP[seg_h(k - 1) + 1], TOP[seg_h(k) + 1] n, x1, y1 = 1, k * SEG + 2, floor((ya + yb) / 2 - up_a(ya, yb) - 4) end if seg_bug(k) then local y = TOP[seg_h(k) + 1] - 20 if n == 0 then n, x1, y1 = 1, k * SEG + 16, y else n, x2, y2 = 2, k * SEG + 16, y end end return n, x1, y1, x2, y2 end local GROUND, GROUND_PIT do local full, pit, row = {}, {}, {} for y = 0, 27 do for x = 0, 31 do local v if y == 0 or y == 3 then v = 4 elseif y <= 2 then v = (y == 2 and x % 4 == 1) and 3 or 2 else local yy = y - 4 if yy % 6 == 5 then v = 4 elseif (x + ((yy // 6) % 2) * 4) % 8 == 7 then v = 4 elseif (x * 7 + y * 3) % 11 == 0 then v = 2 else v = 3 end end row[x + 1] = v end full[y + 1] = string.char(table.unpack(row, 1, 32)) pit[y + 1] = string.rep("\0", 8) .. "\4" .. string.char(table.unpack(row, 10, 32)) end GROUND, GROUND_PIT = table.concat(full), table.concat(pit) end local SPIKES = cells({ "...d...d", "..dcd.dc", "dddddddd" }) local PLAT = cells({ "dddddddddddddddd", "dbbbbbbbbbbbbbbd", "dccdccdccdccdccd", "dddddddddddddddd", }) local COIN = { { 4, cells({ ".dd.", "dabd", "dabd", "dbbd", ".dd." }) }, { 2, cells({ "dd", "db", "db", "db", "dd" }) }, { 1, cells({ "d", "d", "d", "d", "d" }) }, } local COIN_SEQ = { 1, 2, 3, 2 } local SPARK = cells({ ".d.", "dad", ".d." }) local BUG = { cells({ "..dddd..", ".dccccd.", "dcadcadc", "dccccccd", ".dddddd.", ".d.dd.d." }), cells({ "..dddd..", ".dccccd.", "dcadcadc", "dccccccd", ".dddddd.", "d..dd..d" }), } local HERO_TOP = { "..dddd.", ".dddddd", ".daada.", ".daaaa.", "..ccc..", ".cccccd", "..ccc.." } local function hero(l1, l2) local r = {} for i = 1, #HERO_TOP do r[i] = HERO_TOP[i] end r[8], r[9] = l1, l2 return cells(r) end local HERO = { hero(".d..d..", ".d...d."), hero("..dd...", "..dd..."), hero(".d...d.", "d.....d"), } local FLAG = { cells({ "ddddd", "dbbbd", "dbbd.", "ddd.." }), cells({ "dddd.", "dbbbd", "dbbbd", "ddddd" }) } local BG = nil local function build_bg() local A, B, Cc = C[1], C[2], C[3] rect(0, 0, W, H, A[1], A[2], A[3]) for y = -6, 6 do for x = -6, 6 do local d = x * x + y * y if d <= 36 then local c = d > 24 and B or A px(48 + x, 12 + y, c[1], c[2], c[3]) end end end for _, c in ipairs({ { 6, 8, 14 }, { 26, 18, 10 } }) do local x, y, L = c[1], c[2], c[3] rect(x + 2, y, L - 4, 1, B[1], B[2], B[3]) rect(x, y + 1, 1, 2, B[1], B[2], B[3]); rect(x + L - 1, y + 1, 1, 2, B[1], B[2], B[3]) rect(x + 1, y + 3, L - 2, 1, B[1], B[2], B[3]) end for x = 0, W - 1 do local t1 = floor(34 + 6 * math.sin(x * 0.09 + 0.5) + 0.5) rect(x, t1, 1, H - t1, B[1], B[2], B[3]) if (x % 4) == 2 then px(x, t1 + 4, Cc[1], Cc[2], Cc[3]) end local t2 = floor(46 + 3 * math.sin(x * 0.17 + 2) + 0.5) rect(x, t2, 1, H - t2, Cc[1], Cc[2], Cc[3]) end BG = gfx.snapshot(0, 0, W, H) end local F_HUD = { font = "3x5" } local SHOW_HUD = true local PRE, PRE_LVL = {}, -1 local function draw_hud(hx, t) local lvl = hx // (SEG * LC) if lvl ~= PRE_LVL then local s = 0 for j = 0, LC - 1 do PRE[j] = s; s = s + (seg_coins(lvl * LC + j)) end PRE_LVL = lvl end local k = hx // SEG local got = PRE[k - lvl * LC] local n, x1, _, x2 = seg_coins(k) if n >= 1 and hx >= x1 then got = got + 1 end if n >= 2 and hx >= x2 then got = got + 1 end local D = C[4] blit_indexed(COIN[1][2], PAL, 2, 2, 4, 5, 4, 5, true) gfx.text(8, 2, string.format("x%02d", got), D[1], D[2], D[3], F_HUD) local s = string.format("%d-%d", lvl // 4 + 1, lvl % 4 + 1) gfx.text(W - 2 - gfx.text_width(s, F_HUD), 2, s, D[1], D[2], D[3], F_HUD) end function on_settings() C = PALS[(cfg.pal or 0) + 1] or PALS[1] PAL = { 0, 0, 0 } for i = 1, 4 do PAL[#PAL + 1] = C[i][1]; PAL[#PAL + 1] = C[i][2]; PAL[#PAL + 1] = C[i][3] end SHOW_HUD = cfg.hud ~= false BG = nil end local function draw_coin(x, y, t, d) if d >= 0 then if d < 8 then blit_indexed(SPARK, PAL, x - 1, y - 2 - d // 2, 3, 3, 3, 3, true) end return end local c = COIN[COIN_SEQ[(t // 5) % 4 + 1]] blit_indexed(c[2], PAL, x - c[1] // 2, y - 2, c[1], 5, c[1], 5, true) end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS local hx = t + HX local x0 = hx - HX local D = C[4] for k = x0 // SEG, (x0 + W - 1) // SEG do local sx = k * SEG - x0 local top = TOP[seg_h(k) + 1] local j = k % LC if seg_plat(k) then local py = top - 24 blit_indexed(PLAT, PAL, sx + 8, py, 16, 4, 16, 4, true) for i = 0, 2 do draw_coin(sx + 11 + i * 5, py - 4, t + i * 3, -1) end end local pit = seg_pit(k) blit_indexed(pit and GROUND_PIT or GROUND, PAL, sx, top, SEG, 28, SEG, 28, true) if pit then rect(sx, 58, 8, 6, D[1], D[2], D[3]) blit_indexed(SPIKES, PAL, sx, 55, 8, 3, 8, 3, true) end local hn = TOP[seg_h(k + 1) + 1] if hn > top or seg_pit(k + 1) then rect(sx + SEG - 1, top, 1, H - top, D[1], D[2], D[3]) end local hp = TOP[seg_h(k - 1) + 1] if hp > top and not pit then rect(sx, top, 1, H - top, D[1], D[2], D[3]) end if j == LC - 2 then rect(sx + 20, top - 26, 1, 26, D[1], D[2], D[3]) px(sx + 20, top - 27, C[3][1], C[3][2], C[3][3]) blit_indexed(FLAG[(t // 8) % 2 + 1], PAL, sx + 21, top - 26, 5, 4, 5, 4, true) end if seg_bug(k) then local p = t % 24 local bx = sx + 16 + (p < 12 and p or 24 - p) - 6 blit_indexed(BUG[(t // 4) % 2 + 1], PAL, bx - 4, top - 6, 8, 6, 8, 6, true) end local n, x1, y1, x2, y2 = seg_coins(k) if n >= 1 then draw_coin(x1 - x0, y1, t, hx - x1) end if n >= 2 then draw_coin(x2 - x0, y2, t, hx - x2) end end local fy, air = hero_y(hx) local f = air and 3 or ((t // 4) % 2 + 1) blit_indexed(HERO[f], PAL, HX - 3, floor(fy + 0.5) - 9, 7, 9, 7, 9, true) if SHOW_HUD then draw_hud(hx, t) end end