info = { type = "image", title = "Meadow pan", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Camera pan over hills, trees, fence and sheep", api = 33, } local ltr = ({ sk = { ["Speed"] = "Rýchlosť", ["Sheep"] = "Ovce", ["Fence"] = "Plot" }, cs = { ["Speed"] = "Rychlost", ["Sheep"] = "Ovce", ["Fence"] = "Plot" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "speed", label = LT("Speed"), type = "int", min = 1, max = 5, default = 3 }, { key = "sheep", label = LT("Sheep"), type = "int", min = 0, max = 3, default = 3 }, { key = "fence", label = LT("Fence"), 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, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.2831853 local LP = { 192, 192, 192, 256 } local FACT = { 0.12, 0.3, 0.55, 1.0 } local function profile(L, base, a1, n1, p1, a2, n2, p2) local t = {} for i = 0, L - 1 do t[i + 1] = base + a1 * sin(TAU * n1 * i / L + p1) + a2 * sin(TAU * n2 * i / L + p2) end return t end local FAR = profile(192, 39, 3.0, 3, 1.0, 0.9, 7, 0.5) local MID = profile(192, 45, 3.4, 2, 2.0, 1.2, 5, 0.3) local FIELD = profile(192, 51, 2.6, 2, 0.4, 0.9, 6, 1.7) local NEAR = profile(256, 57, 1.4, 3, 0.8, 0.6, 7, 2.2) local PROF = { FAR, MID, FIELD, NEAR } local COL = { { 118, 182, 186 }, { 88, 172, 100 }, { 164, 206, 76 }, { 64, 152, 60 } } local TOP = { false, { 130, 206, 114 }, { 200, 228, 104 }, { 108, 188, 74 } } local STEP, CYC = {}, {} local function gcd(a, b) while b ~= 0 do a, b = b, a % b end return a end function on_settings() local near = 6 * (cfg.speed or 3) for k = 1, 4 do local step = math.max(1, floor(near * FACT[k] * 256 / FPS + 0.5)) STEP[k] = step CYC[k] = LP[k] * 256 // gcd(step, LP[k] * 256) end end local function scroll(k) return (frame % CYC[k]) * STEP[k] % (LP[k] * 256) / 256 end local function fbox(xl, xr, y, h, r, g, b) local il, ir = floor(xl), floor(xr) if ir <= il then return end if ir - il > 1 then rect(il + 1, y, ir - il - 1, h, r, g, b) end blend_rect(il, y, 1, h, r, g, b, 1 - (xl - il)) blend_rect(ir, y, 1, h, r, g, b, xr - ir) end local function hills(k) local T, L, s, c, top = PROF[k], LP[k], scroll(k), COL[k], TOP[k] local rx, ry for x = 0, W do local u = x + s local i = floor(u) local f = u - i i = i % L local y = x < W and floor(T[i + 1] * (1 - f) + T[(i + 1) % L + 1] * f + 0.5) or -1 if y ~= ry then if ry then rect(rx, ry, x - rx, H - ry, c[1], c[2], c[3]) if top then rect(rx, ry, x - rx, 1, top[1], top[2], top[3]) end end rx, ry = x, y end end end local TREE = { r = { 1.5, 2.5, 3, 3, 2.5, 1.5 }, p = { 0.5, 1.5, 1.5, 2.5, 2.5, 3.5 }, R = { 2, 3.5, 4.5, 5, 5, 4.5, 3.5, 2 }, P = { 0.5, 1.5, 1.5, 2.5, 2.5, 3.5, 3.5, 4.5, 4.5 }, } local TCOL = { r = { 40, 118, 74 }, p = { 30, 100, 80 }, R = { 52, 140, 60 }, P = { 36, 112, 70 } } local function tree(cx, yb, kind) local rows, c = TREE[kind], TCOL[kind] local big = kind == "R" or kind == "P" local th = big and 3 or 2 fbox(cx - 0.5, cx + 0.5 + (big and 1 or 0), yb - th, th, 110, 72, 44) local y = yb - th - #rows for i = 1, #rows do local hw = rows[i] if i <= 2 then fbox(cx - hw, cx + hw, y + i - 1, 1, c[1] + 40, c[2] + 34, c[3] + 20) else fbox(cx - hw, cx + hw, y + i - 1, 1, c[1], c[2], c[3]) end end end local function sample(T, L, wx) return floor(T[wx % L + 1] + 0.5) + 1 end local TM = { { 14, "r" }, { 41, "p" }, { 67, "r" }, { 96, "p" }, { 118, "r" }, { 137, "p" }, { 160, "r" }, { 181, "p" } } local TF = { { 25, "R" }, { 85, "P" }, { 130, "R" }, { 171, "P" } } local function trees(list, T, L, s) for i = 1, #list do local o = list[i] local sx = (o[1] - s + 10) % L - 10 if sx < W + 10 then tree(sx, sample(T, L, o[1]), o[2]) end end end local FENCE = { { 40, 112 }, { 170, 230 } } local SHEEP = { { 78, 1 }, { 150, -1 }, { 214, 1 } } local FLOW, TUFT = {}, {} for i = 1, 14 do local h = hash32(i * 0x9E3779B1 + 11) FLOW[i] = { umod(h, 256), 58 + umod(h >> 8, 4), umod(h >> 16, 3) + 1 } end for i = 1, 36 do local h = hash32(i * 0x85EBCA6B + 4) TUFT[i] = { umod(h, 256), 58 + umod(h >> 8, 4), umod(h >> 16, 2) } end local FC = { { 255, 255, 246 }, { 255, 224, 80 }, { 244, 150, 190 } } local TC = { { 38, 112, 50 }, { 136, 208, 92 } } local function dot(x, y, c, a) local ix = floor(x) local f = x - ix blend_rect(ix, y, 1, 1, c[1], c[2], c[3], a * (1 - f)) blend_rect(ix + 1, y, 1, 1, c[1], c[2], c[3], a * f) end local function sheep(cx, dir, ph, t) local graze = (t * 0.25 + ph) % 1 < 0.35 local rows = { 3, 5, 6, 6, 5 } for i = 1, 5 do local hw = rows[i] if i == 5 then fbox(cx - hw, cx + hw, 52 + i, 1, 214, 214, 206) else fbox(cx - hw, cx + hw, 52 + i, 1, 248, 248, 240) end end fbox(cx - 4, cx - 3, 58, 2, 58, 50, 50) fbox(cx + 3, cx + 4, 58, 2, 58, 50, 50) local hx = cx + dir * 7 local hy = graze and 56 or 54 fbox(hx - 1.5, hx + 1.5, hy, 3, 62, 54, 52) px(floor(hx + dir * 2 + 0.5) - (dir > 0 and 0 or 1), hy + 1, 62, 54, 52) end local function near_layer(s, t) local L = 256 if cfg.fence ~= false then for k = 1, #FENCE do local a, b = FENCE[k][1], FENCE[k][2] local sa = (a - s + 20) % L - 20 local len = b - a if sa < W and sa + len > 0 then fbox(sa, sa + len, 55, 1, 232, 178, 114) fbox(sa, sa + len, 58, 1, 232, 178, 114) for x = 0, len, 12 do local px0 = sa + x if px0 > -2 and px0 < W then fbox(px0, px0 + 2, 53, 7, 206, 150, 92) end end end end end for i = 1, cfg.sheep or 3 do local o = SHEEP[i] local sx = (o[1] - s + 12) % L - 12 if sx < W + 12 then sheep(sx, o[2], i * 0.37, t) end end for i = 1, #TUFT do local o = TUFT[i] local sx = (o[1] - s + 2) % L - 2 if sx < W then dot(sx, o[2], TC[o[3] + 1], 0.8) end end for i = 1, #FLOW do local o = FLOW[i] local sx = (o[1] - s + 2) % L - 2 if sx < W then dot(sx, o[2], FC[o[3]], 1) end end end local BG = nil local function build_bg() for y = 0, 49 do local t = y / 49 rect(0, y, W, 1, floor(62 + 98 * t + 0.5), floor(164 + 56 * t + 0.5), floor(236 + 10 * t + 0.5)) end for y = 0, 16 do for x = 0, 16 do local d = sqrt((x - 2) ^ 2 + (y - 2) ^ 2) if d <= 8 then px(x, y, 255, 228, 64) elseif d <= 9.5 then px(x, y, 255, 244, 150) elseif d <= 11.5 then blend_rect(x, y, 1, 1, 255, 248, 190, 0.28) elseif d <= 14 then blend_rect(x, y, 1, 1, 255, 248, 190, 0.13) end end end BG = snapshot(0, 0, W, 50) end function render() if not BG then build_bg() end blit_rgb(BG, 0, 0, W, 50) local t = frame / FPS local alt = (frame // 14) % 2 for k = 0, 5 do local a = k * 0.3142 local n = (k + alt) % 2 == 0 and 3 or 2 for r = 16, 15 + n do px(floor(2 + cos(a) * r + 0.5), floor(2 + sin(a) * r + 0.5), 255, 236, 110) end end hills(1) hills(2) trees(TM, MID, 192, scroll(2)) hills(3) trees(TF, FIELD, 192, scroll(3)) hills(4) rect(0, 61, W, 3, 150, 100, 60) rect(0, 61, W, 1, 112, 72, 44) near_layer(scroll(4), t) end