#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>

ConVar g_hCVar_Gravity;

float g_flClientGravityReplicate[MAXPLAYERS+1];

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public Plugin myinfo =
{
	name         = "ReplicateGravity",
	author       = "xen, zaCade",
	description  = "Allow client prediction for gravity",
	version      = "1.0.0"
};

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnPluginStart()
{
	if ((g_hCVar_Gravity = FindConVar("sv_gravity")) == null)
	{
		SetFailState("Unable to find ConVar \"sv_gravity\"!");
		return;
	}
}

//----------------------------------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------------------------------
public void OnGameFrame()
{
	float flServerGravity = g_hCVar_Gravity.FloatValue;

	for (int client = 1; client < MaxClients; client++)
	{
		if (!IsClientInGame(client) || !IsPlayerAlive(client) || IsFakeClient(client))
		{
			g_flClientGravityReplicate[client] = flServerGravity;
			continue;
		}

		float flClientGravity = GetEntityGravity(client);
		if (flClientGravity == 0.0)
			flClientGravity = 1.0;

		float flClientGravityReplicate = flClientGravity * flServerGravity;

		if (flClientGravityReplicate != g_flClientGravityReplicate[client])
		{
			char szGravity[8];
			FloatToString(flClientGravityReplicate, szGravity, sizeof(szGravity));

			g_hCVar_Gravity.ReplicateToClient(client, szGravity);

			g_flClientGravityReplicate[client] = flClientGravityReplicate;
		}
	}
}

//probably not needed at all. 
public void OnClientDisconnect(int client)
{
    float flServerGravity = g_hCVar_Gravity.FloatValue;
    g_flClientGravityReplicate[client] = flServerGravity;
}

public void OnClientPostAdminCheck(int client)
{
    float flServerGravity = g_hCVar_Gravity.FloatValue;
    g_flClientGravityReplicate[client] = flServerGravity;
}
