info = { type = "image", title = "Spherical cow", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A spherical cow rolls in, moos and rolls away", api = 33, } local tr = ({ sk = { ["Spot colour"] = "Farba škvŕn", ["Speed"] = "Rýchlosť", ["Moo bubble"] = "Bublina „bú“" }, cs = { ["Spot colour"] = "Barva skvrn", ["Speed"] = "Rychlost", ["Moo bubble"] = "Bublina „bů“" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "spots", label = T("Spot colour"), type = "color", default = "#4A3A34" }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "moo", label = T("Moo bubble"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, ceil, sin, cos, sqrt, abs = math.floor, math.ceil, math.sin, math.cos, math.sqrt, math.abs local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI = 3.14159265 local SKY = { 120, 198, 255 } local CLOUD, CLOUD_D = { 255, 255, 255 }, { 206, 228, 252 } local HILL, TREE = { 96, 186, 124 }, { 52, 136, 84 } local MEAD1, MEAD2 = { 120, 214, 84 }, { 98, 192, 66 } local GRASS, GRASS_L = { 56, 150, 52 }, { 150, 232, 96 } local SHADOW = { 70, 160, 56 } local BARN, BARN_D = { 214, 64, 52 }, { 150, 40, 40 } local WHITE, SHADE = { 255, 255, 255 }, { 206, 220, 244 } local PINK, HORN, EAR = { 255, 150, 176 }, { 236, 184, 80 }, { 176, 118, 92 } local BLACK = { 30, 24, 28 } local FLOWERS = { { 255, 228, 70 }, { 255, 255, 255 }, { 255, 140, 176 } } local R = 12 local GY = 58 local XC = 32 local XIN, XOUT = -R - 2, W + R + 2 local IL, IR, HL, HR = {}, {}, {}, {} for i = 1, 2 * R do local dy = i - R - 0.5 local hw = sqrt(R * R - dy * dy) IL[i], IR[i] = ceil(-hw - 0.5), floor(hw - 0.5) local r2 = R - 2.2 local d2 = r2 * r2 - (dy + 1.6) * (dy + 1.6) if d2 > 0 then local h2 = sqrt(d2) HL[i], HR[i] = ceil(-1.6 - h2 - 0.5), floor(-1.6 + h2 - 0.5) else HL[i], HR[i] = 1, 0 end end local SPOT = { { 0.70, 0.55, 3.2 }, { 1.45, -0.10, 3.8 }, { 2.30, 0.50, 3.0 }, { 3.10, -0.40, 3.6 }, { 3.90, 0.35, 3.4 }, { 4.70, -0.15, 3.0 }, { 5.50, 0.55, 3.5 }, { 0.55, -0.62, 2.6 }, { 2.00, -0.70, 2.4 }, { 4.15, -0.78, 2.6 }, { 5.15, 0.05, 2.2 }, { 1.00, 0.95, 2.0 }, } for i = 1, #SPOT do local s = SPOT[i] local lon, lat = s[1], s[2] s[4], s[5], s[6] = cos(lat) * sin(lon), sin(lat), cos(lat) * cos(lon) end local BODY = {} do local function v(lon, lat, r, c, k) BODY[#BODY + 1] = { cos(lat) * sin(lon), sin(lat), cos(lat) * cos(lon), r, c, k } end v(-0.36, 0.52, 1.7, HORN, 0); v(0.36, 0.52, 1.7, HORN, 0) v(-1.38, 0.28, 2.6, EAR, 0); v(1.38, 0.28, 2.6, EAR, 0) v(0, -0.10, 4.1, PINK, 0) v(-0.07, -0.08, 0.7, BLACK, 0); v(0.07, -0.08, 0.7, BLACK, 0) v(-0.36, 0.20, 1.3, BLACK, 1); v(0.36, 0.20, 1.3, BLACK, 1) v(0, -0.30, 1.8, BLACK, 2) end local BG = nil local TUFT = {} local function c3(c) return c[1], c[2], c[3] end local function build_bg() rect(0, 0, W, H, c3(SKY)) for x = 0, W - 1 do local top = floor(25 + 2.5 * sin(x * 0.10) + 1.8 * sin(x * 0.27 + 1.0) + 0.5) rect(x, top, 1, 31 - top, c3(HILL)) end rect(7, 24, 11, 7, c3(BARN)); rect(6, 23, 13, 1, c3(BARN_D)); rect(8, 21, 9, 2, c3(BARN_D)) rect(10, 26, 5, 5, c3(WHITE)); rect(11, 27, 3, 4, c3(BARN_D)); px(12, 27, c3(BARN)) rect(50, 21, 7, 6, c3(TREE)); rect(51, 19, 5, 2, c3(TREE)); rect(53, 27, 1, 4, c3(BARN_D)) local ys = { 30, 33, 37, 42, 48, 55, 64 } for i = 1, #ys - 1 do local c = (i % 2 == 1) and MEAD1 or MEAD2 rect(0, ys[i], W, ys[i + 1] - ys[i], c3(c)) end for i = 1, 22 do local h = math.hash32(i * 0x9E3779B1) local x, y = math.umod(h, W), 32 + math.umod(h >> 8, 30) local c = FLOWERS[1 + math.umod(h >> 16, 3)] px(x, y, c3(c)) if y > 46 then px(x + 1, y, c3(c)); px(x, y + 1, c3(GRASS_L)) end end BG = gfx.snapshot(0, 0, W, H) for i = 1, 7 do local h = math.hash32(i * 0x85EBCA6B) TUFT[i] = { x = 3 + (i - 1) * 9 + math.umod(h, 4), y = 61 + math.umod(h >> 8, 2), ph = math.umod(h >> 12, 628) / 100.0 } end end local CLOUDS = { { 4, 20, 2.4, 6 }, { 13, 14, -1.5, 40 }, { 1, 17, 3.3, 12 } } local function hrect(x, y, w, h, c, a) local xi = floor(x) local f = x - xi rect(xi + 1, y, w - 1, h, c3(c)) if f < 0.98 then blend_rect(xi, y, 1, h, c[1], c[2], c[3], 1 - f) end if f > 0.02 then blend_rect(xi + w, y, 1, h, c[1], c[2], c[3], f) end end local function draw_clouds(t) for i = 1, #CLOUDS do local c = CLOUDS[i] local L = c[2] local x = (c[4] + t * c[3]) % (W + L + 8) - L - 4 hrect(x + 4, c[1], L - 8, 1, CLOUD) hrect(x, c[1] + 1, L, 2, CLOUD) hrect(x + 2, c[1] + 3, L - 4, 1, CLOUD_D) end end local SPOTC = { 74, 58, 52 } local function project(vx, vy, vz, cy_, sy_, cr, sr, cxf, cyf) local x1 = vx * cy_ + vz * sy_ local z1 = -vx * sy_ + vz * cy_ local xr = x1 * cr - vy * sr local yr = x1 * sr + vy * cr return cxf + R * xr, cyf - R * yr, z1, xr, yr end local function blob(sx, sy, z, xr, yr, r, squash, c, cxi, cyi) if z < 0.10 then return end local rho = sqrt(xr * xr + yr * yr) local ux, uy = 1, 0 if rho > 0.01 then ux, uy = xr / rho, yr / rho end local a = r * sqrt(ux * ux * z * z + uy * uy) local b = r * squash * sqrt(uy * uy * z * z + ux * ux) if a < 0.6 then a = 0.6 end if b < 0.6 then b = 0.6 end local y0, y1 = floor(sy - b + 0.5), ceil(sy + b - 0.5) if y1 < y0 then y1 = y0 end for y = y0, y1 do local dy = (y + 0.5 - sy) / b local q = 1 - dy * dy if q > 0 then local hw = a * sqrt(q) local x1, x2 = ceil(sx - hw - 0.5), floor(sx + hw - 0.5) if x2 < x1 then x1, x2 = floor(sx), floor(sx) end local i = y - (cyi - R) + 1 if i >= 1 and i <= 2 * R then local lo, hi = cxi + IL[i], cxi + IR[i] if x1 < lo then x1 = lo end if x2 > hi then x2 = hi end if x2 >= x1 then rect(x1, y, x2 - x1 + 1, 1, c3(c)) end end end end end local function draw_cow(x, hop, yaw, blink, moo) local cxi = floor(x + 0.5) local cyi = GY - R - floor(hop + 0.5) local sw = R - floor(hop / 2) rect(cxi - sw, GY - 1, 2 * sw, 2, c3(SHADOW)) for i = 1, 2 * R do local y = cyi - R + i - 1 rect(cxi + IL[i], y, IR[i] - IL[i] + 1, 1, c3(SHADE)) if HR[i] >= HL[i] then rect(cxi + HL[i], y, HR[i] - HL[i] + 1, 1, c3(WHITE)) end end local phi = -(x - XC) / R local cy_, sy_, cr, sr = cos(yaw), sin(yaw), cos(phi), sin(phi) local cxf, cyf = x, cyi for i = 1, #SPOT do local s = SPOT[i] local sx, sy, z, xr, yr = project(s[4], s[5], s[6], cy_, sy_, cr, sr, cxf, cyf) blob(sx, sy, z, xr, yr, s[3], 1, SPOTC, cxi, cyi) end for i = 1, #BODY do local b = BODY[i] local sq = 1 local skip = false if b[6] == 1 and blink then sq = 0.3 end if b[6] == 2 and not moo then skip = true end if not skip then local sx, sy, z, xr, yr = project(b[1], b[2], b[3], cy_, sy_, cr, sr, cxf, cyf) blob(sx, sy, z, xr, yr, b[4], sq, b[5], cxi, cyi) end end end local F_MOO = { font = "5x7", w = 21, align = "center" } local MOO_T, MOO_B = gfx.text_ink("MOO", { font = "5x7" }) local function draw_bubble(age) local k = age < 4 and (age + 1) / 5 or 1 local w, h = floor(23 * k), floor(13 * k) local cx, cy = 47, 17 local x, y = cx - w // 2, cy - h // 2 rect(x + 1, y, w - 2, h, 20, 20, 24); rect(x, y + 1, w, h - 2, 20, 20, 24) rect(x + 2, y + 1, w - 4, h - 2, 255, 255, 255); rect(x + 1, y + 2, w - 2, h - 4, 255, 255, 255) if k >= 1 then px(40, y + h - 1, 255, 255, 255); px(41, y + h - 1, 255, 255, 255) px(39, y + h, 20, 20, 24); px(40, y + h, 255, 255, 255); px(41, y + h, 20, 20, 24) px(38, y + h + 1, 20, 20, 24); px(39, y + h + 1, 255, 255, 255); px(40, y + h + 1, 20, 20, 24) px(38, y + h + 2, 20, 20, 24) gfx.text(x + 1, y + 1 + (11 - (MOO_B - MOO_T + 1)) // 2 - MOO_T, "MOO", 20, 20, 24, F_MOO) end end local T_IN0, T_IN1 = 0.8, 3.2 local T_OUT0, T_OUT1 = 10.4, 12.8 local CYCLE = 13.8 local function hop_arc(v) return 4 * v * (1 - v) end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local sp = cfg.speed or 3 local tau = frame / FPS * sp / 3 SPOTC[1], SPOTC[2], SPOTC[3] = 74, 58, 52 local sc = cfg.spots if sc then SPOTC[1], SPOTC[2], SPOTC[3] = sc.r, sc.g, sc.b end draw_clouds(tau) local t = tau % CYCLE local x, hop, yaw, blink, moo, bub = nil, 0, 0, false, false, -1 if t >= T_IN0 and t < T_IN1 then local u = (t - T_IN0) / (T_IN1 - T_IN0) x = XIN + (XC - XIN) * (1 - (1 - u) * (1 - u)) local v = (u * 3) % 1 hop = hop_arc(v) * 6 * (1 - u * 0.85) elseif t >= T_IN1 and t < T_OUT0 then x = XC local s = t - T_IN1 yaw = 0.95 * sin(s * 2 * PI / 7.2) * (s < 7.2 and 1 or 0) blink = (s % 2.6) < 0.12 or (s > 3.0 and s < 3.1) if s > 1.0 and s < 2.6 then moo = s > 1.2 and s < 2.4 if cfg.moo ~= false then bub = floor((s - 1.2) * FPS) end end if s > 1.0 and s < 1.5 then hop = hop_arc((s - 1.0) / 0.5) * 4 end if s > 4.6 and s < 5.1 then hop = hop_arc((s - 4.6) / 0.5) * 3 end elseif t >= T_OUT0 and t < T_OUT1 then local u = (t - T_OUT0) / (T_OUT1 - T_OUT0) x = XC + (XOUT - XC) * u * u local v = (u * 2) % 1 hop = hop_arc(v) * 5 * u end if x then draw_cow(x, hop, yaw, blink, moo) end if bub >= 0 and bub < 36 then draw_bubble(bub) end for i = 1, #TUFT do local g = TUFT[i] local s = floor(1.5 * sin(tau * 2.2 + g.ph) + 0.5) px(g.x, g.y, c3(GRASS)); px(g.x + 2, g.y, c3(GRASS)); px(g.x - 2, g.y, c3(GRASS)) px(g.x + (s > 0 and 1 or 0), g.y - 1, c3(GRASS)); px(g.x - 1 + s, g.y - 2, c3(GRASS)) px(g.x + 2 + s, g.y - 2, c3(GRASS_L)); px(g.x + 1 + s // 2, g.y - 1, c3(GRASS_L)) end end