pico8-0hh1/states/loading.lua
2022-07-03 19:09:15 +02:00

134 lines
2.9 KiB
Lua

function state_loading()
local board = Board.new()
local spinner = split"-,\\,|,/"
local loading_messages = {
"rETICULATING SPLINES",
"aLLOCATING RESSOURCES",
"eMULATING HARDWARE",
"uNCLOGGING MEMORY BUS",
"sPINNING UP ai AGENT",
"sIDE-STEPPING VIRTUAL MACHINE",
"iNSTALLING BACKTRACKING WIZARD",
"eXFOLIATING PIXELS",
"sCAFFOLDING RAY-TRACING ALGORITHM",
"sWEEPING PARTICLES",
"pRESSURIZING USER INTERFACE",
"sHAKING RED AND BLUE PAINT BUCKETS",
"gATHERING GRAVITY",
"sERIALIZING BOARD MATRIX",
"bACKPORTING e2e ENCRYPTION",
}
local message = ""
local messages_str = ""
local messages_x = 128
local loaded = false
local removing_tile = 0
local create_board = cocreate(function()
local start = time()
repeat
board:reset()
repeat
board:solve_step(true)
yield()
until board:is_complete()
until board:is_valid()
-- Remove tiles that can be removed
local previous = {board:get_tiles_copy()} -- initial state
local size = board:get_size()*board:get_size()
while true do
board:set_tiles(previous[#previous])
-- remove a random tile
repeat
removing_tile += 1
until removing_tile == size or board:get_tiles_copy()[removing_tile] ~= 0
if removing_tile == size then
break
end
board:fill(removing_tile, 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("board generated in "..time()-start.." seconds")
board:set_tiles(previous[#previous])
printh(board:tostring())
printh(count(board:get_tiles_copy(), 0).." zeroes")
loaded = true
end)
local function draw_tiles(board)
local w = board:get_size()
local tiles = board:get_tiles_copy()
for k,v in ipairs(tiles) do
if k < removing_tile then
board:draw_tile(k)
end
end
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 ..= "tHANK YOU FOR YOUR PATIENCE"
custom_font()
startcoroutine(create_board)
end
local function _update()
board:lock_tiles()
messages_x -= 4
if loaded then
set_state(states.game, board)
end
end
local function _draw()
cls()
board:draw_bg()
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 colors = {7,6,5,5}
local ci = removing_tile\20+1
local c = colors[ci]
if not loaded then
rectfill2(0,y-2,127,10,0)
print(messages_str, messages_x, y, c, 0)
-- print(spinner[t()\.25%4+1], 120, 120)
end
end
return {
_enter = _enter,
_update = _update,
_draw = _draw,
}
end