info = { type = "image", title = "Windmill isle", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Red windmill on an island, rippling reflection", api = 44, } local tr = ({ sk = { ["Time of day"] = "Denná doba", ["Day"] = "Deň", ["Sunset"] = "Západ", ["Sail speed"] = "Rýchlosť lopatiek", ["Birds"] = "Vtáky" }, cs = { ["Time of day"] = "Denní doba", ["Day"] = "Den", ["Sunset"] = "Západ", ["Sail speed"] = "Rychlost lopatek", ["Birds"] = "Ptáci" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "time", label = T("Time of day"), type = "int", default = "day", opts = { keys = { "day", "sunset" }, T("Day"), T("Sunset") } }, { key = "speed", label = T("Sail speed"), type = "int", min = 1, max = 5, default = 2 }, { key = "birds", label = T("Birds"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot, clip = gfx.blit_rgb, gfx.snapshot, gfx.clip local floor, sin, cos, sqrt, min, max = math.floor, math.sin, math.cos, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local MX, HUBY, WL = 46, 16, 36 local RX0, RY0, RW, RH = 28, 8, 36, 32 local KR, KG, KB = 255, 0, 255 local PALS = { day = { top = { 36, 116, 226 }, mid = { 100, 170, 240 }, hor = { 156, 208, 250 }, hillF = { 92, 146, 190 }, hillN = { 58, 124, 130 }, tree = { 34, 96, 70 }, house = { 214, 96, 80 }, wN = { 70, 140, 214 }, wF = { 24, 86, 196 }, rt = { 48, 110, 196 }, brick = { 204, 54, 46 }, brickL = { 238, 98, 74 }, brickD = { 142, 34, 40 }, roof = { 74, 56, 78 }, roofL = { 118, 92, 112 }, wood = { 116, 78, 46 }, sail = { 244, 236, 214 }, gl = { 128, 206, 92 }, gm = { 70, 162, 62 }, gd = { 38, 108, 56 }, earth = { 120, 86, 54 }, cloud = { 255, 255, 255 }, cloudD = { 196, 214, 240 }, bank = { 36, 120, 50 }, bankL = { 104, 184, 76 }, bankD = { 22, 84, 42 }, }, sunset = { top = { 58, 60, 160 }, mid = { 226, 100, 130 }, hor = { 255, 190, 114 }, hillF = { 134, 88, 150 }, hillN = { 90, 62, 118 }, tree = { 52, 44, 90 }, house = { 226, 120, 90 }, wN = { 232, 134, 124 }, wF = { 52, 58, 150 }, rt = { 120, 80, 150 }, brick = { 184, 46, 58 }, brickL = { 248, 112, 88 }, brickD = { 116, 30, 52 }, roof = { 58, 42, 76 }, roofL = { 120, 78, 108 }, wood = { 104, 66, 52 }, sail = { 255, 222, 190 }, gl = { 196, 192, 96 }, gm = { 104, 140, 74 }, gd = { 50, 76, 70 }, earth = { 100, 68, 70 }, cloud = { 255, 206, 176 }, cloudD = { 186, 112, 150 }, bank = { 44, 90, 70 }, bankL = { 150, 160, 90 }, bankD = { 28, 56, 64 }, }, } local P = PALS.day local function lerp(a, b, t) return a + (b - a) * t end local function mix(c1, c2, t) return lerp(c1[1], c2[1], t), lerp(c1[2], c2[2], t), lerp(c1[3], c2[3], t) end local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function vn(i, seed) return (hash32(i * 0x9E3779B1 + seed) & 0x7FFF) / 32767 end local function c3(c) return c[1], c[2], c[3] end local TOP, WATER, WROW, RROW, ISL, BANK = nil, nil, {}, {}, nil, nil local SRC = {} local function sky() for y = 0, 31 do local t = y / 31 local r, g, b if P == PALS.day then r, g, b = mix(P.top, P.hor, t ^ 1.2) elseif t < 0.6 then r, g, b = mix(P.top, P.mid, t / 0.6) else r, g, b = mix(P.mid, P.hor, (t - 0.6) / 0.4) end rect(0, y, W, 1, rgb(r, g, b)) end if P == PALS.sunset then for dy = -4, 4 do local hw = floor(sqrt(17 - dy * dy) + 0.5) rect(12 - hw, 26 + dy, 2 * hw + 1, 1, 255, 238, 196) end end for x = 0, W - 1 do local hf = floor(25 + 2 * sin(x * 0.09 + 1) + vn(x // 2, 3) * 1.8) rect(x, hf, 1, 32 - hf, c3(P.hillF)) local hn = floor(28 + 1.5 * sin(x * 0.17 + 2) + vn(x, 9) * 1.2) rect(x, hn, 1, 32 - hn, c3(P.hillN)) rect(x, 30 - (vn(x, 21) > 0.6 and 1 or 0), 1, 3, c3(P.tree)) end blend_rect(0, 28, W, 4, P.hor[1], P.hor[2], P.hor[3], 0.25) rect(6, 29, 3, 2, c3(P.house)); px(7, 28, c3(P.roof)); rect(12, 30, 2, 1, c3(P.house)) TOP = snapshot(0, 0, W, 32) for y = 3, 31 do SRC[y] = snapshot(0, y, W, 1) end end local function lake() for y = 32, 60 do local d = (y - 32) / 28 blit_rgb(SRC[63 - y], 0, y, W, 1) local r, g, b = rgb(mix(P.wN, P.wF, d)) blend_rect(0, y, W, 1, r, g, b, 0.3 + 0.42 * d) WROW[y] = snapshot(0, y, W, 1) end WATER = snapshot(0, 32, W, 29) end local function draw_mill(k) local function col(c) return rgb(mix(c, P.rt, k)) end rect(RX0, RY0, RW, RH, KR, KG, KB) for dy = 0, 4 do local hw = floor(15 * sqrt(1 - ((dy - 2) / 3) ^ 2) + 0.5) local c = dy == 0 and P.gl or dy < 3 and P.gm or P.gd if dy == 4 then c, hw = P.earth, 12 end rect(MX - hw, 34 + dy, 2 * hw + 1, 1, col(c)) end rect(33, 33, 3, 1, col(P.gm)); rect(57, 33, 3, 1, col(P.gm)) px(37, 34, col({ 255, 236, 120 })); px(54, 34, col({ 250, 160, 190 })); px(40, 33, col(P.gl)) for y = 18, 35 do local half = 3.5 + 2 * (y - 18) / 17 local xl = floor(MX - half + 0.5) local w = 2 * floor(half + 0.5) + 1 rect(xl, y, w, 1, col(P.brick)) rect(xl, y, 2, 1, col(P.brickL)) rect(xl + w - 2, y, 2, 1, col(P.brickD)) if y % 3 == 0 then blend_rect(xl, y, w, 1, 70, 16, 22, 0.28) end end rect(MX - 1, 23, 3, 4, col({ 40, 44, 70 })); px(MX - 1, 23, col(P.sail)); rect(MX - 1, 27, 3, 1, col(P.wood)) rect(MX - 1, 32, 3, 4, col(P.wood)); px(MX + 1, 34, col(P.sail)) rect(MX - 6, 28, 13, 1, col(P.wood)) for x = MX - 6, MX + 6, 2 do rect(x, 26, 1, 2, col(P.wood)) end for i = 0, 5 do local hw = i < 5 and (1 + i) or 5 rect(MX - hw, 12 + i, 2 * hw + 1, 1, col(P.roof)) rect(MX - hw, 12 + i, min(2, 2 * hw + 1), 1, col(P.roofL)) end rect(MX - 5, 17, 11, 1, col(P.brickD)) px(MX, 11, col(P.roofL)) end local function mill() draw_mill(0) ISL = snapshot(RX0, RY0, RW, RH) draw_mill(0.5) for y = RY0, RY0 + RH - 1 do RROW[y] = snapshot(RX0, y, RW, 1) end end local function bank() rect(0, 50, W, 14, KR, KG, KB) for x = 0, W - 1 do local top = floor(57 + 1.5 * sin(x * 0.12 + 1) + (x > 40 and 0.06 * (x - 40) or 0) - (x < 14 and 2 or 0) + 0.5) rect(x, top, 1, 64 - top, c3(P.bank)) rect(x, top, 1, 1, c3(P.bankL)) rect(x, top + 5, 1, 59 - top, c3(P.bankD)) end for i = 1, 11 do local h = hash32(i * 0x85EBCA6B) local x = umod(h, W) local top = floor(57 + 1.5 * sin(x * 0.12 + 1) + (x > 40 and 0.06 * (x - 40) or 0) - (x < 14 and 2 or 0) + 0.5) local len = 4 + umod(h >> 8, 6) rect(x, top - len, 1, len, c3(P.gd)) if umod(h >> 14, 2) == 0 then rect(x, top - len - 1, 1, 2, c3(P.wood)) end end for i = 1, 8 do local h = hash32(i * 0xC2B2AE35) local x = umod(h, W) px(x, 58 + umod(h >> 8, 4), umod(h >> 14, 2) == 0 and 255 or 250, umod(h >> 14, 2) == 0 and 226 or 170, 120) end BANK = snapshot(0, 50, W, 14) end local STAGES = { sky, lake, mill, bank } local STAGE = 1 local READY = false function on_settings() P = PALS[cfg.time] or PALS.day READY, STAGE = false, 1 end local GL = {} for i = 1, 8 do local h = hash32(i * 0x27D4EB2F) GL[i] = { x = umod(h, W), y = 40 + umod(h >> 8, 12), w = 1.2 + umod(h >> 14, 8) * 0.3, ph = umod(h >> 18, 628) / 100 } end local CLOUDS = { { 4, 24, 2.4, 6 }, { 10, 17, 1.5, 40 }, { 16, 21, 1.0, 22 } } local RP = {} local function ripple(y, t, sp) local d = (y - 32) / 28 return floor((0.35 + 1.25 * d) * (sin(y * 0.95 - t * 6.9 * sp) + 0.7 * sin(y * 1.9 + t * 5.1 * sp + 1.3)) + 0.5) end local function span(xl, xr, y, c, a) local il, ir = floor(xl), floor(xr) if ir <= il or y < 0 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 cloud(c, t, flip, sp) local L = c[2] local x = (c[4] + t * c[3]) % (W + 2 * L) - L local rows = { { 0.25, 0.75, c[1], 0 }, { 0, 1, c[1] + 1, 0 }, { 0.05, 0.95, c[1] + 2, 1 } } for i = 1, 3 do local r = rows[i] local y = r[3] local col = r[4] == 1 and P.cloudD or P.cloud if flip then y = 63 - y local dx = RP[y] or 0 span(x + L * r[1] + dx, x + L * r[2] + dx, y, col, 0.32) else span(x + L * r[1], x + L * r[2], y, col, 0.95) end end end local function sails(t, flip, sp) local ang = (0.25 + 0.3 * (cfg.speed or 2)) * t + 0.2 * sin(t * 0.5) local wc, sc = P.wood, P.sail for k = 0, 3 do local a = ang + k * 1.5708 local dx, dy = cos(a), sin(a) for r = 2, 13 do local x, y = MX + r * dx, HUBY + r * dy if flip then for q = 0, (r >= 4 and 2 or 0), 2 do local ry = 72 - floor(y + q * dx + 0.5) if ry < 60 then local c = q == 0 and wc or sc blend(floor(x - q * dy + 0.5) + RP[ry], ry, c[1], c[2], c[3], 0.4) end end else px(floor(x + 0.5), floor(y + 0.5), wc[1], wc[2], wc[3]) if r >= 4 then px(floor(x - dy + 0.5), floor(y + dx + 0.5), sc[1], sc[2], sc[3]) px(floor(x - 2 * dy + 0.5), floor(y + 2 * dx + 0.5), sc[1], sc[2], sc[3]) end end end end if not flip then rect(MX - 1, HUBY - 1, 3, 3, 52, 40, 44); px(MX, HUBY, 150, 120, 100) end end local function bird(t, flip) local s = t % 17 if s > 10 then return end local x = -4 + s * 7.2 local y = 7 + 3 * sin(s * 0.9) + s * 0.4 local up = (frame // 4) % 2 == 0 local ix = floor(x) local f = x - ix local pts = up and { { 0, 0 }, { -1, -1 }, { -2, -2 }, { 1, -1 }, { 2, -2 } } or { { 0, 0 }, { -1, 0 }, { -2, 1 }, { 1, 0 }, { 2, 1 } } local iy = floor(y + 0.5) for i = 1, 5 do local p = pts[i] local py = iy + p[2] local a, c = 0.9, 40 if flip then py, a, c = 63 - py, 0.3, 20 end if P == PALS.sunset then c = 30 end blend(ix + p[1], py, c, c, c + 30, a * (1 - f)) blend(ix + p[1] + 1, py, c, c, c + 30, a * f) end end function render() if not READY then STAGES[STAGE]() STAGE = STAGE + 1 if STAGE > #STAGES then READY = true end gfx.clear(0, 0, 0) if TOP then blit_rgb(TOP, 0, 0, W, 32) end if WATER then blit_rgb(WATER, 0, 32, W, 29) end gfx.ready(false) return end local t = frame / FPS local sp = 1 blit_rgb(TOP, 0, 0, W, 32) for i = 1, #CLOUDS do cloud(CLOUDS[i], t, false, sp) end blit_rgb(WATER, 0, 32, W, 29) for y = 32, 60 do local dx = ripple(y, t, sp) RP[y] = dx if dx ~= 0 then blit_rgb(WROW[y], dx, y, W, 1) end end for i = 1, #CLOUDS do cloud(CLOUDS[i], t, true, sp) end for y = 37, 58 do local src = 72 - y if src >= RY0 then blit_rgb(RROW[src], RX0 + RP[y], y, RW, 1, KR, KG, KB) end end sails(t, true, sp) blit_rgb(ISL, RX0, RY0, RW, RH, KR, KG, KB) sails(t, false, sp) if cfg.birds ~= false then bird(t, true); bird(t, false) end blit_rgb(BANK, 0, 50, W, 14, KR, KG, KB) for i = 1, #GL do local g = GL[i] local v = sin(t * g.w + g.ph) if v > 0.45 then blend_rect(g.x, g.y, 2, 1, 255, 255, 255, (v - 0.45) * 1.1) end end end