Optimize replay storing, saving and reading

This commit is contained in:
Squishy (C6H12O6+NaCl+H2O)
2024-05-17 20:17:10 +07:00
parent 64f85ae4e9
commit bc7cc6c193

View File

@@ -2,8 +2,6 @@ local Object = require 'libs.classic'
local bit = require("bit") local bit = require("bit")
local lualzw = require 'libs.lualzw' local lualzw = require 'libs.lualzw'
local bitser = require 'libs.bitser' local bitser = require 'libs.bitser'
require 'funcs'
require 'settings'
local playedReadySE = false local playedReadySE = false
local playedGoSE = false local playedGoSE = false
@@ -287,64 +285,55 @@ function GameMode:saveInputs()
inputfile:close() inputfile:close()
end end
local GAME_input_code_list = {
right = 1 ,
rotate_right2 = 2 ,
up = 4 ,
rotate_left = 8 ,
left = 16 ,
down = 32 ,
rotate_right = 64 ,
rotate_left2 = 128,
}
function GameMode:storeInput(input_set) function GameMode:storeInput(input_set)
local input_code_list = {
right = 1 ,
rotate_right2 = 2 ,
up = 4 ,
rotate_left = 8 ,
left = 16 ,
down = 32 ,
rotate_right = 64 ,
rotate_left2 = 128,
}
local current_frame_input = 0 local current_frame_input = 0
for key, code in pairs(GAME_input_code_list) do
for key, code in pairs(input_code_list) do
if input_set[key] then if input_set[key] then
current_frame_input = bit.bor(current_frame_input, code) current_frame_input = bit.bor(current_frame_input, code)
end end
end end
self.recorded_inputs[self.frames] = current_frame_input self.recorded_inputs[self.frames] = current_frame_input
end end
function GameMode:getReplayInputs(input_file) function GameMode:getReplayInputs(input_file)
local i = 1
local replay_inputs = {} local replay_inputs = {}
local reached_inputs = false local semicolon_position = string.find(input_file, ';', 1, true)
for _=1,1e99 do -- for _=1,1e99 do
local c = string.sub(input_file,_,_) -- local c = string.sub(input_file,_,_)
if c == ';' then -- if c == ';' then
reached_inputs = true -- reached_inputs = true
i = _ + 1 -- i = _ + 1
end -- end
if reached_inputs then break end -- if reached_inputs then break end
end -- end
if reached_inputs then
while i <= #input_file - 3 do if semicolon_position then
for i = semicolon_position + 1, #input_file, 3 do
local input_list = {right = false, rotate_right2 = false, up = false, rotate_left = false, left = false, down = false, rotate_right = false, rotate_left2 = false} local input_list = {right = false, rotate_right2 = false, up = false, rotate_left = false, left = false, down = false, rotate_right = false, rotate_left2 = false}
local coded_input = tonumber(string.sub(input_file, i, i+2)) local coded_input = tonumber(string.sub(input_file, i, i+2))
if bit.band(coded_input, 1 ) == 1 then input_list["right"] = true end for key, code in pairs(GAME_input_code_list) do
if bit.band(coded_input, 2 ) == 2 then input_list["rotate_right2"] = true end ---@diagnostic disable-next-line: param-type-mismatch
if bit.band(coded_input, 4 ) == 4 then input_list["up"] = true end if bit.band(coded_input, code) == code then input_list[key] = true end
if bit.band(coded_input, 8 ) == 8 then input_list["rotate_left"] = true end end
if bit.band(coded_input, 16 ) == 16 then input_list["left"] = true end
if bit.band(coded_input, 32 ) == 32 then input_list["down"] = true end
if bit.band(coded_input, 64 ) == 64 then input_list["rotate_right"] = true end
if bit.band(coded_input, 128) == 128 then input_list["rotate_left2"] = true end
table.insert(replay_inputs, input_list) table.insert(replay_inputs, input_list)
i = i + 3
end end
end end
return replay_inputs return replay_inputs
end end
function GameMode:getInputPieceSeq(input_file) function GameMode:getInputPieceSeq(input_file)
local seed = ''
local string_start local string_start
if string.sub(input_file, 1, 3) == 'NEW' then if string.sub(input_file, 1, 3) == 'NEW' then
string_start = 4 string_start = 4
@@ -353,13 +342,16 @@ function GameMode:getInputPieceSeq(input_file)
string_start = 1 string_start = 1
self.old_replay = true self.old_replay = true
end end
for i = string_start, #input_file do return string.sub(
local c = string.sub(input_file, i, i) input_file, string_start,
if c == ';' then break string.find(input_file, ';', string_start, true) - 1
else seed = seed..c ) -- seed
end -- for i = string_start, #input_file do
end -- local c = string.sub(input_file, i, i)
return seed -- if c == ';' then break
-- else seed = seed..c
-- end
-- end
end end
function GameMode:update(inputs, ruleset) function GameMode:update(inputs, ruleset)