Code formatting

This commit is contained in:
Simon Cambier 2023-10-07 19:12:53 +02:00
parent bc408aeeb3
commit cb74bdf740
16 changed files with 1066 additions and 1067 deletions

View File

@ -1,4 +1,4 @@
{
"editor.insertSpaces": false,
"editor.detectIndentation": false
"editor.insertSpaces": true,
"editor.detectIndentation": false,
}

View File

@ -2,7 +2,6 @@ pico-8 cartridge // http://www.pico-8.com
version 38
__lua__
-- Font M3X6 by daniel linssen
poke(0x5600, 4, 4, 7)
poke4(0x5700, unpack(split "0x0000.0000,0x0000.0000,0x0202.0202,0x0000.0200,0x0000.0505,0x0000.0000,0x0505.0705,0x0000.0507,0x0407.0106,0x0000.0203,0x0204.0100,0x0000.0401,0x0102.0502,0x0000.0305,0x0000.0102,0x0000.0000,0x0101.0102,0x0000.0201,0x0202.0201,0x0000.0102,0x0205.0000,0x0000.0005,0x0702.0000,0x0000.0002,0x0000.0000,0x0000.0102,0x0700.0000,0x0000.0000,0x0000.0000,0x0000.0200,0x0202.0404,0x0000.0101,0x0505.0506,0x0000.0305,0x0202.0302,0x0000.0702,0x0204.0403,0x0000.0701,0x0403.0403,0x0000.0304,0x0406.0505,0x0000.0404,0x0403.0107,0x0000.0304,0x0503.0106,0x0000.0605,0x0204.0407,0x0000.0202,0x0502.0506,0x0000.0305,0x0605.0503,0x0000.0304,0x0002.0000,0x0000.0002,0x0002.0000,0x0000.0102,0x0102.0400,0x0000.0402,0x0007.0000,0x0000.0007,0x0402.0100,0x0000.0102,0x0204.0403,0x0000.0200,0x0505.0506,0x0000.0601,0x0604.0300,0x0000.0705,0x0505.0301,0x0000.0705,0x0101.0600,0x0000.0701,0x0505.0604,0x0000.0705,0x0705.0600,0x0000.0601,0x0702.0204,0x0000.0202,0x0705.0600,0x0000.0304,0x0505.0301,0x0000.0505,0x0202.0002,0x0000.0202,0x0202.0002,0x0000.0102,0x0305.0101,0x0000.0505,0x0202.0202,0x0000.0402,0x0707.0300,0x0000.0507,0x0505.0300,0x0000.0505,0x0505.0600,0x0000.0305,0x0305.0700,0x0000.0101,0x0705.0600,0x0000.0404,0x0101.0600,0x0000.0101,0x0701.0600,0x0000.0304,0x0207.0202,0x0000.0202,0x0505.0500,0x0000.0705,0x0505.0500,0x0000.0205,0x0705.0500,0x0000.0507,0x0205.0500,0x0000.0505,0x0605.0500,0x0000.0304,0x0204.0700,0x0000.0701,0x0101.0103,0x0000.0301,0x0202.0101,0x0000.0404,0x0202.0203,0x0000.0302,0x0000.0502,0x0000.0000,0x0000.0000,0x0000.0403,0x0000.0201,0x0000.0000,0x0507.0506,0x0000.0505,0x0507.0503,0x0000.0705,0x0101.0106,0x0000.0701,0x0505.0503,0x0000.0305,0x0103.0107,0x0000.0701,0x0301.0106,0x0000.0101,0x0501.0106,0x0000.0705,0x0507.0505,0x0000.0505,0x0202.0207,0x0000.0702,0x0404.0407,0x0000.0304,0x0503.0505,0x0000.0505,0x0101.0101,0x0000.0701,0x0507.0705,0x0000.0505,0x0505.0503,0x0000.0505,0x0505.0506,0x0000.0305,0x0103.0507,0x0000.0101,0x0505.0506,0x0000.0403,0x0503.0507,0x0000.0505,0x0407.0106,0x0000.0304,0x0202.0207,0x0000.0202,0x0505.0505,0x0000.0705,0x0505.0505,0x0000.0205,0x0705.0505,0x0000.0507,0x0202.0505,0x0000.0505,0x0205.0505,0x0000.0202,0x0202.0407,0x0000.0701,0x0302.0204,0x0000.0402,0x0202.0202,0x0000.0202,0x0602.0201,0x0000.0102,0x0704.0000,0x0000.0001,0x0205.0200,0x0000.0000"))

7
bg.lua
View File

@ -14,7 +14,8 @@ function draw_animated_bg(startx)
a,
i,
b,
color)
color
)
end
-- horizontal lines
@ -26,10 +27,10 @@ function draw_animated_bg(startx)
i,
b,
i,
color)
color
)
end
end
function draw_bg_menu()
end

View File

@ -2,11 +2,14 @@ local Board = {}
function Board.new()
local debug = false
local width = board_size -- tiles
local tile_width = 8 -- pixels
local width = board_size
-- tiles
local tile_width = 8
-- pixels
local padding = 1
local tiles = {}
local locked = {} -- list of indexes
local locked = {}
-- list of indexes
local reset = function()
tiles = {}
@ -47,8 +50,7 @@ function Board.new()
draw_coords = function(self, x, y)
local margin = (128 - (tile_width + padding) * width) / 2 - padding
if not y then x, y = self:idx_xy(x) end
return margin + (x-1)*tile_width + (x-1)*padding,
margin + (y-1)*tile_width + (y-1)*padding
return margin + (x - 1) * tile_width + (x - 1) * padding, margin + (y - 1) * tile_width + (y - 1) * padding
end,
get_size = function(self)
@ -75,7 +77,8 @@ function Board.new()
to_color = YELLOW
elseif tiles[id] == YELLOW then
to_color = BLUE
else tiles[id] = 0
else
tiles[id] = 0
-- empty tile
end
if to_color then
@ -88,7 +91,7 @@ function Board.new()
get_rows = function(self)
local ret = {}
for i = 1, width do
add(ret, slice(tiles, ((i - 1) * width) + 1, i * width))
add(ret, slice(tiles, (i - 1) * width + 1, i * width))
end
return ret
end,
@ -102,10 +105,12 @@ function Board.new()
return ret
end,
--- Returns true if all tiles are filled
is_complete = function(self)
return count(tiles, 0) == 0
end,
--- Returns true if the board is valid (respects the rules)
is_valid = function(self)
return #self:get_issues() == 0
end,
@ -119,22 +124,19 @@ function Board.new()
-- check count
if filled and count(row, BLUE) ~= count(row, YELLOW) then
add(issues, { "row", "count", row, y })
if (debug) printh("uneven count on row "..y)
if (not details) return issues
if (debug) printh("uneven count on row " .. y) if (not details) return issues
end
-- check identical lines
for k, other in ipairs(rows) do
if filled and equal(other, row) and other ~= row then
add(issues, { "row", "identical", row, y, k })
if (debug) printh("equal rows "..k)
if (not details) return issues
if (debug) printh("equal rows " .. k) if (not details) return issues
end
end
-- check triples
if self:count_consecutives(row) > 2 then
add(issues, { "row", "triples", row, y })
if (debug) printh("triples")
if (not details) return issues
if (debug) printh("triples") if (not details) return issues
end
end
@ -145,22 +147,19 @@ function Board.new()
-- check count
if filled and count(col, BLUE) ~= count(col, YELLOW) then
add(issues, { "col", "count", col, x })
if (debug) printh("uneven count")
if (not details) return issues
if (debug) printh("uneven count") if (not details) return issues
end
-- check identical lines
for k, other in ipairs(cols) do
if filled and equal(other, col) and other ~= col then
add(issues, { "col", "identical", col, x, k })
if (debug) printh("equal cols")
if (not details) return issues
if (debug) printh("equal cols") if (not details) return issues
end
end
-- check triples
if self:count_consecutives(col) > 2 then
add(issues, { "col", "triples", col, x })
if (debug) printh("triples")
if (not details) return issues
if (debug) printh("triples") if (not details) return issues
end
end
return issues
@ -231,8 +230,7 @@ function Board.new()
-- Set a random color
local z = self:get_random_zero()
self:fill(z, rnd({ BLUE, YELLOW }))
if (debug) printh("!!!!!!!!!!!!!!!!! RANDOM FILL AT " .. z)
return "invalid"
if (debug) printh("!!!!!!!!!!!!!!!!! RANDOM FILL AT " .. z) return "invalid"
end
return (changed or self:is_complete()) and "valid" or "invalid"
end,
@ -265,8 +263,7 @@ function Board.new()
-- do the surrounding
for item in all(neighbors) do
if item[1] then
if (debug) printh("Surrounding at " .. item[1])
self:fill(item[1], tiles[item[2]], true)
if (debug) printh("Surrounding at " .. item[1]) self:fill(item[1], tiles[item[2]], true)
end
end
end
@ -277,14 +274,12 @@ function Board.new()
for idx, col in ipairs(tiles) do
local x, y = self:idx_xy(idx)
if col == 0 then
if x > 1 and x < width then
-- check horizontal
local prev = tiles[idx - 1]
local next = tiles[idx + 1]
if prev ~= 0 and prev == next then
if (debug) printh("Splitting at " .. idx)
self:fill(idx, prev, true)
if (debug) printh("Splitting at " .. idx) self:fill(idx, prev, true)
end
end
@ -293,8 +288,7 @@ function Board.new()
local prev = tiles[idx - width]
local next = tiles[idx + width]
if prev ~= 0 and prev == next then
if (debug) printh("Splitting at " .. idx)
self:fill(idx, prev, true)
if (debug) printh("Splitting at " .. idx) self:fill(idx, prev, true)
end
end
end
@ -329,7 +323,7 @@ function Board.new()
fill_row = function(self, y, color)
if (debug) printh("Filling line " .. y .. " in " .. (color == BLUE and "blue" or "yellow"))
local idx = self:xy_idx(1, y)
for i = idx, (idx+width-1) do
for i = idx, idx + width - 1 do
if self:get_tile(i) == 0 then
self:fill(i, color)
end
@ -442,6 +436,6 @@ function Board.new()
fillp()
self:draw_bg_tile(idx)
end
end,
end
}
end

View File

@ -2,7 +2,8 @@ local coroutines={}
-- starts a coroutine and saves it for future reference
function startcoroutine(co)
assert(tostr(co) == "[thread]") -- make sure that co is a coroutine, the return value of cocreate()
assert(tostr(co) == "[thread]")
-- make sure that co is a coroutine, the return value of cocreate()
add(coroutines, co)
end

View File

@ -31,5 +31,5 @@ local patterns = {
0b1111110111110101,
0b1111110111110111,
0b1111111111110111,
0b1111111111111111,
0b1111111111111111
}

View File

@ -15,7 +15,7 @@ function idx_xy(idx, width)
end
function xy_idx(x, y, width)
return ((y - 1) * width) + x
return (y - 1) * width + x
end
function map(tbl, f)
@ -30,9 +30,10 @@ function filter(tbl, f, keepindex)
local ret = {}
for k, v in pairs(tbl) do
if f(v) then
if keepindex
then ret[k] = v
else add(ret, v)
if keepindex then
ret[k] = v
else
add(ret, v)
end
end
end
@ -41,7 +42,7 @@ end
function slice(tbl, first, last, step)
local sliced = {}
for i = (first or 1), (last or #tbl), (step or 1) do
for i = first or 1, last or #tbl, step or 1 do
sliced[#sliced + 1] = tbl[i]
end
return sliced
@ -74,7 +75,6 @@ end
-- return c
-- end
-- function tostring(any)
-- if (type(any)~="table") return tostr(any)
-- local str = "{"
@ -85,7 +85,6 @@ end
-- return str.."}"
-- end
--
-- main loop
--
@ -101,7 +100,6 @@ function _init()
srand(date)
printh("seed " .. date)
frame_count = 0
states = {
@ -109,11 +107,10 @@ function _init()
menu = state_menu(),
loading = state_loading(),
game = state_game(),
endgame = state_endgame(),
endgame = state_endgame()
}
set_state(states.menu)
end
function _update60()

View File

@ -7,9 +7,11 @@ function state_endgame()
set_state(states.menu)
end
local btn_back = make_button({x=1, y=118, h=7, text="🅾️/C bACK TO mENU", color=8,
local btn_back = make_button({
x = 1, y = 118, h = 7, text = "🅾️/C bACK TO mENU", color = 8,
on_click = function() go_to_menu() end,
on_hover=function(btn) btn.color = 7 end})
on_hover = function(btn) btn.color = 7 end
})
local function _enter(_board)
board = _board
@ -29,7 +31,8 @@ function state_endgame()
-- wavy message
local offset = 0
for i = 1, #message, 1 do -- loop through every letter
for i = 1, #message, 1 do
-- loop through every letter
letter = sub(message, i, i) -- grab this letter
local st = t() + 0.125 * i -- create a modified time for this letter
print(letter, message_x + offset, 20 + sin(st * 0.5) * 10, 7) -- draw this letter

View File

@ -1,5 +1,4 @@
function state_game()
local board
local selected_id = 1
local timer_clue = 0
@ -93,7 +92,6 @@ function state_game()
if (x > size) x = 1
if (y < 1) y = size
if (y > size) y = 1
check_endgame()
end
@ -114,7 +112,8 @@ function state_game()
x, y = board:draw_coords((clue.pos - 1) * board:get_size() + 1)
x = -32
spr(19, x + clue.t % 144, y + 1 + sin(t()) * 2)
else -- col
else
-- col
x, y = board:draw_coords(clue.pos)
y = -32
spr(19, x + 2 + sin(t()) * 2, y + clue.t % 144)

View File

@ -20,7 +20,7 @@ function state_loading()
"sHAKING RED AND BLUE PAINT BUCKETS",
"gATHERING GRAVITY",
"sERIALIZING BOARD MATRIX",
"bACKPORTING e2e ENCRYPTION",
"bACKPORTING e2e ENCRYPTION"
}
local message = ""
@ -41,10 +41,10 @@ function state_loading()
until board:is_valid()
-- Remove tiles that can be removed
local previous = {board:get_tiles_copy()} -- initial state
local previous = { board:get_tiles_copy() }
-- initial state
done = {}
while true do
board:set_tiles(previous[#previous])
-- remove a random tile
repeat
@ -74,7 +74,8 @@ function state_loading()
if solved == "invalid" then
deli(previous)
end
end -- end while
end
-- end while
printh("board generated in " .. time() - start .. " seconds")
startcoroutine(cocreate(function()
repeat
@ -104,7 +105,8 @@ function state_loading()
printh("enter loading")
board = Board:new()
size = board:get_size() * board:get_size()
og_rt = rnd(size)\1+1 -- original removing tile
og_rt = rnd(size) \ 1 + 1
-- original removing tile
removing_tile = og_rt
amplitude = 64
@ -141,7 +143,7 @@ function state_loading()
draw_tiles(board)
local s = board:get_size() * board:get_size()
local l = print(message, 0, -100)
local y = 118+(removing_tile/s)*100/8
local y = 118 + removing_tile / s * 100 / 8
local ci = removing_tile \ 20 + 1
-- if not loaded then
-- rectfill2(0,y-2,127,10,0)
@ -153,6 +155,6 @@ function state_loading()
return {
_enter = _enter,
_update = _update,
_draw = _draw,
_draw = _draw
}
end

View File

@ -16,24 +16,26 @@ function state_menu()
{ "lARGE bOARD - 10X10", 10 }
}
for k, item in ipairs(game_sizes) do
add(buttons,
add(
buttons,
make_button({
x = 10, y = 10 + k * 10,
selected_color = 7,
text = item[1],
on_click = function() board_size = item[2] set_state(states.loading) end,
on_hover=function() selected=k end,
on_hover = function() selected = k end
})
)
end
local rulesId = #buttons + 1
add(buttons,
make_button({x=10, y=20+(#game_sizes+1)*10, w=30, text="rULES",
add(
buttons,
make_button({
x = 10, y = 20 + (#game_sizes + 1) * 10, w = 30, text = "rULES",
color = 13, selected_color = 12,
on_click = function() set_state(states.rules) end,
on_hover=function() selected=rulesId end,
on_hover = function() selected = rulesId end
})
)
@ -70,7 +72,6 @@ function state_menu()
buttons[selected]:on_click()
end
selected = mid(1, selected, #buttons)
end,
_draw = _draw

View File

@ -1,5 +1,4 @@
function state_rules()
local fade = split "0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,4,9,10,9,4,2,1"
local color = 1
@ -11,9 +10,11 @@ function state_rules()
set_state(states.menu)
end
local btn_back = make_button({x=1, y=118, w=30, h=7, text="🅾️/C mENU", color=8,
local btn_back = make_button({
x = 1, y = 118, w = 30, h = 7, text = "🅾️/C mENU", color = 8,
on_click = function() go_back() end,
on_hover=function(btn) btn.color = 7 end})
on_hover = function(btn) btn.color = 7 end
})
return {
_enter = function()
@ -61,7 +62,6 @@ function state_rules()
blink(x + 5, y + 2, 3, 9)
blink(x + 31, y + 2, 3, 9)
--------------
x = 44
@ -89,6 +89,6 @@ function state_rules()
--------------
btn_back:draw()
end,
end
}
end

7
ui.lua
View File

@ -1,5 +1,6 @@
function make_button(options)
local state = 0 -- 0 = normal, 1 = hovered, 2 = pressed
local state = 0
-- 0 = normal, 1 = hovered, 2 = pressed
local w = print(options.text, 0, -100)
return {
x = options.x,
@ -23,8 +24,8 @@ function make_button(options)
update = function(self)
self.color = self.ogColor
if mouse_x >= self.x and mouse_x <= self.x + self.w and
mouse_y >= self.y and mouse_y <= self.y + self.h then
if mouse_x >= self.x and mouse_x <= self.x + self.w
and mouse_y >= self.y and mouse_y <= self.y + self.h then
if stat(34) & 1 == 0 and state == 2 and self.on_click then
self.on_click(self)
end

View File

@ -27,7 +27,6 @@ function set_state(state, ...)
if gs and gs._enter then gs._enter(unpack(args)) end
end
--
-- Returns the indices of found occurences of `o` within `tbl`
--

View File

@ -24,7 +24,8 @@ function spawn_tile_transition(x, y, w, h, from_tint, to_tint)
local p = #patterns
local from_color = get_main_color(from_tint)
local to_color = get_main_color(to_tint)
add(overlays, {
add(
overlays, {
_draw = function(self)
fillp()
rectfill(x, y, x + w, y + h, from_color)
@ -35,5 +36,6 @@ function spawn_tile_transition(x, y, w, h, from_tint, to_tint)
del(overlays, self)
end
end
})
}
)
end