pico8-0hh1/states/loading.lua
2022-07-08 22:47:26 +02:00

152 lines
3.3 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 done = {}
local size = board:get_size()*board:get_size()
local og_rt = rnd(size)\1+1 -- original removing tile
local removing_tile = og_rt
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
done = {}
while true do
board:set_tiles(previous[#previous])
-- remove a random tile
repeat
removing_tile += 1
if removing_tile > size then
removing_tile = 1
end
until removing_tile == size or board:get_tiles_copy()[removing_tile] ~= 0
if removing_tile == og_rt then
break
end
board:fill(removing_tile, 0)
add(done, removing_tile)
add(previous, board:get_tiles_copy())
-- try to solve the board
local solved = ""
yield()
repeat
solved = board:solve_step()
if solved == "valid" then
amplitude -= .125
end
until board:is_complete() or solved == "invalid"
if solved == "invalid" then
deli(previous)
end
end -- end while
printh("board generated in "..time()-start.." seconds")
startcoroutine(cocreate(function()
repeat
amplitude -= .5
yield()
until amplitude <= 0
end))
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 v==0 or contains(done, k) then
board:draw_tile(k)
end
end
end
local function _enter()
amplitude = 64
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()
local x,y = board:draw_coords(1)
draw_animated_bg(x)
-- 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 ci = removing_tile\20+1
-- if not loaded then
-- rectfill2(0,y-2,127,10,0)
-- print(messages_str, messages_x, y, 3, 0)
-- -- print(spinner[t()\.25%4+1], 120, 120)
-- end
end
return {
_enter = _enter,
_update = _update,
_draw = _draw,
}
end