3 * ===============================================================
4 * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC.
5 * By David "BAILOPAN" Anderson
7 * ===============================================================
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
27 #include "cssdm_events.h"
28 #include "cssdm_players.h"
29 #include "cssdm_utils.h"
30 #include "cssdm_config.h"
31 #include "cssdm_headers.h"
32 #include "cssdm_callbacks.h"
33 #include "cssdm_weapons.h"
34 #include "cssdm_detours.h"
37 using namespace SourceHook;
39 bool g_InRoundRestart = false;
40 List<ITimer *> g_RagdollTimers;
42 class RagdollRemoval : public ITimedEvent
45 ResultType OnTimer(ITimer *pTimer, void *pData)
49 IDataPack *pack = (IDataPack *)pData;
51 int index = pack->ReadCell();
52 int serial = pack->ReadCell();
53 edict_t *pEdict = engine->PEntityOfEntIndex(index);
54 if (DM_CheckSerial(pEdict, serial))
56 CBaseEntity *pEntity = DM_GetBaseEntity(index);
59 DM_RemoveEntity(pEntity);
63 g_RagdollTimers.remove(pTimer);
68 void OnTimerEnd(ITimer *pTimer, void *pData)
70 g_pSM->FreeDataPack((IDataPack *)pData);
74 class PlayerSpawner : public ITimedEvent
77 ResultType OnTimer(ITimer *pTimer, void *pData)
85 IDataPack *pack = (IDataPack *)pData;
87 int client = pack->ReadCell();
89 dm_player_t *player = DM_GetPlayer(client);
91 /* Make sure the team is valid */
92 int team = player->pInfo->GetTeamIndex();
93 if (team != CS_TEAM_T && team != CS_TEAM_CT)
98 /* Make sure they're dead */
99 if (DM_IsPlayerAlive(client))
104 DM_RespawnPlayer(client);
109 void OnTimerEnd(ITimer *pTimer, void *pData)
111 IDataPack *pack = (IDataPack *)pData;
113 int client = pack->ReadCell();
115 dm_player_t *player = DM_GetPlayer(client);
116 if (player->respawn_timer == pTimer)
118 player->respawn_timer = NULL;
121 g_pSM->FreeDataPack((IDataPack *)pData);
125 void DM_SchedRespawn(int client)
127 dm_player_t *player = DM_GetPlayer(client);
129 if (player->respawn_timer)
131 timersys->KillTimer(player->respawn_timer);
134 IDataPack *pack = g_pSM->CreateDataPack();
135 pack->PackCell(client);
137 player->respawn_timer = timersys->CreateTimer(&s_PlayerSpawner, DM_GetRespawnWait(), pack, 0);
140 void OnClientCommand_Post(edict_t *edict)
147 const char *cmd = engine->Cmd_Argv(0);
149 if (strcmp(cmd, "joinclass") == 0)
151 int client = engine->IndexOfEdict(edict);
152 dm_player_t *player = DM_GetPlayer(client);
153 if (!player || !player->pEntity)
158 if (DM_IsPlayerAlive(client))
163 if (!player->will_respawn_on_class)
168 player->will_respawn_on_class = false;
171 DM_SchedRespawn(client);
175 void OnClientDropWeapons(CBaseEntity *pEntity)
179 /* Block them from having a defuse kit so they don't drop it. */
180 DM_SetDefuseKit(pEntity, false);
184 void OnClientDroppedWeapon(CBaseEntity *pEntity, CBaseEntity *pWeapon)
186 if (!g_IsRunning || !pWeapon)
193 edict_t *pEdict = gameents->BaseEntityToEdict(pWeapon);
194 if (pEdict && strcmp(pEdict->GetClassName(), "weapon_c4") == 0)
200 DM_RemoveEntity(pWeapon);
203 #define IMPLEMENT_EVENT(name) \
204 cls_event_##name g_cls_event_##name; \
205 void cls_event_##name::FireGameEvent(IGameEvent *event)
207 IMPLEMENT_EVENT(player_death)
214 int userid = event->GetInt("userid");
216 int client = playerhelpers->GetClientOfUserId(userid);
222 dm_player_t *player = DM_GetPlayer(client);
228 player->is_spawned = false;
231 unsigned int ragdollTime = DM_GetBodyStayTime();
232 if (ragdollTime <= 20 && !g_InRoundRestart)
235 CBaseEntity *ragdoll = DM_GetAndClearRagdoll(player->pEntity, serial);
236 if (ragdollTime == 0)
238 DM_RemoveEntity(ragdoll);
240 edict_t *pEdict = gameents->BaseEntityToEdict(ragdoll);
243 int index = engine->IndexOfEdict(pEdict);
244 IDataPack *pack = g_pSM->CreateDataPack();
245 pack->PackCell(index);
246 pack->PackCell(serial);
247 ITimer *timer = timersys->CreateTimer(&s_RagdollRemover, ragdollTime, pack, 0);
248 g_RagdollTimers.push_back(timer);
253 if (!DM_OnClientDeath(client))
258 DM_SchedRespawn(client);
261 IMPLEMENT_EVENT(player_team)
268 int userid = event->GetInt("userid");
269 int client = playerhelpers->GetClientOfUserId(userid);
275 dm_player_t *player = DM_GetPlayer(client);
276 if (!player || !player->pEntity)
281 if (DM_IsPlayerAlive(client) || event->GetBool("disconnect"))
286 int old_team = event->GetInt("oldteam");
287 if (old_team == CS_TEAM_T || old_team == CS_TEAM_CT)
292 int new_team = event->GetInt("team");
293 if (new_team != CS_TEAM_T && new_team != CS_TEAM_CT)
300 DM_SchedRespawn(client);
302 player->will_respawn_on_class = true;
306 IMPLEMENT_EVENT(player_spawn)
313 int userid = event->GetInt("userid");
314 int client = playerhelpers->GetClientOfUserId(userid);
320 dm_player_t *player = DM_GetPlayer(client);
321 if (!player || !player->pEntity || (player->is_spawned && !g_InRoundRestart))
326 /* This can be fired before a player gets a team! */
327 int team_idx = player->pInfo->GetTeamIndex();
328 if (team_idx != CS_TEAM_T
329 && team_idx != CS_TEAM_CT)
334 bool old_value = player->is_spawned;
335 player->is_spawned = true;
337 /* If we were waiting to respawn, cancel the timer */
338 if (player->respawn_timer)
340 timersys->KillTimer(player->respawn_timer);
341 player->respawn_timer = NULL;
344 DM_OnClientSpawned(client, old_value);
347 IMPLEMENT_EVENT(server_shutdown)
349 g_IsInGlobalShutdown = true;
352 IMPLEMENT_EVENT(round_start)
354 g_InRoundRestart = false;
357 IMPLEMENT_EVENT(round_end)
359 g_InRoundRestart = true;
361 /* Clear our ragdoll timers to prevent a crash after map cleanup */
362 List<ITimer *>::iterator iter = g_RagdollTimers.begin();
363 while (iter != g_RagdollTimers.end())
365 timersys->KillTimer((*iter));
366 iter = g_RagdollTimers.erase(iter);
369 for (int i=1; i<=gpGlobals->maxClients; i++)
371 dm_player_t *player = DM_GetPlayer(i);
372 player->is_spawned = false;
376 IMPLEMENT_EVENT(item_pickup)
378 if (!g_IsRunning || DM_AllowC4())
383 const char *weapon = event->GetString("item");
384 if (strcmp(weapon, "c4") != 0)
389 int userid = event->GetInt("userid");
390 int client = playerhelpers->GetClientOfUserId(userid);
396 dm_player_t *player = DM_GetPlayer(client);
397 if (!player || !player->pEntity)
402 CBaseEntity *pWeapon = DM_GetWeaponFromSlot(player->pEntity, (int)WeaponType_C4);
405 DM_DropWeapon(player->pEntity, pWeapon);