summaryrefslogtreecommitdiff
path: root/sourcemod/scripting/gokz-core/teamnumfix.sp
blob: 0c5d81d607cbaf20a50cb6c93e6a00c98437dd98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
static Handle H_RemovePlayer;
static int teamEntID[4];
static int oldTeam[MAXPLAYERS + 1];
static int realTeam[MAXPLAYERS + 1];

void OnPluginStart_TeamNumber()
{
	GameData gamedataConf = LoadGameConfigFile("gokz-core.games");
	if (gamedataConf == null)
	{
		SetFailState("Failed to load gokz-core gamedata");
	}
	
	StartPrepSDKCall(SDKCall_Entity);
	PrepSDKCall_SetVirtual(gamedataConf.GetOffset("CCSTeam::RemovePlayer"));
	PrepSDKCall_AddParameter(SDKType_CBasePlayer, SDKPass_Pointer);
	H_RemovePlayer = EndPrepSDKCall();
	if (H_RemovePlayer == INVALID_HANDLE)
	{
		SetFailState("Unable to prepare SDKCall for CCSTeam::RemovePlayer!");
	}
}

void OnMapStart_TeamNumber()
{
	// Fetch the entity ID of team entities and store them.
	int team = FindEntityByClassname(MaxClients + 1, "cs_team_manager");
	while (team != -1)
	{
		int teamNum = GetEntProp(team, Prop_Send, "m_iTeamNum");
		teamEntID[teamNum] = team;
		team = FindEntityByClassname(team, "cs_team_manager");
	}
}

void OnGameFrame_TeamNumber()
{
	for (int client = 1; client <= MaxClients; client++)
	{
		if (!IsClientInGame(client) || !IsPlayerAlive(client))
		{
			continue;
		}
		int team = GetEntProp(client, Prop_Data, "m_iTeamNum");
		// If the entprop changed, remove the player from the old team, but make sure it's a valid team first
		if (team != oldTeam[client] && oldTeam[client] < 4 && oldTeam[client] > 0)
		{
			SDKCall(H_RemovePlayer, teamEntID[oldTeam[client]], client);
		}
		oldTeam[client] = team;
	}
}

void OnPlayerJoinTeam_TeamNumber(Event event, int client)
{
	// If the old team value is invalid, fix it.
	if (event.GetInt("oldteam") > 4 || event.GetInt("oldteam") < 0)
	{
		event.SetInt("oldteam", 0);
	}
	realTeam[client] = event.GetInt("team");
}

void OnPlayerDeath_TeamNumber(int client)
{
	// Switch the client's team to a valid team to prevent crashes.
	CS_SwitchTeam(client, realTeam[client]);
}