pico8-0hh1/states/loading.lua

132 lines
2.7 KiB
Lua
Raw Normal View History

2022-06-02 22:39:23 +02:00
function state_loading()
local board = Board.new()
local spinner = split"-,\\,|,/"
local loading_messages = {
"rETICULATING SPLINES",
"aLLOCATING RESSOURCES",
"eMULATING HARDWARE",
"uNCLOGGING MEMORY BUS",
2022-06-02 22:39:23 +02:00
"sPINNING UP ai AGENT",
2022-06-03 23:31:26 +02:00
"sIDE-STEPPING VIRTUAL MACHINE",
"iNSTALLING BACKTRACKING WIZARD",
"eXFOLIATING PIXELS",
"sCAFFOLDING RAY-TRACING ALGORITHM",
"sWEEPING PARTICLES",
"pRESSURIZING USER INTERFACE",
2022-06-03 23:31:26 +02:00
"sERIALIZING BOARD MATRIX",
"bACKPORTING e2e ENCRYPTION",
2022-06-02 22:39:23 +02:00
}
local message = ""
local messages_str = ""
local messages_x = 128
local loaded = false
local removing_tile = 0
2022-06-02 22:39:23 +02:00
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())
2022-06-03 23:31:26 +02:00
-- Remove tiles that can be removed
2022-06-02 22:39:23 +02:00
local previous = {board:get_tiles_copy()} -- initial state
while true do
2022-06-03 23:31:26 +02:00
2022-06-02 22:39:23 +02:00
board:set_tiles(previous[#previous])
-- remove a random tile
repeat
removing_tile += 1
2022-06-03 23:31:26 +02:00
until removing_tile == 100 or board:get_tiles_copy()[removing_tile] ~= 0
if removing_tile == 100 then
2022-06-02 22:39:23 +02:00
break
end
board:fill(removing_tile, 0)
2022-06-02 22:39:23 +02:00
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 draw_tiles(board)
2022-06-03 23:31:26 +02:00
local w = board:get_size()
local tiles = board:get_tiles_copy()
for k,v in ipairs(tiles) do
2022-06-03 23:31:26 +02:00
if k < removing_tile then
board:draw_tile(k)
end
end
end
2022-06-02 22:39:23 +02:00
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 ..= "tHANK YOU FOR YOUR PATIENCE"
2022-06-02 22:39:23 +02:00
custom_font()
startcoroutine(create_board)
end
local function _update()
2022-06-03 23:31:26 +02:00
board:lock_tiles()
messages_x -= 5
2022-06-02 22:39:23 +02:00
if loaded then
set_state(states.game, board)
end
end
local function _draw()
cls()
board:draw_bg()
draw_tiles(board)
2022-06-02 22:39:23 +02:00
local l = print(message, 0, -100)
2022-06-03 23:31:26 +02:00
local y = 118+removing_tile/8
local colors = {7,6,5,5}
local ci = removing_tile\20+1
local c = colors[ci]
if not loaded then
2022-06-03 23:31:26 +02:00
rectfill2(0,y-2,127,10,0)
print(messages_str, messages_x, y, c, 0)
-- print(spinner[t()\.25%4+1], 120, 120)
end
2022-06-02 22:39:23 +02:00
end
return {
_enter = _enter,
_update = _update,
_draw = _draw,
}
end