From c5db9f717ff9e6d19b605c64a1054c2803192ddb Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Thu, 2 Jun 2022 22:39:23 +0200 Subject: [PATCH] Loading & messages --- 0hh1.p8 | 1 + board.lua | 38 ++++++++-------- coroutines.lua | 29 +++++------- main.lua | 5 ++- states/game.lua | 53 +++------------------- states/loading.lua | 108 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 145 insertions(+), 89 deletions(-) create mode 100644 states/loading.lua diff --git a/0hh1.p8 b/0hh1.p8 index ac0aecd..89ea964 100644 --- a/0hh1.p8 +++ b/0hh1.p8 @@ -13,6 +13,7 @@ poke4(0x5700,unpack(split"0x0000.0000,0x0000.0000,0x0202.0202,0x0000.0200,0x0000 #include coroutines.lua #include ui.lua #include states/menu.lua +#include states/loading.lua #include states/game.lua #include states/rules.lua diff --git a/board.lua b/board.lua index 8cecd74..8ae2e08 100644 --- a/board.lua +++ b/board.lua @@ -7,28 +7,26 @@ function Board.new() local margin = 4 local padding = 1 - local t = [[ -0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 1, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0 - ]] - a = {foo = 3} - local tiles = split(t) + local reset = function() + local t = [[ + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0 + ]] + tiles = split(t) + end + + reset() return { - reset = function(self) - local t = [[ - 0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0 - ]] - tiles = split(t) - end, + reset = reset, get_tile = function(self, idx) return tiles[idx] diff --git a/coroutines.lua b/coroutines.lua index 52603e2..749f207 100644 --- a/coroutines.lua +++ b/coroutines.lua @@ -1,33 +1,24 @@ local coroutines={} -local _cocounter = 0 -local _cocreate = cocreate - -function cocreate(fn) - _cocounter += 1 - k = k or _cocounter - coroutines[_cocounter] = _cocreate(fn, k) - return coroutines[_cocounter], _cocounter +-- starts a coroutine and returns its key +function startcoroutine(fn, key) + add(coroutines, fn) end -function getcoroutine(key) - return coroutines[key] +function stepcoroutine(fn) + coresume(fn) end -function stepcoroutine(key) - coresume(coroutines[key]) -end - -function stopcoroutine(key) - coroutines[key] = nil +function stopcoroutine(fn) + del(coroutines, fn) end function _coresolve() - for k,co in pairs(coroutines) do + for co in all(coroutines) do if costatus(co)!='dead' then - coresume(co) + assert(coresume(co)) else - stopcoroutine(k) + stopcoroutine(co) end end end diff --git a/main.lua b/main.lua index 1956b4b..559d687 100644 --- a/main.lua +++ b/main.lua @@ -142,10 +142,11 @@ function _init() states = { rules = state_rules(), menu = state_menu(), - game = state_game() + game = state_game(), + loading = state_loading(), } - set_state(states.game) + set_state(states.loading) end diff --git a/states/game.lua b/states/game.lua index 4b809ad..0aa8433 100644 --- a/states/game.lua +++ b/states/game.lua @@ -1,61 +1,18 @@ function state_game() - local board = Board.new() + local board local selected_id = 1 local function drawSelectedTile() local x, y = board:draw_coords(selected_id) local w = board:get_tile_width() + fillp(▒) rect2(x-1, y-1, w+2, w+2, 6) + fillp(█) end - local function create_board() - printh("a: " .. time()) - - repeat - board:reset() - repeat - board:solve_step(true) - yield() - until board:is_complete() - until board:is_valid() - - printh("b: " .. time()) - -- Remove tiles until randomness is unavoidable - local previous = {board:get_tiles_copy()} -- initial state - local i = 0 - while true do - board:set_tiles(previous[#previous]) - -- remove a random tile - repeat - i += 1 - until i == 100 or board:get_tiles_copy()[i] ~= 0 - if i == 100 then - break - end - - board:fill(i, 0) - add(previous, board:get_tiles_copy()) - - -- try to solve the board - local solved = "" - yield() - repeat - solved = board:solve_step() - until board:is_complete() or solved == "invalid" - if solved == "invalid" then - deli(previous) - end - end -- end while - printh("c: " .. time()) - - board:set_tiles(previous[#previous]) - printh(board:tostring()) - printh(count(board:get_tiles_copy(), 0).." zeroes") - end - - local function _enter() - cocreate(create_board) + local function _enter(_board) + board = _board end local function _draw() diff --git a/states/loading.lua b/states/loading.lua new file mode 100644 index 0000000..6291ca2 --- /dev/null +++ b/states/loading.lua @@ -0,0 +1,108 @@ +function state_loading() + local board = Board.new() + + local spinner = split"-,\\,|,/" + local loading_messages = { + "lOADING PATHFINDING ALGORITHM", + "rETICULATING SPLINES", + "aLLOCATING MEMORY BANDWIDTH", + "iNITIALIZING HARDWARE", + "sPINNING UP ai AGENT", + "PINGING aZURE AGENT POOL", + "bOOTING VIRTUAL MACHINE", + "cOUNTING PIXELS", + "cOLORING TILES", + "tIP: THIS GAME RUNS BETTER ON iNTERNET eXPLORER 6", + "lOADING A NEW BOARD", + "iF YOU CAN'T SOLVE A BOARD, ASK HELP FROM AN ADULT", + "lOADING RAY-TRACING SETTINGS", + "aDJUSTING DIFFICULTY FOR YOUR PLAY STYLE", + } + + local message = "" + local messages_str = "" + local messages_x = 128 + local loaded = false + + local create_board = cocreate(function() + repeat + board:reset() + repeat + board:solve_step(true) + yield() + until board:is_complete() + until board:is_valid() + + printh("b: " .. time()) + -- Remove tiles until randomness is unavoidable + local previous = {board:get_tiles_copy()} -- initial state + local i = 0 + while true do + board:set_tiles(previous[#previous]) + -- remove a random tile + repeat + i += 1 + until i == 100 or board:get_tiles_copy()[i] ~= 0 + if i == 100 then + break + end + + board:fill(i, 0) + add(previous, board:get_tiles_copy()) + + -- try to solve the board + local solved = "" + yield() + repeat + solved = board:solve_step() + until board:is_complete() or solved == "invalid" + if solved == "invalid" then + deli(previous) + end + end -- end while + printh("c: " .. time()) + + board:set_tiles(previous[#previous]) + printh(board:tostring()) + printh(count(board:get_tiles_copy(), 0).." zeroes") + + loaded = true + end) + + local function _enter() + loaded = false + + local copy_messages = copy(loading_messages) + messages_str = "" + messages_x = 128 + while #copy_messages > 0 do + local m = rnd(copy_messages) + messages_str ..= m .. " " + del(copy_messages, m) + end + messages_str ..= "[END OF MESSAGES, THANK YOU FOR YOUR PATIENCE]" + + custom_font() + startcoroutine(create_board) + end + + local function _update() + messages_x -= 2 + if loaded then + set_state(states.game, board) + end + end + + local function _draw() + cls() + local l = print(message, 0, -100) + print(messages_str, messages_x, 50, 7) + print(spinner[t()\.25%4+1], 120, 120) + end + + return { + _enter = _enter, + _update = _update, + _draw = _draw, + } +end \ No newline at end of file