pico8-0hh1/states/loading.lua

152 lines
3.3 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-07-01 20:20:57 +02:00
"sHAKING RED AND BLUE PAINT BUCKETS",
"gATHERING GRAVITY",
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
2022-07-08 22:47:26 +02:00
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
2022-06-02 22:39:23 +02:00
local create_board = cocreate(function()
2022-07-03 19:09:15 +02:00
local start = time()
2022-06-02 22:39:23 +02:00
repeat
board:reset()
repeat
board:solve_step(true)
yield()
until board:is_complete()
until board:is_valid()
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
2022-07-08 22:47:26 +02:00
done = {}
2022-06-02 22:39:23 +02:00
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-07-08 22:47:26 +02:00
if removing_tile > size then
removing_tile = 1
end
2022-07-01 19:50:08 +02:00
until removing_tile == size or board:get_tiles_copy()[removing_tile] ~= 0
2022-07-08 22:47:26 +02:00
if removing_tile == og_rt then
2022-06-02 22:39:23 +02:00
break
end
board:fill(removing_tile, 0)
2022-07-08 22:47:26 +02:00
add(done, removing_tile)
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()
2022-07-07 23:06:28 +02:00
if solved == "valid" then
amplitude -= .125
end
2022-06-02 22:39:23 +02:00
until board:is_complete() or solved == "invalid"
2022-07-07 23:06:28 +02:00
2022-06-02 22:39:23 +02:00
if solved == "invalid" then
deli(previous)
end
end -- end while
2022-07-03 19:09:15 +02:00
printh("board generated in "..time()-start.." seconds")
2022-07-07 23:06:28 +02:00
startcoroutine(cocreate(function()
repeat
amplitude -= .5
yield()
until amplitude <= 0
end))
2022-06-02 22:39:23 +02:00
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
2022-07-08 22:47:26 +02:00
if v==0 or contains(done, k) then
2022-06-03 23:31:26 +02:00
board:draw_tile(k)
end
end
end
2022-06-02 22:39:23 +02:00
local function _enter()
2022-07-07 23:06:28 +02:00
amplitude = 64
2022-06-02 22:39:23 +02:00
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()
2022-07-01 20:20:57 +02:00
messages_x -= 4
2022-06-02 22:39:23 +02:00
if loaded then
set_state(states.game, board)
end
end
2022-07-07 23:06:28 +02:00
local function _draw()
2022-06-02 22:39:23 +02:00
cls()
2022-07-07 23:06:28 +02:00
local x,y = board:draw_coords(1)
draw_animated_bg(x)
-- board:draw_bg()
draw_tiles(board)
2022-07-01 19:50:08 +02:00
local s = board:get_size()*board:get_size()
2022-06-02 22:39:23 +02:00
local l = print(message, 0, -100)
2022-07-01 19:50:08 +02:00
local y = 118+(removing_tile/s)*100/8
2022-06-03 23:31:26 +02:00
local ci = removing_tile\20+1
2022-07-08 22:47:26 +02:00
-- 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
2022-06-02 22:39:23 +02:00
end
return {
_enter = _enter,
_update = _update,
_draw = _draw,
}
end