info = { type = "image", title = "Misty lake castle", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Castle islet and stone bridge in morning mist", api = 44, } local tr = ({ sk = { ["Mist"] = "Hmla", ["Ripple"] = "Vlnky" }, cs = { ["Mist"] = "Mlha", ["Ripple"] = "Vlnky" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "mist", label = T("Mist"), type = "int", min = 0, max = 5, default = 3 }, { key = "ripple", label = T("Ripple"), type = "int", min = 1, max = 5, default = 3 }, } 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, exp, min, max = math.floor, math.sin, math.sqrt, math.exp, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local HZ, WL = 34, 43 local KEY = { 255, 0, 255 } local HAZE = { 214, 224, 240 } local LOW = 10 local function mix(a, b, k) return a + (b - a) * k end local function col(c, k) k = k or 0.14 return floor(mix(c[1], HAZE[1], k) + 0.5), floor(mix(c[2], HAZE[2], k) + 0.5), floor(mix(c[3], HAZE[3], k) + 0.5) end 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 R1, R2 = {}, {} for x = 0, W - 1 do R1[x] = 21 + 4 * noise(x / 7, 11) + 1.2 * noise(x / 2.1, 5) R2[x] = 25 + 4 * noise(x / 5.5, 23) + 1.5 * noise(x / 1.6, 3) - 6 * exp(-((x - 50) / 9) ^ 2) end local FAR, NEAR, CROWN = { 150, 180, 198 }, { 84, 128, 112 }, { 122, 164, 130 } local L_, M_, D_ = { 200, 186, 166 }, { 168, 154, 138 }, { 124, 114, 110 } local SLATE, SLATE_L = { 90, 102, 128 }, { 124, 138, 164 } local WIN, LIT = { 56, 50, 60 }, { 252, 196, 98 } local TREE, PINE = { 52, 96, 66 }, { 50, 88, 80 } local TOP, TOPF, OBJ, OBJLOW, WATER, SKY = nil, nil, nil, nil, nil, nil local ORR, RS, WROW = {}, {}, {} local function cover() if TOP then blit_rgb(TOP, 0, 0, W, HZ) end rect(0, HZ, W, H - HZ, 196, 212, 238) if OBJ then blit_rgb(OBJ, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) end end local function s_sky() for y = 0, HZ - 1 do local t = (y / (HZ - 1)) ^ 1.2 rect(0, y, W, 1, floor(mix(118, 226, t) + 0.5), floor(mix(152, 234, t) + 0.5), floor(mix(228, 248, t) + 0.5)) end SKY = snapshot(0, 0, W, HZ) end local function ridge(Y, base, h0, h1, nb, crown) for x = 0, W - 1 do local y0 = floor(Y[x] + 0.5) local h = HZ - y0 local n = min(nb, h) for k = 0, n - 1 do local ya, yb = y0 + h * k // n, y0 + h * (k + 1) // n rect(x, ya, 1, yb - ya, col(base, h0 + (h1 - h0) * (k + 0.5) / n)) end if crown and vn(x, 71) > 0.5 then px(x, y0, col(crown, 0.3)) end end end local function s_ridges() blit_rgb(SKY, 0, 0, W, HZ) ridge(R1, FAR, 0.35, 0.75, 4) ridge(R2, NEAR, 0.2, 0.7, 6, CROWN) TOP = snapshot(0, 0, W, HZ) end local function ell(cx, cy, rx, ry, c, k) for dy = -ry, ry do local dx = floor(rx * sqrt(1 - (dy / ry) ^ 2) + 0.5) rect(cx - dx, cy + dy, 2 * dx + 1, 1, col(c, k)) end end local function pine(cx, base, h) for r = 0, h - 1 do local hw = floor(r * 4 / h) + (r % 4 == 3 and 1 or 0) rect(cx - hw, base - h + r, 2 * hw + 1, 1, col(PINE, 0.3)) end end local function wall(x, y, w, h) rect(x, y, w, h, col(M_)) for yy = y + 4, y + h - 2, 4 do rect(x, yy, w, 1, col(D_, 0.2)) end rect(x, y, 1, h, col(L_)); rect(x + w - 1, y, 1, h, col(D_)) end local function roof(cx, y, hws) for i = 1, #hws do rect(cx - hws[i], y + i - 1, 2 * hws[i] + 1, 1, col(SLATE)) px(cx - hws[i], y + i - 1, col(SLATE_L)) end end local ISL = { { 6, 34 }, { 4, 36 }, { 3, 37 }, { 3, 37 }, { 4, 36 }, { 5, 35 }, { 7, 33 }, { 10, 30 } } local ISL_C = { { 112, 144, 78 }, { 90, 122, 66 }, { 76, 106, 60 }, { 120, 108, 96 }, { 120, 108, 96 }, { 98, 88, 80 }, { 80, 72, 68 }, { 66, 60, 60 } } local function s_objects() gfx.clear(KEY[1], KEY[2], KEY[3]) rect(48, 32, 16, 4, col(PINE, 0.4)) for _, p in ipairs({ { 49, 35, 9 }, { 52, 35, 13 }, { 57, 35, 17 }, { 60, 35, 11 }, { 62, 35, 15 } }) do pine(p[1], p[2], p[3]) end ell(5, 29, 4, 6, TREE, 0.2); ell(35, 31, 3, 5, TREE, 0.22) px(4, 25, col({ 84, 128, 84 })); px(5, 24, col({ 84, 128, 84 })); px(34, 28, col({ 84, 128, 84 })) for i, s in ipairs(ISL) do rect(s[1], 34 + i, s[2] - s[1] + 1, 1, col(ISL_C[i], 0.1)) end for i = 1, 12 do local x = 5 + umod(hash32(i * 0x85EBCA6B), 30) px(x, 35 + umod(hash32(i * 0xC2B2AE35), 2), col({ 150, 176, 100 }, 0.1)) px(x, 38 + umod(hash32(i * 0x27D4EB2F), 4), col({ 150, 136, 120 }, 0.1)) end wall(11, 24, 17, 13) for x = 11, 26, 3 do rect(x, 22, 2, 2, col(M_)) end wall(17, 15, 5, 8); roof(19, 11, { 0, 1, 2, 3 }) wall(7, 13, 7, 24); roof(10, 5, { 0, 1, 1, 2, 3, 3, 4, 4 }) wall(26, 16, 7, 21); roof(29, 9, { 0, 1, 1, 2, 3, 3, 4, 4 }) rect(10, 18, 1, 3, col(WIN)); rect(10, 26, 1, 3, col(LIT, 0.05)) rect(29, 21, 1, 3, col(WIN)); rect(29, 28, 1, 3, col(WIN)) rect(14, 27, 2, 3, col(LIT, 0.05)); rect(23, 27, 2, 3, col(WIN)) rect(18, 31, 3, 6, col(WIN)); px(19, 30, col(WIN)) OBJ = snapshot(0, 0, W, H) cover() end local ARCH = { 3, 3, 2, 1 } local function s_bridge() gfx.clear(KEY[1], KEY[2], KEY[3]) blit_rgb(OBJ, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) rect(38, 34, 26, 1, col(L_)); rect(38, 35, 26, 9, col(M_)) rect(38, 36, 26, 1, col(D_, 0.1)); rect(38, 39, 26, 1, col(D_, 0.2)) for x = 38, 63, 5 do px(x, 37, col(D_, 0.2)); px(x + 2, 38, col(D_, 0.2)) end for _, cx in ipairs({ 45, 53, 61 }) do for k = 1, 4 do rect(cx - ARCH[k], 44 - k, 2 * ARCH[k], 1, KEY[1], KEY[2], KEY[3]) end rect(cx - 4, 43, 1, 1, col(D_, 0.2)); rect(cx + 3, 43, 1, 1, col(D_, 0.2)) end OBJ = snapshot(0, 0, W, H) for r = 22, 42 do ORR[r] = snapshot(0, r, W, 1) end OBJLOW = snapshot(0, HZ, W, LOW) cover() end local function s_top() blit_rgb(TOP, 0, 0, W, HZ) blit_rgb(OBJ, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) TOPF = snapshot(0, 0, W, HZ) cover() end local WN, WF = { 150, 186, 226 }, { 70, 110, 178 } local function s_water() blit_rgb(TOP, 0, 0, W, HZ) for r = 4, HZ - 1 do RS[r] = snapshot(0, r, W, 1) end for y = HZ, H - 1 do local d = (y - HZ) / (H - 1 - HZ) blit_rgb(RS[2 * HZ - 1 - y], 0, y, W, 1) local o = ORR[2 * WL - 1 - y] if y >= WL and o then blit_rgb(o, 0, y, W, 1, KEY[1], KEY[2], KEY[3]) end blend_rect(0, y, W, 1, floor(mix(WN[1], WF[1], d) + 0.5), floor(mix(WN[2], WF[2], d) + 0.5), floor(mix(WN[3], WF[3], d) + 0.5), 0.3 + 0.35 * d) WROW[y] = snapshot(0, y, W, 1) end WATER = snapshot(0, HZ, W, H - HZ) end local STAGES = { s_sky, s_ridges, s_objects, s_bridge, s_top, s_water } local STAGE = 1 local function build() STAGES[STAGE]() if STAGE < 3 then rect(0, HZ, W, H - HZ, 196, 212, 238) end STAGE = STAGE + 1 end local GL = {} for i = 1, 8 do local h = hash32(i * 0xC2B2AE35) GL[i] = { y = 46 + umod(h, 16), len = 4 + umod(h >> 8, 7), x0 = umod(h >> 12, W + 12), v = (umod(h >> 16, 8) + 3) / 10 * (umod(h >> 20, 2) * 2 - 1), w = 1.2 + umod(h >> 22, 6) * 0.3, ph = umod(h >> 10, 628) / 100 } end local ML = { { 39, 30, 3.5, 0.20, 2 }, { 27, 34, 2.2, 0.18, 2 }, { 17, 26, 1.5, 0.14, 2 }, { 52, 32, 4.5, 0.18, 2 }, } local MC = { 240, 246, 255 } local function mist_row(xl, xr, y, a) local il, ir = floor(xl), floor(xr) if ir <= il then return end blend_rect(il + 1, y, ir - il - 1, 1, MC[1], MC[2], MC[3], a) blend_rect(il, y, 1, 1, MC[1], MC[2], MC[3], a * (1 - (xl - il))) blend_rect(ir, y, 1, 1, MC[1], MC[2], MC[3], a * (xr - ir)) end function render() if STAGE <= #STAGES then build() if STAGE <= #STAGES then gfx.ready(false) return end end local t = frame / FPS blit_rgb(TOPF, 0, 0, W, HZ) blit_rgb(WATER, 0, HZ, W, H - HZ) local sp = 0.55 + 0.15 * (cfg.ripple or 3) local p1, p2 = t * 6.9 * sp, t * 5.1 * sp for y = HZ + 1, H - 1 do local d = (y - HZ) / (H - 1 - HZ) local dx = floor((0.3 + 1.2 * d) * (sin(y * 0.95 - p1) + 0.7 * sin(y * 1.9 + p2 + 1.3)) + 0.5) if dx ~= 0 then blit_rgb(WROW[y], dx, y, W, 1) end end blit_rgb(OBJLOW, 0, HZ, W, LOW, KEY[1], KEY[2], KEY[3]) for i = 1, #GL do local g = GL[i] local a = 0.2 * (0.5 + 0.5 * sin(t * g.w + g.ph)) local x = floor((g.x0 + t * g.v) % (W + g.len) - g.len + 0.5) blend_rect(x, g.y, g.len, 1, 255, 255, 255, a) end local nm = cfg.mist or 3 local boost = nm >= 5 and 1.4 or 1 for li = 1, min(4, nm) do local m = ML[li] local L = m[2] local span = W + 2 * L for i = 1, m[5] do local x = (i * span / m[5] + li * 17 + t * m[3]) % span - L local a = m[4] * boost mist_row(x + L * 0.25, x + L * 0.75, m[1], a * 0.8) mist_row(x, x + L, m[1] + 1, a) mist_row(x - L * 0.1, x + L * 1.1, m[1] + 2, a * 0.9) mist_row(x + L * 0.15, x + L * 0.85, m[1] + 3, a * 0.6) end end end