Raised This Month: $344 Target: $400
 86%   Almost there!

plugin causing csgo srcds freeze


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
AuricYoutube
Senior Member
Join Date: Aug 2016
Location: Here
Old 06-16-2022 , 10:27   plugin causing csgo srcds freeze
Reply With Quote #1

Code:
#include <sourcemod>
#include <sdktools>
#pragma newdecls required
#pragma semicolon 1

float gF_LastSpeed[MAXPLAYERS + 1];
float g_flOldYawAngle[MAXPLAYERS + 1];
bool gB_TouchingTrigger[MAXPLAYERS + 1];

public void OnPluginStart()
{
	HookEvent("round_start", OnRoundStart);
}

public void OnRoundStart(Event event, const char[] name, bool dontBroadcast)
{
	HookEntityOutput("trigger_push", "OnStartTouch", StartTouchTrigger);
	HookEntityOutput("trigger_push", "OnEndTouch", EndTouchTrigger);
}

public Action OnPlayerRunCmd(int client, int &buttons, int &impulse, float vel[3], float angles[3])
{
	float oldyaw = g_flOldYawAngle[client];
	float delta = AngleNormalize(angles[1] - oldyaw);
    
	g_flOldYawAngle[client] = angles[1];
	
	float fAbsVelocity[3];
	GetEntPropVector(client, Prop_Data, "m_vecAbsVelocity", fAbsVelocity);
	
	float fCurrentSpeed = SquareRoot(Pow(fAbsVelocity[0], 2.0) + Pow(fAbsVelocity[1], 2.0));
	float fGain = GetVelocityGain(client, fCurrentSpeed);
	float fAngleDiff = GetAngleDifference(client, angles);
	bool bPlayerStrafing = ((vel[0] != 0.0 || vel[1] != 0.0) && fAngleDiff != 0.0);
	
	if(!bPlayerStrafing || gB_TouchingTrigger[client] || (GetEntityFlags(client) & FL_ONGROUND) || GetEntityMoveType(client) != MOVETYPE_WALK)
	{
		return Plugin_Continue;
	}
	
	if((buttons & IN_FORWARD && buttons & IN_MOVELEFT) || (buttons & IN_FORWARD && buttons & IN_MOVERIGHT) || (buttons & IN_BACK && buttons & IN_MOVELEFT) || (buttons & IN_BACK && buttons & IN_MOVERIGHT))
	{
		return Plugin_Continue;
	}
	
	if((buttons & IN_FORWARD) && delta < 0.0)
	{
		return Plugin_Continue;
	}
	
	if((buttons & IN_BACK) && delta > 0.0)
	{
		return Plugin_Continue;
	}
		
	if((buttons & IN_MOVELEFT) && delta < 0.0)
	{
		return Plugin_Continue;
	}

	if((buttons & IN_MOVERIGHT) && delta > 0.0)
	{
		return Plugin_Continue;
	}
	
	float fTickrate = 1.0 / GetTickInterval();
	float fTickDiff = 100.0 / fTickrate;
	
	float fStrafingAngle = GetStrafingAngle(fAngleDiff, fTickDiff);
	
	SimulateStrafingTickrate(client, fAbsVelocity, fCurrentSpeed, fGain, fStrafingAngle, fTickrate, fTickDiff);
	
	return Plugin_Continue;
}

void SimulateStrafingTickrate(int client, float fAbsVelocity[3], float fCurrentSpeed, float fGain, float fStrafingAngle, float fTickrate, float fTickDiff)
{
	float fMultiplier = 128.0 / 100.0;
	float fNewGain = fCurrentSpeed / (fCurrentSpeed + (fMultiplier * (fStrafingAngle * 0.1) * fTickDiff));
	fAbsVelocity[0] /= fNewGain;
	fAbsVelocity[1] /= fNewGain;
	TeleportEntity(client, NULL_VECTOR, NULL_VECTOR, fAbsVelocity);
}

float GetVelocityGain(int client, float fCurrentSpeed)
{
	float fGain = fCurrentSpeed - gF_LastSpeed[client];
	gF_LastSpeed[client] = fCurrentSpeed;
	
	return fGain;
}

float GetAngleDifference(int client, float angles[3])
{
	float fTempAngle = angles[1];
	
	float fAngles[3];
	GetClientEyeAngles(client, fAngles);
	float fAngleDiff = (fTempAngle - fAngles[1]);
	
	if(fAngleDiff < 0.0)
	{
		fAngleDiff = -fAngleDiff;
	}
	
	return fAngleDiff;
}

float GetStrafingAngle(float fAngleDiff, float fTickDiff)
{
	if(fAngleDiff > (fTickDiff * 10.0))
	{
		fAngleDiff = ((fTickDiff * 10.0) * 2.0) - fAngleDiff;
		
		if(fAngleDiff < 0.0)
		{
			fAngleDiff = 0.0;
		}
	}
	
	return fAngleDiff;
}

float AngleNormalize(float flAngle)
{
	if (flAngle > 180.0)
		flAngle -= 360.0;
	else if (flAngle < -180.0)
		flAngle += 360.0;

	return flAngle;
}

public int StartTouchTrigger(const char[] output, int entity, int client, float delay)
{
	if(client < 1 || client > MaxClients)
	{
		return;
	}
	
	if(!IsClientInGame(client) || !IsPlayerAlive(client))
	{
		return;
	}
	
	RequestFrame(StopPlugin, GetClientSerial(client));
}

void StopPlugin(int data)
{
	int client = GetClientFromSerial(data);
	gB_TouchingTrigger[client] = true;
}

public int EndTouchTrigger(const char[] output, int entity, int client, float delay)
{
	if(client < 1 || client > MaxClients)
	{
		return;
	}
	
	if(!IsClientInGame(client) || !IsPlayerAlive(client))
	{
		return;
	}
	
	RequestFrame(ResumePlugin, GetClientSerial(client));
}

void ResumePlugin(int data)
{
	int client = GetClientFromSerial(data);
	gB_TouchingTrigger[client] = false;
}
Anyone know what is causing csgo srcds to freeze?
AuricYoutube is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 06-16-2022 , 15:57   Re: plugin causing csgo srcds freeze
Reply With Quote #2

Maybe too much stuff OnPlayerRunCmd, this one runs for every client at every tick.

Perhaps you should check the buttons first before doing all stuff
__________________
Marttt is offline
AuricYoutube
Senior Member
Join Date: Aug 2016
Location: Here
Old 06-16-2022 , 19:12   Re: plugin causing csgo srcds freeze
Reply With Quote #3

Quote:
Originally Posted by Marttt View Post
Maybe too much stuff OnPlayerRunCmd, this one runs for every client at every tick.

Perhaps you should check the buttons first before doing all stuff
How would I do that?
AuricYoutube is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 06-17-2022 , 05:26   Re: plugin causing csgo srcds freeze
Reply With Quote #4

Users need avoid using OnGameFrame and OnPlayerRunCmd, if you not have a clue how those works.

Event repeating timer with 0.1 seconds is better solution.
Bacardi is online now
AuricYoutube
Senior Member
Join Date: Aug 2016
Location: Here
Old 06-17-2022 , 07:08   Re: plugin causing csgo srcds freeze
Reply With Quote #5

Quote:
Originally Posted by Bacardi View Post
Users need avoid using OnGameFrame and OnPlayerRunCmd, if you not have a clue how those works.

Event repeating timer with 0.1 seconds is better solution.
Can you explain how to do it here for this plugin?
AuricYoutube is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 06-17-2022 , 11:12   Re: plugin causing csgo srcds freeze
Reply With Quote #6

Basically, you will have to create a timer, you can find how to create one in the wiki.

https://wiki.alliedmods.net/Timers_(...Mod_Scripting)

And move your logic from "OnPlayerRunCmd" to the timer, inside a for loop through all valid clients (IsConnected)

The only thing you "don't" have ready to use is the client button, but is possible to get that with some basic functions (GetClientButtons)

This is the basis, if you don't know what I'm talking about I believe you won't be able to do it on your own and will need a code "ready to go".
__________________

Last edited by Marttt; 06-23-2022 at 08:00.
Marttt is offline
AuricYoutube
Senior Member
Join Date: Aug 2016
Location: Here
Old 06-22-2022 , 17:00   Re: plugin causing csgo srcds freeze
Reply With Quote #7

Quote:
Originally Posted by Marttt View Post
Basically, you will have to create a timer, you can find how to create one in the wiki.

https://wiki.alliedmods.net/Timers_(SourceMod_Scripting)

And move your logic from "OnPlayerRunCmd" to the timer, inside a for loop through all valid clients (IsConnected)

The only thing you "don't" have ready to use is the client button, but is possible to get that with some basic functions (GetClientButtons)

This is the basis, if you don't know what I'm talking about I believe you won't be able to do it on your own and will need a code "ready to go".
I tried to understand what you sent but there is nothing in the link which you have sent. I apologize for my lack of coding knowledge.
AuricYoutube is offline
azalty
AlliedModders Donor
Join Date: Feb 2020
Location: France
Old 06-22-2022 , 18:14   Re: plugin causing csgo srcds freeze
Reply With Quote #8

Avoid declaring variables inside frequently ran functions. Use global variables (either by declaring your variables outside or by using the 'static' keyword behind a variable).
Ex:

Code:
static float oldyaw;
oldyaw = g_flOldYawAngle[client];
See https://wiki.alliedmods.net/Introduc...7#Local_static

OnPlayerRunCmd is ran once every tick for every player. Assuming you have 20 players and your tickrate is 64, that makes 1280 calls per second. When you have that many calls, do as little checks and calculations as you can. Variable declaration is expensive.
__________________

Last edited by azalty; 06-22-2022 at 18:19.
azalty is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 06-23-2022 , 08:02   Re: plugin causing csgo srcds freeze
Reply With Quote #9

Quote:
Originally Posted by AuricYoutube View Post
I tried to understand what you sent but there is nothing in the link which you have sent.
Some kind of forum bug, btw with a simple google search you would have found
__________________
Marttt is offline
azalty
AlliedModders Donor
Join Date: Feb 2020
Location: France
Old 06-23-2022 , 08:21   Re: plugin causing csgo srcds freeze
Reply With Quote #10

Quote:
Originally Posted by Bacardi View Post
Users need avoid using OnGameFrame and OnPlayerRunCmd, if you not have a clue how those works.

Event repeating timer with 0.1 seconds is better solution.
It doesn't seem to be a good one in this situation, they seem to want to emulate strafing. For more precise results, something that runs every frame is needed.

"SimulateStrafingTickrate()"
__________________

Last edited by azalty; 06-23-2022 at 08:22.
azalty is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:19.


Powered by vBulletin®
Copyright ©2000 - 2022, vBulletin Solutions, Inc.
Theme made by Freecode