info = { type = "image", title = "Iso camp", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Isometric cabin, campfire and frozen lake", api = 44, } local tr = ({ sk = { ["Snow"] = "Sneh", ["Kid"] = "Chlapec", ["Chimney smoke"] = "Dym z komína" }, cs = { ["Snow"] = "Sníh", ["Kid"] = "Kluk", ["Chimney smoke"] = "Kouř z komína" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "snow", label = T("Snow"), type = "int", min = 0, max = 6, default = 3 }, { key = "kid", label = T("Kid"), type = "bool", default = true }, { key = "smoke", label = T("Chimney smoke"), type = "bool", default = true }, } local rect, px, blend_rect, blend_mask = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend_mask local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local function c3(c) return c[1], c[2], c[3] end local SKY_T, SKY_B = { 14, 20, 52 }, { 36, 46, 100 } local SNOW, SNOW_HI, SNOW_LO = { 226, 236, 250 }, { 246, 250, 255 }, { 198, 212, 238 } local CLIFF_L, CLIFF_R, LIP = { 120, 140, 186 }, { 90, 108, 156 }, { 204, 218, 240 } local ICE, ICE_HI, ICE_LO = { 150, 206, 240 }, { 208, 238, 252 }, { 92, 144, 198 } local ICE_CL, ICE_CR = { 132, 192, 232 }, { 100, 160, 212 } local LOG_HI, LOG_L, LOG_LD = { 158, 106, 62 }, { 122, 78, 48 }, { 84, 52, 36 } local LOG_R, LOG_RD = { 98, 62, 40 }, { 66, 40, 30 } local function yline(x, y0, x0) return floor(y0 + (x + 0.5 - x0) * 0.5) end local function island_top(x) return floor(27 + 0.5 * math.abs(x + 0.5 - 32)) end local function island_bot(x) return floor(55 - 0.5 * math.abs(x + 0.5 - 32) + 0.999) end local function stage1() for y = 0, H - 1 do local t = y / (H - 1) rect(0, y, W, 1, floor(SKY_T[1] + (SKY_B[1] - SKY_T[1]) * t + 0.5), floor(SKY_T[2] + (SKY_B[2] - SKY_T[2]) * t + 0.5), floor(SKY_T[3] + (SKY_B[3] - SKY_T[3]) * t + 0.5)) end for x = 4, 59 do local yt, yb = island_top(x), island_bot(x) local lake_edge = x >= 48 rect(x, yt, 1, yb - yt, c3(SNOW)) rect(x, yb, 1, 2, c3(LIP)) local c = x < 32 and CLIFF_L or (lake_edge and ICE_CR or CLIFF_R) rect(x, yb + 2, 1, 4, c3(c)) end for i = 1, 70 do local h = hash32(i * 0x9E3779B1) local x, y = 6 + umod(h, 52), 28 + umod(h >> 8, 26) if y >= island_top(x) + 1 and y < island_bot(x) - 1 then px(x, y, c3(SNOW_HI)); px(x + 1, y + 1, c3(SNOW_LO)) end end for x = 36, 59 do local d = 0.5 * math.abs(x + 0.5 - 48) local yt, yb = floor(35 + d), floor(47 - d + 0.999) rect(x, yt, 1, yb - yt, c3(ICE_LO)) rect(x, yt + 1, 1, yb - yt - 2, c3(ICE)) end for i = 0, 5 do local x0, y0 = 40 + i * 3, 38 + (i % 3) for k = 0, 2 do px(x0 + k * 2, y0 + k, c3(ICE_HI)); px(x0 + k * 2 + 1, y0 + k, c3(ICE_HI)) end end px(52, 41, c3(ICE_LO)); px(53, 41, c3(ICE_LO)); px(54, 42, c3(ICE_LO)) px(44, 43, c3(ICE_LO)); px(45, 44, c3(ICE_LO)) end local function fir(cx, by, tiers) rect(cx, by - 1, 2, 2, 70, 48, 36) for k = 1, tiers do local w = 6 - k local y = by - 2 - (k - 1) * 3 for r = 0, 3 do local hw = w - 3 + r if hw >= 1 then rect(cx + 1 - hw, y - 3 + r, 2 * hw, 1, r == 0 and 238 or 26, r == 0 and 244 or 66, r == 0 and 255 or 56) end end end end local function stage_cabin() for p = 0, 5 do for side = 0, 1 do local x = side == 0 and 20 + 2 * p or 42 - 2 * p local xx = side == 0 and x or 63 - x - 1 local yt = yline(xx, 22, 20) local base, hi, lo = LOG_L, LOG_HI, LOG_LD if side == 1 then base, hi, lo = LOG_R, LOG_L, LOG_RD end rect(x, yt, 2, 11, c3(base)) for j = 0, 3 do rect(x, yt + 3 * j, 2, 1, c3(hi)) rect(x, yt + 3 * j + 2, 2, 1, c3(lo)) end end end for x = 24, 27 do local yb = yline(x, 33, 20) rect(x, yb - 7, 1, 7, 52, 32, 24) if x == 24 or x == 27 then rect(x, yb - 8, 1, 8, 150, 98, 58) end end px(27, yline(27, 33, 20) - 4, 230, 190, 90) for x = 36, 39 do local yt = yline(63 - x - 1, 22, 20) rect(x, yt + 3, 1, 4, 255, 204, 104) end rect(37, yline(63 - 37 - 1, 22, 20) + 3, 1, 4, 96, 58, 34) rect(36, yline(63 - 36 - 1, 22, 20) + 5, 4, 1, 96, 58, 34) end local function stage_roof() for x = 18, 45 do local xx = x < 32 and x or 63 - x local top = floor(23 - (xx + 0.5 - 18) * 10 / 14) local bot = floor(23 + (xx + 0.5 - 18) * 0.5) + 1 local sn = floor(top + 7 + (xx % 3 == 0 and 1 or 0)) if sn > bot - 1 then sn = bot - 1 end local lit = x < 32 rect(x, top, 1, bot - top, lit and 88 or 66, lit and 60 or 44, lit and 52 or 44) rect(x, top, 1, sn - top, lit and 236 or 204, lit and 242 or 216, lit and 253 or 240) rect(x, bot - 1, 1, 1, 48, 32, 32) end px(22, 24, 232, 240, 255); px(26, 27, 232, 240, 255); px(40, 26, 200, 212, 238) rect(38, 9, 3, 10, 96, 90, 100); rect(38, 8, 3, 1, 238, 244, 255); rect(39, 9, 1, 1, 40, 36, 44) end local function stage_props() fir(9, 41, 3); fir(14, 45, 2) for r = 0, 2 do local w = 10 - 2 * r for k = 0, w - 1 do px(12 + r * 1 + k, 37 - r * 2 + k // 3, 150, 96, 52) px(12 + r * 1 + k, 38 - r * 2 + k // 3, 104, 64, 38) end px(12 + r + w, 37 - r * 2 + (w - 1) // 3, 214, 168, 108) px(12 + r + w, 38 - r * 2 + (w - 1) // 3, 186, 140, 88) end rect(31, 46, 11, 3, 44, 36, 36); rect(32, 45, 9, 1, 44, 36, 36); rect(32, 49, 9, 1, 44, 36, 36) for k = 0, 9 do local a = k * 6.2832 / 10 local sx, sy = 36 + floor(6 * math.cos(a) + 0.5), 47 + floor(3 * sin(a) + 0.5) rect(sx - 1, sy - 1, 2, 2, 118, 122, 138); px(sx - 1, sy - 1, 170, 176, 190) end for k = 0, 5 do px(33 + k, 47 + k // 3, 110, 66, 38); px(33 + k, 46 - k // 3 + 1, 150, 96, 52) px(39 - k, 47 + k // 3, 110, 66, 38) end if cfg.kid ~= false then for _, p in ipairs({ { 27, 38 }, { 30, 40 }, { 33, 41 }, { 30, 43 }, { 27, 44 }, { 25, 38 }, { 31, 42 } }) do px(p[1], p[2], 176, 192, 224) end end end local STAGES = { stage1, stage_cabin, stage_roof, stage_props } local BG, STAGE = nil, 0 function on_settings() STAGE = 0 end local function build() if STAGE > 0 then gfx.blit_rgb(BG, 0, 0, W, H) end STAGES[STAGE + 1]() BG = gfx.snapshot(0, 0, W, H) STAGE = STAGE + 1 end local FLAME_PAL = { 0, 0, 0, 190, 36, 20, 255, 118, 20, 255, 200, 50, 255, 242, 176 } local function sprite(rows) local cells = {} for i = 1, #rows do for c = 1, #rows[i] do cells[#cells + 1] = string.char(tonumber(rows[i]:sub(c, c)) or 0) end end return table.concat(cells) end local FLAMES = { sprite({ "...2...", "..22...", "..222..", ".22322.", ".23332.", "2233322", "2334332", ".13431.", "..111.." }), sprite({ "..2....", "..22...", ".222...", ".22322.", "223332.", "2233322", "2334332", ".13431.", "..111.." }), sprite({ "....2..", "...22..", "..222..", ".22322.", ".233322", "2233322", "2334332", ".13431.", "..111.." }), } local FLARE = sprite({ "....2....", "....2....", "...222...", "..2232.2.", "..22332..", ".2233322.", ".2333332.", "223343322", "233444332", ".2334332.", "..13431.." }) local GLOW do local rows = {} for y = -8, 8 do for x = -15, 15 do local d = sqrt((x / 15) ^ 2 + (y / 7.5) ^ 2) rows[#rows + 1] = string.char(d < 1 and floor(255 * (1 - d) + 0.5) or 0) end end GLOW = table.concat(rows) end local KW, KH = 8, 11 local LAYERS = { { "wr", { 200, 40, 44 } }, { "s", { 238, 188, 150 } }, { "bp", { 40, 74, 150 } }, { "c", { 226, 120, 48 } }, { "hdl", { 112, 70, 40 } }, } local FRONT = { "...w...", "..rrr..", "..sss..", "..bbb..", ".ccccc.", ".ccccc.", "..ccc..", "..ccc..", "..p.p..", "..d.d.." } local THROW = { "lllllll", ".crrrc.", ".csssc.", "..bbb..", "..ccc..", "..ccc..", "..ccc..", "..ccc..", "..p.p..", "..d.d.." } local CH16 = {} for k = 0, 16 do CH16[k] = string.char(floor(255 * k / 16 + 0.5)) end local LAY, KMASKS, KCOUNT = {}, {}, 0 local function layers(dir, step, mode) local key = (dir * 2 + step) * 3 + mode if LAY[key] then return LAY[key] end local src = mode == 2 and THROW or FRONT local rows = {} for r = 1, 10 do local s = src[r] if r == 9 and step == 1 then s = "...pp.." elseif r == 10 and step == 1 then s = "...dd.." end if mode == 1 and r == 6 then s = "lllllll" end if dir >= 3 then s = s:gsub("s", "h") end if dir == 2 or dir == 4 then s = s:reverse() end rows[r] = s end local lay = {} for li = 1, #LAYERS do local chars, g = LAYERS[li][1], {} for r = 1, 10 do local s = rows[r] for c = 1, 7 do g[(r - 1) * 7 + c] = chars:find(s:sub(c, c), 1, true) and 1 or 0 end end lay[li] = g end LAY[key] = lay return lay end local function kid_masks(dir, step, mode, qx, qy) local key = ((((dir * 2 + step) * 3 + mode) * 4) + qx) * 4 + qy local m = KMASKS[key] if m then return m end if KCOUNT >= 40 then KMASKS, KCOUNT = {}, 0 end local a, b, c, d = (4 - qx) * (4 - qy), qx * (4 - qy), (4 - qx) * qy, qx * qy local lay = layers(dir, step, mode) m = {} for li = 1, #LAYERS do local g, out, n = lay[li], {}, 0 for y = 0, KH - 1 do for x = 0, KW - 1 do n = n + 1 local i = (y - 1) * 7 + x + 1 local v = 0 if x <= 6 and y >= 1 and y <= 10 then v = a * g[i] end if x >= 1 and x <= 7 and y >= 1 and y <= 10 then v = v + b * g[i - 1] end if x <= 6 and y >= 2 then v = v + c * g[i - 7] end if x >= 1 and x <= 7 and y >= 2 then v = v + d * g[i - 8] end out[n] = CH16[v] end end m[li] = table.concat(out) end KMASKS[key] = m KCOUNT = KCOUNT + 1 return m end local AX, AY, BX, BY, CX, CY = 24, 37, 32, 41, 26, 44 local CYCLE = 244 local function kid_at(f) f = f % CYCLE local function lerp(a, b, p) return a + (b - a) * p end local x, y, dir, mode, flare = AX, AY, 1, 0, -1 if f < 30 then if f >= 20 then mode = 1 end elseif f < 75 then local p = (f - 30) / 45 x, y, dir, mode = lerp(AX, BX, p), lerp(AY, BY, p), 1, 1 elseif f < 109 then local p = (f - 75) / 34 x, y, dir, mode = lerp(BX, CX, p), lerp(BY, CY, p), 2, 1 elseif f < 125 then x, y, dir, mode = CX, CY, 1, f < 115 and 2 or 0 elseif f < 165 then x, y, dir, flare = CX, CY, 1, f - 125 elseif f < 199 then local p = (f - 165) / 34 x, y, dir = lerp(CX, BX, p), lerp(CY, BY, p), 3 else local p = (f - 199) / 45 x, y, dir = lerp(BX, AX, p), lerp(BY, AY, p), 4 end if f >= 109 and f < 125 then flare = -1 end return x, y, dir, floor(f / 7) % 2, mode, flare end local function soft(x, y, w, h, r, g, b, a) local iy = floor(y) local f = y - iy blend_rect(x, iy, w, h, r, g, b, a * (1 - f)) if f > 0.04 then blend_rect(x, iy + h, w, 1, r, g, b, a * f) end end local SPARK, FLAKE = {}, {} for i = 1, 9 do local h = hash32(i * 0x85EBCA6B) SPARK[i] = { P = 40 + umod(h, 30), off = umod(h >> 6, 70), dx = umod(h >> 10, 5) - 2, w = 0.6 + umod(h >> 14, 8) / 10, ph = umod(h >> 18, 628) / 100 } end for i = 1, 42 do local h = hash32(i * 0xC2B2AE35) FLAKE[i] = { x = umod(h, W), y = umod(h >> 6, H + 6), v = 5 + umod(h >> 12, 8), sw = 1 + umod(h >> 16, 20) / 10, ph = umod(h >> 21, 628) / 100, a = 0.55 + umod(h >> 8, 4) / 10 } end local function puffs(t, x0, y0, n, wind, rise) for i = 0, n - 1 do local age = (t * 0.55 + i * (1 / n)) % 1 local x = x0 + wind * age * 7 + 1.2 * sin(age * 6 + i) local y = y0 - age * rise local s = 2 + floor(age * 3) soft(floor(x) - s // 2, y, s, s - 1, 158, 160, 184, 0.28 * (1 - age)) end end function render() if STAGE < #STAGES then build(); if STAGE < #STAGES then gfx.ready(false) end return end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS local kx, ky, dir, step, mode, flare = kid_at(frame) local flaring = flare >= 0 and flare < 40 if cfg.kid == false then flaring = false end if cfg.kid ~= false then local x, y = kx - 3, ky - 9 local ix, iy = floor(x), floor(y) local qx, qy = floor((x - ix) * 4 + 0.5), floor((y - iy) * 4 + 0.5) if qx == 4 then ix, qx = ix + 1, 0 end if qy == 4 then iy, qy = iy + 1, 0 end local moving = (frame % CYCLE >= 30 and frame % CYCLE < 109) or frame % CYCLE >= 165 local m = kid_masks(dir, moving and step or 0, mode, qx, qy) for li = 1, #LAYERS do local c = LAYERS[li][2] blend_mask(m[li], c[1], c[2], c[3], ix, iy - 1, KW, KH, KW, KH, 1) end end local pulse = 0.34 + 0.07 * sin(t * 9) + 0.05 * sin(t * 23) blend_mask(GLOW, 255, 150, 60, 21, 39, 31, 17, 31, 17, flaring and pulse * 1.35 or pulse) if flaring then gfx.blit_indexed(FLARE, FLAME_PAL, 32, 36, 9, 11, 9, 11, true) else gfx.blit_indexed(FLAMES[hash32(frame // 3) % 3 + 1], FLAME_PAL, 33, 38, 7, 9, 7, 9, true) end local cf = frame % CYCLE if cfg.kid ~= false and cf >= 115 and cf < 125 then local p = (cf - 115) / 10 soft(29 + floor(7 * p), 38 + 5 * p - 6 * sin(p * 3.1416), 4, 1, 150, 96, 52, 1) end for i = 1, flaring and 9 or 6 do local s = SPARK[i] local age = (frame + s.off) % s.P local k = age / s.P local x = 36 + s.dx + 2 * sin(age * 0.18 + s.ph) + 0.05 * age local y = 40 - age * s.w * 0.55 local g = floor(210 - 120 * k) soft(floor(x), y, 1, 1, 255, g, 60, 0.95 * (1 - k)) end puffs(t, 37, 38, 4, 1, 10) if cfg.smoke ~= false then puffs(t * 0.8, 39, 9, 4, 1.2, 9) end local gp = floor(t * 1.4) % 3 local gx, gy = ({ 52, 43, 48 })[gp + 1], ({ 38, 40, 44 })[gp + 1] local ga = sin((t * 1.4 % 1) * 3.1416) blend_rect(gx - 1, gy, 3, 1, 255, 255, 255, 0.8 * ga); blend_rect(gx, gy - 1, 1, 3, 255, 255, 255, 0.8 * ga) for i = 1, (cfg.snow or 3) * 7 do local f = FLAKE[i] local y = (f.y + f.v * t) % (H + 6) - 3 local x = floor(f.x + f.sw * sin(t * 0.9 + f.ph) + 0.5) % W soft(x, y, 1, 1, 255, 255, 255, f.a) end end