Okay. So. The following code works great if I start a server by myself and someone new joins in. I start off alone, and when a new client joins a bot is created. When they join the game, they take the bot's place. When they leave, the bot goes bye-bye.
But if I start a game with two people in a lobby, "survivor_limit" is increased to four and we get two bots, aka a full team.
Can anyone figure out why?
PHP Code:
#include <sourcemod>
#include <sdktools>
#define PLUGIN_VERSION "2.0"
public Plugin:myinfo =
{
name = "L4D/L4D2 Less Than 4 Dead (v2)",
author = "chinagreenelvis",
description = "Removes survivor bots from multiplayer games",
version = PLUGIN_VERSION,
url = "http://www.chinagreenelvis.com"
}
new potentialsurvivors = 0;
public OnPluginStart()
{
CreateConVar("l4d_2_lessthan4dead_v2_minsurvivors", "1", "Minimum number of survivors to allow", FCVAR_PLUGIN);
CreateConVar("l4d_2_lessthan4dead_v2_maxsurvivors", "4", "Maximum number of survivors to allow (additional plugins required for more than 4 players)", FCVAR_PLUGIN);
SetConVarInt(FindConVar("director_no_survivor_bots"), 1);
SetConVarInt(FindConVar("survivor_limit"), 1);
}
public OnClientConnected(client)
{
if (!IsFakeClient(client))
{
PrintToChatAll("potentialsurvivors = %i", potentialsurvivors);
new minsurvivors = GetConVarInt(FindConVar("l4d_2_lessthan4dead_v2_minsurvivors"));
new maxsurvivors = GetConVarInt(FindConVar("l4d_2_lessthan4dead_v2_maxsurvivors"));
new survivorlimit = GetConVarInt(FindConVar("survivor_limit"));
if (potentialsurvivors == survivorlimit)
{
if (potentialsurvivors >= minsurvivors)
{
if (survivorlimit < maxsurvivors)
{
CreateTimer(1.0, Timer_SurvivorLimitAdd);
}
}
else if (potentialsurvivors < minsurvivors)
{
ServerCommand("sb_add");
}
}
potentialsurvivors++;
PrintToChatAll("potentialsurvivors = %i", potentialsurvivors);
}
}
public OnClientDisconnect(client)
{
if (!IsFakeClient(client))
{
new minsurvivors = GetConVarInt(FindConVar("l4d_2_lessthan4dead_v2_minsurvivors"));
new survivorlimit = GetConVarInt(FindConVar("survivor_limit"));
PrintToChatAll("potentialsurvivors = %i", potentialsurvivors);
if (potentialsurvivors > minsurvivors)
{
if (survivorlimit > minsurvivors)
{
CreateTimer(1.0, Timer_SurvivorLimitSubtract);
}
}
potentialsurvivors--;
PrintToChatAll("potentialsurvivors = %i", potentialsurvivors);
}
}
public Action:Timer_SurvivorLimitAdd(Handle:timer)
{
new maxsurvivors = GetConVarInt(FindConVar("l4d_2_lessthan4dead_v2_maxsurvivors"));
new survivorlimit = GetConVarInt(FindConVar("survivor_limit"));
if (survivorlimit < maxsurvivors)
{
SetConVarInt(FindConVar("survivor_limit"), survivorlimit + 1, false, false);
ServerCommand("sb_add");
}
}
public Action:Timer_SurvivorLimitSubtract(Handle:timer)
{
new minsurvivors = GetConVarInt(FindConVar("l4d_2_lessthan4dead_v2_minsurvivors"));
new survivorlimit = GetConVarInt(FindConVar("survivor_limit"));
if (survivorlimit > minsurvivors)
{
SetConVarInt(FindConVar("survivor_limit"), survivorlimit - 1, false, false);
KickBot();
}
}
KickBot()
{
PrintToChatAll("A bot should be about to be kicked.")
new bool:ABotHasBeenKicked = false;
for (new i = 1; i <= MaxClients; i++)
{
if (ABotHasBeenKicked == false)
{
if (IsClientConnected(i) && IsClientInGame(i) && GetClientTeam(i) == 2)
{
PrintToChatAll("A bot is very likely about to be kicked.")
if (IsFakeClient(i) && GetEntProp(i, Prop_Send, "m_humanSpectatorUserID") == 0)
{
PrintToChatAll("A bot is definitely about to be kicked.")
if (IsPlayerAlive(i))
{
ForcePlayerSuicide(i);
}
KickClient(i);
ABotHasBeenKicked = true;
}
}
}
}
}