info = { type = "image", title = "Woodcutter", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "Woodcutter fells a tree, a truck takes the logs", api = 33, } local tr = ({ sk = { ["Season"] = "Ročné obdobie", ["Summer"] = "Leto", ["Autumn"] = "Jeseň", ["Winter"] = "Zima", ["Story speed"] = "Rýchlosť príbehu" }, cs = { ["Season"] = "Roční období", ["Summer"] = "Léto", ["Autumn"] = "Podzim", ["Winter"] = "Zima", ["Story speed"] = "Rychlost příběhu" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "season", label = T("Season"), type = "int", default = "summer", opts = { keys = { "summer", "autumn", "winter" }, T("Summer"), T("Autumn"), T("Winter") } }, { key = "speed", label = T("Story speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_idx, blit_rgb, snapshot = gfx.blit_indexed, gfx.blit_rgb, gfx.snapshot local floor, sin, cos, sqrt, max, min = math.floor, math.sin, math.cos, math.sqrt, math.max, math.min local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local SEASONS = { summer = { s0 = { 62, 132, 226 }, s1 = { 178, 222, 250 }, sun = { 255, 246, 200 }, far = { 140, 184, 208 }, near = { 78, 152, 86 }, g = { { 110, 184, 76 }, { 78, 152, 60 }, { 56, 122, 50 } }, soil = { 120, 86, 54 }, road = { 170, 142, 102 }, mark = { 228, 210, 164 }, cloud = { 255, 255, 255 }, leaf = { { 26, 92, 46 }, { 42, 130, 56 }, { 82, 174, 76 }, { 150, 214, 110 } } }, autumn = { s0 = { 98, 150, 214 }, s1 = { 252, 216, 172 }, sun = { 255, 218, 150 }, far = { 190, 160, 150 }, near = { 150, 134, 62 }, g = { { 170, 160, 70 }, { 138, 130, 56 }, { 108, 100, 46 } }, soil = { 118, 82, 52 }, road = { 168, 136, 98 }, mark = { 226, 204, 160 }, cloud = { 255, 244, 232 }, leaf = { { 150, 54, 26 }, { 204, 100, 30 }, { 238, 156, 52 }, { 252, 206, 96 } } }, winter = { s0 = { 118, 150, 200 }, s1 = { 216, 228, 242 }, sun = { 250, 250, 255 }, far = { 178, 194, 218 }, near = { 208, 220, 238 }, g = { { 238, 244, 252 }, { 206, 220, 240 }, { 174, 192, 222 } }, soil = { 134, 124, 126 }, road = { 188, 192, 206 }, mark = { 240, 244, 250 }, cloud = { 240, 244, 252 }, leaf = { { 28, 82, 76 }, { 52, 118, 100 }, { 112, 168, 140 }, { 232, 242, 248 } } }, } local P = SEASONS.summer local K_SPEED = { 0.6, 0.8, 1.0, 1.3, 1.7 } local FEET = 53 local PIVX, PIVY = 22.5, 50.0 local TRUNK = 16 local CR = 7 local FALL_DEG = 78 local KR, KG, KB = 255, 0, 255 local function canvas(w, h) local g = {} for i = 1, w * h do g[i] = 0 end return { w = w, h = h, g = g } end local function fill(c, v, x0, y0, x1, y1) for y = y0, y1 do for x = x0, x1 do c.g[y * c.w + x + 1] = v end end end local function pack(c, flip) local rows = {} for y = 0, c.h - 1 do local r = {} for x = 1, c.w do r[x] = c.g[y * c.w + (flip and c.w - x + 1 or x)] end rows[y + 1] = string.char(table.unpack(r)) end return table.concat(rows) end local MPAL = { 0, 0, 0, 238, 186, 146, 196, 40, 40, 120, 26, 34, 48, 66, 128, 52, 36, 30, 120, 74, 38, 232, 150, 40, 30, 24, 28 } local BODY, BODYF = {}, {} local POSE = { hold = { 7, 7, 9, 2, 9, 0, 3, 3 }, raise = { 7, 5, 6, -3, 7, -4, 3, 3 }, mid = { 8, 5, 11, 1, 11, 0, 3, 3 }, hit = { 7, 7, 9, 7, 9, 5, 3, 5 }, } local TPAL = { 0, 0, 0, 208, 38, 34, 136, 22, 30, 152, 214, 238, 176, 128, 72, 118, 82, 46, 36, 36, 44, 255, 226, 110, 200, 204, 214, 70, 72, 84 } local TRUCK local function build_sprites() for legs = 0, 1 do local c = canvas(12, 13) fill(c, 7, 3, 0, 6, 0); fill(c, 7, 2, 1, 7, 1) fill(c, 1, 3, 2, 6, 3); fill(c, 8, 5, 2, 5, 2) fill(c, 6, 3, 4, 6, 4); fill(c, 6, 3, 3, 3, 3) fill(c, 2, 2, 5, 6, 8); fill(c, 3, 2, 5, 2, 8) fill(c, 3, 3, 6, 3, 6); fill(c, 3, 5, 6, 5, 6); fill(c, 3, 4, 7, 4, 7); fill(c, 3, 6, 7, 6, 7) fill(c, 4, 3, 9, 6, 10) if legs == 0 then fill(c, 4, 3, 11, 6, 11); fill(c, 5, 3, 12, 4, 12); fill(c, 5, 5, 12, 7, 12) else fill(c, 4, 2, 11, 3, 11); fill(c, 4, 6, 11, 7, 11); fill(c, 5, 1, 12, 3, 12); fill(c, 5, 6, 12, 8, 12) end BODY[legs], BODYF[legs] = pack(c), pack(c, true) end do local c = canvas(24, 9) fill(c, 1, 4, 0, 9, 0); fill(c, 1, 3, 1, 9, 1); fill(c, 1, 2, 2, 9, 3); fill(c, 1, 0, 4, 9, 7) fill(c, 3, 5, 1, 7, 1); fill(c, 3, 4, 2, 7, 2); fill(c, 3, 3, 3, 7, 3) fill(c, 2, 0, 5, 0, 6); fill(c, 7, 0, 4, 0, 4); fill(c, 8, 0, 7, 1, 7); fill(c, 6, 0, 6, 0, 6) fill(c, 2, 8, 4, 9, 4) fill(c, 9, 0, 8, 23, 8) fill(c, 4, 10, 6, 23, 6); fill(c, 5, 10, 7, 23, 7) fill(c, 5, 10, 1, 10, 5); fill(c, 5, 23, 1, 23, 5) TRUCK = pack(c) end end local BG, CROWN = nil, nil local function bake_crown(R) local n = 2 * R + 1 rect(0, 0, n, n, KR, KG, KB) local L = P.leaf for y = 0, n - 1 do for x = 0, n - 1 do local dx, dy = x - R, y - R local d = sqrt(dx * dx + dy * dy) if d <= R + 0.35 then local v = (-0.6 * dx - 0.7 * dy) / (R + 0.01) v = v + (umod(hash32(x * 73 + y * 179 + R * 7), 100) / 100 - 0.5) * 0.55 if d > R - 1.1 then v = v - 0.25 end local i = v < -0.4 and 1 or v < 0.2 and 2 or v < 0.7 and 3 or 4 px(x, y, L[i][1], L[i][2], L[i][3]) end end end return snapshot(0, 0, n, n) end local function lerp3(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 HILL_F, HILL_N = {}, {} for x = 0, W - 1 do HILL_F[x] = floor(43 + 3 * sin(x * 0.09 + 1) + 2 * sin(x * 0.23) + 0.5) HILL_N[x] = floor(47 + 2 * sin(x * 0.13 + 3) + 1 * sin(x * 0.37) + 0.5) end local function build() build_sprites() CROWN = {} for _, R in ipairs({ 1, 2, 3, 5, 6, 7 }) do CROWN[R] = bake_crown(R) end for y = 0, 49 do rect(0, y, W, 1, lerp3(P.s0, P.s1, y / 49)) end local s = P.sun rect(49, 9, 6, 6, s[1], s[2], s[3]); rect(48, 10, 8, 4, s[1], s[2], s[3]); rect(50, 8, 4, 8, s[1], s[2], s[3]) for x = 0, W - 1 do rect(x, HILL_F[x], 1, 51 - HILL_F[x], P.far[1], P.far[2], P.far[3]) rect(x, HILL_N[x], 1, 51 - HILL_N[x], P.near[1], P.near[2], P.near[3]) end local g = P.g rect(0, 50, W, 1, g[1][1], g[1][2], g[1][3]) rect(0, 51, W, 2, g[2][1], g[2][2], g[2][3]) rect(0, 53, W, 2, g[3][1], g[3][2], g[3][3]) rect(0, 55, W, 2, P.soil[1], P.soil[2], P.soil[3]) rect(0, 57, W, 7, P.road[1], P.road[2], P.road[3]) rect(0, 57, W, 1, P.soil[1], P.soil[2], P.soil[3]) for x = 2, W - 1, 12 do rect(x, 60, 6, 1, P.mark[1], P.mark[2], P.mark[3]) end for i = 1, 14 do local h = hash32(i * 0x9E3779B1) px(umod(h, W), 58 + umod(h >> 8, 5), P.soil[1], P.soil[2], P.soil[3]) end if P == SEASONS.summer then for i = 1, 9 do local h = hash32(i * 0x85EBCA6B) local c = umod(h >> 12, 3) px(umod(h, W), 51 + umod(h >> 8, 2), c == 0 and 255 or 250, c == 1 and 120 or 236, c == 0 and 120 or 90) end elseif P == SEASONS.autumn then for i = 1, 10 do local h = hash32(i * 0x85EBCA6B) px(umod(h, W), 51 + umod(h >> 8, 3), 204, 100, 30) end end BG = snapshot(0, 0, W, H) end function on_settings() P = SEASONS[cfg.season] or SEASONS.summer BG = nil end local LOOP = 28.0 local GROW_DT = 0.55 local STAGES = { { 2, 0 }, { 5, 0 }, { 8, 1 }, { 11, 2 }, { 14, 3 }, { 16, 5 }, { 18, 6 }, { 19, 7 } } local T_WALK, T_HIT, HIT_DT = 5.2, 9.0, 1.1 local T_FALL = 12.2 local FALL_DT, B1_DT, B2_DT = 1.3, 0.4, 0.25 local T_CHOP, CHOP_DT = 15.2, 1.0 local T_CUT = T_CHOP + CHOP_DT + 0.55 local T_TRUCK, T_LOAD, LOAD_DT, FLIGHT = 17.4, 19.8, 1.0, 0.8 local T_LEAVE = 23.4 local X_STOP = 38 local TRUCK_Y = 50 local function man_state(ts) if ts < T_WALK then return nil end local u = (ts - T_WALK) / 2.6 if u < 1 then return -12 + 22 * u, "hold", 1 end if ts < T_HIT then return 10, "hold", 1 end if ts < T_FALL then local p = (ts - T_HIT) % HIT_DT return 10, p < 0.55 and "raise" or p < 0.65 and "mid" or "hit", 1 end if ts < 14.4 then return 10, "hold", 1 end if ts < T_CHOP then return 10 + 2 * min(1, (ts - 14.4) / 0.7), "hold", 1 end if ts < T_CUT + 0.35 then local p = ts - T_CHOP if p < 0 then return 12, "hold", 1 end p = p % CHOP_DT return 12, p < 0.45 and "raise" or p < 0.55 and "mid" or "hit", 1 end if ts < T_CUT + 0.8 then return 12 - 2 * (ts - T_CUT - 0.35) / 0.45, "hold", 1 end if ts < T_LEAVE + 0.6 then local k = (ts - T_LOAD) / LOAD_DT local ph = k - floor(k) if ts > T_LOAD - 0.25 and ts < T_LOAD + 3 * LOAD_DT - 0.5 and (ph < 0.15 or ph > 0.75) then return 10, "throw", 1 end return 10, "hold", 1 end local x = 10 - 8.5 * (ts - T_LEAVE - 0.6) if x < -14 then return nil end return x, "hold", -1 end local function line(ax, ay, bx, by, r, g, b) local n = max(math.abs(bx - ax), math.abs(by - ay)) if n == 0 then px(ax, ay, r, g, b) return end for i = 0, n do px(floor(ax + (bx - ax) * i / n + 0.5), floor(ay + (by - ay) * i / n + 0.5), r, g, b) end end local function draw_man(L, pose, dir, step) local legs = (pose == "hold" and floor(step) % 2 == 1) and 1 or 0 blit_idx(dir > 0 and BODY[legs] or BODYF[legs], MPAL, L, 41, 12, 13, 12, 13, true) local function mx(lx) return dir > 0 and L + lx or L + 11 - lx end if pose == "throw" then line(mx(5), 47, mx(8), 44, 196, 40, 40); px(mx(8), 43, 238, 186, 146) line(mx(2), 47, mx(0), 44, 196, 40, 40); px(mx(0), 43, 238, 186, 146) return end local p = POSE[pose] local hx, hy, tx, ty = p[1], p[2], p[3], p[4] line(mx(5), 47, mx(hx), 41 + hy, 196, 40, 40) px(mx(hx), 41 + hy, 238, 186, 146) line(mx(hx), 41 + hy, mx(tx), 41 + ty, 160, 110, 56) local bx = dir > 0 and L + p[5] or L + 11 - (p[5] + p[7] - 1) rect(bx, 41 + p[6], p[7], p[8], 150, 160, 178) rect(dir > 0 and bx + p[7] - 1 or bx, 41 + p[6], 1, p[8], 226, 232, 242) end local BARK_L, BARK_M, BARK_D = { 148, 102, 60 }, { 120, 80, 46 }, { 86, 56, 34 } local CUT = { 224, 188, 128 } local function stump(cut, a) if a then blend_rect(21, 50, 1, 3, BARK_L[1], BARK_L[2], BARK_L[3], a) blend_rect(22, 50, 1, 3, BARK_M[1], BARK_M[2], BARK_M[3], a) blend_rect(23, 50, 1, 3, BARK_D[1], BARK_D[2], BARK_D[3], a) blend_rect(21, 50, 3, 1, CUT[1], CUT[2], CUT[3], a) return end rect(21, 50, 1, 3, BARK_L[1], BARK_L[2], BARK_L[3]) rect(22, 50, 1, 3, BARK_M[1], BARK_M[2], BARK_M[3]) rect(23, 50, 1, 3, BARK_D[1], BARK_D[2], BARK_D[3]) if cut then rect(21, 50, 3, 1, CUT[1], CUT[2], CUT[3]) end end local function crown(cx, cy, R) local n = 2 * R + 1 blit_rgb(CROWN[R], floor(cx) - R, floor(cy) - R, n, n, KR, KG, KB) end local function tree(theta, hits) local sn, cs = sin(theta), cos(theta) if theta == 0 then rect(21, 50 - TRUNK, 1, TRUNK + 3, BARK_L[1], BARK_L[2], BARK_L[3]) rect(22, 50 - TRUNK, 1, TRUNK + 3, BARK_M[1], BARK_M[2], BARK_M[3]) rect(23, 50 - TRUNK, 1, TRUNK + 3, BARK_D[1], BARK_D[2], BARK_D[3]) if hits > 0 then rect(21, 49 - hits, hits, hits + 1, 40, 24, 16) end else stump(true) for s = 0, TRUNK do local x, y = floor(PIVX + s * sn), floor(PIVY - s * cs) rect(x - 1, y - 1, 3, 3, BARK_M[1], BARK_M[2], BARK_M[3]) px(x - 1, y - 1, BARK_L[1], BARK_L[2], BARK_L[3]) px(x + 1, y + 1, BARK_D[1], BARK_D[2], BARK_D[3]) end end local D = TRUNK + CR * 0.5 crown(PIVX + D * sn, PIVY - D * cs, CR) end local function grow(ts) local k = min(8, floor(ts / GROW_DT) + 1) local th, R = STAGES[k][1], STAGES[k][2] if k == 8 then tree(0, 0) return end if th < 9 then rect(22, FEET - th, 1, th, BARK_M[1], BARK_M[2], BARK_M[3]) else rect(21, FEET - th, 1, th, BARK_L[1], BARK_L[2], BARK_L[3]) rect(22, FEET - th, 1, th, BARK_M[1], BARK_M[2], BARK_M[3]) rect(23, FEET - th, 1, th, BARK_D[1], BARK_D[2], BARK_D[3]) end if R == 0 then local l = P.leaf[3] px(22, FEET - th - 1, l[1], l[2], l[3]); px(21 + (k % 2) * 2, FEET - th, l[1], l[2], l[3]) else crown(22, FEET - th - R * 0.5, R) end end local LOGX = { 26, 32, 38 } local LOGH = { 3, 4, 5 } local SLOT = { { 11, 3 }, { 16, 3 }, { 13, 0 } } local function log(x, y) rect(x, y, 5, 3, BARK_M[1], BARK_M[2], BARK_M[3]) rect(x, y, 5, 1, BARK_L[1], BARK_L[2], BARK_L[3]) rect(x + 4, y + 2, 1, 1, BARK_D[1], BARK_D[2], BARK_D[3]) rect(x, y, 1, 3, CUT[1], CUT[2], CUT[3]) end local function truck_x(ts) local u = (ts - T_TRUCK) / 2.0 if ts < T_TRUCK then return nil end if u < 1 then return X_STOP + (W - X_STOP) * (1 - u) * (1 - u) end if ts < T_LEAVE then return X_STOP end local tau = ts - T_LEAVE local x = X_STOP - 0.5 * 45 * tau * tau if x < -25 then return nil end return x end local function wheel(cx, cy, ph) rect(cx - 1, cy - 2, 3, 5, 36, 36, 44); rect(cx - 2, cy - 1, 5, 3, 36, 36, 44) px(cx, cy, 120, 124, 136) local a = ph % 4 px(cx + (a == 0 and 1 or a == 2 and -1 or 0), cy + (a == 1 and 1 or a == 3 and -1 or 0), 200, 204, 214) end local function draw_truck(x, ts) local xi = floor(x + 0.5) blend_rect(xi + 1, 63, 23, 1, 0, 0, 0, 0.3) blit_idx(TRUCK, TPAL, xi, TRUCK_Y, 24, 9, 24, 9, true) local ph = floor(x * 0.8) wheel(xi + 4, TRUCK_Y + 10, ph); wheel(xi + 18, TRUCK_Y + 10, ph) local n = (ts >= T_LEAVE) and 3 or min(3, floor((ts - T_LOAD - FLIGHT) / LOAD_DT) + 1) for j = 1, n do log(xi + SLOT[j][1], TRUCK_Y + SLOT[j][2]) end end local function burst(ts, t0, life, n, x0, y0, vxs, vyb, cols, seed) local tau = ts - t0 if tau < 0 or tau > life then return end for i = 1, n do local h = hash32(i * 0x9E3779B1 + seed) local vx = (umod(h, 200) - 100) / 100 * vxs local vy = -(vyb * 0.4 + umod(h >> 8, 100) / 100 * vyb) local x, y = x0 + vx * tau, y0 + vy * tau + 35 * tau * tau if y < 52 then local c = cols[umod(h >> 16, #cols) + 1] px(floor(x), floor(y), c[1], c[2], c[3]) end end end local CLOUDS = {} for i = 1, 3 do local h = hash32(i * 0x27D4EB2F) CLOUDS[i] = { y = 3 + (i - 1) * 7 + umod(h, 3), len = 14 + umod(h >> 6, 8), x0 = umod(h >> 10, 80), v = 1.6 + umod(h >> 16, 8) / 10 } end local function cloud_row(xl, xr, y, a, c) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend_rect(il + 1, y, ir - il - 1, 1, c[1], c[2], c[3], a) blend_rect(il, y, 1, 1, c[1], c[2], c[3], a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, c[1], c[2], c[3], a * (xr - ir)) end local function draw_clouds(t) local c = P.cloud for i = 1, 3 do local d = CLOUDS[i] local L = d.len local x = (d.x0 + t * d.v) % (W + 2 * L) - L cloud_row(x + L * 0.25, x + L * 0.75, d.y, 0.85, c) cloud_row(x, x + L, d.y + 1, 0.9, c) cloud_row(x + L * 0.1, x + L * 0.9, d.y + 2, 0.7, c) end end function render() if not BG then build() end blit_rgb(BG, 0, 0, W, H) local t = frame / FPS draw_clouds(t) if P == SEASONS.winter then for i = 1, 16 do local h = hash32(i * 0xC2B2AE35) local y = (umod(h, 50) + t * (4 + umod(h >> 8, 5))) % 52 local x = umod(h >> 12, W) + 2 * sin(t * 0.8 + i) blend_rect(floor(x), floor(y), 1, 1, 255, 255, 255, 0.9) end end local ts = t * K_SPEED[cfg.speed or 3] % LOOP if ts < T_FALL then if ts < T_WALK - 0.2 then grow(ts) else local hits = 0 if ts >= T_HIT then hits = min(3, floor((ts - T_HIT - 0.65) / HIT_DT) + 1) if hits < 0 then hits = 0 end end tree(0, hits) end else local tau = ts - T_FALL local th if tau < FALL_DT then th = FALL_DEG * (tau / FALL_DT) ^ 2.2 elseif tau < FALL_DT + B1_DT then local u = (tau - FALL_DT) / B1_DT; th = FALL_DEG - 8 * 4 * u * (1 - u) elseif tau < FALL_DT + B1_DT + B2_DT then local u = (tau - FALL_DT - B1_DT) / B2_DT; th = FALL_DEG - 2.5 * 4 * u * (1 - u) else th = FALL_DEG end if ts < T_CUT then tree(th * 0.0174533, 3) else stump(true, ts > LOOP - 1 and (LOOP - ts) or nil) local tc = ts - T_CUT for j = 1, 3 do local x, y = LOGX[j], 51 local launched = ts - (T_LOAD + (j - 1) * LOAD_DT) if launched < 0 then y = y - LOGH[j] * max(0, 1 - tc / 0.3) ^ 2 log(x, floor(y + 0.5)) elseif launched < FLIGHT then local u = launched / FLIGHT local tx = X_STOP + SLOT[j][1] local ty = TRUCK_Y + SLOT[j][2] local lx = x + (tx - x) * u local ly = y + (ty - y) * u - 4 * 11 * u * (1 - u) log(floor(lx + 0.5), floor(ly + 0.5)) end end end end if ts < T_FALL - 0.1 and ts >= T_WALK - 0.2 and ts < T_HIT then stump(false) end local leafs = { P.leaf[2], P.leaf[3], P.leaf[4] } local chips = { CUT, BARK_L, BARK_M } for n = 1, 3 do burst(ts, T_HIT + (n - 1) * HIT_DT + 0.65, 0.5, 5, 20, 48, 26, 18, chips, n) end burst(ts, T_FALL + FALL_DT, 0.7, 10, 44, 51, 22, 22, leafs, 11) burst(ts, T_FALL + FALL_DT + B1_DT, 0.5, 6, 44, 51, 16, 14, leafs, 12) burst(ts, T_CHOP + 0.55, 0.45, 5, 24, 49, 20, 16, chips, 21) burst(ts, T_CUT, 0.8, 14, 40, 47, 24, 22, leafs, 22) burst(ts, T_CUT, 0.5, 5, 24, 49, 20, 16, chips, 23) local L, pose, dir = man_state(ts) if L then draw_man(floor(L + 0.5), pose, dir, L / 2) end local tx = truck_x(ts) if tx then draw_truck(tx, ts) end end