pico8-0hh1/states/loading.lua

124 lines
2.6 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",
"bOOTING VIRTUAL MACHINE",
"cOLLECTING PIXELS TO BUILD TILES",
"lOADING RAY-TRACING ALGORITHM",
"sWEEPING PARTICLES",
"pRESSURIZING USER INTERFACE",
"rECOMPUTING BOARD MATRIX"
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())
-- Remove tiles until randomness is unavoidable
local previous = {board:get_tiles_copy()} -- initial state
while true do
board:set_tiles(previous[#previous])
-- remove a random tile
repeat
removing_tile += 1
until removing_tile == 100 or board:get_tiles_copy()[iremoving_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)
local w = board:get_size()
local tiles = board:get_tiles_copy()
for k,v in ipairs(tiles) do
if k<removing_tile and v > 0 then
local x,y = board:draw_coords(k)
local color = v == BLUE and 12 or 8
if color == 1 then fillp() else fillp() end
rectfill2(x, y, w, w, color)
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()
messages_x -= 2
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)
if not loaded then
print(messages_str, messages_x, 50, 7, 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