info = { type = "image", title = "Garden wake-up", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "Sleepy cabbages and carrots wake up one by one", api = 44, } local tr = ({ sk = { ["Speed"] = "Rýchlosť", ["Snail"] = "Slimák" }, cs = { ["Speed"] = "Rychlost", ["Snail"] = "Šnek" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "snail", label = T("Snail"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, sqrt, min, max = math.floor, math.sin, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local SOIL = 46 local CYCLE = 30 local SKY_T, SKY_H = { 140, 188, 236 }, { 255, 224, 170 } local CD, CM, CL, CS, CF, CC = { 24, 76, 48 }, { 74, 160, 84 }, { 150, 214, 118 }, { 46, 116, 64 }, { 186, 228, 148 }, { 50, 118, 66 } local OR, OR_L, OR_S, OR_R = { 236, 128, 44 }, { 255, 178, 98 }, { 190, 94, 34 }, { 206, 104, 36 } local INK, MOUTH = { 30, 34, 44 }, { 96, 38, 46 } local FD, FM, FL = { 44, 122, 62 }, { 70, 156, 76 }, { 110, 190, 92 } local function c3(c) return c[1], c[2], c[3] end local function mix(a, b, t) return floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5) end local function band(xl, xr, y, h, r, g, b, a) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend_rect(il + 1, y, ir - il - 1, h, r, g, b, a) blend_rect(il, y, 1, h, r, g, b, a * (1 - (xl - il))) blend_rect(ir, y, 1, h, r, g, b, a * (xr - ir)) end local PLANTS = { { "cab", 9, 2, 5.5, 24.2 }, { "car", 22, 5, 9, 24.7 }, { "cab", 34, 8, 12.5, 25.2 }, { "car", 48, 11, 14, 25.7 }, { "cab", 58, 14, 18.5, 26.2 }, } local CAB_Y, CAR_Y = 45, 48 local SNEEZE = 17.2 local function cabbage(cx, cy) for dy = -6, 6 do local y, e = cy + dy, dy / 6.8 local hw = floor(7.4 * sqrt(1 - e * e) + 0.5) rect(cx - hw, y, 2 * hw + 1, 1, c3(CM)) if dy > -4 then rect(cx - hw + 1, y, 2, 1, c3(CS)) end if dy > -5 and dy < 2 then rect(cx + hw - 2, y, 2, 1, c3(CL)) end rect(cx - hw, y, 1, 1, c3(CD)); rect(cx + hw, y, 1, 1, c3(CD)) if dy == -6 or dy == 6 then rect(cx - hw, y, 2 * hw + 1, 1, c3(CD)) end end for dy = -4, 3 do px(cx - 5 + (dy + 4) ^ 2 // 20, cy + dy, c3(CC)) px(cx + 5 - (dy + 4) ^ 2 // 24, cy + dy, c3(CC)) end rect(cx + 6, cy - 5, 3, 1, c3(CD)); rect(cx + 7, cy - 4, 2, 1, c3(CM)) rect(cx - 2, cy - 3, 5, 1, c3(CF)); rect(cx - 3, cy - 2, 7, 4, c3(CF)); rect(cx - 2, cy + 2, 5, 1, c3(CF)) end local function carrot(cx) for i = 0, 8 do local y = 44 + i local hw = (i == 0 or i == 8) and 3 or 4 rect(cx - hw, y, 2 * hw + 1, 1, c3(OR)) rect(cx - hw, y, 1, 1, c3(OR_S)); rect(cx + hw - 1, y, 2, 1, c3(OR_L)) end rect(cx - 3, 46, 3, 1, c3(OR_R)); rect(cx + 1, 50, 3, 1, c3(OR_R)) end local TOP_UP = { { 0, -9, 1, 9, FD }, { -1, -10, 3, 1, FM }, { -1, -8, 1, 1, FL }, { 1, -6, 1, 1, FL }, { -1, -5, 1, 1, FL }, { 1, -3, 1, 1, FL }, { -1, -1, 1, 2, FD }, { -2, -3, 1, 2, FD }, { -3, -5, 1, 2, FD }, { -4, -7, 1, 2, FD }, { -5, -8, 2, 1, FL }, { -4, -9, 1, 1, FL }, { 1, -1, 1, 2, FD }, { 2, -3, 1, 2, FD }, { 3, -5, 1, 2, FD }, { 4, -7, 1, 2, FD }, { 4, -8, 2, 1, FL }, { 4, -9, 1, 1, FL }, } local TOP_DOWN = { { 0, -4, 1, 4, FD }, { 1, -5, 2, 1, FD }, { 3, -4, 1, 2, FD }, { 3, -2, 1, 1, FL }, { -1, -2, 1, 2, FD }, { -2, -3, 1, 1, FD }, { -3, -3, 1, 1, FD }, { -4, -2, 1, 2, FD }, { -4, 0, 1, 1, FL }, { 1, -1, 1, 1, FD }, { 2, -2, 1, 1, FD }, { 4, -1, 1, 1, FD }, { 5, 0, 1, 1, FL }, } local function top(cx, set) for i = 1, #set do local r = set[i] rect(cx + r[1], 43 + r[2], r[3], r[4], c3(r[5])) end end local BGN, PART, STAGE = nil, nil, 1 local V_CAB, V_TOP = {}, {} local RX, RY, RW, RH = 26, 38, 18, 15 local CABPOSE = { { 0, 0 }, { -1, 0 }, { 1, 1 } } local function st_scene() for y = 0, 33 do rect(0, y, W, 1, mix(SKY_T, SKY_H, y / 33)) end local function disc(cx, cy, r, R, G, B, a) for dy = -r, r do local w = floor(sqrt(r * r + 0.5 - dy * dy)) blend_rect(cx - w, cy + dy, 2 * w + 1, 1, R, G, B, a) end end disc(50, 17, 10, 255, 240, 190, 0.22); disc(50, 17, 7, 255, 240, 190, 0.3); disc(50, 17, 4, 255, 252, 226, 1) for x = 0, W - 1 do rect(x, 27 + floor(2.4 * sin(x * 0.13) + 1.6 * sin(x * 0.37 + 1) + 0.5), 1, 7, 170, 198, 200) end for x = 0, W - 1 do local y = 31 + floor(2 * sin(x * 0.21 + 2) + 1.5 * sin(x * 0.5) + 0.5) rect(x, y, 1, 34 - y, 124, 176, 126) end for y = 34, SOIL - 1 do rect(0, y, W, 1, mix({ 134, 188, 106 }, { 92, 150, 78 }, (y - 34) / 12)) end for x = 1, W, 6 do rect(x, 33, 3, 13, 232, 208, 162); rect(x + 2, 33, 1, 13, 202, 172, 124); px(x + 1, 32, 232, 208, 162) end rect(0, 36, W, 1, 206, 176, 128); rect(0, 41, W, 1, 206, 176, 128) rect(0, 37, W, 1, 168, 140, 100); rect(0, 42, W, 1, 168, 140, 100) rect(0, SOIL, W, 4, 152, 100, 66); rect(0, SOIL + 4, W, 4, 128, 82, 54) for i = 1, 26 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), SOIL + umod(h >> 8, 8) if h & 0x10000 == 0 then rect(x, y, 2, 1, 98, 62, 42) else px(x, y, 192, 152, 112) end end rect(0, 54, W, 1, 208, 162, 106); rect(0, 55, W, 4, 178, 130, 82); rect(0, 59, W, 1, 118, 80, 50); rect(0, 60, W, 4, 160, 114, 70) for x = 0, W - 1, 21 do rect(x, 54, 1, 10, 118, 80, 50) end end local function st_plants() blit_rgb(PART, 0, 0, W, H) carrot(PLANTS[2][2]); carrot(PLANTS[4][2]) cabbage(PLANTS[1][2], CAB_Y); cabbage(PLANTS[5][2], CAB_Y) px(PLANTS[1][2] + 3, CAB_Y - 4, 236, 252, 255); px(PLANTS[5][2] - 2, CAB_Y - 3, 236, 252, 255) BGN = snapshot(0, 0, W, H) end local function st_cab(k) return function() blit_rgb(BGN, 0, 0, W, H) local p = CABPOSE[k] cabbage(PLANTS[3][2] + p[1], CAB_Y + p[2]) V_CAB[k] = snapshot(RX, RY, RW, RH) end end local function st_tops() for i = 2, 4, 2 do local cx = PLANTS[i][2] blit_rgb(BGN, 0, 0, W, H) top(cx, TOP_DOWN); V_TOP[i] = { snapshot(cx - 6, 32, 13, 12) } blit_rgb(BGN, 0, 0, W, H) top(cx, TOP_UP); V_TOP[i][2] = snapshot(cx - 6, 32, 13, 12) end end local STAGES = { st_scene, st_plants, st_cab(1), st_cab(2), st_cab(3), st_tops } local function face(cx, cy, st, look) local x, y = cx, cy local i = INK if st == "sleep" then rect(x - 2, y - 1, 2, 1, c3(i)); rect(x + 1, y - 1, 2, 1, c3(i)) rect(x - 1, y + 1, 2, 1, c3(MOUTH)) elseif st == "half" then rect(x - 2, y - 2, 2, 1, 236, 244, 214); rect(x + 1, y - 2, 2, 1, 236, 244, 214) rect(x - 2, y - 1, 2, 1, c3(i)); rect(x + 1, y - 1, 2, 1, c3(i)) rect(x - 1, y + 1, 2, 1, c3(MOUTH)) elseif st == "blink" then rect(x - 2, y - 1, 2, 1, c3(i)); rect(x + 1, y - 1, 2, 1, c3(i)) rect(x - 1, y + 2, 3, 1, c3(MOUTH)); px(x - 2, y + 1, c3(MOUTH)); px(x + 2, y + 1, c3(MOUTH)) elseif st == "yawn" then rect(x - 2, y - 1, 2, 1, c3(i)); rect(x + 1, y - 1, 2, 1, c3(i)) rect(x - 1, y + 1, 3, 3, c3(MOUTH)); rect(x, y + 3, 1, 1, 190, 84, 96) elseif st == "ah" then rect(x - 2, y - 1, 2, 1, c3(i)); rect(x + 1, y - 1, 2, 1, c3(i)) rect(x - 1, y + 1, 3, 2, c3(MOUTH)) elseif st == "choo" then px(x - 2, y - 2, c3(i)); px(x - 1, y - 1, c3(i)); px(x + 2, y - 2, c3(i)); px(x + 1, y - 1, c3(i)) rect(x - 2, y + 1, 5, 3, c3(MOUTH)) else rect(x - 2, y - 2, 2, 2, c3(i)); rect(x + 1, y - 2, 2, 2, c3(i)) px(x - 2 + look, y - 2, 250, 252, 255); px(x + 1 + look, y - 2, 250, 252, 255) rect(x - 1, y + 2, 3, 1, c3(MOUTH)); px(x - 2, y + 1, c3(MOUTH)); px(x + 2, y + 1, c3(MOUTH)) end end local function state(i, u) local p = PLANTS[i] local w, ya, fa = p[3], p[4], p[5] if u < w - 0.5 or u >= fa + 0.6 then return "sleep" end if u < w or u >= fa then return "half" end if i == 3 then if u >= SNEEZE and u < SNEEZE + 0.8 then return "ah" end if u >= SNEEZE + 0.8 and u < SNEEZE + 1.3 then return "choo" end end if u >= ya and u < ya + 1.2 then return "yawn" end if (u * 10 + i * 7) % 42 < 3 then return "blink" end return "open" end local function zz(x, y, t, i) local k = floor((t * 2.5 + i * 1.3) % 6) local zy = y - k local a = 0.85 - k * 0.1 blend_rect(x, zy, 3, 1, 84, 112, 170, a); blend_rect(x + 1, zy + 1, 1, 1, 84, 112, 170, a); blend_rect(x, zy + 2, 3, 1, 84, 112, 170, a) end local B, BD, SH, SD, E = { 210, 202, 156 }, { 162, 150, 106 }, { 222, 160, 96 }, { 150, 98, 52 }, { 50, 40, 40 } local SNAIL = { { 6, 0, 13, B }, { 7, 1, 12, BD }, { 5, 9, 13, B }, { 4, 10, 13, B }, { 3, 11, 11, B }, { 3, 13, 13, B }, { 2, 11, 11, E }, { 2, 13, 13, E }, { 0, 3, 6, SD }, { 1, 1, 8, SH }, { 2, 0, 9, SH }, { 3, 0, 9, SH }, { 4, 0, 9, SH }, { 5, 1, 8, SH }, { 6, 3, 6, SD }, { 2, 3, 6, SD }, { 3, 3, 3, SD }, { 3, 6, 6, SD }, { 4, 3, 6, SD }, } local function snail(t) local span = W + 28 local d = (t * 2.3) % span local x = d - 14 local p = d / span local y0 = 46 if p > 0.04 then local fade = p > 0.85 and (1 - (p - 0.85) / 0.15) or 1 band(0, x + 2, y0 + 7, 1, 255, 250, 232, 0.3 * fade) end for i = 1, #SNAIL do local s = SNAIL[i] local c = s[4] band(x + s[2], x + s[3] + 1, y0 + s[1], 1, c[1], c[2], c[3], 1) end if floor(t * 1.2) % 3 == 0 then band(x + 10.5, x + 11.5, y0 + 2, 1, E[1], E[2], E[3], 1); band(x + 11, x + 12, y0 + 2, 1, B[1], B[2], B[3], 1) end end local DROP_X, DROP_Y = 43, 41 function render() if STAGE <= #STAGES then STAGES[STAGE]() STAGE = STAGE + 1 if BGN then blit_rgb(BGN, 0, 0, W, H) else PART = snapshot(0, 0, W, H) end gfx.ready(false) return end local t = frame / FPS local sp = 0.6 + 0.2 * (cfg.speed or 3) local u = (t * sp) % CYCLE blit_rgb(BGN, 0, 0, W, H) for i = 2, 4, 2 do local p = PLANTS[i] local up = u >= p[3] - 0.2 and u < p[5] + 0.2 local cx = p[2] blit_rgb(V_TOP[i][up and 2 or 1], cx - 6, 32, 13, 12) end local k = 1 if u >= SNEEZE + 0.3 and u < SNEEZE + 0.8 then k = 2 elseif u >= SNEEZE + 0.8 and u < SNEEZE + 1.05 then k = 3 end blit_rgb(V_CAB[k], RX, RY, RW, RH) local pp = CABPOSE[k] for i = 1, 5 do local p = PLANTS[i] local st = state(i, u) local cy = p[1] == "cab" and CAB_Y or CAR_Y local cx = p[2] if i == 3 then cx, cy = cx + pp[1], cy + pp[2] end face(cx, cy, st, floor(u / 1.8 + i) % 2) if st == "sleep" then zz(cx + 6, cy - 7, t, i) end end local rel = SNEEZE + 0.9 local fall = 0.45 if u < rel or u >= SNEEZE + 4 then local a = u >= SNEEZE + 4 and min(1, (u - SNEEZE - 4) / 1.5) or 1 blend_rect(DROP_X + pp[1], DROP_Y + pp[2], 1, 2, 232, 250, 255, a) blend_rect(DROP_X + pp[1], DROP_Y + pp[2] + 1, 1, 1, 150, 200, 240, a) elseif u < rel + fall then local tau = u - rel local y = DROP_Y + 49.4 * tau * tau blend_rect(DROP_X + 1, floor(y), 1, 2, 232, 250, 255, 1) elseif u < rel + fall + 0.35 then local tau = u - rel - fall for j = -1, 1 do local sx, sy = DROP_X + 1 + j * 2.2 * tau * 5, 51 - (6 * tau - 14 * tau * tau) * (j == 0 and 1.4 or 1) blend_rect(floor(sx), floor(sy), 1, 1, 232, 250, 255, 1 - tau * 2) end end if u >= rel + fall and u < rel + fall + 2 then blend_rect(DROP_X - 1, 51, 5, 1, 90, 56, 38, 0.4 * (1 - (u - rel - fall) / 2)) end if u >= SNEEZE + 0.8 and u < SNEEZE + 1.25 then local tau = u - SNEEZE - 0.8 for j = 0, 3 do blend_rect(floor(pp[1] + 38 + tau * (10 + j * 4)), floor(pp[2] + 46 + j + tau * (5 + j * 3)), 1, 1, 220, 240, 255, 0.8 - tau * 1.7) end end if cfg.snail ~= false then snail(t) end end