info = { type = "image", title = "Love tunnel", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Two swans kiss before a tunnel of hearts", api = 33, } local tr = ({ sk = { ["Tunnel speed"] = "Rýchlosť tunela", ["slow"] = "pomaly", ["normal"] = "normálne", ["fast"] = "rýchlo", ["Floating hearts"] = "Plávajúce srdiečka" }, cs = { ["Tunnel speed"] = "Rychlost tunelu", ["slow"] = "pomalu", ["normal"] = "normálně", ["fast"] = "rychle", ["Floating hearts"] = "Plovoucí srdíčka" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Tunnel speed"), type = "int", default = 1, opts = { T("slow"), T("normal"), T("fast") } }, { key = "hearts", label = T("Floating hearts"), type = "int", min = 0, max = 6, default = 4 }, } local rect, blend = gfx.rect, gfx.blend local floor, sqrt, log = math.floor, math.sqrt, math.log local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local WY = 50 local CX, CY = 31.5, 23.5 local SUB, NC = 8, 4 local BANDS = { { 255, 72, 146 }, { 222, 30, 66 }, { 255, 156, 206 }, { 176, 40, 168 } } local LINE = { 92, 10, 64 } local HOLE = { 120, 24, 84 } local S0, RATIO = 1.6, 1.30 local GRID, NQ = nil, 0 local PAL = { 0, 0, 0, HOLE[1], HOLE[2], HOLE[3] } do local AX, AY, A = 0.45, -0.30, 0.45 * 0.45 + 0.30 * 0.30 - 0.60 * 0.60 local K = 1.2 local Q = SUB / log(RATIO) local LUT = {} for i = 0, 399 do local sv = i / 4 LUT[i] = sv < S0 and 1 or 2 + floor(Q * log(sv / S0)) end NQ = LUT[399] local rows, row = {}, {} local nA = -A local KX, PX, X2 = {}, {}, {} for x = 32, 63 do local ax = x + 0.5 - CX KX[x], PX[x], X2[x] = K * ax, ax * AX, ax * ax end for y = 0, WY - 1 do local dy = y + 0.5 - CY local dyAY, dy2, cap = dy * AY, dy * dy, -dy / 0.3 for x = 32, 63 do local s = KX[x] + dy if cap > s then s = cap end if s * 0.3 > dy then local pc = PX[x] + dyAY local sc = (sqrt(pc * pc + nA * (X2[x] + dy2)) - pc) / nA if sc < s then s = sc end end local v = LUT[s * 4 // 1] row[x + 1] = v row[64 - x] = v end rows[y + 1] = string.char(table.unpack(row, 1, 64)) end GRID = table.concat(rows) for v = 2, NQ do PAL[3 * v + 1], PAL[3 * v + 2], PAL[3 * v + 3] = 0, 0, 0 end end local SPD = 1 local function update_palette(t) local shift = t * SPD local cyc = SUB * NC for v = 2, NQ do local m = (v - shift) % cyc local c = (m % SUB) >= SUB - 2 and LINE or BANDS[m // SUB + 1] local k = 3 * v PAL[k + 1], PAL[k + 2], PAL[k + 3] = c[1], c[2], c[3] end end local SWAN_A = { "..........WWW...", ".........WWKWOO.", ".........WWW....", "..........WW....", "..........WW....", ".........WW.....", "........WW......", "........WW......", "W.......WW......", "WW.....WWWW.....", "WWWW.WWWWWWW....", ".WWWWGGWWWWWW...", "..WWWWGGGWWWW...", "....WWWWWWWW....", } local SWAN_B = { "...........WWW..", "..........WWKWOO", ".........WW.....", "........WW......", "........WW......", "........WW......", ".........WW.....", ".........WW.....", "W........WW.....", "WW.......WWW....", "WWWW.WWWWWWWW...", ".WWWWGGWWWWWWW..", "..WWWWGGGWWWWW..", "....WWWWWWWWW...", } local SWAN_C = { "......WWWW......", ".....WW..WWW....", "....WW.....WWK..", "....WW......WWOO", ".....WW.......O.", "......WW........", ".......WW.......", "........WW......", "W........WW.....", "WW........WW....", "WWWW..WWWWWWW...", ".WWWWGGWWWWWWWW.", "..WWWWGGGWWWWWWW", "....WWWWWWWWWWW.", } local MAP = { ["."] = "\0", W = "\1", G = "\2", K = "\3", O = "\4" } local function sprite(rows, mirror) local t = {} for i = 1, #rows do local s = mirror and rows[i]:reverse() or rows[i] t[i] = s:gsub(".", MAP) end return table.concat(t) end local SL = { sprite(SWAN_A), sprite(SWAN_B), sprite(SWAN_C) } local SR = { sprite(SWAN_A, true), sprite(SWAN_B, true), sprite(SWAN_C, true) } local PAL_W = { 0, 0, 0, 250, 250, 255, 186, 194, 232, 24, 22, 34, 255, 142, 40 } local PAL_K = { 0, 0, 0, 52, 48, 72, 104, 98, 140, 250, 250, 250, 236, 48, 60 } local PAL_RIM = { 0, 0, 0, 236, 214, 255, 236, 214, 255, 236, 214, 255, 236, 214, 255 } local SWY = WY - 11 local HCOL = { 1, 1, 0, 3, 1, 3, 0, 3, 1, 1 } local HX, HV, HP = {}, {}, {} for i = 1, 6 do local h = hash32(i * 0x9E3779B1) HV[i] = 0.30 + umod(h, 25) / 100.0 HP[i] = umod(h >> 8, 200) HX[i] = h end local NH = 4 function on_settings() SPD = (cfg.speed or 1) + 1 NH = cfg.hearts or 4 end local function heart_v(x, yf, r, g, b) local yi = floor(yf) local f = yf - yi for c = 0, 4 do local y0, n = HCOL[2 * c + 1], HCOL[2 * c + 2] local y = yi + y0 if n > 1 then rect(x + c, y + 1, 1, n - 1, r, g, b) end blend(x + c, y, r, g, b, 1 - f) blend(x + c, y + n, r, g, b, f) end end local function draw_hearts(t) local span = WY + 8 for i = 1, NH do local p = HP[i] + t * HV[i] local n = floor(p / span) local yf = WY - 2 - (p - n * span) local x = 2 + umod(hash32(HX[i] + n * 0x85EBCA6B), W - 8) heart_v(x, yf, 255, 228, 242) end end local RIP_A = { 4, 53, 6, 22, 56, 5, 40, 54, 7, 52, 59, 6, 12, 61, 5, 30, 62, 6 } local RIP_B = { 8, 54, 5, 26, 53, 6, 46, 56, 6, 2, 59, 6, 36, 60, 5, 54, 62, 5 } local function draw_water(t) rect(0, WY, W, H - WY, 56, 42, 150) rect(0, WY, W, 1, 120, 96, 214) local R = ((t // 12) % 2 == 0) and RIP_A or RIP_B for k = 1, #R, 3 do rect(R[k], R[k + 1], R[k + 2], 1, 118, 104, 220) end local o = (t // 12) % 2 rect(19 + o, WY + 3, 9, 1, 196, 196, 240); rect(21 - o, WY + 5, 6, 1, 196, 196, 240) rect(36 - o, WY + 3, 9, 1, 96, 88, 150); rect(38 + o, WY + 5, 6, 1, 96, 88, 150) end local KISS = { 0, 0, 0, 255, 255, 255 } local KH = string.char(0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0) function render() local t = frame * STEP_HZ // FPS update_palette(t) gfx.blit_indexed(GRID, PAL, 0, 0, W, WY, W, WY, false) draw_water(t) draw_hearts(t) local c = t % (6 * STEP_HZ) local p = 1 if c >= 70 and c < 82 then p = 2 elseif c >= 82 and c < 166 then p = 3 elseif c >= 166 then p = 2 end gfx.blit_indexed(SL[p], PAL_W, 16, SWY, 16, 14, 16, 14, true) gfx.blit_indexed(SR[p], PAL_RIM, 31, SWY, 16, 14, 16, 14, true) gfx.blit_indexed(SR[p], PAL_RIM, 33, SWY, 16, 14, 16, 14, true) gfx.blit_indexed(SR[p], PAL_RIM, 32, SWY - 1, 16, 14, 16, 14, true) gfx.blit_indexed(SR[p], PAL_RIM, 32, SWY + 1, 16, 14, 16, 14, true) gfx.blit_indexed(SR[p], PAL_K, 32, SWY, 16, 14, 16, 14, true) if p == 3 and c >= 92 then local y = SWY - 6 - (c < 96 and 0 or 2) gfx.blit_indexed(KH, KISS, 29, y, 5, 4, 5, 4, true) end end