info = { type = "image", title = "Camping night", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "Two tents and a campfire under the moon", api = 33, } local tr = ({ sk = { ["Sparks"] = "Iskry", ["Fireflies"] = "Svetlušky", ["Moon"] = "Mesiac" }, cs = { ["Sparks"] = "Jiskry", ["Fireflies"] = "Světlušky", ["Moon"] = "Měsíc" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "sparks", label = T("Sparks"), type = "int", min = 0, max = 10, default = 6 }, { key = "flies", label = T("Fireflies"), type = "int", min = 0, max = 8, default = 5 }, { key = "moon", label = T("Moon"), type = "color", default = "#F6E4B4" }, } local rect, px, blend = gfx.rect, gfx.px, gfx.blend local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local TAU = 6.28318 local function c3(c) return c[1], c[2], c[3] end local function ell(cx, cy, rx, ry, c) local r, g, b = c[1], c[2], c[3] for y = floor(cy - ry + 0.5), floor(cy + ry - 0.5) do local dy = (y + 0.5 - cy) / ry local k = 1 - dy * dy if k > 0 then local hw = rx * sqrt(k) local x0 = floor(cx - hw + 0.5) rect(x0, y, floor(cx + hw + 0.5) - x0, 1, r, g, b) end end end local SKY1, SKY2, SKY3 = { 50, 42, 128 }, { 68, 56, 154 }, { 90, 72, 176 } local HALO, STAR, STAR_D = { 112, 92, 196 }, { 240, 236, 255 }, { 140, 130, 220 } local PINE, RIM = { 44, 124, 112 }, { 100, 186, 152 } local GROUND, GROUND2 = { 96, 68, 108 }, { 78, 54, 92 } local GLOW1, GLOW2 = { 120, 76, 102 }, { 156, 96, 100 } local MOON = { 246, 228, 180 } local OUTER, MID, CORE = { 232, 60, 30 }, { 255, 150, 30 }, { 255, 232, 120 } local LOG, LOG_D = { 130, 78, 54 }, { 84, 48, 40 } local SKIN, JEANS = { 244, 196, 152 }, { 70, 92, 176 } local MX, MY = 47, 12 local function spruce(x, base, h, hw) local tiers = 3 local th = (h * 5) // 9 for k = 0, tiers - 1 do local y0 = base - h + (k * (h - th)) // (tiers - 1) local hk = hw * (6 + 2 * k) // 10 for i = 0, th - 1 do local w = 1 + (i * hk) // th rect(x - w, y0 + i, 2 * w + 1, 1, c3(PINE)) px(x + w, y0 + i, c3(RIM)) end end rect(x - 1, base - 3, 3, 5, c3(PINE)) end local function tent(ax, ay, hw, base, c1, c2, door) for y = ay, base do local w = ((y - ay) * hw) // (base - ay) rect(ax - w, y, w + 1, 1, c3(c1)) rect(ax + 1, y, w, 1, c3(c2)) end px(ax, ay - 1, 200, 200, 220) local dh = (base - ay) * 2 // 3 for i = 0, dh do local w = (i * 3) // dh rect(ax - w + 1 - 1, base - dh + i, 2 * w + 1, 1, c3(door)) end end local SX, SY, SP = {}, {}, {} do local k, i = 0, 0 while k < 14 and i < 300 do i = i + 1 local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), umod(h >> 8, 28) local dx, dy = x - MX, y - MY if dx * dx + dy * dy > 100 then k = k + 1 SX[k], SY[k], SP[k] = x, y, umod(h >> 16, 628) / 100.0 end end end local BG = nil local function build_bg() local m = cfg.moon or { r = 246, g = 228, b = 180 } rect(0, 0, W, 20, c3(SKY1)); rect(0, 20, W, 14, c3(SKY2)); rect(0, 34, W, 14, c3(SKY3)) ell(MX + 0.5, MY + 0.5, 9, 9, HALO) ell(MX + 0.5, MY + 0.5, 6.5, 6.5, { m.r, m.g, m.b }) px(MX - 2, MY - 2, c3(MOON)); px(MX + 2, MY + 2, 200, 184, 140) rect(MX - 2, MY - 2, 2, 1, 210, 192, 148); rect(MX + 1, MY + 1, 2, 2, 210, 192, 148) spruce(5, 48, 22, 7); spruce(15, 47, 17, 6); spruce(26, 46, 14, 5) spruce(38, 46, 15, 5); spruce(49, 47, 19, 6); spruce(59, 48, 24, 7) rect(0, 46, W, 10, c3(GROUND)); rect(0, 56, W, 8, c3(GROUND2)) ell(32.5, 58, 16, 5, GLOW1); ell(32.5, 58, 9, 3, GLOW2) tent(11, 36, 10, 54, { 240, 160, 72 }, { 204, 112, 62 }, { 70, 40, 62 }) tent(53, 38, 10, 55, { 96, 160, 220 }, { 62, 112, 176 }, { 36, 50, 112 }) rect(0, 56, W, 1, c3(GROUND2)) rect(28, 58, 9, 2, c3(LOG)); rect(29, 56, 7, 2, c3(LOG_D)); rect(28, 58, 2, 2, 200, 140, 96); rect(35, 58, 2, 2, 200, 140, 96) BG = gfx.snapshot(0, 0, W, H) end function on_settings() BG = nil end local FLAME = { 3, 7, 10, 11, 7, 3 } local function draw_fire(f) local s = floor(f * 10) for i = 1, 6 do local x = 29 + i local h = FLAME[i] + (hash32(x * 31 + s * 977) & 3) - 1 if h < 2 then h = 2 end rect(x, 56 - h, 1, h, c3(OUTER)) rect(x, 56 - h + 3, 1, h - 3, c3(MID)) if i >= 2 and i <= 5 and h > 5 then rect(x, 56 - h + 6, 1, h - 6, c3(CORE)) end end end local LIFE = 1.47 local function draw_sparks(f, n) for k = 1, n do local h0 = hash32(k * 0x85EBCA6B) local t = f + umod(h0, 147) / 100 local cyc = floor(t / LIFE) local age = t % LIFE local h = hash32(k * 7919 + cyc * 104729) & 0x7FFF local dx = (h % 11) - 5 local x = 32 + floor(dx * age / 0.6) local y = 46 - floor(age * 22.5) if age < 0.6 then px(x, y, c3(CORE)) elseif age < 1.07 then px(x, y, c3(MID)) else px(x, y, 210, 70, 40) end end end local function draw_flies(f, n) for k = 1, n do local h = hash32(k * 0xC2B2AE35) local ph = umod(h, 628) / 100.0 local x = 8 + umod(h >> 10, 48) + 5 * sin(f * 0.63 + ph) local y = 34 + umod(h >> 20, 14) + 3 * sin(f * 0.99 + ph * 1.7) local lum = 0.35 + 0.65 * (0.5 + 0.5 * sin(f * 2.7 + ph * 2)) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, 200, 255, 90, lum * (1 - fx) * (1 - fy)) blend(ix + 1, iy, 200, 255, 90, lum * fx * (1 - fy)) blend(ix, iy + 1, 200, 255, 90, lum * (1 - fx) * fy) blend(ix + 1, iy + 1, 200, 255, 90, lum * fx * fy) end end local function draw_people(f) rect(21, 49, 4, 7, 222, 64, 74) rect(21, 45, 4, 4, c3(SKIN)); rect(21, 45, 4, 1, 54, 38, 42); rect(21, 46, 1, 2, 54, 38, 42) rect(21, 56, 7, 2, c3(JEANS)); rect(24, 52, 3, 1, c3(SKIN)) px(24, 47, 40, 30, 36) rect(40, 49, 4, 7, 244, 204, 72) rect(40, 45, 4, 4, c3(SKIN)); rect(40, 45, 4, 1, 176, 94, 52); rect(43, 46, 1, 2, 176, 94, 52) rect(37, 56, 7, 2, c3(JEANS)) px(40, 47, 40, 30, 36) local ph = f % 5 if ph >= 1 and ph < 3.33 then rect(39, 43, 1, 7, 244, 204, 72) local wv = floor(f * 6) % 2 == 0 and 38 or 40 rect(wv, 41, 2, 2, c3(SKIN)) else rect(37, 52, 3, 1, c3(SKIN)) end end local STARCOL = { STAR, STAR_D } function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame / gfx.FPS local a = f * (TAU / 3) for k = 1, #SX do local c = sin(a + SP[k] * 3) > -0.2 and STAR or STAR_D px(SX[k], SY[k], c3(c)) end draw_fire(f) draw_sparks(f, cfg.sparks or 6) draw_people(f) draw_flies(f, cfg.flies or 5) end