info = { type = "image", title = "Clover run", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A leprechaun runs through a clover meadow", api = 33, } local tr = ({ sk = { ["Coat colour"] = "Farba srsti", ["Rainbow"] = "Dúha", ["Gold coins"] = "Zlaté mince" }, cs = { ["Coat colour"] = "Barva srsti", ["Rainbow"] = "Duha", ["Gold coins"] = "Zlaté mince" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "coat", label = T("Coat colour"), type = "color", default = "#2EAA4A" }, { key = "rainbow", label = T("Rainbow"), type = "bool", default = true }, { key = "coins", label = T("Gold coins"), type = "bool", default = true }, } local rect, px = gfx.rect, gfx.px 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 STEP_HZ = 30 local SKY = { 112, 192, 255 } local CLOUD = { 244, 250, 255 } local SUN, SUN_R = { 255, 236, 120 }, { 255, 206, 90 } local HILL1, HILL2 = { 136, 210, 170 }, { 84, 176, 112 } local BUSH, BUSH_L = { 34, 128, 66 }, { 70, 170, 84 } local GRASS, GRASS_D, GRASS_L = { 70, 176, 58 }, { 44, 134, 48 }, { 140, 226, 100 } local RAIN = { { 236, 50, 60 }, { 255, 150, 40 }, { 255, 230, 60 }, { 70, 200, 80 }, { 60, 130, 240 }, { 150, 80, 220 } } local SKY_H = 47 local NEAR_Y = 47 local HEAD = { ".....GGGGG......", ".....GGGGG......", ".....GGGGG......", ".....KKYKK......", "...GGGGGGGGGG...", ".....SSSSSS.....", ".....SSSSKSS....", "....RSSSSSSSS...", "....RRRSSSRRR...", "....RRRRRRRRR...", ".....RRRRRRR....", "......RRRRR.....", ".....CCWWCC.....", "....CCCCCCCC....", "....CCCCCCCC....", "....KKKKYKKK....", "....CCCCCCCC....", ".....LLLLLL.....", } local LEGS = { { "....lll..LLL....", "...lll....LLL...", "..WWW......WWW..", ".NNN.......NNYN.", "................" }, { "...lllLLL.......", "..ll..LLL.......", ".NN...LLL.......", "......WWW.......", "......NNYN......" }, { "....LLL..lll....", "...LLL....lll...", "..WWW......WWW..", ".NNN.......NNYN.", "................" }, { "...LLLlll.......", "..LL..lll.......", ".NN...lll.......", "......WWW.......", "......NNYN......" }, } local ARMS = { { { 4,13,"D", 3,14,"D", 2,15,"D", 1,16,"S", 2,16,"S" }, { 12,13,"F", 13,12,"F", 14,11,"S" } }, { { 8,13,"D", 8,14,"D", 9,15,"D", 9,16,"S" }, {} }, { { 11,13,"D", 12,12,"D", 13,11,"D", 14,10,"S", 14,11,"S" }, { 3,14,"F", 2,15,"F", 1,16,"S" } }, { { 8,13,"D", 8,14,"D", 9,15,"D", 9,16,"S" }, {} }, } local MAP = { ["."] = "\0", G = "\1", D = "\2", K = "\3", Y = "\4", S = "\5", R = "\6", C = "\7", W = "\8", L = "\9", l = "\10", F = "\11", N = "\12" } local RW, RH = 16, 23 local RUN = {} do local function put(g, x, y, ch, empty) local r = g[y + 1] if empty and r:sub(x + 1, x + 1) ~= "." then return end g[y + 1] = r:sub(1, x) .. ch .. r:sub(x + 2) end for f = 1, 4 do local g = {} for y = 1, RH do g[y] = y <= #HEAD and HEAD[y] or LEGS[f][y - #HEAD] end local far, near = ARMS[f][2], ARMS[f][1] for k = 1, #far, 3 do put(g, far[k], far[k + 1], far[k + 2], true) end for k = 1, #near, 3 do put(g, near[k], near[k + 1], near[k + 2], false) end for y = 1, RH do g[y] = g[y]:gsub(".", MAP) end RUN[f] = table.concat(g) end end local PAL = { 0, 0, 0, 46, 170, 74, 28, 104, 46, 30, 22, 24, 252, 206, 60, 252, 196, 158, 232, 112, 34, 46, 170, 74, 246, 246, 240, 24, 92, 44, 14, 60, 32, 22, 84, 40, 44, 30, 30 } local SKY0, SKY1, NEAR = nil, nil, nil local function c3(c) return c[1], c[2], c[3] end local function cloud(x, y, w) rect(x + 2, y, w - 5, 1, c3(CLOUD)); rect(x, y + 1, w, 2, c3(CLOUD)); rect(x + 3, y - 1, 4, 1, c3(CLOUD)) end local function hills() for x = 0, W - 1 do local a = floor(33.5 + 3 * sin(x * 0.08 + 1.0) + 1.5 * sin(x * 0.21)) rect(x, a, 1, SKY_H - a, c3(HILL1)) local b = floor(39.5 + 2.5 * sin(x * 0.12 + 3.0) + sin(x * 0.31)) rect(x, b, 1, SKY_H - b, c3(HILL2)) end end local function build_sky(rainbow) rect(0, 0, W, SKY_H, c3(SKY)) for y = -4, 4 do for x = -4, 4 do local d = x * x + y * y if d <= 9 then px(53 + x, 9 + y, c3(SUN)) elseif d <= 18 then px(53 + x, 9 + y, c3(SUN_R)) end end end if rainbow then for y = 14, SKY_H - 1 do local dy = y - 52 for x = 0, W - 1 do local dx = x - 29.5 local d = sqrt(dx * dx + dy * dy) local k = floor((36 - d) / 2) if k >= 0 and k < 6 then px(x, y, c3(RAIN[k + 1])) end end end end cloud(4, 8, 13); cloud(27, 4, 10); cloud(40, 18, 12) hills() return gfx.snapshot(0, 0, W, SKY_H) end local function clover(x, y) for o = 0, W, W do local X = x - o rect(X + 1, y, 1, 1, c3(GRASS_L)); rect(X + 3, y, 1, 1, c3(GRASS_L)) rect(X, y + 1, 2, 1, c3(GRASS_L)); rect(X + 3, y + 1, 2, 1, c3(GRASS_L)) rect(X + 2, y + 2, 1, 1, c3(GRASS_D)) rect(X, y + 3, 2, 1, c3(GRASS_L)); rect(X + 3, y + 3, 2, 1, c3(GRASS_L)) rect(X + 1, y + 4, 1, 1, c3(GRASS_L)); rect(X + 3, y + 4, 1, 1, c3(GRASS_L)) rect(X + 4, y + 5, 1, 1, c3(GRASS_D)) end end local function flower(x, y, c) for o = 0, W, W do local X = x - o rect(X + 1, y, 1, 3, c[1], c[2], c[3]); rect(X, y + 1, 3, 1, c[1], c[2], c[3]) rect(X + 1, y + 1, 1, 1, 255, 220, 60) rect(X + 1, y + 3, 1, 2, c3(GRASS_D)) end end local function shroom(x, y) for o = 0, W, W do local X = x - o rect(X + 1, y, 4, 1, 220, 40, 40); rect(X, y + 1, 6, 2, 220, 40, 40) rect(X + 1, y + 1, 1, 1, 255, 250, 240); rect(X + 4, y + 2, 1, 1, 255, 250, 240) rect(X + 3, y, 1, 1, 255, 250, 240) rect(X + 2, y + 3, 2, 3, 246, 236, 214) end end local function build_near() rect(0, NEAR_Y, W, H - NEAR_Y, c3(GRASS)) rect(0, NEAR_Y, W, 1, c3(GRASS_L)) for i = 0, 23 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), NEAR_Y + 2 + umod(h >> 8, 15) rect(x, y, 1, 2, c3(GRASS_D)); rect(x + 1, y + 1, 1, 1, c3(GRASS_D)) end clover(4, 50); clover(37, 57); clover(55, 49); clover(20, 58) flower(14, 49, { 255, 255, 255 }); flower(46, 52, { 190, 120, 255 }) flower(29, 50, { 255, 110, 160 }); flower(60, 57, { 255, 255, 255 }); flower(9, 58, { 120, 170, 255 }) shroom(24, 52); shroom(50, 56) NEAR = gfx.snapshot(0, NEAR_Y, W, H - NEAR_Y) end local BUSHES = { 0, 14, 7, 22, 9, 5, 38, 16, 9, 61, 11, 6, 78, 13, 8 } local function draw_bushes(t) for k = 1, #BUSHES, 3 do local x = (BUSHES[k] - t) % 96 - 16 local w, h = BUSHES[k + 1], BUSHES[k + 2] local y = NEAR_Y - h rect(x + 2, y, w - 4, 1, c3(BUSH_L)) rect(x + 1, y + 1, w - 2, 1, c3(BUSH)) rect(x, y + 2, w, h - 2, c3(BUSH)) rect(x + 2, y + 1, 2, 1, c3(BUSH_L)) px(x + w // 2, y + 3, 255, 236, 120); px(x + 2, y + h - 2, 255, 140, 190) end end local RAIN_ON = true local COINS_ON = true function on_settings() local c = cfg.coat or { r = 46, g = 170, b = 74 } PAL[4], PAL[5], PAL[6] = c.r, c.g, c.b PAL[22], PAL[23], PAL[24] = c.r, c.g, c.b PAL[7], PAL[8], PAL[9] = c.r * 6 // 10, c.g * 6 // 10, c.b * 6 // 10 PAL[34], PAL[35], PAL[36] = c.r // 2, c.g // 2, c.b // 2 RAIN_ON = cfg.rainbow ~= false COINS_ON = cfg.coins ~= false end local COINW = { 5, 3, 1, 3 } local function draw_coin(t) local c = t % (9 * STEP_HZ) if c >= 2 * STEP_HZ + 20 then return end local p = c / (2 * STEP_HZ + 20) local x = floor(70 - p * 80 + 0.5) local y = floor(16 - 10 * sin(p * 3.14159) + 0.5) local w = COINW[(t // 3) % 4 + 1] local x0 = x + (5 - w) // 2 rect(x0, y + 1, w, 3, 250, 196, 40) if w > 2 then rect(x0 + 1, y, w - 2, 1, 250, 196, 40); rect(x0 + 1, y + 4, w - 2, 1, 250, 196, 40) end rect(x0 + (w > 1 and 1 or 0), y + 1, 1, 2, 255, 248, 190) if (t // 2) % 2 == 0 then px(x + 7, y + 2, 255, 240, 160) end end function render() if not SKY0 then SKY1 = build_sky(true) SKY0 = build_sky(false) build_near() end local t = frame * STEP_HZ // FPS gfx.blit_rgb(SKY0, 0, 0, W, SKY_H) if RAIN_ON then local c = t % (20 * STEP_HZ) local a, b = 0, 0 if c < STEP_HZ then a, b = 0, c * W // STEP_HZ elseif c < 6 * STEP_HZ then a, b = 0, W elseif c < 7 * STEP_HZ then a, b = (c - 6 * STEP_HZ) * W // STEP_HZ, W end if b > a then gfx.clip(a, 0, b - a, SKY_H) gfx.blit_rgb(SKY1, 0, 0, W, SKY_H) gfx.clip() end end draw_bushes(t) local o = (t * 2) % W gfx.blit_rgb(NEAR, -o, NEAR_Y, W, H - NEAR_Y) gfx.blit_rgb(NEAR, W - o, NEAR_Y, W, H - NEAR_Y) if COINS_ON then draw_coin(t) end local f = (t // 3) % 4 + 1 local bob = (f % 2 == 1) and 1 or 0 rect(20, 57, 13, 1, 44, 134, 48) gfx.blit_indexed(RUN[f], PAL, 18, 34 + bob, RW, RH, RW, RH, true) end