info = { type = "image", title = "Hopping mango", category = "food", author = "BRRRO", version = "1.0", license = "MIT", desc = "A cheeky mango hops and sticks its tongue out", api = 33, } local ltr = ({ sk = { ["Speed"] = "Rýchlosť", ["Skin"] = "Pokožka", ["classic"] = "klasická", ["sunset"] = "západ slnka", ["green"] = "zelená", ["Tongue"] = "Jazyk" }, cs = { ["Speed"] = "Rychlost", ["Skin"] = "Pokožka", ["classic"] = "klasická", ["sunset"] = "západ slunce", ["green"] = "zelená", ["Tongue"] = "Jazyk" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "speed", label = LT("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "skin", label = LT("Skin"), type = "int", default = 0, opts = { LT("classic"), LT("sunset"), LT("green") } }, { key = "tongue", label = LT("Tongue"), type = "bool", default = true }, } 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, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local PI = 3.14159265 local GY = 58 local CX = 32 local PALS = { { { 228, 44, 44 }, { 255, 142, 34 }, { 255, 214, 60 }, { 112, 208, 60 } }, { { 222, 44, 122 }, { 255, 100, 60 }, { 255, 170, 40 }, { 255, 226, 84 } }, { { 168, 228, 76 }, { 100, 202, 62 }, { 54, 170, 72 }, { 34, 130, 66 } }, } local STOPS = { 0, 0.38, 0.62, 1 } local CR, CG, CB = {}, {}, {} local FLOOR, FLOOR_L, SHADOW = { 70, 60, 94 }, { 100, 88, 132 }, { 34, 28, 50 } local STEM, LEAF, LEAF_L = { 120, 76, 34 }, { 30, 128, 58 }, { 74, 200, 96 } local DARK, PINK, PINK_D = { 124, 30, 32 }, { 246, 98, 130 }, { 198, 62, 96 } local EYEK = { 22, 14, 18 } local SK, TONGUE, KT = 0, true, 1.0 function on_settings() SK = cfg.skin or 0 local P = PALS[SK + 1] or PALS[1] for i = 0, 63 do local t = i / 63 local j = 1 while j < 3 and t > STOPS[j + 1] do j = j + 1 end local u = (t - STOPS[j]) / (STOPS[j + 1] - STOPS[j]) local a, b = P[j], P[j + 1] CR[i] = floor(a[1] + (b[1] - a[1]) * u + 0.5) CG[i] = floor(a[2] + (b[2] - a[2]) * u + 0.5) CB[i] = floor(a[3] + (b[3] - a[3]) * u + 0.5) end TONGUE = cfg.tongue ~= false KT = 0.7 + 0.1 * (cfg.speed or 3) end local function ell(cx, cy, rx, ry, a, r, g, b, al) al = al or 1 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) * al if cov > 0.1 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 TOT, OFF = 166, 88 local HS = { 0, 43, 86 } local HT = { 22, 22, 27 } local HHT = { 11, 11, 17 } local function mstate(c) if c >= 136 then return 0, 0.07 * sin((c - 136) * 0.4) end local k = (c < 43 and 1) or (c < 86 and 2) or 3 local s = c - HS[k] local T, Hh = HT[k], HHT[k] if s < 7 then return 0, sin(s / 7 * PI / 2) end s = s - 7 if s < T then local u = s / T local cr if s < 3 then cr = 1 - 1.7 * s / 3 elseif s < T * 0.5 then cr = -0.7 * (1 - (s - 3) / (T * 0.5 - 3)) elseif s > T - 5 then cr = 0.25 * (s - (T - 5)) / 5 else cr = 0 end return 4 * Hh * u * (1 - u), cr end s = s - T return 0, (0.8 + 0.4 * (Hh - 11) / 6) * cos(0.55 * s) * exp(-0.15 * s) end local BG = nil local SX, SY, SP = {}, {}, {} for i = 1, 8 do local h = hash32(i * 0x9E3779B1) & 0x7FFF SX[i], SY[i], SP[i] = h % W, (h >> 6) % 44, (h >> 10) % 60 end local function build_bg() rect(0, 0, W, H, 0, 0, 0) rect(0, GY, W, H - GY, FLOOR[1], FLOOR[2], FLOOR[3]) rect(0, GY, W, 1, FLOOR_L[1], FLOOR_L[2], FLOOR_L[3]) BG = gfx.snapshot(0, 0, W, H) end local P1 = { 0, 0, 0, 0, 0, 0 } local P2 = { 0, 0, 0, 0, 0, 0 } local function prep(p, cx, cy, rx, ry, a) local c, s = cos(a), sin(a) local ix, iy = 1 / (rx * rx), 1 / (ry * ry) p[1], p[2] = cx, cy p[3] = c * c * ix + s * s * iy p[4] = 2 * c * s * (ix - iy) p[5] = s * s * ix + c * c * iy p[6] = sqrt(rx * rx * s * s + ry * ry * c * c) end local function span(p, y) local dy = y - p[2] local A, B = p[3], p[4] * dy local disc = B * B - 4 * A * (p[5] * dy * dy - 1) if disc <= 0 then return nil end local sq = sqrt(disc) / (2 * A) local m = p[1] - B / (2 * A) return m - sq, m + sq end local function body(cx, cy, sx, sy, tau) local c, s = cos(tau), sin(tau) prep(P1, cx, cy, 13 * sx, 16 * sy, tau) prep(P2, cx + 3.5 * sx * c - 9.5 * sy * s, cy + 3.5 * sx * s + 9.5 * sy * c, 7 * sx, 8.5 * sy, tau) local top = P1[2] - P1[6] local t2 = P2[2] - P2[6] if t2 < top then top = t2 end local bot = P1[2] + P1[6] local b2 = P2[2] + P2[6] if b2 > bot then bot = b2 end local len = bot - top 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.1 then local ym = (y0 + y1) * 0.5 local a1, b1 = span(P1, ym) local a2, b2_ = span(P2, ym) local lo, hi = a1, b1 if a2 and (not lo or a2 < lo) then lo = a2 end if b2_ and (not hi or b2_ > hi) then hi = b2_ end if lo then local xs, xe = floor(lo + 0.5), floor(hi + 0.5) if xe > xs then local i = floor((ym - top) / len * 63) if cov > 0.97 then rect(xs, y, xe - xs, 1, CR[i], CG[i], CB[i]) else blend_rect(xs, y, xe - xs, 1, CR[i], CG[i], CB[i], cov) end end end end end return bot end local SPK = { { -7, -3, 1 }, { 5, -9, 2 }, { 8, 3, 1 }, { -4, 9, 2 }, { 2, 12, 1 }, { -9, 6, 2 }, { 6, 8, 1 }, { 10, -4, 2 } } function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame * STEP_HZ // FPS for i = 1, 8 do local lit = (f + SP[i]) % 60 < 42 if lit then px(SX[i], SY[i], 150, 150, 206) else px(SX[i], SY[i], 80, 80, 130) end end local c = (f * KT + OFF) % TOT local h, cr = mstate(c) local h1 = mstate((c - 1) % TOT) local h2, cr2 = mstate((c - 2) % TOT) local h3, cr3 = mstate((c - 3) % TOT) local sy = 1 - 0.26 * cr local sx = 1 / sqrt(sy) local tau = -0.3 - 0.07 * (h - h1) local cs, sn = cos(tau), sin(tau) local cy0 = 30 local bot0 = 0 do local a = 16 * sy * cs local b = 13 * sx * sn local hh1 = sqrt(b * b + a * a) local oy = 3.5 * sx * sn + 9.5 * sy * cs local a2 = 8.5 * sy * cs local b2 = 7 * sx * sn local hh2 = sqrt(b2 * b2 + a2 * a2) bot0 = hh1 if oy + hh2 > bot0 then bot0 = oy + hh2 end end local cy = GY - h - bot0 local shw = (12 - h * 0.35) * sx ell(CX, GY + 1.8, shw, 1.6, 0, SHADOW[1], SHADOW[2], SHADOW[3]) local function P(lx, ly) return CX + lx * sx * cs - ly * sy * sn, cy + lx * sx * sn + ly * sy * cs end local ax, ay = P(-2, -15.4) local ix_, iy_ = floor(ax + 0.5), floor(ay + 0.5) rect(ix_ - 1, iy_ - 4, 2, 5, STEM[1], STEM[2], STEM[3]) local al = 0.7 - 0.3 * (h2 - h3) + 2.0 * (cr2 - cr3) + 0.06 * sin(f * 0.2) local lx, ly = ax + 5.5 * cos(al), ay - 4 - 5.5 * sin(al) ell(lx, ly, 6.2, 2.4, -al, LEAF[1], LEAF[2], LEAF[3]) ell(lx + 0.5 * cos(al), ly - 0.8, 4.2, 1.1, -al, LEAF_L[1], LEAF_L[2], LEAF_L[3]) body(CX, cy, sx, sy, tau) local hx, hy = P(-6.5, -8) ell(hx, hy, 1.8, 4.6, tau, 255, 255, 255, 0.32) for i = 1, #SPK do local q = SPK[i] local x, y = P(q[1], q[2]) if q[3] == 1 then px(floor(x + 0.5), floor(y + 0.5), 255, 236, 140) else px(floor(x + 0.5), floor(y + 0.5), 226, 100, 36) end end local vy = h - h1 local look = vy > 0.25 and -1 or (vy < -0.25 and 1 or 0) local blink = (f % 79) < 3 or ((f + 9) % 79) < 3 for e = -1, 1, 2 do local x, y = P(e * 4.9, -3.4) local ex, ey = floor(x + 0.5), floor(y + 0.5) if blink then rect(ex - 2, ey, 4, 1, EYEK[1], EYEK[2], EYEK[3]) else ell(x, y, 2.3, 2.9, 0, 255, 255, 255) rect(ex - 1 + (e > 0 and 0 or 0), ey - 1 + look, 2, 3, EYEK[1], EYEK[2], EYEK[3]) px(ex - 1, ey - 1 + look, 255, 255, 255) end local cxk, cyk = P(e * 8.6, 1.6) blend_rect(floor(cxk + 0.5) - 1, floor(cyk + 0.5), 3, 2, 255, 90, 120, 0.4) end local mx, my = P(0.5, 4.6) mx, my = floor(mx + 0.5), floor(my + 0.5) local tong = TONGUE and c >= 140 and c < 162 if tong then local w = floor(sin(f * 0.45) * 1.4 + 0.5) rect(mx - 2, my, 5, 2, DARK[1], DARK[2], DARK[3]) rect(mx - 1 + w, my + 1, 3, 3, PINK[1], PINK[2], PINK[3]) rect(mx + w, my + 4, 1, 1, PINK[1], PINK[2], PINK[3]) px(mx + w, my + 2, PINK_D[1], PINK_D[2], PINK_D[3]) elseif h > 0.5 then rect(mx - 1, my, 3, 2, DARK[1], DARK[2], DARK[3]) else px(mx - 2, my, DARK[1], DARK[2], DARK[3]) rect(mx - 1, my + 1, 3, 1, DARK[1], DARK[2], DARK[3]) px(mx + 2, my, DARK[1], DARK[2], DARK[3]) end end