AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   plugin causing csgo srcds freeze (https://forums.alliedmods.net/showthread.php?t=338194)

AuricYoutube 06-16-2022 10:27

plugin causing csgo srcds freeze
 
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?

Marttt 06-16-2022 15:57

Re: plugin causing csgo srcds freeze
 
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

AuricYoutube 06-16-2022 19:12

Re: plugin causing csgo srcds freeze
 
Quote:

Originally Posted by Marttt (Post 2781795)
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?

Bacardi 06-17-2022 05:26

Re: plugin causing csgo srcds freeze
 
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.

AuricYoutube 06-17-2022 07:08

Re: plugin causing csgo srcds freeze
 
Quote:

Originally Posted by Bacardi (Post 2781838)
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?

Marttt 06-17-2022 11:12

Re: plugin causing csgo srcds freeze
 
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".

AuricYoutube 06-22-2022 17:00

Re: plugin causing csgo srcds freeze
 
Quote:

Originally Posted by Marttt (Post 2781851)
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.

azalty 06-22-2022 18:14

Re: plugin causing csgo srcds freeze
 
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.

Marttt 06-23-2022 08:02

Re: plugin causing csgo srcds freeze
 
Quote:

Originally Posted by AuricYoutube (Post 2782163)
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

azalty 06-23-2022 08:21

Re: plugin causing csgo srcds freeze
 
Quote:

Originally Posted by Bacardi (Post 2781838)
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()"


All times are GMT -4. The time now is 13:38.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.