From 871731e6159ec358c6ff0ac835c131d9ac77fce4 Mon Sep 17 00:00:00 2001
From: BotoX <github@botox.bz>
Date: Sun, 3 Nov 2019 01:11:40 +0100
Subject: [PATCH] LagCompensation: fix off-by-one error and iPlayerSimTick
 being calculated wrong

---
 LagCompensation/scripting/LagCompensation.sp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/LagCompensation/scripting/LagCompensation.sp b/LagCompensation/scripting/LagCompensation.sp
index c6b06761..2f94839a 100644
--- a/LagCompensation/scripting/LagCompensation.sp
+++ b/LagCompensation/scripting/LagCompensation.sp
@@ -465,7 +465,7 @@ public Action OnPlayerRunCmd(int client, int &buttons, int &impulse, float vel[3
 	if(iDelta > MAX_RECORDS)
 		iDelta = MAX_RECORDS;
 
-	int iPlayerSimTick = iGameTick + iDelta;
+	int iPlayerSimTick = iGameTick - iDelta;
 
 	for(int i = 0; i < g_iNumEntities; i++)
 	{
@@ -497,7 +497,10 @@ public Action OnPlayerRunCmd(int client, int &buttons, int &impulse, float vel[3
 		if(iDelta >= g_aEntityLagData[i].iNumRecords)
 			iDelta = g_aEntityLagData[i].iNumRecords - 1;
 
-		int iRecordIndex = g_aEntityLagData[i].iRecordIndex - iDelta;
+		// +1 because the newest record in the list is one tick old
+		// this is because we simulate players first
+		// hence no new entity record was inserted on the current tick
+		int iRecordIndex = g_aEntityLagData[i].iRecordIndex - iDelta + 1;
 		if(iRecordIndex < 0)
 			iRecordIndex += MAX_RECORDS;