info = { type = "image", title = "Moon oak", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Bare oak against a huge moon, drifting mist", api = 44, } local tr = ({ sk = { ["Blinking stars"] = "Blikajúce hviezdy", ["Mist"] = "Hmla", ["Night flyer"] = "Nočný letec", ["None"] = "Žiadny", ["Bat"] = "Netopier", ["Owl"] = "Sova", ["Both"] = "Oba" }, cs = { ["Blinking stars"] = "Blikající hvězdy", ["Mist"] = "Mlha", ["Night flyer"] = "Noční letec", ["None"] = "Žádný", ["Bat"] = "Netopýr", ["Owl"] = "Sova", ["Both"] = "Oba" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "stars", label = T("Blinking stars"), type = "int", min = 0, max = 20, default = 12 }, { key = "mist", label = T("Mist"), type = "int", min = 0, max = 4, default = 3 }, { key = "flyer", label = T("Night flyer"), type = "int", default = 3, opts = { T("None"), T("Bat"), T("Owl"), T("Both") } }, } local rect, blend, blend_rect = gfx.rect, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, abs, max = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local MX, MY, MR = 36, 26, 24 local INK = { 3, 4, 9 } local function C(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function disc(cx, cy, r, cr, cg, cb, a) for y = -floor(r), floor(r) do local hw = floor(sqrt(r * r - y * y) + 0.5) if a then blend_rect(cx - hw, cy + y, 2 * hw + 1, 1, cr, cg, cb, a) else rect(cx - hw, cy + y, 2 * hw + 1, 1, cr, cg, cb) end end end local function span(xl, xr, y, r, g, b, a) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend(il, y, r, g, b, a * (1 - (xl - il))) if ir - il > 1 then blend_rect(il + 1, y, ir - il - 1, 1, r, g, b, a) end blend(ir, y, r, g, b, a * (xr - ir)) end local HILL = {} for x = 0, W - 1 do HILL[x] = floor(52 + 2 * sin(x * 0.13) + 1.5 * sin(x * 0.37 + 1) + 0.5) end local function st_sky() for y = 0, H - 1 do rect(0, y, W, 1, C(16 + y * 0.06, 18 + y * 0.1, 72 + y * 0.45)) end for i = 1, 10 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), umod(h >> 8, 44) local dx, dy = x - MX, y - MY if dx * dx + dy * dy > (MR + 10) * (MR + 10) then rect(x, y, 1, 1, 96, 108, 176) end end end local function halo(r0, r1) return function() for r = r0, r1, -1 do disc(MX, MY, r, 88, 100, 176, 0.1) end end end local function st_moon() for k = 0, 8 do local t = k / 8 disc(MX - 0.8 * k, MY - 0.8 * k, MR - 1.9 * k * 0.9, C(204 + 46 * t, 196 + 46 * t, 152 + 48 * t)) end end local function st_craters() disc(MX - 9, MY - 3, 6, 188, 180, 142, 0.35); disc(MX + 6, MY + 10, 7, 188, 180, 142, 0.35) disc(MX + 10, MY - 9, 5, 188, 180, 142, 0.3) local CR = { { -12, -10, 3 }, { 2, -14, 2 }, { -3, 5, 4 }, { 13, 2, 3 }, { -15, 8, 2 }, { 6, 16, 3 }, { 16, -4, 2 } } for _, c in ipairs(CR) do disc(MX + c[1], MY + c[2], c[3], 190, 182, 142) disc(MX + c[1] - 1, MY + c[2] - 1, c[3] - 1, 208, 200, 158) blend_rect(MX + c[1] + c[3] - 1, MY + c[2] + 1, 1, c[3] - 1, 255, 250, 214, 0.7) end end local function st_ground() for x = 0, W - 1 do local top = HILL[x] rect(x, top, 1, H - top, 6, 10, 15) local h = hash32(x * 0x85EBCA6B) if umod(h, 3) == 0 then rect(x, top - 1 - umod(h >> 8, 3), 1, 2, 6, 10, 15) end end for i = 1, 46 do local h = hash32(i * 0xC2B2AE35) local x = umod(h, W) local y = HILL[x] + 3 + umod(h >> 8, 8) if y < H then rect(x, y, 1 + umod(h >> 16, 2), 1, 22, 48, 52) end end end local DMIN, DMAX = 0, 0 local THICK = { 5, 3, 2, 2, 1, 1, 1, 1 } local function seg(x, y, x2, y2, t) local n = floor(max(abs(x2 - x), abs(y2 - y)) * 1.5) + 1 local o = (t - 1) / 2 for i = 0, n do local u = i / n rect(floor(x + (x2 - x) * u - o + 0.5), floor(y + (y2 - y) * u - o + 0.5), t, t, INK[1], INK[2], INK[3]) end end local function grow(x, y, a, len, d, id) local x2, y2 = x + sin(a) * len, y - cos(a) * len if d >= DMIN then seg(x, y, x2, y2, THICK[d + 1]) end if d + 1 >= DMAX then return end local n = (d == 0 or (d < 3 and umod(hash32(id * 0x9E3779B1), 100) < 30)) and 3 or 2 for k = 1, n do local cid = id * 4 + k local r = umod(hash32(cid * 0x85EBCA6B), 1000) / 1000 local side = (k == 1) and -1 or (k == 2 and 1 or 0) grow(x2, y2, a + side * (0.42 + 0.5 * r) + (r - 0.5) * 0.3, len * (0.64 + 0.16 * r), d + 1, cid) end end local function tree(d0, d1) DMIN, DMAX = d0, d1 grow(31, 61, 0.05, 17, 0, 3) end local STAGES = { st_sky, halo(MR + 9, MR + 7), halo(MR + 6, MR + 4), halo(MR + 3, MR + 1), st_moon, st_craters, st_ground, function() tree(0, 3) end, function() tree(3, 5) end, function() tree(5, 6) end, function() tree(6, 7) end, function() tree(7, 8) end, } local stage, BG = 0, nil local SP, SOFF, SLIFE = {}, {}, 40 local SC, SX, SY = {}, {}, {} for i = 1, 20 do local h = hash32(i * 0xA511E9B3) SP[i], SOFF[i] = 60 + umod(h, 90), umod(h >> 8, 400) SC[i] = -1 end local function star_pos(i, c) for k = 0, 7 do local h = hash32((i * 977 + c * 131 + k) * 0x9E3779B1) local x, y = umod(h, W), umod(h >> 8, 46) local dx, dy = x - MX, y - MY if dx * dx + dy * dy > (MR + 3) * (MR + 3) then return x, y end end return 1, 1 end local function draw_stars(n) for i = 1, n do local ph = frame + SOFF[i] local c = ph // SP[i] local p = ph % SP[i] if p < SLIFE then if SC[i] ~= c then SC[i] = c; SX[i], SY[i] = star_pos(i, c) end local u = p / SLIFE local a = u < 0.5 and u * 2 or (1 - u) * 2 local x, y = SX[i], SY[i] blend(x, y, 214, 224, 255, 0.25 + 0.7 * a) if a > 0.7 then blend(x - 1, y, 200, 210, 255, 0.4); blend(x + 1, y, 200, 210, 255, 0.4) blend(x, y - 1, 200, 210, 255, 0.4); blend(x, y + 1, 200, 210, 255, 0.4) end end end end local WY, WL, WV, WX = { 53, 56, 50, 58 }, { 30, 36, 26, 34 }, { 3.0, 2.2, 3.8, 2.6 }, { 6, 30, 50, 20 } local function draw_mist(n, t) for i = 1, n do local len = WL[i] local xl = (WX[i] + WV[i] * t) % (W + len) - len local y = WY[i] span(xl + 3, xl + len - 3, y, 126, 146, 190, 0.12) span(xl, xl + len, y + 1, 126, 146, 190, 0.22) span(xl + 2, xl + len - 2, y + 2, 126, 146, 190, 0.14) end end local function runs(rows) local out = {} for dy, row in ipairs(rows) do local x = 1 while x <= #row do local s = row:find("X", x, true) if not s then break end local e = row:find(".", s, true) or (#row + 1) out[#out + 1] = { s - 1, dy - 1, e - s } x = e end end return out end local BAT = { runs { "X.X.....X.X", "XXX.X.X.XXX", "XXXXXXXXXXX", ".XXX.X.XXX.", "..X.....X.." }, runs { "....X.X....", "..XXXXXXX..", ".XXXXXXXXX.", "XXX.XXX.XXX", "X.........X" }, } local OWL = { runs { ".....X..X.....", ".....XXXX.....", "XX..XXXXXX..XX", "XXXXXXXXXXXXXX", ".XXXXXXXXXXXX.", "..XXXXXXXXXX..", ".....XXXX....." }, runs { ".....X..X.....", ".....XXXX.....", "....XXXXXX....", "XX.XXXXXXXX.XX", "XXXXXXXXXXXXXX", ".XXXXXXXXXXXX.", ".....XXXX....." }, } local function sprite(S, x, y) for i = 1, #S do local r = S[i]; rect(x + r[1], y + r[2], r[3], 1, INK[1], INK[2], INK[3]) end end local EV, EVLEN = 330, 96 local function draw_flyer(mode) local c, p = (frame + 120) // EV, (frame + 120) % EV if p >= EVLEN then return end local owl = mode == 2 or (mode == 3 and c % 2 == 1) local x = (c % 2 == 0 and p or EVLEN - p) - 14 local y = 15 + umod(hash32(c * 0x9E3779B1), 14) + floor(2 * sin(p * 0.09) + 0.5) if owl then sprite(OWL[(p // 15) % 2 + 1], x, y) rect(x + 5, y + 2, 1, 1, 240, 214, 110); rect(x + 8, y + 2, 1, 1, 240, 214, 110) else sprite(BAT[(p // 3) % 2 + 1], x, y) end end function render() if stage < #STAGES then if BG then gfx.blit_rgb(BG, 0, 0, W, H) else gfx.clear(16, 20, 80) end stage = stage + 1 STAGES[stage]() BG = gfx.snapshot(0, 0, W, H) gfx.ready(false) return end gfx.blit_rgb(BG, 0, 0, W, H) draw_stars(cfg.stars or 12) draw_mist(cfg.mist or 3, frame / FPS) if (cfg.flyer or 3) > 0 then draw_flyer(cfg.flyer or 3) end end