1.0.4: Optimizations, more to come.

This commit is contained in:
BotoX 2020-03-01 19:35:00 +01:00
parent 4e5255b835
commit a4bcdb2313

View File

@ -5,7 +5,7 @@
#include <dhooks> #include <dhooks>
#include <clientprefs> #include <clientprefs>
#define PLUGIN_VERSION "1.0.3" #define PLUGIN_VERSION "1.0.4"
#define SetBit(%1,%2) ((%1)[(%2) >> 5] |= (1 << ((%2) & 31))) #define SetBit(%1,%2) ((%1)[(%2) >> 5] |= (1 << ((%2) & 31)))
#define ClearBit(%1,%2) ((%1)[(%2) >> 5] &= ~(1 << ((%2) & 31))) #define ClearBit(%1,%2) ((%1)[(%2) >> 5] &= ~(1 << ((%2) & 31)))
@ -25,6 +25,7 @@ public Plugin myinfo =
bool g_bLateLoad = false; bool g_bLateLoad = false;
bool g_bHasPhysHooks = true; bool g_bHasPhysHooks = true;
bool g_bHasOnEntitySpawned = false;
// Don't change this. // Don't change this.
#define MAX_EDICTS 2048 #define MAX_EDICTS 2048
@ -109,7 +110,10 @@ EntityLagData g_aEntityLagData[MAX_ENTITIES];
int g_iNumEntities = 0; int g_iNumEntities = 0;
bool g_bCleaningUp = true; bool g_bCleaningUp = true;
bool g_bHasOnEntitySpawned = false; // Cache
int g_iGameTick;
float g_fTickInterval;
int g_aLerpTicks[MAXPLAYERS + 1];
// SDKCall // SDKCall
Handle g_hCalcAbsolutePosition; Handle g_hCalcAbsolutePosition;
@ -135,7 +139,6 @@ int g_iSolidFlags;
int g_iSolidType; int g_iSolidType;
int g_iSurroundType; int g_iSurroundType;
int g_iEFlags; int g_iEFlags;
int g_iLerpTime = -1;
int g_iVecOrigin; int g_iVecOrigin;
int g_iVecAbsOrigin; int g_iVecAbsOrigin;
@ -382,6 +385,8 @@ public void OnMapStart()
g_bCleaningUp = false; g_bCleaningUp = false;
g_fTickInterval = GetTickInterval();
g_iParent = FindDataMapInfo(0, "m_pParent"); g_iParent = FindDataMapInfo(0, "m_pParent");
g_iSpawnFlags = FindDataMapInfo(0, "m_spawnflags"); g_iSpawnFlags = FindDataMapInfo(0, "m_spawnflags");
g_iCollision = FindDataMapInfo(0, "m_Collision"); g_iCollision = FindDataMapInfo(0, "m_Collision");
@ -409,7 +414,7 @@ public void OnMapStart()
OnClientConnected(client); OnClientConnected(client);
if(AreClientCookiesCached(client)) if(AreClientCookiesCached(client))
OnClientCookiesCached(client); OnClientCookiesCached(client);
OnClientPutInServer(client); OnClientSettingsChanged(client);
} }
} }
@ -455,12 +460,13 @@ public void OnClientCookiesCached(int client)
g_bDisableLagComp[client] = false; g_bDisableLagComp[client] = false;
} }
public void OnClientPutInServer(int client) public void OnClientSettingsChanged(int client)
{ {
if(g_iLerpTime == -1) if(!IsClientInGame(client))
{ return;
g_iLerpTime = FindDataMapInfo(client, "m_fLerpTime");
} float fLerpTime = GetEntPropFloat(client, Prop_Data, "m_fLerpTime");
g_aLerpTicks[client] = RoundToNearest(fLerpTime / g_fTickInterval);
} }
public void OnClientDisconnect(int client) public void OnClientDisconnect(int client)
@ -822,6 +828,7 @@ public MRESReturn Detour_OnFrameUpdatePostEntityThink()
public void OnRunThinkFunctions(bool simulating) public void OnRunThinkFunctions(bool simulating)
{ {
g_iGameTick = GetGameTickCount();
BlockTriggerTouchPlayers(g_aBlockTriggerTouchPlayers, false); BlockTriggerTouchPlayers(g_aBlockTriggerTouchPlayers, false);
} }
@ -830,29 +837,12 @@ public Action OnPlayerRunCmd(int client, int &buttons, int &impulse, float vel[3
if(!IsPlayerAlive(client) || IsFakeClient(client)) if(!IsPlayerAlive(client) || IsFakeClient(client))
return Plugin_Continue; return Plugin_Continue;
int iTargetTick = tickcount - g_aLerpTicks[client];
// -1 because the newest record in the list is one tick old // -1 because the newest record in the list is one tick old
// this is because we simulate players first // this is because we simulate players first
// hence no new entity record was inserted on the current tick // hence no new entity record was inserted on the current tick
int iGameTick = GetGameTickCount() - 1; int iDelta = g_iGameTick - iTargetTick - 1;
float fTickInterval = GetTickInterval();
float fLerpTime = GetEntDataFloat(client, g_iLerpTime);
// -1 lerp ticks was determined by analyzing hits visually at host_timescale 0.01 and debug_touchlinks 1
int iLerpTicks = RoundToNearest(fLerpTime / fTickInterval) - 1;
int iTargetTick = tickcount - iLerpTicks;
int iDelta = iGameTick - iTargetTick;
float fCorrect = 0.0;
fCorrect += GetClientLatency(client, NetFlow_Outgoing);
fCorrect += iLerpTicks * fTickInterval;
float fDeltaTime = fCorrect - iDelta * fTickInterval;
if(FloatAbs(fDeltaTime) > 0.2)
{
// difference between cmd time and latency is too big > 200ms, use time correction based on latency
iDelta = RoundToNearest(fCorrect / fTickInterval);
}
// The player is stupid and doesn't want lag compensation. // The player is stupid and doesn't want lag compensation.
// To get the original behavior back lets assume they actually have 0 latency. // To get the original behavior back lets assume they actually have 0 latency.
@ -865,7 +855,7 @@ public Action OnPlayerRunCmd(int client, int &buttons, int &impulse, float vel[3
if(iDelta > MAX_RECORDS) if(iDelta > MAX_RECORDS)
iDelta = MAX_RECORDS; iDelta = MAX_RECORDS;
int iPlayerSimTick = iGameTick - iDelta; int iPlayerSimTick = g_iGameTick - iDelta;
for(int i = 0; i < g_iNumEntities; i++) for(int i = 0; i < g_iNumEntities; i++)
{ {
@ -929,7 +919,7 @@ public MRESReturn Hook_EndGameFrame()
{ {
if(g_aEntityLagData[i].iDeleted) if(g_aEntityLagData[i].iDeleted)
{ {
if(g_aEntityLagData[i].iDeleted + MAX_RECORDS <= GetGameTickCount()) if(g_aEntityLagData[i].iDeleted + MAX_RECORDS <= g_iGameTick)
{ {
// calls OnEntityDestroyed right away // calls OnEntityDestroyed right away
// which calls RemoveRecord // which calls RemoveRecord