Raised This Month: $51 Target: $400
 12% 

[L4D2]Ask for help


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
phoenix0001
Senior Member
Join Date: Apr 2010
Location: China
Old 11-15-2017 , 21:04   [L4D2]Ask for help
Reply With Quote #1

Plug-in function
Can automatically adjust the difficulty according to the number of people
4 = Easy 8 = Normal 12 = Hard 16 = Impossible [z_difficulty]

Plug-in does not work after running for some time, very strange.

PHP Code:
#include <sourcemod>

#pragma semicolon 1
#define PLUGIN_VERSION "1.0"
#pragma newdecls required

int g_iClientCount;
ConVar g_cDifficulty;

public 
Plugin myinfo =
{
    
name "[L4D2] Difficulty Adjuster",
    
author "Tak (Chaosxk)",
    
description "Adjusts difficulty based on number of clients in server.",
    
version PLUGIN_VERSION,
    
url "https://forums.alliedmods.net/showthread.php?t=297009"
}

public 
void OnPluginStart()
{
    if ((
g_cDifficulty FindConVar("z_difficulty")) == null)
    {
        
LogError("[SM] Can not find convar z_difficulty");
        
SetFailState("Can not find convar z_difficulty");
    }
    
    for (
int i 1<= MaxClientsi++)
    {
        if (!
IsClientInGame(i) || IsFakeClient(i))
            continue;
        
        
UpdateClientCount(++g_iClientCount);
    }
}

public 
void OnClientPostAdminCheck(int client)
{
    if (
IsFakeClient(client))
        return;
        
    
UpdateClientCount(++g_iClientCount);
}

public 
void OnClientDisconnect(int client)
{
    if (
IsFakeClient(client))
        return;
        
    
UpdateClientCount(--g_iClientCount);
}

public 
void UpdateClientCount(int count)
{
    switch (
count)
    {
        case 
4:
            
g_cDifficulty.SetString("easy");
        case 
8:
            
g_cDifficulty.SetString("normal");
        case 
12:
            
g_cDifficulty.SetString("hard");
        case 
16:
            
g_cDifficulty.SetString("impossible");
    }

__________________
I like this BBS sharing of spirit

I come from China, my English is poor
phoenix0001 is offline
midnight9
Senior Member
Join Date: Nov 2012
Old 11-17-2017 , 21:47   Re: [L4D2]Ask for help
Reply With Quote #2

Maybe because not always OnClientDisconnect is being called when client leaves and that results Client Count having wrong value after a while.
Maybe its worth trying reset the Client Count, OnMapStart or something like that
midnight9 is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 11-17-2017 , 22:07   Re: [L4D2]Ask for help
Reply With Quote #3

Quote:
Originally Posted by midnight9 View Post
Maybe because not always OnClientDisconnect is being called when client leaves and that results Client Count having wrong value after a while.
Maybe its worth trying reset the Client Count, OnMapStart or something like that
Yeah, and maybe OnMapStart there should be a check for client count. Looking at the source code, it just seems like the plugin only adds to the current client count when a player connects but, as you said, the value isn't always being reduced since OnClientDisconnect isn't being called all the time. This could happen when a client leaves during map transition or campaign change.
__________________
Psyk0tik is offline
midnight9
Senior Member
Join Date: Nov 2012
Old 11-18-2017 , 13:21   Re: [L4D2]Ask for help
Reply With Quote #4

Quote:
Originally Posted by Crasher_3637 View Post
Yeah, and maybe OnMapStart there should be a check for client count. Looking at the source code, it just seems like the plugin only adds to the current client count when a player connects but, as you said, the value isn't always being reduced since OnClientDisconnect isn't being called all the time. This could happen when a client leaves during map transition or campaign change.
Yea the map change is exactly what i meant, because i've tested it myself and neither OnclientDisconnect or player_disconnect are being called when client leaves during map change.
midnight9 is offline
phoenix0001
Senior Member
Join Date: Apr 2010
Location: China
Old 11-18-2017 , 19:59   Re: [L4D2]Ask for help
Reply With Quote #5

Quote:
Originally Posted by midnight9 View Post
Maybe because not always OnClientDisconnect is being called when client leaves and that results Client Count having wrong value after a while.
Maybe its worth trying reset the Client Count, OnMapStart or something like that
Can you fix him for me? thank you
__________________
I like this BBS sharing of spirit

I come from China, my English is poor
phoenix0001 is offline
phoenix0001
Senior Member
Join Date: Apr 2010
Location: China
Old 11-18-2017 , 20:00   Re: [L4D2]Ask for help
Reply With Quote #6

Quote:
Originally Posted by Crasher_3637 View Post
Yeah, and maybe OnMapStart there should be a check for client count. Looking at the source code, it just seems like the plugin only adds to the current client count when a player connects but, as you said, the value isn't always being reduced since OnClientDisconnect isn't being called all the time. This could happen when a client leaves during map transition or campaign change.
Can you fix him for me? thank you
__________________
I like this BBS sharing of spirit

I come from China, my English is poor
phoenix0001 is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 11-19-2017 , 04:37   Re: [L4D2]Ask for help
Reply With Quote #7

Quote:
Originally Posted by phoenix0001 View Post
Can you fix him for me? thank you
The problem I'm seeing is that when a client disconnects, regardless of whether a check is called for any disconnect type, the g_iClientCount doesn't reduce.

For example: When a player leaves during a game, if we were already on Expert difficulty and the 13-16th player leaves, we're still gonna be stuck on Expert difficulty.


Side note:

According to the wiki, "++g_iClientCount" would add 2 per client. Use "g_iClientCount++" instead. Same thing for "--g_iClientCount", change to "g_iClientCount--".
__________________
Psyk0tik is offline
8guawong
AlliedModders Donor
Join Date: Dec 2013
Location: BlackMarke7
Old 11-19-2017 , 13:46   Re: [L4D2]Ask for help
Reply With Quote #8

add
PHP Code:
public void OnMapStart() 

    
g_iClientCount 0;

__________________
8guawong is offline
Lux
Veteran Member
Join Date: Jan 2015
Location: Cat
Old 11-19-2017 , 14:07   Re: [L4D2]Ask for help
Reply With Quote #9

PHP Code:
#include <sourcemod>

#pragma semicolon 1
#define PLUGIN_VERSION "1.0"
#pragma newdecls required

ConVar g_cDifficulty;

public 
Plugin myinfo =
{
    
name "[L4D2] Difficulty Adjuster",
    
author "Tak (Chaosxk)",
    
description "Adjusts difficulty based on number of clients in server.",
    
version PLUGIN_VERSION,
    
url "https://forums.alliedmods.net/showthread.php?t=297009"
}

public 
void OnPluginStart()
{
    if ((
g_cDifficulty FindConVar("z_difficulty")) == null)
    {
        
LogError("[SM] Can not find convar z_difficulty");
        
SetFailState("Can not find convar z_difficulty");
    }
    
UpdateClientCount(GetSurvivorCount());
}

public 
void OnClientPostAdminCheck(int client)
{
    if (
IsFakeClient(client))
        return;
    
    
UpdateClientCount(GetSurvivorCount());
}

public 
void OnClientDisconnect(int client)
{
    if (
IsFakeClient(client))
        return;
    
    
UpdateClientCount(GetSurvivorCount());
}

void UpdateClientCount(int count)
{
    switch (
count)
    {
        case 
4:
        
g_cDifficulty.SetString("easy");
        case 
8:
        
g_cDifficulty.SetString("normal");
        case 
12:
        
g_cDifficulty.SetString("hard");
        case 
16:
        
g_cDifficulty.SetString("impossible");
    }
}

int GetSurvivorCount()
{
    
int iCount 0;
    
    for(
int iClient 1iClient <= MaxClientsiClient++) {
        if(!
IsClientInGame(iClient) || IsFakeClient(iClient) || GetClientTeam(iClient) != 2)
            continue;
        
        
iCount++;
    }
    return 
iCount;

Maybe work? :O
__________________
Connect
My Plugins: KlickME
[My GitHub]

Commission me for L4D
Lux is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 11-19-2017 , 19:11   Re: [L4D2]Ask for help
Reply With Quote #10

Quote:
Originally Posted by Lux View Post
PHP Code:
#include <sourcemod>

#pragma semicolon 1
#define PLUGIN_VERSION "1.0"
#pragma newdecls required

ConVar g_cDifficulty;

public 
Plugin myinfo =
{
    
name "[L4D2] Difficulty Adjuster",
    
author "Tak (Chaosxk)",
    
description "Adjusts difficulty based on number of clients in server.",
    
version PLUGIN_VERSION,
    
url "https://forums.alliedmods.net/showthread.php?t=297009"
}

public 
void OnPluginStart()
{
    if ((
g_cDifficulty FindConVar("z_difficulty")) == null)
    {
        
LogError("[SM] Can not find convar z_difficulty");
        
SetFailState("Can not find convar z_difficulty");
    }
    
UpdateClientCount(GetSurvivorCount());
}

public 
void OnClientPostAdminCheck(int client)
{
    if (
IsFakeClient(client))
        return;
    
    
UpdateClientCount(GetSurvivorCount());
}

public 
void OnClientDisconnect(int client)
{
    if (
IsFakeClient(client))
        return;
    
    
UpdateClientCount(GetSurvivorCount());
}

void UpdateClientCount(int count)
{
    switch (
count)
    {
        case 
4:
        
g_cDifficulty.SetString("easy");
        case 
8:
        
g_cDifficulty.SetString("normal");
        case 
12:
        
g_cDifficulty.SetString("hard");
        case 
16:
        
g_cDifficulty.SetString("impossible");
    }
}

int GetSurvivorCount()
{
    
int iCount 0;
    
    for(
int iClient 1iClient <= MaxClientsiClient++) {
        if(!
IsClientInGame(iClient) || IsFakeClient(iClient) || GetClientTeam(iClient) != 2)
            continue;
        
        
iCount++;
    }
    return 
iCount;

Maybe work? :O
That seems to work.

Is it possible to change the required values by cvars?

Ex: To switch to Expert, the default amount of players needed is 16. If there was a cvar to change that, one could lower the requirement to 8, etc.
__________________
Psyk0tik is offline
Reply



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 03:12.


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