info = { type = "image", title = "Skate duck", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A duck rides a skateboard, ollies and quacks", api = 33, } local tr = ({ sk = { ["Duck colour"] = "Farba kačky", ["Speed"] = "Rýchlosť", ["Ollies"] = "Ollie", ["often"] = "často", ["normal"] = "normálne", ["rare"] = "zriedka" }, cs = { ["Duck colour"] = "Barva kachny", ["Speed"] = "Rychlost", ["Ollies"] = "Ollie", ["often"] = "často", ["normal"] = "normálně", ["rare"] = "zřídka" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "col", label = T("Duck colour"), type = "color", default = "#FFA800" }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "ollie", label = T("Ollies"), type = "int", default = 1, opts = { T("often"), T("normal"), T("rare") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, ceil, sin, cos, sqrt, exp = math.floor, math.ceil, math.sin, math.cos, math.sqrt, math.exp local hash32 = math.hash32 local W, H = gfx.W, gfx.H local PI = 3.14159265 local WALL, FAR = { 152, 74, 92 }, { 124, 54, 82 } local FENCE, RAIL = { 206, 128, 126 }, { 176, 98, 108 } local POLE, BULB = { 66, 32, 62 }, { 255, 226, 130 } local WALK, WALK_L = { 178, 140, 152 }, { 206, 170, 178 } local JOINT, CURB = { 142, 104, 122 }, { 96, 62, 90 } local ROAD, DASH = { 84, 58, 94 }, { 222, 196, 202 } local DECK, WHEEL = { 78, 186, 172 }, { 34, 28, 46 } local HUB, LEG = { 206, 206, 220 }, { 226, 96, 32 } local BEAK, BEAKD = { 240, 86, 40 }, { 190, 52, 36 } local MOUTH, INK = { 92, 24, 40 }, { 20, 14, 24 } local function ell(cx, cy, rx, ry, a, r, g, b) local c, s = cos(a), sin(a) local ix, iy = 1 / (rx * rx), 1 / (ry * ry) local A = c * c * ix + s * s * iy local Bk = 2 * c * s * (ix - iy) local Ck = s * s * ix + c * c * iy local hh = sqrt(rx * rx * s * s + ry * ry * c * c) local top, bot = cy - hh, cy + hh for y = floor(top), ceil(bot) - 1 do local y0 = y > top and y or top local y1 = (y + 1 < bot) and (y + 1) or bot local cov = y1 - y0 if cov > 0.12 then local dy = (y0 + y1) * 0.5 - cy local B = Bk * dy local disc = B * B - 4 * A * (Ck * dy * dy - 1) if disc > 0 then local sq = sqrt(disc) / (2 * A) local m = -B / (2 * A) local xs, xe = floor(cx + m - sq + 0.5), floor(cx + m + sq + 0.5) if xe > xs then if cov > 0.97 then rect(xs, y, xe - xs, 1, r, g, b) else blend_rect(xs, y, xe - xs, 1, r, g, b, cov) end end end end end end local function frect(x, y, w, h, r, g, b) local x2 = x + w if x2 <= 0 or x >= W then return end local ix, ix2 = floor(x), floor(x2) if ix2 == ix then blend_rect(ix, y, 1, h, r, g, b, w) return end local cl = ix + 1 - x if cl > 0.04 then blend_rect(ix, y, 1, h, r, g, b, cl) end if ix2 - ix > 1 then rect(ix + 1, y, ix2 - ix - 1, h, r, g, b) end local cr = x2 - ix2 if cr > 0.04 then blend_rect(ix2, y, 1, h, r, g, b, cr) end end local O, Y, DK = { 255, 168, 0 }, { 255, 205, 80 }, { 200, 118, 0 } local VG, N, OFF = 1.0, 120, 74 function on_settings() local c = cfg.col or { r = 255, g = 168, b = 0 } O = { c.r, c.g, c.b } Y = { floor(c.r + (255 - c.r) * 0.4), floor(c.g + (255 - c.g) * 0.4), floor(c.b + (255 - c.b) * 0.4) } DK = { floor(c.r * 0.74), floor(c.g * 0.68), floor(c.b * 0.6) } VG = 0.4 + 0.2 * (cfg.speed or 3) local o = cfg.ollie or 1 N = (o == 0 and 84) or (o == 2 and 200) or 120 OFF = (22 - 120) % N end local BG = nil local function build_bg() rect(0, 0, W, H, WALL[1], WALL[2], WALL[3]) rect(0, 43, W, 1, RAIL[1], RAIL[2], RAIL[3]) rect(0, 48, W, 1, RAIL[1], RAIL[2], RAIL[3]) rect(0, 51, W, 4, WALK[1], WALK[2], WALK[3]) rect(0, 51, W, 1, WALK_L[1], WALK_L[2], WALK_L[3]) rect(0, 55, W, 1, CURB[1], CURB[2], CURB[3]) rect(0, 56, W, 8, ROAD[1], ROAD[2], ROAD[3]) BG = gfx.snapshot(0, 0, W, H) end local BXs, BWs, BTs, NB, PER = {}, {}, {}, 0, 0 do local x, i = 0, 0 while x < 96 do i = i + 1 local h = hash32(i * 0x9E3779B1) & 0x7FFF local w = 9 + h % 7 NB = NB + 1 BXs[NB], BWs[NB], BTs[NB] = x, w, 22 + (h >> 4) % 14 x = x + w + (h >> 8) % 3 end PER = x end local function pose(c) local h, cr, p = 0, 0, 0 if c >= 16 and c < 24 then cr = sin((c - 16) / 8 * PI / 2) elseif c >= 24 and c < 46 then local s = c - 24 local u = s / 22 h = 56 * u * (1 - u) if s < 3 then cr = 1 - 1.6 * s / 3 elseif s < 11 then cr = -0.6 * (1 - (s - 3) / 8) elseif s >= 17 then cr = 0.25 * (s - 17) / 5 end if s < 11 then p = 0.5 * sin(PI * s / 11) else p = -0.25 * sin(PI * (s - 11) / 11) end elseif c >= 46 and c < 70 then local s = c - 46 cr = cos(0.5 * s) * exp(-0.14 * s) end return h, cr, p end local RING = { { 0, 0 }, { 1, 0 }, { 2, 1 }, { 2, 2 }, { 1, 3 }, { 0, 3 }, { -1, 2 }, { -1, 1 } } local BUB = { font = "4x6", w = 23, align = "center" } local BUB_Y = nil local function wheel(wx, wy, idx) rect(wx + 1, wy, 2, 4, WHEEL[1], WHEEL[2], WHEEL[3]) rect(wx, wy + 1, 4, 2, WHEEL[1], WHEEL[2], WHEEL[3]) local q = RING[idx] px(wx + q[1], wy + q[2], HUB[1], HUB[2], HUB[3]) end local STEP_HZ = 30 local function draw_duck(frame) local BX = 26 local c = (frame + OFF) % N local h, cr, p = pose(c) local hd, crd = pose((c - 2) % N) local D = 56 - h for k = 0, 5 do local x0 = BX - 11 + 4 * k local w = (k == 5) and 2 or 4 local yy = D - p * (x0 + w * 0.5 - BX) - ((k == 0 or k == 5) and 1 or 0) local iy = floor(yy) local f = yy - iy rect(x0, iy + 1, w, 1, DECK[1], DECK[2], DECK[3]) if f < 0.96 then blend_rect(x0, iy, w, 1, DECK[1], DECK[2], DECK[3], 1 - f) end if f > 0.04 then blend_rect(x0, iy + 2, w, 1, DECK[1], DECK[2], DECK[3], f) end end local idx = floor(frame * VG / 2) % 8 + 1 wheel(BX - 9, floor(D + 7 * p + 0.5) + 2, idx) wheel(BX + 5, floor(D - 7 * p + 0.5) + 2, idx) local iD = floor(D + 0.5) rect(BX - 5, iD - 3, 2, 3, LEG[1], LEG[2], LEG[3]) rect(BX + 2, iD - 3, 2, 3, LEG[1], LEG[2], LEG[3]) rect(BX - 6, iD - 1, 4, 1, LEG[1], LEG[2], LEG[3]) rect(BX + 1, iD - 1, 4, 1, LEG[1], LEG[2], LEG[3]) local bcy = D - 7.6 + 3.2 * cr local ry, rx = 5.2 * (1 - 0.22 * cr), 8.6 * (1 + 0.1 * cr) local ang = -0.1 - 0.5 * p ell(BX - rx + 1.2, bcy - 2.6, 3.4, 1.6, -0.75 + 0.2 * sin(frame * 0.5), DK[1], DK[2], DK[3]) ell(BX - 1, bcy, rx, ry, ang, O[1], O[2], O[3]) ell(BX - 2.5, bcy + 0.6, 4.6, 2.4, 0.12, DK[1], DK[2], DK[3]) ell(BX - 4, bcy - 1.6, 3.2, 1.1, ang, Y[1], Y[2], Y[3]) local Dd = 56 - hd local bcyd = Dd - 7.6 + 3.2 * crd local hy = bcyd - 5.2 * (1 - 0.22 * crd) - 3.0 local hx = floor(BX - 1 + rx * 0.55 + 1.5 + 2 * p + 0.5) local ny = floor(hy + 2) local nh = floor(bcy - ry + 1.5) - ny if nh > 0 then rect(hx - 2, ny, 4, nh, O[1], O[2], O[3]) end ell(hx, hy, 4.5, 4.5, 0, O[1], O[2], O[3]) ell(hx - 1.2, hy - 1.8, 2.2, 1.4, 0, Y[1], Y[2], Y[3]) local iy = floor(hy + 0.5) local q = (c >= 52 and c < 57) or (c >= 59 and c < 64) if q then rect(hx + 3, iy - 3, 6, 2, BEAK[1], BEAK[2], BEAK[3]) rect(hx + 3, iy + 2, 5, 2, BEAKD[1], BEAKD[2], BEAKD[3]) rect(hx + 3, iy - 1, 5, 3, MOUTH[1], MOUTH[2], MOUTH[3]) px(hx + 4, iy - 1, 255, 255, 255); px(hx + 6, iy - 1, 255, 255, 255) px(hx + 5, iy + 1, 255, 255, 255); px(hx + 7, iy + 1, 255, 255, 255) else rect(hx + 3, iy - 1, 6, 2, BEAK[1], BEAK[2], BEAK[3]) rect(hx + 3, iy + 1, 5, 1, BEAKD[1], BEAKD[2], BEAKD[3]) end if frame % 97 < 3 then rect(hx - 1, iy - 2, 4, 1, INK[1], INK[2], INK[3]) else rect(hx - 1, iy - 3, 4, 4, INK[1], INK[2], INK[3]) rect(hx, iy - 2, 2, 2, 255, 255, 255) px(hx + 1, iy - 1, INK[1], INK[2], INK[3]) end if c >= 51 and c < 67 then local by = 20 - ((c < 54) and 1 or 0) gfx.round_rect(38, by, 25, 11, 255, 246, 246, 150, 60, 84, 2) rect(40, by + 11, 3, 1, 255, 246, 246) rect(40, by + 12, 2, 1, 255, 246, 246) px(40, by + 13, 255, 246, 246) if not BUB_Y then local t, b = gfx.text_ink("QUACK", BUB) BUB_Y = (11 - (b - t + 1)) // 2 - t end gfx.text(39, by + BUB_Y, "QUACK", 160, 40, 70, BUB) end local sw = floor(18 - h * 0.7 + 0.5) blend_rect(BX - sw // 2, 62, sw, 2, 0, 0, 0, 0.3) end local function draw_world(frame) local d = frame * VG local off = (d * 0.12) % PER for k = 0, 1 do for i = 1, NB do local x = BXs[i] - off + k * PER if x < W and x + BWs[i] > 0 then frect(x, BTs[i], BWs[i], 46 - BTs[i], FAR[1], FAR[2], FAR[3]) end end end local o2 = (d * 0.5) % 5 for k = 0, 13 do frect(k * 5 - o2, 40, 2, 11, FENCE[1], FENCE[2], FENCE[3]) end local o3 = (d * 0.5) % 60 for k = 0, 1 do local x = 12 + 60 * k - o3 if x > -8 and x < W + 2 then frect(x, 16, 2, 35, POLE[1], POLE[2], POLE[3]) frect(x, 14, 6, 2, POLE[1], POLE[2], POLE[3]) frect(x + 3, 16, 3, 1, BULB[1], BULB[2], BULB[3]) end end local o4 = (d * 0.8) % 16 for k = 0, 4 do frect(k * 16 - o4, 52, 1, 3, JOINT[1], JOINT[2], JOINT[3]) end local o5 = d % 20 for k = 0, 3 do frect(k * 20 - o5, 62, 10, 2, DASH[1], DASH[2], DASH[3]) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local fr = frame * STEP_HZ // gfx.FPS draw_world(fr) draw_duck(fr) end