info = { type = "image", title = "Waving robot", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A little robot waves while a cloud drips", api = 33, } local tr = ({ sk = { ["Robot colour"] = "Farba robota", ["Waving"] = "Mávanie", ["rare"] = "zriedka", ["normal"] = "normálne", ["often"] = "často", ["Raindrops"] = "Dažďové kvapky" }, cs = { ["Robot colour"] = "Barva robota", ["Waving"] = "Mávání", ["rare"] = "zřídka", ["normal"] = "normálně", ["often"] = "často", ["Raindrops"] = "Dešťové kapky" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "body", label = T("Robot colour"), type = "color", default = "#FFC83C" }, { key = "wave", label = T("Waving"), type = "int", default = 1, opts = { T("rare"), T("normal"), T("often") } }, { key = "rain", label = T("Raindrops"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, abs, min = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local SKY0, SKY1 = { 30, 28, 84 }, { 30, 28, 128 } local HILL, GRASS, GRASS_L = { 36, 70, 100 }, { 70, 170, 60 }, { 110, 210, 80 } local DIRT, DIRT_L, DIRT_D = { 118, 72, 44 }, { 150, 98, 60 }, { 84, 52, 34 } local METAL, METAL_D, WHEEL = { 150, 158, 180 }, { 96, 104, 124 }, { 70, 76, 94 } local SCREEN, EYE = { 28, 48, 100 }, { 255, 245, 150 } local CLOUD, CLOUD_D = { 206, 202, 232 }, { 150, 148, 196 } local DROP = { 140, 200, 255 } local BODY, BODY_L, BODY_D = { 255, 200, 60 }, { 255, 224, 130 }, { 190, 140, 30 } local function span(xa, xb, y, r, g, b) if xb - xa <= 0.05 then return end local ia, ib = floor(xa), floor(xb) if ia == ib then blend(ia, y, r, g, b, xb - xa) return end local a0 = ia if xa - ia > 0.03 then blend(ia, y, r, g, b, 1 - (xa - ia)) a0 = ia + 1 end if xb - ib > 0.03 then blend(ib, y, r, g, b, xb - ib) end if ib > a0 then rect(a0, y, ib - a0, 1, r, g, b) end end local function rr(x, y, w, h, c) rect(x + 1, y, w - 2, h, c[1], c[2], c[3]) rect(x, y + 1, w, h - 2, c[1], c[2], c[3]) end local BG = nil local function build_bg() for y = 0, 53 do rect(0, y, W, 1, SKY0[1], SKY0[2], floor(SKY0[3] + (SKY1[3] - SKY0[3]) * y / 53 + 0.5)) end for i = 1, 12 do local h = hash32(i * 0x9E3779B1) px(umod(h, W), umod(h >> 8, 30), 170, 186, 240) end for _, d in ipairs({ { 10, 18, 9, { 40, 40, 100 } }, { 10, 18, 7, { 50, 50, 118 } }, { 10, 18, 4, { 250, 232, 170 } } }) do local cx, cy, r, c = d[1], d[2], d[3], d[4] for y = cy - r, cy + r do local dy = y - cy local s = r * r - dy * dy if s >= 0 then local hw = floor(sqrt(s) + 0.5); rect(cx - hw, y, 2 * hw + 1, 1, c[1], c[2], c[3]) end end end for y = 15, 21 do local dy = y - 17 local s = 3.3 * 3.3 - dy * dy if s >= 0 then local hw = floor(sqrt(s) + 0.5); rect(10 - 2 - hw, y, 2 * hw + 1, 1, 50, 50, 118) end end for x = 0, W - 1 do local top = 47 + floor(3 * sin(x * 0.13 + 1) + 2 * sin(x * 0.31) + 0.5) rect(x, top, 1, 54 - top, HILL[1], HILL[2], HILL[3]) end rect(0, 54, W, 3, GRASS[1], GRASS[2], GRASS[3]) rect(0, 54, W, 1, GRASS_L[1], GRASS_L[2], GRASS_L[3]) for x = 0, W - 1 do local h = hash32(x * 0x85EBCA6B) if umod(h, 4) == 0 then px(x, 53, GRASS[1], GRASS[2], GRASS[3]) end if umod(h >> 6, 5) == 0 then px(x, 56, GRASS_L[1], GRASS_L[2], GRASS_L[3]) end end rect(0, 57, W, 7, DIRT[1], DIRT[2], DIRT[3]) for i = 1, 36 do local h = hash32(i * 0xC2B2AE35) local c = (i % 2 == 0) and DIRT_L or DIRT_D px(umod(h, W), 58 + umod(h >> 8, 6), c[1], c[2], c[3]) end for _, x in ipairs({ 8, 55 }) do rect(x, 50, 1, 4, 60, 150, 60) rect(x - 1, 48, 3, 1, 255, 120, 170); rect(x - 1, 50, 3, 1, 255, 120, 170) rect(x - 1, 49, 1, 1, 255, 120, 170); rect(x + 1, 49, 1, 1, 255, 120, 170) px(x, 49, 255, 220, 60) end rect(22, 50, 21, 5, WHEEL[1], WHEEL[2], WHEEL[3]) for _, wx in ipairs({ 24, 30, 36 }) do rect(wx, 51, 4, 4, METAL_D[1], METAL_D[2], METAL_D[3]) px(wx + 1, 52, METAL[1], METAL[2], METAL[3]); px(wx + 2, 53, METAL[1], METAL[2], METAL[3]) end rr(22, 35, 21, 15, BODY) rect(41, 36, 2, 13, BODY_D[1], BODY_D[2], BODY_D[3]) rect(23, 48, 18, 2, BODY_D[1], BODY_D[2], BODY_D[3]) rect(22, 36, 1, 12, BODY_L[1], BODY_L[2], BODY_L[3]) rect(26, 38, 13, 9, 46, 38, 80) rect(30, 32, 5, 3, METAL_D[1], METAL_D[2], METAL_D[3]) rr(17, 16, 31, 16, BODY) rect(46, 17, 2, 14, BODY_D[1], BODY_D[2], BODY_D[3]) rect(18, 29, 28, 2, BODY_D[1], BODY_D[2], BODY_D[3]) rect(18, 17, 28, 1, BODY_L[1], BODY_L[2], BODY_L[3]) rr(20, 19, 25, 10, SCREEN) rect(15, 21, 2, 6, METAL[1], METAL[2], METAL[3]); rect(48, 21, 2, 6, METAL[1], METAL[2], METAL[3]) rect(32, 12, 1, 4, METAL[1], METAL[2], METAL[3]) BG = gfx.snapshot(0, 0, W, H) end local CYC = 300 local function ease(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local RAIN = true function on_settings() local c = cfg.body or { r = 255, g = 200, b = 60 } BODY = { c.r, c.g, c.b } BODY_L = { c.r + (255 - c.r) // 2, c.g + (255 - c.g) // 2, c.b + (255 - c.b) // 2 } BODY_D = { c.r * 3 // 4, c.g * 3 // 4, c.b * 3 // 4 } CYC = ({ [0] = 520, [1] = 300, [2] = 200 })[cfg.wave or 1] or 300 RAIN = cfg.rain ~= false BG = nil end local function limb(x0, y0, a, len, col) return x0 + len * sin(a), y0 + len * cos(a) end local function bone(x0, y0, x1, y1, c) local n = floor(sqrt((x1 - x0) ^ 2 + (y1 - y0) ^ 2) + 0.5) if n < 1 then n = 1 end for i = 0, n do local x, y = x0 + (x1 - x0) * i / n, y0 + (y1 - y0) * i / n rect(floor(x - 1), floor(y - 1), 3, 3, c[1], c[2], c[3]) end end local function arms(t) local lx, ly = 21, 38 local la = -0.22 - 0.04 * sin(t * 0.05) local ex, ey = limb(lx, ly, la, 6, 0) local hx, hy = limb(ex, ey, la - 0.05, 5, 0) bone(lx, ly, ex, ey, BODY_D); bone(ex, ey, hx, hy, BODY_D) rect(floor(hx - 1), floor(hy), 3, 2, METAL[1], METAL[2], METAL[3]) local T0 = 40 local tc = t % CYC local up, amp = 0, 0 if tc >= T0 and tc < T0 + 20 then up = ease((tc - T0) / 20) elseif tc >= T0 + 20 and tc < T0 + 90 then up = 1 elseif tc >= T0 + 90 and tc < T0 + 114 then up = 1 - ease((tc - T0 - 90) / 24) end if tc >= T0 + 20 and tc < T0 + 90 then amp = 0.5 * min(1, (tc - T0 - 20) / 8) * min(1, (T0 + 90 - tc) / 16) end local sx, sy = 43, 38 local a1 = 0.12 + (1.85 - 0.12) * up local g = a1 + (2.9 - a1) * up + amp * sin(tc * 0.5) local ex2, ey2 = limb(sx, sy, a1, 8, 0) local hx2, hy2 = limb(ex2, ey2, g, 7, 0) bone(sx, sy, ex2, ey2, BODY_D); bone(ex2, ey2, hx2, hy2, BODY_D) rect(floor(ex2 - 1), floor(ey2 - 1), 3, 3, METAL_D[1], METAL_D[2], METAL_D[3]) rect(floor(hx2 - 1), floor(hy2 - 1), 3, 3, METAL[1], METAL[2], METAL[3]) return up end local function cloud_x(t) return (t * 0.30 + 14) % (W + 24) - 24 end local CL = { { 6, 8 }, { 3, 14 }, { 1, 18 }, { 0, 20 }, { 1, 18 }, { 3, 14 } } local function draw_cloud(t) local x = cloud_x(t) local fx = x - floor(x) local ix = floor(x) for i = 1, 6 do local o, w = CL[i][1], CL[i][2] local c = (i >= 5) and CLOUD_D or CLOUD span(ix + o + fx, ix + o + w + fx, 1 + i, c[1], c[2], c[3]) end end local function draw_drop(t) if not RAIN then return end for k = floor((t - 80) / 150), floor(t / 150) do local tk = k * 150 + 40 local dt = t - tk if dt >= 0 and dt < 52 then local x = cloud_x(tk) + 10 + umod(hash32(k * 0x9E3779B1), 7) - 3 if x < 13 or x > 51 then local yg = 53 local y = 8 + 0.5 * 0.07 * dt * dt local ix = floor(x + 0.5) if y < yg then local v = 0.07 * dt local len = 1 + floor(min(2, v / 1.2)) rect(ix, floor(y), 2, len + 1, DROP[1], DROP[2], DROP[3]) px(ix, floor(y), 230, 245, 255) if dt < 3 then blend_rect(ix - 1, 8, 3, 1, DROP[1], DROP[2], DROP[3], 0.5) end else local ts = dt - sqrt(2 * (yg - 8) / 0.07) if ts >= 0 and ts < 9 then local r = 1 + floor(ts * 0.45) local a = 1 - ts / 9 blend(ix - r, yg, DROP[1], DROP[2], DROP[3], a) blend(ix + r, yg, DROP[1], DROP[2], DROP[3], a) blend(ix - floor(r / 2), yg - 1 - (r > 1 and 1 or 0), DROP[1], DROP[2], DROP[3], a) blend(ix + floor(r / 2), yg - 1 - (r > 1 and 1 or 0), DROP[1], DROP[2], DROP[3], a) end end end end end end local function eyes(t, up) local nb = t // 110 local tb = nb * 110 + 30 + umod(hash32(nb * 0x9E3779B1), 50) local d = t - tb local lid = 0 if d >= 0 and d < 6 then lid = d < 2 and d / 2 or (d < 3 and 1 or 1 - (d - 3) / 3) end local cx = cloud_x(t) + 10 local gx = (cx - 32) / 30 if gx > 1 then gx = 1 elseif gx < -1 then gx = -1 end gx = gx * (1 - up) + up * 0.0 for _, ex in ipairs({ 24, 36 }) do rect(ex, 20, 5, 8, EYE[1], EYE[2], EYE[3]) rect(ex + 1, 19, 3, 1, EYE[1], EYE[2], EYE[3]) local pxo = floor(1 + gx + 0.5) rect(ex + pxo, 22 + (up > 0.5 and 0 or 1), 2, 3, 20, 20, 40) if lid > 0 then local h = floor(lid * 9 + 0.5) rect(ex, 19, 5, h, SCREEN[1], SCREEN[2], SCREEN[3]) if lid >= 0.99 then rect(ex, 24, 5, 1, EYE[1], EYE[2], EYE[3]) end end end 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 na = sin(t * 0.07) * 1.0 + 0.4 * sin(t * 0.23) local nx, ny = 32.5 + 4 * sin(na), 45 - 4 * cos(na) rect(30, 40, 5, 4, 90, 90, 140) rect(31, 41, 3, 2, 255, 240, 120) px(floor(32 + 2 * sin(na)), floor(42 - 1 * cos(na)), 200, 50, 60) local on = (t % 36) < 6 if on then rect(31, 9, 3, 3, 255, 70, 70); px(32, 8, 255, 180, 180) else rect(31, 10, 3, 2, 110, 30, 40) end draw_cloud(t) draw_drop(t) local up = arms(t) eyes(t, up) end