info = { type = "image", title = "Archery range", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "An archer shoots at a straw target in the wind", api = 44, } local ltr = ({ sk = { ["Wind"] = "Vietor", ["Calm"] = "Bezvetrie", ["Breeze"] = "Vánok", ["Gale"] = "Víchrica", ["Arrows"] = "Šípy" }, cs = { ["Wind"] = "Vítr", ["Calm"] = "Bezvětří", ["Breeze"] = "Vánek", ["Gale"] = "Vichřice", ["Arrows"] = "Šípy" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "wind", label = LT("Wind"), type = "int", default = "breeze", opts = { keys = { "calm", "breeze", "gale" }, LT("Calm"), LT("Breeze"), LT("Gale") } }, { key = "n", label = LT("Arrows"), type = "int", min = 3, max = 6, default = 4 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, cos, sqrt, max, min, abs = math.floor, math.sin, math.cos, math.sqrt, math.max, math.min, math.abs local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local AX, AY = 3, 55 local TX, TY = 51, 36 local FX, FY = 30, 12 local GROUND = 40 local KEY = { 255, 0, 255 } local SHOT, FLY, HIT = 70, 14, 30 local TEXT = { font = "3x5" } 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 SKY0, SKY1 = { 98, 170, 238 }, { 190, 228, 252 } local STRAW, RED, WHITE, BLUE, GOLD = { 218, 174, 96 }, { 210, 58, 50 }, { 246, 240, 226 }, { 60, 124, 208 }, { 252, 208, 64 } local WOOD, WOOD_D = { 126, 84, 48 }, { 92, 60, 36 } local SKIN, SHAFT, HEAD, FLETCH = { 240, 196, 160 }, { 96, 62, 38 }, { 150, 154, 168 }, { 250, 250, 250 } local RINGS = { { 10, STRAW }, { 8, RED }, { 6.2, WHITE }, { 4.4, BLUE }, { 2.2, GOLD } } local ZONES = { 2.2, 4.4, 6.2, 8, 10 } local function vn(i, seed) return (hash32(i * 0x9E3779B1 + seed) & 0x7FFF) / 32767 end local function noise(x, seed) local i = floor(x) local f = x - i f = f * f * (3 - 2 * f) return vn(i, seed) * (1 - f) + vn(i + 1, seed) * f end local BG, FACE, STAGE = nil, nil, 1 local function scene() for y = 0, GROUND - 1 do rect(0, y, W, 1, mix(SKY0, SKY1, y / GROUND)) end for x = 0, W - 1 do local hy = 30 + floor(noise(x / 9, 3) * 6 + 0.5) rect(x, hy, 1, GROUND - hy, 156, 200, 212) local ty = 35 + floor(noise(x / 2.4, 8) * 3 + 0.5) - (x % 3 == 0 and 1 or 0) rect(x, ty, 1, GROUND - ty, 64, 122, 104) if vn(x, 5) > 0.55 then px(x, ty, 92, 150, 118) end end for y = GROUND, H - 1 do local band = ((y - GROUND) // 3) % 2 == 0 and 5 or 0 local r, g, b = mix({ 120, 192, 96 }, { 60, 138, 58 }, (y - GROUND) / 23) rect(0, y, W, 1, r - band // 2, g + band, b - band // 2) end for i = 1, 36 do local h = hash32(i * 0x85EBCA6B) local y = GROUND + 1 + umod(h >> 8, 22) px(umod(h, W), y, 52, 124, 52) px(umod(h, W) + 1, y - 1, 140, 208, 112) end blend_rect(TX - 12, 52, 26, 3, 30, 80, 40, 0.3) for k = 0, 9 do px(TX - 4 - k // 2, TY + 8 + k, c3(WOOD)); px(TX - 3 - k // 2, TY + 8 + k, c3(WOOD_D)) px(TX + 4 + k // 2, TY + 8 + k, c3(WOOD)); px(TX + 5 + k // 2, TY + 8 + k, c3(WOOD_D)) end rect(TX, TY + 9, 2, 9, c3(WOOD_D)) rect(FX, FY - 1, 1, 33, c3(WOOD)) blend_rect(FX - 2, 44, 6, 2, 30, 80, 40, 0.3) end local function face() rect(0, 0, 21, 21, c3(KEY)) for dy = 0, 20 do local y = dy - 9.5 for k = 1, #RINGS do local r = RINGS[k][1] local q = r * r - y * y if q > 0 then local hw = sqrt(q) local x0 = floor(10.5 - hw + 0.5) rect(x0, dy, floor(10.5 + hw + 0.5) - x0, 1, c3(RINGS[k][2])) end end end for i = 1, 14 do local a = i * 0.9 local x, y = floor(10.5 + 9.2 * cos(a)), floor(10.5 + 9.2 * sin(a)) px(x, y, 178, 134, 66) end for i = 1, 8 do px(floor(10.5 + 9 * cos(i * 0.785 + 0.3)), floor(10.5 + 9 * sin(i * 0.785 + 0.3)), 240, 208, 130) end FACE = snapshot(0, 0, 21, 21) end local function build() if STAGE == 1 then scene(); BG = snapshot(0, 0, W, H) elseif STAGE == 2 then face(); blit_rgb(BG, 0, 0, W, H) end STAGE = STAGE + 1 end function on_settings() STAGE, BG = 1, nil end local function line(x0, y0, x1, y1, c) local n = floor(max(abs(x1 - x0), abs(y1 - y0)) + 0.5) for k = 0, n do local t = n == 0 and 0 or k / n px(floor(x0 + (x1 - x0) * t + 0.5), floor(y0 + (y1 - y0) * t + 0.5), c3(c)) end end local function arrow(hx, hy, dx, dy, len) for k = 0, len do local c = k == 0 and HEAD or (k >= len - 1 and FLETCH or SHAFT) px(floor(hx - dx * k + 0.5), floor(hy - dy * k + 0.5), c3(c)) end end local BOW = {} for r = -8, 8 do BOW[r] = AX + 14 - floor(r * r / 20 + 0.5) end local HAND = { [1] = 11, [2] = 8, [3] = 5, [4] = 3 } local function archer(pose) local x, y = AX, AY rect(x + 3, y - 10, 2, 8, 88, 66, 48); rect(x + 7, y - 10, 2, 8, 88, 66, 48) rect(x + 3, y - 2, 3, 2, 46, 34, 28); rect(x + 7, y - 2, 4, 2, 46, 34, 28) rect(x + 3, y - 18, 6, 8, 62, 124, 72) rect(x + 3, y - 12, 6, 1, 46, 34, 28) rect(x + 4, y - 24, 5, 5, c3(SKIN)) px(x + 7, y - 22, 40, 36, 52) rect(x + 4, y - 26, 5, 2, 196, 56, 48) px(x + 3, y - 27, 244, 236, 200); px(x + 2, y - 26, 244, 236, 200) rect(x + 8, y - 17, 6, 2, c3(SKIN)) for r = -8, 8 do px(BOW[r], y - 16 + r, c3(WOOD)) if r >= -1 and r <= 1 then px(BOW[r] - 1, y - 16 + r, c3(WOOD)) end end local hx = HAND[pose] local sx = x + 11 if hx then local hx2 = x + hx line(sx, y - 24, hx2, y - 16, { 240, 236, 222 }); line(sx, y - 8, hx2, y - 16, { 240, 236, 222 }) line(x + 5, y - 16, hx2, y - 16, SKIN) if pose <= 3 then for k = hx2, x + 17 do px(k, y - 16, c3(k >= x + 17 and HEAD or (k <= hx2 + 1 and FLETCH or SHAFT))) end end else line(sx, y - 24, sx, y - 8, { 240, 236, 222 }) rect(x + 4, y - 16, 2, 6, c3(SKIN)) end if pose == 4 then line(sx, y - 24, sx, y - 8, { 240, 236, 222 }) end end local function helper(x, y, dir, step, reach) local function R(ox, oy, w, h, c) rect(dir > 0 and x + ox or x - ox - w + 1, y + oy, w, h, c3(c)) end local s = step % 2 R(0, -8 + s, 2, 8 - s, { 70, 84, 130 }); R(3, -9 + (1 - s), 2, 9 - (1 - s), { 70, 84, 130 }) R(0, -1, 3, 1, { 46, 34, 28 }); R(3, -1, 3, 1, { 46, 34, 28 }) R(0, -17, 6, 9, { 200, 112, 58 }) R(0, -12, 6, 1, { 46, 34, 28 }) R(1, -22, 4, 5, SKIN) R(4, -20, 1, 1, { 40, 36, 52 }) R(-1, -24, 8, 2, { 232, 198, 112 }); R(1, -26, 4, 2, { 232, 198, 112 }) if reach then local sx, sy = x + dir * 3, y - 15 local dx, dy = reach[1] - sx, reach[2] - sy local d = sqrt(dx * dx + dy * dy) if d > 11 then dx, dy = dx * 11 / d, dy * 11 / d end line(sx, sy, sx + dx, sy + dy, SKIN) else R(2, -16, 2, 6, SKIN) end end local function aim(rnd, k, n, wind) local h = hash32((rnd * 16 + k + 1) * 0x9E3779B1) local missK = umod(hash32(rnd * 0x85EBCA6B + 7), 2) == 0 and umod(hash32(rnd * 0xC2B2AE35 + 3), n) or -1 local ang, rr = umod(h, 628) / 100, (umod(h >> 10, 100) / 100) ^ 0.8 * 7 local ox, oy = rr * cos(ang), rr * sin(ang) oy = oy + wind * (umod(h >> 20, 2) * 2 - 1) * (1 + umod(h >> 22, 3) * 0.9) local r = sqrt(ox * ox + oy * oy) if r > 9.5 then ox, oy, r = ox * 9.5 / r, oy * 9.5 / r, 9.5 end if k == missK then return { miss = true, ix = 36 + umod(h >> 12, 6), iy = 49 + umod(h >> 16, 4), score = 0 } end local sc = 2 for z = 1, #ZONES do if r < ZONES[z] then sc = 12 - 2 * z break end end return { ix = TX + floor(ox + 0.5), iy = TY + floor(oy + 0.5), score = sc } end local XS, YS = AX + 16, AY - 16 local function flight(a, u) local hmax = a.miss and 3 or 5 local x = XS + (a.ix - XS) * u local y = YS + (a.iy - YS) * u - 4 * hmax * u * (1 - u) local dx, dy = a.ix - XS, (a.iy - YS) - 4 * hmax * (1 - 2 * u) local d = sqrt(dx * dx + dy * dy) return x, y, dx / d, dy / d end local CLOUDS = { { 8, 16, 1.0, 6 }, { 22, 12, 0.7, 40 } } function render() if STAGE <= 2 then build(); gfx.ready(false) return end local wind = ({ calm = 0, breeze = 1, gale = 2 })[cfg.wind] or 1 local n = cfg.n or 4 local PULL = n * SHOT local ROUND = PULL + 146 + 12 * n + 12 local f, rnd = frame % ROUND, frame // ROUND local t = frame / 30 blit_rgb(BG, 0, 0, W, H) for i = 1, #CLOUDS do local c = CLOUDS[i] local len = c[2] local x = (c[4] + t * c[3] * (1 + wind * 1.5)) % (W + len) - len local il = floor(x) local fr = x - il blend_rect(il + 2, c[1], len - 4, 1, 255, 255, 255, 0.55) blend_rect(il + 1, c[1] + 1, len - 1, 2, 255, 255, 255, 0.5) blend_rect(il, c[1] + 1, 1, 2, 255, 255, 255, 0.5 * (1 - fr)) blend_rect(il + len, c[1] + 1, 1, 2, 255, 255, 255, 0.5 * fr) end for c = 0, 6 do local droop = c * (1 - wind * 0.5) * 0.7 local wave = sin(t * 5 * (0.4 + wind * 0.5) - c * 0.8) * (0.3 + wind * 0.5) * (c / 3) local y0 = FY + floor(droop + wave + 0.5) rect(FX + 1 + c, y0, 1, 4, c3(RED)) if c % 2 == 1 then rect(FX + 1 + c, y0 + 1, 1, 2, c3(WHITE)) end end local shots, total = {}, 0 local live = min(n - 1, f // SHOT) local hf0 = f - PULL local pulled = hf0 >= 78 and min(n, (hf0 - 78) // 12 + 1) or 0 local off, hitk = 0, nil for k = 0, n - 1 do local a = aim(rnd, k, n, wind) a.s = f - k * SHOT shots[k + 1] = a end for k = 1, n do local a, s = shots[k], shots[k].s if not a.miss and s >= HIT + FLY and s < HIT + FLY + 14 then local q = s - HIT - FLY off = floor(1.6 * sin(q * 1.7) * (1 - q / 14) + 0.5) end if s >= HIT + FLY then total = total + a.score end end blit_rgb(FACE, TX - 10 + off, TY - 10, 21, 21, 255, 0, 255) for k = 1, n do local a, s = shots[k], shots[k].s if s >= HIT + FLY and k > pulled then local ox = a.miss and 0 or off local _, _, dx, dy = flight(a, 1) arrow(a.ix + ox + dx * 2, a.iy + dy * 2, dx, dy, 8) end end local sh = f < PULL and f % SHOT or 99 local pose = 0 if sh >= 6 and sh < 12 then pose = 1 elseif sh >= 12 and sh < 18 then pose = 2 elseif sh >= 18 and sh < HIT then pose = 3 elseif sh >= HIT and sh < HIT + 8 then pose = 4 end archer(pose) if f < PULL then local a = shots[live + 1] local u = (a.s - HIT) / FLY if u >= 0 and u < 1 then local x, y, dx, dy = flight(a, u) arrow(x, y, dx, dy, 8) end end if f >= PULL then local hf = f - PULL if hf < 64 then helper(floor(70 - hf * 0.5), 56, -1, hf // 6, nil) elseif hf < 72 + 12 * n + 10 then local j = (hf - 72) // 12 local reach if j >= 0 and j < n then local a = shots[j + 1] local _, _, dx, dy = flight(a, 1) reach = { a.ix - dx * 6, a.iy - dy * 6 } end helper(38, 56, 1, 0, reach) else helper(floor(38 + (hf - 72 - 12 * n - 10) * 0.5), 56, 1, hf // 6, nil) end end gfx.text(2, 2, "SCORE " .. total, 28, 56, 112, TEXT) for k = 1, n do local a, s = shots[k], shots[k].s if s >= HIT + FLY and s < HIT + FLY + 24 then gfx.text(a.miss and 34 or TX - 4, (a.miss and 44 or TY - 14) - (s - HIT - FLY) // 3, "+" .. a.score, 255, 236, 120, TEXT) end end end