EDIT:
Never mind!
As a software developer a good way to figure out some problem is to describe it to someone else. Often times you will find the solution while describing it.
There was no reason to hook into the player_team event when just creating a command to switch the teams and then do a mp_restartgame to reset the scores works good enough for what I need.
I updated the source with the working plugin that
auto selcts the team for connecting humans who are not the first human on the server and
there is a swapTeams commands that handles swapping and restarts the round.
All good.
--------------------------------------
I wrote a plugin that forces connecting humans to jon the same team as other humans on the server
and when a human player uses the choose team command to change teams
all the other humans are switched to the same team and
all bots switch teams.
It works (with other bugs listed below) but everyone dies when switched.
1) Is there a way to do the team switch so everyone switches teams without dying?
Switching teams this way causes the team score to increase and I am trying to avoid that.
The game engine will switch teams without the players dying if you are using these and you switch them and do a restart game but I am not using those settings.
mp_humanteam
bot_join_team
mp_restartgame 1
2) I am also forcing the team of humans connecting to be the same team as already in game humans.
That was working until I added the swap team feature. It looks like I need another check in the EventPlayerTeam to not switch humans that are -just connecting- and not already in game. How would I do that?
3) I have been wondering what the
bool dontBroadcast
parms is used for in these hooks?
Sort of working plugin with death and bugs with more than 1 human connecting:
PHP Code:
#include <sourcemod>
#include <sdktools>
#include <cstrike>
#pragma semicolon 1
//-----------------------------------------------------------------------------
public void OnPluginStart()
{
RegConsoleCmd("sm_st", Command_SwitchTeams);
HookEvent("player_connect_full", Event_PlayerConnectFull);
}
//-----------------------------------------------------------------------------
public void Event_PlayerConnectFull(Event event, const char[] name, bool dontBroadcast)
{
int team = GetHumanTeam();
if (team != CS_TEAM_NONE)
{
int client = GetClientOfUserId(event.GetInt("userid"));
// check if the client is valid and not assigned to a team yet
if (client && !GetClientTeam(client))
{
ChangeClientTeam(client, team);
}
}
}
//-----------------------------------------------------------------------------
public Action Command_SwitchTeams(int client, int args)
{
int newTeam;
if (GetClientTeam(client) == CS_TEAM_NONE)
return Plugin_Continue;
if (GetClientTeam(client) == CS_TEAM_T)
newTeam = CS_TEAM_CT;
else
newTeam = CS_TEAM_T;
PrintToServer("TEAMCHANGE user=%d", client);
for (int i = 1; i <= MaxClients; i++)
{
if (IsClientInGame(i))
{
// set all humans to the team the human chose to change to
if (!IsFakeClient(i))
{
ChangeClientTeam(i, newTeam);
}
else
{
// Swap the bots
if (GetClientTeam(i) == CS_TEAM_T)
ChangeClientTeam(i,CS_TEAM_CT);
else if (GetClientTeam(i) == CS_TEAM_CT)
ChangeClientTeam(i,CS_TEAM_T);
}
}
}
ServerCommand("mp_restartgame 1");
return Plugin_Continue;
}
//-----------------------------------------------------------------------------
public int GetHumanTeam()
{
for (new client = 1; client <= MaxClients; client++)
if (IsClientInGame(client) && !IsFakeClient(client))
return GetClientTeam(client);
return CS_TEAM_NONE;
}