Replays are now fully functional.

This commit is contained in:
BoatsandJoes 2021-12-05 22:17:44 -06:00
parent 81ab7cd4de
commit febd1de0ef
2 changed files with 50 additions and 31 deletions

View File

@ -1,3 +1,5 @@
local Sequence = require 'tetris.randomizers.fixed_sequence'
local ReplayScene = Scene:extend() local ReplayScene = Scene:extend()
ReplayScene.title = "Replay" ReplayScene.title = "Replay"
@ -6,7 +8,10 @@ function ReplayScene:new(replay, game_mode, ruleset, inputs)
self.secret_inputs = inputs self.secret_inputs = inputs
self.game = game_mode(self.secret_inputs) self.game = game_mode(self.secret_inputs)
self.ruleset = ruleset(self.game) self.ruleset = ruleset(self.game)
self.game:initialize(self.ruleset) -- Replace piece randomizer with replay piece sequence
local randomizer = Sequence(table.keys(ruleset.colourscheme))
randomizer.sequence = replay["pieces"]
self.game:initializeReplay(self.ruleset, randomizer)
self.inputs = { self.inputs = {
left=false, left=false,
right=false, right=false,

View File

@ -74,7 +74,8 @@ function GameMode:new(secret_inputs)
self.section_times = { [0] = 0 } self.section_times = { [0] = 0 }
self.secondary_section_times = { [0] = 0 } self.secondary_section_times = { [0] = 0 }
self.replay_inputs = {} self.replay_inputs = {}
self.replay_pieces = {} self.replay_pieces = ""
self.save_replay = true
end end
function GameMode:getARR() return 1 end function GameMode:getARR() return 1 end
@ -89,7 +90,7 @@ function GameMode:getGravity() return 1/64 end
function GameMode:getNextPiece(ruleset) function GameMode:getNextPiece(ruleset)
local shape = self.used_randomizer:nextPiece() local shape = self.used_randomizer:nextPiece()
self.replay_pieces[#self.replay_pieces + 1] = shape self.replay_pieces = self.replay_pieces..shape
return { return {
skin = self:getSkin(), skin = self:getSkin(),
shape = shape, shape = shape,
@ -114,7 +115,18 @@ function GameMode:initialize(ruleset)
for i = 1, math.max(self.next_queue_length, 1) do for i = 1, math.max(self.next_queue_length, 1) do
table.insert(self.next_queue, self:getNextPiece(ruleset)) table.insert(self.next_queue, self:getNextPiece(ruleset))
end end
self.lock_on_soft_drop = ({ruleset.softdrop_lock, self.instant_soft_drop, false, true })[config.gamesettings.manlock] self.lock_on_soft_drop = ({ruleset.softdrop_lock, self.instant_soft_drop, false, true})[config.gamesettings.manlock]
self.lock_on_hard_drop = ({ruleset.harddrop_lock, self.instant_hard_drop, true, false})[config.gamesettings.manlock]
end
function GameMode:initializeReplay(ruleset, randomizer)
self.used_randomizer = randomizer
self.save_replay = false
self.ruleset = ruleset
for i = 1, math.max(self.next_queue_length, 1) do
table.insert(self.next_queue, self:getNextPiece(ruleset))
end
self.lock_on_soft_drop = ({ruleset.softdrop_lock, self.instant_soft_drop, false, true})[config.gamesettings.manlock]
self.lock_on_hard_drop = ({ruleset.harddrop_lock, self.instant_hard_drop, true, false})[config.gamesettings.manlock] self.lock_on_hard_drop = ({ruleset.harddrop_lock, self.instant_hard_drop, true, false})[config.gamesettings.manlock]
end end
@ -361,36 +373,38 @@ function GameMode:onGameOver()
local animation_length = 120 local animation_length = 120
if self.game_over_frames == 1 then if self.game_over_frames == 1 then
alpha = 1 alpha = 1
-- Save replay. if self.save_replay then
local replay = {} -- Save replay.
replay["inputs"] = self.replay_inputs local replay = {}
replay["pieces"] = self.replay_pieces replay["inputs"] = self.replay_inputs
replay["mode"] = self.name replay["pieces"] = self.replay_pieces
replay["ruleset"] = self.ruleset.name replay["mode"] = self.name
replay["timer"] = self.frames replay["ruleset"] = self.ruleset.name
replay["score"] = self.score replay["timer"] = self.frames
replay["level"] = self.level replay["score"] = self.score
replay["lines"] = self.lines replay["level"] = self.level
replay["gamesettings"] = config.gamesettings replay["lines"] = self.lines
replay["timestamp"] = os.time() replay["gamesettings"] = config.gamesettings
if love.filesystem.getInfo("replays") == nil then replay["timestamp"] = os.time()
love.filesystem.createDirectory("replays") if love.filesystem.getInfo("replays") == nil then
end love.filesystem.createDirectory("replays")
local replay_files = love.filesystem.getDirectoryItems("replays") end
-- Select replay filename that doesn't collide with an existing one local replay_files = love.filesystem.getDirectoryItems("replays")
local replay_number = 0 -- Select replay filename that doesn't collide with an existing one
local collision = true local replay_number = 0
while collision do local collision = true
collision = false while collision do
replay_number = replay_number + 1 collision = false
for key, file in pairs(replay_files) do replay_number = replay_number + 1
if file == replay_number..".rply" then for key, file in pairs(replay_files) do
collision = true if file == replay_number..".rply" then
break collision = true
break
end
end end
end end
love.filesystem.write("replays/"..replay_number..".rply", binser.serialize(replay))
end end
love.filesystem.write("replays/"..replay_number..".rply", binser.serialize(replay))
elseif self.game_over_frames < animation_length then elseif self.game_over_frames < animation_length then
-- Show field for a bit, then fade out. -- Show field for a bit, then fade out.
alpha = math.pow(2048, self.game_over_frames/animation_length - 1) alpha = math.pow(2048, self.game_over_frames/animation_length - 1)