Half-assed replay implementation

This commit is contained in:
--global 2023-11-16 17:30:40 +07:00
parent 253a1951e9
commit 96adc9c89c

View file

@ -1,5 +1,7 @@
if SERVER then if SERVER then
util.AddNetworkString("ReplaySendToClient") util.AddNetworkString("ReplaySendToClient")
util.AddNetworkString("ReplayReturnToClient")
util.AddNetworkString("PlayerRequestedReplay")
end end
function ReplayCmd(ply, cmd) function ReplayCmd(ply, cmd)
@ -12,19 +14,9 @@ function ReplayCmd(ply, cmd)
local ang = cmd:GetViewAngles() local ang = cmd:GetViewAngles()
if ply.ReplayLastAng == ang then
ang = 0
else
ply.ReplayLastAng = ang
end
local curtick = cmd:TickCount() - ply.ReplayFirstTick + 1 local curtick = cmd:TickCount() - ply.ReplayFirstTick + 1
if ang == 0 then
ply.ReplayTicks[curtick] = {cmd:GetButtons(), cmd:GetForwardMove(), cmd:GetSideMove()}
else
ply.ReplayTicks[curtick] = {cmd:GetButtons(), ang.x, ang.y, cmd:GetForwardMove(), cmd:GetSideMove()} ply.ReplayTicks[curtick] = {cmd:GetButtons(), ang.x, ang.y, cmd:GetForwardMove(), cmd:GetSideMove()}
end
if curtick > 23760 then if curtick > 23760 then
print("Replay recording stopped - too long") print("Replay recording stopped - too long")
@ -37,10 +29,10 @@ hook.Add("StartCommand", "ReplayStart", ReplayCmd)
function ReplayStart(ply) function ReplayStart(ply)
--if not game.SinglePlayer() then return end --if not game.SinglePlayer() then return end
if ply.InReplay then return end
print("Starting Replay") print("Starting Replay")
ply.InReplay = false
ply.ReplayRecording = true ply.ReplayRecording = true
ply.ReplayTicks = {} ply.ReplayTicks = {}
ply.ReplayFirstTick = false ply.ReplayFirstTick = false
@ -51,7 +43,6 @@ end
function ReplayStop(ply, debugdump) function ReplayStop(ply, debugdump)
--if not game.SinglePlayer() then return end --if not game.SinglePlayer() then return end
if not ply.ReplayTicks then return end if not ply.ReplayTicks then return end
if ply.InReplay then return end
print("Ending Replay (" .. #ply.ReplayTicks .. "ticks)") print("Ending Replay (" .. #ply.ReplayTicks .. "ticks)")
@ -65,6 +56,21 @@ function ReplayStop(ply, debugdump)
file.CreateDir(dir) file.CreateDir(dir)
file.Write(dir .. "replaydump.txt", replay) file.Write(dir .. "replaydump.txt", replay)
ReplaySendToClient(ply)
end
function ReplayStartPlaying(ply) -- i suck at naming stuff
ply.InReplay = true
ply.ReplayRecording = false
local dir = "beatrun/replays/" .. game.GetMap() .. "/"
local replay = file.Read(dir .. "replaydump.txt")
replay = replay and util.JSONToTable(util.Decompress(replay))
ply.ReplayStartPos = replay[1]
ply.ReplayTicks = replay[2]
ply:Spawn()
ReplayReturnToClient(ply)
end end
local RFF = true local RFF = true
@ -96,23 +102,16 @@ function ReplayPlayback(ply, cmd)
end end
local tickdata = ply.ReplayTicks[tickcount] local tickdata = ply.ReplayTicks[tickcount]
local shortdata = #tickdata == 3
local ang = shortdata and 0 or Angle(tickdata[2], tickdata[3], cmd:GetViewAngles().z)
if not shortdata then local ang = Angle(tickdata[2], tickdata[3], cmd:GetViewAngles().z)
ply.ReplayLastAng = ang
end
cmd:SetButtons(tickdata[1]) cmd:SetButtons(tickdata[1])
cmd:SetViewAngles(ply.ReplayLastAng) PrintTable(tickdata)
cmd:SetViewAngles(ang)
ply:SetEyeAngles(ang)
if shortdata then
cmd:SetForwardMove(tickdata[2])
cmd:SetSideMove(tickdata[3])
else
cmd:SetForwardMove(tickdata[4]) cmd:SetForwardMove(tickdata[4])
cmd:SetSideMove(tickdata[5]) cmd:SetSideMove(tickdata[5])
end
cmd:RemoveKey(IN_RELOAD) cmd:RemoveKey(IN_RELOAD)
elseif SERVER and cmdtc - firsttick + 1 > 0 or CLIENT and not ply:GetNWBool("InReplay") and RFF < CurTime() then elseif SERVER and cmdtc - firsttick + 1 > 0 or CLIENT and not ply:GetNWBool("InReplay") and RFF < CurTime() then
@ -151,6 +150,20 @@ function ReplaySendToClient(ply)
net.Start("ReplaySendToClient") net.Start("ReplaySendToClient")
net.Send(ply) net.Send(ply)
end
function ReplayReturnToClient(ply)
--if not game.SinglePlayer() then return end
local replaydata = util.JSONToTable(util.Decompress(file.Read("beatrun/replays/" .. game.GetMap() .. "/replaydump.txt", "DATA")))
ply.ReplayFirstTick = false
ply.ReplayStartPos = replaydata[1]
ply.ReplayTicks = replaydata[2]
ply:SetNWBool("InReplay", true)
net.Start("ReplayReturnToClient")
net.Send(ply)
ply.InReplay = true ply.InReplay = true
ply:SetPos(ply.ReplayStartPos) ply:SetPos(ply.ReplayStartPos)
@ -172,11 +185,14 @@ if SERVER then
--end) --end)
function GM:SetupMove(ply, mv, cmd) function GM:SetupMove(ply, mv, cmd)
if ply.ReplayRecording then if ply.ReplayRecording then
print("attempting to record tick")
ReplayCmd(ply, cmd) ReplayCmd(ply, cmd)
--PrintTable(v.ReplayTicks) --PrintTable(v.ReplayTicks)
end -- how not to record replays 101 end -- how not to record replays 101
end end
net.Receive("PlayerRequestedReplay", function(len, ply)
ReplayStartPlaying(ply)
end)
end end
if CLIENT then if CLIENT then
@ -193,7 +209,7 @@ if CLIENT then
} }
local function BeatrunReplayVision() local function BeatrunReplayVision()
if LocalPlayer().ReplayFirstTick then if LocalPlayer().InReplay then
DrawColorModify(tab) DrawColorModify(tab)
end end
end end
@ -226,15 +242,19 @@ if CLIENT then
net.Receive("ReplayRequest", ReplayBegin) net.Receive("ReplayRequest", ReplayBegin)
net.Receive("ReplaySendToClient", function(length) net.Receive("ReplaySendToClient", function(length)
if length < 100 then
LocalPlayer().ReplayTicks = util.JSONToTable(util.Decompress(file.Read("beatrun/replays/" .. game.GetMap() .. "/replaydump.txt", "DATA")))[2]
else
LocalPlayer().ReplayTicks = util.JSONToTable(util.Decompress(net.ReadData(length))) LocalPlayer().ReplayTicks = util.JSONToTable(util.Decompress(net.ReadData(length)))
end end)
net.Receive("ReplayReturnToClient", function(length)
LocalPlayer().ReplayTicks = util.JSONToTable(util.Decompress(net.ReadData(length)))
LocalPlayer().ReplayFirstTick = false LocalPlayer().ReplayFirstTick = false
ReplayBegin() ReplayBegin()
end) end)
concommand.Add("Beatrun_RequestReplay", function(ply)
net.Start("PlayerRequestedReplay")
net.SendToServer()
end)
end end
function ReplayCancel(ply) function ReplayCancel(ply)