info = { type = "image", title = "Star angler", category = "nature", author = "BRRRO", version = "1.0", license = "MIT", desc = "A fisherman hauls a star out of the night lake", api = 44, } local tr = ({ sk = { ["Moon"] = "Mesiac", ["Full"] = "Spln", ["Crescent"] = "Kosák" }, cs = { ["Moon"] = "Měsíc", ["Full"] = "Úplněk", ["Crescent"] = "Srpek" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "moon", label = T("Moon"), type = "int", default = 0, opts = { T("Full"), T("Crescent") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, abs, max, min = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.max, math.min local hash32 = math.hash32 local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local function lerp(a, b, t) return a + (b - a) * t end local function rnd(v) return floor(v + 0.5) end local function hs(a, b) return hash32(a * 7919 + b * 104729) & 0x7FFF end local function sm(x) x = max(0, min(1, x)); return x * x * (3 - 2 * x) end local MX, MY, MR = 42, 23, 14 local HOR = 39 local S0X, S0Y = 9, 12 local PX_, PY_ = S0X, rnd(HOR + (HOR - S0Y) * 0.45) local BX, BY0 = 40, 31 local SW, SH = 34, 15 local CELLS, RCELLS = {}, {} local function fill(x, y, w, h, v) for j = y, y + h - 1 do for i = x, x + w - 1 do CELLS[j * SW + i + 1] = v end end end for i = 1, SW * SH do CELLS[i] = 0 end fill(0, 9, 2, 2, 1); fill(1, 10, 3, 1, 1) fill(1, 11, 32, 1, 3); fill(2, 12, 30, 1, 1); fill(4, 13, 26, 1, 1); fill(7, 14, 20, 1, 1) fill(32, 10, 2, 1, 1); fill(33, 9, 1, 1, 1) fill(17, 9, 10, 2, 1) fill(23, 4, 6, 6, 1); fill(24, 3, 5, 1, 1) fill(15, 6, 9, 2, 1) fill(22, 0, 5, 4, 1); fill(21, 1, 7, 1, 1) fill(20, 2, 2, 1, 1) for j = 1, SH - 1 do for i = 0, SW - 1 do if CELLS[j * SW + i + 1] == 1 and CELLS[(j - 1) * SW + i + 1] == 0 then CELLS[j * SW + i + 1] = 2 end end end for i = 0, SW - 1 do if CELLS[i + 1] == 1 then CELLS[i + 1] = 2 end end local HANDX, HANDY = 15, 6 local STRIPS, OVER = {}, {} do local cols = {} for i = 0, SW - 1 do local j = 0 while j < SH do if CELLS[j * SW + i + 1] ~= 0 then local j0 = j while j < SH and CELLS[j * SW + i + 1] ~= 0 do j = j + 1 end cols[#cols + 1] = { i, j0, j } else j = j + 1 end end end for _, c in ipairs(cols) do local last = STRIPS[#STRIPS] if last and last[1] + last[2] == c[1] and last[3] == c[2] and last[4] == c[3] then last[2] = last[2] + 1 else STRIPS[#STRIPS + 1] = { c[1], 1, c[2], c[3] } end end for j = 0, SH - 1 do local i = 0 while i < SW do local v = CELLS[j * SW + i + 1] if v == 2 or v == 3 then local i0 = i while i < SW and CELLS[j * SW + i + 1] == v do i = i + 1 end OVER[#OVER + 1] = { i0, i - i0, j, v } else i = i + 1 end end end end local PRP = { 4, 78, 22 } local PGP = { 8, 108, 32 } local PBP = { 26, 178, 64 } local function draw_boat(x, y) local iy = floor(y) local f = y - iy for k = 1, #STRIPS do local s = STRIPS[k] local x0, w, top, bot = x + s[1], s[2], y + s[3], y + s[4] local t0, b0 = floor(top), floor(bot) local ft, fb = top - t0, bot - b0 if t0 == b0 then blend_rect(x0, t0, w, 1, 4, 8, 26, bot - top) else if ft > 0.02 then blend_rect(x0, t0, w, 1, 4, 8, 26, 1 - ft); t0 = t0 + 1 end if b0 > t0 then rect(x0, t0, w, b0 - t0, 4, 8, 26) end if fb > 0.02 then blend_rect(x0, b0, w, 1, 4, 8, 26, fb) end end end for k = 1, #OVER do local o = OVER[k] local v = o[4] local row = iy + o[3] blend_rect(x + o[1], row, o[2], 1, PRP[v], PGP[v], PBP[v], 1 - f) if f > 0.02 then blend_rect(x + o[1], row + 1, o[2], 1, PRP[v], PGP[v], PBP[v], f) end end end local function draw_reflection(x, y) for k = 1, #STRIPS do local s = STRIPS[k] blend_rect(x + s[1], rnd(y + SH + (SH - s[4]) + 1), s[2], s[4] - s[3], 4, 8, 26, 0.38) end end local MR2 = MR * MR local function wu(x0, y0, x1, y1, r, g, b, al) local dx, dy = x1 - x0, y1 - y0 local steep = abs(dx) < abs(dy) local n = floor(steep and abs(dy) or abs(dx)) if n < 1 then return end for i = 0, n do local u = i / n local x, y = x0 + dx * u, y0 + dy * u local ax, ay, bx, by, f if steep then ax, ay = floor(x), rnd(y); f = x - ax; bx, by = ax + 1, ay else ax, ay = rnd(x), floor(y); f = y - ay; bx, by = ax, ay + 1 end local ddx, ddy = ax - MX, ay - MY local rr, gg, bb = r, g, b if ddx * ddx + ddy * ddy < MR2 then rr, gg, bb = 22, 32, 76 end blend(ax, ay, rr, gg, bb, al * (1 - f)) if f > 0.12 then blend(bx, by, rr, gg, bb, al * f) end end end local SX, SY, SP, SC = {}, {}, {}, {} do local k = 0 for i = 1, 200 do if k >= 24 then break end local x, y = hs(i, 1) % W, 1 + hs(i, 2) % 35 local dx, dy = x - MX, y - MY if dx * dx + dy * dy > 21 * 21 and (abs(x - S0X) > 4 or abs(y - S0Y) > 4) then k = k + 1 SX[k], SY[k], SP[k] = x, y, TAU * (hs(i, 3) % 100) / 100 SC[k] = (hs(i, 4) % 4 == 0) end end end local NS = #SX local function ry(y) return rnd(HOR + (HOR - y) * 0.45) end local BG, MOON_NOW local BSTEP, BROW = 0, 0 local function build_step(m) if BSTEP == 0 then for y = 0, HOR - 1 do local t = y / (HOR - 1) rect(0, y, W, 1, rnd(lerp(8, 30, t)), rnd(lerp(16, 52, t)), rnd(lerp(54, 122, t))) end if m == 0 then for _, h in ipairs({ { 25, 0.05 }, { 21, 0.06 }, { 18, 0.08 } }) do for y = MY - h[1], MY + h[1] do local hw = sqrt(h[1] * h[1] - (y - MY) * (y - MY)) if y >= 0 and y < HOR then blend_rect(rnd(MX - hw), y, rnd(2 * hw), 1, 150, 184, 255, h[2]) end end end end BSTEP, BROW = 1, MY - MR elseif BSTEP == 1 then for y = BROW, min(BROW + 3, MY + MR) do for x = MX - MR, MX + MR do local dx, dy = x - MX, y - MY local d2 = dx * dx + dy * dy if d2 <= MR * MR + 1 then local nx, ny = dx / MR, dy / MR local nz = sqrt(max(0, 1 - nx * nx - ny * ny)) local lit = max(0, min(1, 0.55 + 0.35 * nz - 0.22 * nx - 0.22 * ny)) local mar = sin(x * 0.5 + 1) * sin(y * 0.42) + sin((x + y) * 0.31) * 0.7 mar = max(0, min(1, (mar - 0.35) * 1.6)) lit = lit * (1 - 0.28 * mar) local r, g, b = lerp(100, 222, lit), lerp(124, 234, lit), lerp(184, 252, lit) if m == 1 then local ex, ey = dx - 6.5, dy + 1.5 local inner = ex * ex + ey * ey <= 13.2 * 13.2 if inner then r, g, b = 22, 34, 76 end end px(x, y, rnd(r), rnd(g), rnd(b)) end end end BROW = BROW + 4 if BROW > MY + MR then BSTEP = 2 end elseif BSTEP == 2 then for x = 0, W - 1 do local h = floor(1.5 + 1.2 * sin(x * 0.19) + 0.8 * sin(x * 0.07 + 2) + (x < 14 and (14 - x) * 0.18 or 0) + (x > 56 and (x - 56) * 0.2 or 0)) rect(x, HOR - h, 1, h, 10, 20, 58) end for y = HOR, H - 1 do local t = (y - HOR) / (H - 1 - HOR) rect(0, y, W, 1, rnd(lerp(34, 8, t)), rnd(lerp(66, 22, t)), rnd(lerp(128, 62, t))) end rect(0, HOR, W, 1, 70, 104, 164) BG = gfx.snapshot(0, 0, W, H) BSTEP = 3 end end function on_settings() MOON_NOW = nil; BG = nil; BSTEP = 0 end local CYCLE = 20 * FPS local function arc(x0, y0, x1, y1, e, h) return lerp(x0, x1, e), lerp(y0, y1, e) - h * 4 * e * (1 - e) end local function star_shape(x, y, a, big) local ix, iy = rnd(x), rnd(y) blend(ix, iy, 255, 226, 120, a) blend(ix - 1, iy, 255, 214, 100, a * 0.7); blend(ix + 1, iy, 255, 214, 100, a * 0.7) blend(ix, iy - 1, 255, 214, 100, a * 0.7); blend(ix, iy + 1, 255, 214, 100, a * 0.7) if big then blend(ix - 2, iy, 255, 200, 90, a * 0.35); blend(ix + 2, iy, 255, 200, 90, a * 0.35) blend(ix, iy - 2, 255, 200, 90, a * 0.35); blend(ix, iy + 2, 255, 200, 90, a * 0.35) blend(ix - 1, iy - 1, 255, 200, 90, a * 0.25); blend(ix + 1, iy - 1, 255, 200, 90, a * 0.25) blend(ix - 1, iy + 1, 255, 200, 90, a * 0.25); blend(ix + 1, iy + 1, 255, 200, 90, a * 0.25) end end function render() MOON_NOW = cfg.moon or 0 if not BG then build_step(MOON_NOW); gfx.ready(false) return end gfx.blit_rgb(BG, 0, 0, W, H) local t = (frame % CYCLE) / FPS local ts = frame / FPS local sa = 1 if t >= 6.6 and t < 7.4 then sa = 1 - (t - 6.6) / 0.8 elseif t >= 7.4 and t < 13.8 then sa = 0 elseif t >= 13.8 and t < 14.6 then sa = (t - 13.8) / 0.8 end for i = 1, NS do local tw = 0.55 + 0.45 * sin(ts * 1.3 + SP[i]) local r, g, b = 196, 214, 255 if SC[i] then r, g, b = 255, 238, 176 end blend(SX[i], SY[i], r, g, b, 0.35 + 0.6 * tw) blend(SX[i] + rnd(sin(ts * 1.7 + SP[i])), ry(SY[i]), r, g, b, 0.12 + 0.2 * tw) end if sa > 0 then local tw = 0.8 + 0.2 * sin(ts * 1.6) star_shape(S0X, S0Y, sa * tw, true) blend(PX_ + rnd(sin(ts * 1.7)), PY_, 255, 226, 120, sa * (0.3 + 0.25 * sin(ts * 2.1)) + ((t > 4.4 and t < 6.6) and 0.25 or 0)) end for y = HOR + 1, H - 1 do local k = y - HOR local s1 = sin(k * 1.7 + ts * 2.2) if sin(k * 2.9 + ts * 1.3) > -0.55 then local w = 11 - k * 0.2 + 2.5 * s1 local ox = 2 * sin(k * 0.9 + ts * 1.1) blend_rect(rnd(MX - w / 2 + ox), y, max(2, rnd(w)), 1, 196, 224, 255, (MOON_NOW == 0 and 0.5 or 0.28) * (1 - k / 28) * (0.7 + 0.3 * s1)) end end for i = 1, 9 do local y = HOR + 3 + (i * 7) % 21 local x = (i * 23 + ts * (2 + i % 3)) % (W + 10) - 8 blend_rect(rnd(x), y, 4 + i % 3, 1, 90, 130, 200, 0.28) end local bob = 0.8 * sin(TAU * ts / 3.4) local sx = BX - 17 + 0.5 * sin(TAU * ts / 5.1) local sy = BY0 + bob draw_reflection(rnd(sx), sy) draw_boat(rnd(sx), sy) local a, bend, sag = 36, 0.8, 3 local ex, ey local star_out, sx2, sy2 local hx, hy = rnd(sx) + HANDX, sy + HANDY local function tip_of(deg, len) local r = deg * 0.0174533 return hx - len * cos(r), hy - len * sin(r) end local FX, FY = 18, 47 + 0.4 * sin(ts * 1.5) local HOLDX, HOLDY if t < 2.2 then ex, ey = FX, FY elseif t < 2.8 then local e = sm((t - 2.2) / 0.6) a = lerp(36, 108, e); ex, ey = hx + 4 * e, hy - 4 + 14 * e; sag = 1 elseif t < 3.5 then local e = sm((t - 2.8) / 0.7) a = lerp(108, 22, e) local tx, ty = tip_of(a, 15) ex, ey = arc(tx + 4, ty + 10, PX_, PY_, e, 12); sag = 2 * e elseif t < 6.6 then a = 26 + 3 * sin(ts * 1.4); ex, ey = PX_, PY_ + 0.5 * sin(ts * 2.4); sag = 3 elseif t < 7.4 then local e = (t - 6.6) / 0.8 a = 26 - 6 * sin(e * 3.1); ex, ey = PX_, PY_ + 1.2 * sin(e * 6.2); sag = 3 * (1 - e) elseif t < 10.8 then local e = sm((t - 7.4) / 3.4) a = lerp(26, 58, e); bend = lerp(0.8, 2.6, min(1, e * 3)); sag = 0 local tx, ty = tip_of(58, 15) HOLDX, HOLDY = tx - 3, ty + 12 sx2, sy2 = arc(PX_, PY_, HOLDX, HOLDY, e, 14) star_out = true; ex, ey = sx2, sy2 elseif t < 12.4 then a, bend, sag = 58, 2.4, 0 local tx, ty = tip_of(58, 15) sx2, sy2 = tx - 3 + 1.6 * sin((t - 10.8) * 5) * (1 - (t - 10.8) / 1.6), ty + 12 + 0.4 * sin((t - 10.8) * 5) star_out = true; ex, ey = sx2, sy2 elseif t < 13.8 then local e = sm((t - 12.4) / 1.4) a = lerp(58, 36, e); bend = lerp(2.4, 0.8, e); sag = 4 * e local tx, ty = tip_of(58, 15) sx2, sy2 = arc(tx - 3, ty + 12, S0X, S0Y, e, 8) star_out = true local tx2, ty2 = tip_of(a, 15) ex, ey = tx2 - 1, ty2 + lerp(12, 8, e) else local e = sm((t - 13.8) / 1.6) local tx, ty = tip_of(36, 15) ex, ey = lerp(tx - 1, FX, e), lerp(ty + 8, FY, e) - 5 * 4 * e * (1 - e); sag = 3 * e end local tx, ty = tip_of(a, 15) local m1x, m1y = hx + (tx - hx) * 0.5, hy + (ty - hy) * 0.5 + bend * 0.35 local tyb = ty + bend wu(hx, hy, m1x, m1y, 140, 160, 206, 1) wu(m1x, m1y, tx, tyb, 140, 160, 206, 1) local px0, py0 = tx, tyb for k = 1, 8 do local u = k / 8 local x1, y1 = lerp(tx, ex, u), lerp(tyb, ey, u) + sag * 4 * u * (1 - u) wu(px0, py0, x1, y1, 170, 196, 240, 0.8) px0, py0 = x1, y1 end if not star_out then local dip = (t >= 6.6 and t < 7.4) and 0.3 or 1 blend(rnd(ex), rnd(ey) - 1, 210, 228, 255, dip); blend(rnd(ex), rnd(ey), 150, 176, 230, dip) else local glow = (t > 10.8 and t < 12.4) and 1 or 0.85 star_shape(sx2, sy2, glow, true) end end