cssdm_events.cpp
changeset 0 67dcd379a2d5
child 21 158fd55fde91
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/cssdm_events.cpp	Sun Jun 24 21:58:09 2007 +0000
     1.3 @@ -0,0 +1,408 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * ===============================================================
     1.7 + * CS:S DM, Copyright (C) 2004-2007 AlliedModders LLC. 
     1.8 + * By David "BAILOPAN" Anderson
     1.9 + * All rights reserved.
    1.10 + * ===============================================================
    1.11 + *
    1.12 + * This program is free software; you can redistribute it and/or modify
    1.13 + * it under the terms of the GNU General Public License as published by
    1.14 + * the Free Software Foundation; either version 2 of the License, or (at
    1.15 + * your option) any later version.
    1.16 + * 
    1.17 + * This program is distributed in the hope that it will be useful, but
    1.18 + * WITHOUT ANY WARRANTY; without even the implied warranty of
    1.19 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.20 + * General Public License for more details.
    1.21 + * 
    1.22 + * You should have received a copy of the GNU General Public License
    1.23 + * along with this program; see the file COPYING; if not, write to the
    1.24 + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
    1.25 + * MA 02110-1301 USA
    1.26 + * 
    1.27 + * Version: $Id$
    1.28 + */
    1.29 +
    1.30 +#include "cssdm_events.h"
    1.31 +#include "cssdm_players.h"
    1.32 +#include "cssdm_utils.h"
    1.33 +#include "cssdm_config.h"
    1.34 +#include "cssdm_headers.h"
    1.35 +#include "cssdm_callbacks.h"
    1.36 +#include "cssdm_weapons.h"
    1.37 +#include "cssdm_detours.h"
    1.38 +#include <sh_list.h>
    1.39 +
    1.40 +using namespace SourceHook;
    1.41 +
    1.42 +bool g_InRoundRestart = false;
    1.43 +List<ITimer *> g_RagdollTimers;
    1.44 +
    1.45 +class RagdollRemoval : public ITimedEvent
    1.46 +{
    1.47 +public:
    1.48 +	ResultType OnTimer(ITimer *pTimer, void *pData)
    1.49 +	{
    1.50 +		if (g_IsRunning)
    1.51 +		{
    1.52 +			IDataPack *pack = (IDataPack *)pData;
    1.53 +			pack->Reset();
    1.54 +			int index = pack->ReadCell();
    1.55 +			int serial = pack->ReadCell();
    1.56 +			edict_t *pEdict = engine->PEntityOfEntIndex(index);
    1.57 +			if (DM_CheckSerial(pEdict, serial))
    1.58 +			{
    1.59 +				CBaseEntity *pEntity = DM_GetBaseEntity(index);
    1.60 +				if (pEntity)
    1.61 +				{
    1.62 +					DM_RemoveEntity(pEntity);
    1.63 +				}
    1.64 +			}
    1.65 +		}
    1.66 +		g_RagdollTimers.remove(pTimer);
    1.67 +
    1.68 +		return Pl_Stop;
    1.69 +	}
    1.70 +
    1.71 +	void OnTimerEnd(ITimer *pTimer, void *pData)
    1.72 +	{
    1.73 +		g_pSM->FreeDataPack((IDataPack *)pData);
    1.74 +	}
    1.75 +} s_RagdollRemover;
    1.76 +
    1.77 +class PlayerSpawner : public ITimedEvent
    1.78 +{
    1.79 +public:
    1.80 +	ResultType OnTimer(ITimer *pTimer, void *pData)
    1.81 +	{
    1.82 +		if (!g_IsRunning)
    1.83 +		{
    1.84 +			return Pl_Stop;
    1.85 +		}
    1.86 +
    1.87 +		/* Read pack data */
    1.88 +		IDataPack *pack = (IDataPack *)pData;
    1.89 +		pack->Reset();
    1.90 +		int client = pack->ReadCell();
    1.91 +
    1.92 +		dm_player_t *player = DM_GetPlayer(client);
    1.93 +
    1.94 +		/* Make sure the team is valid */
    1.95 +		int team = player->pInfo->GetTeamIndex();
    1.96 +		if (team != CS_TEAM_T && team != CS_TEAM_CT)
    1.97 +		{
    1.98 +			return Pl_Stop;
    1.99 +		}
   1.100 +
   1.101 +		/* Make sure they're dead */
   1.102 +		if (DM_IsPlayerAlive(client))
   1.103 +		{
   1.104 +			return Pl_Stop;
   1.105 +		}
   1.106 +
   1.107 +		DM_RespawnPlayer(client);
   1.108 +
   1.109 +		return Pl_Stop;
   1.110 +	}
   1.111 +
   1.112 +	void OnTimerEnd(ITimer *pTimer, void *pData)
   1.113 +	{
   1.114 +		IDataPack *pack = (IDataPack *)pData;
   1.115 +		pack->Reset();
   1.116 +		int client = pack->ReadCell();
   1.117 +
   1.118 +		dm_player_t *player = DM_GetPlayer(client);
   1.119 +		if (player->respawn_timer == pTimer)
   1.120 +		{
   1.121 +			player->respawn_timer = NULL;
   1.122 +		}
   1.123 +
   1.124 +		g_pSM->FreeDataPack((IDataPack *)pData);
   1.125 +	}
   1.126 +} s_PlayerSpawner;
   1.127 +
   1.128 +void DM_SchedRespawn(int client)
   1.129 +{
   1.130 +	dm_player_t *player = DM_GetPlayer(client);
   1.131 +
   1.132 +	if (player->respawn_timer)
   1.133 +	{
   1.134 +		timersys->KillTimer(player->respawn_timer);
   1.135 +	}
   1.136 +
   1.137 +	IDataPack *pack = g_pSM->CreateDataPack();
   1.138 +	pack->PackCell(client);
   1.139 +
   1.140 +	player->respawn_timer = timersys->CreateTimer(&s_PlayerSpawner, DM_GetRespawnWait(), pack, 0);
   1.141 +}
   1.142 +
   1.143 +void OnClientCommand_Post(edict_t *edict)
   1.144 +{
   1.145 +	if (!g_IsRunning)
   1.146 +	{
   1.147 +		return;
   1.148 +	}
   1.149 +
   1.150 +	const char *cmd = engine->Cmd_Argv(0);
   1.151 +
   1.152 +	if (strcmp(cmd, "joinclass") == 0)
   1.153 +	{
   1.154 +		int client = engine->IndexOfEdict(edict);
   1.155 +		dm_player_t *player = DM_GetPlayer(client);
   1.156 +		if (!player || !player->pEntity)
   1.157 +		{
   1.158 +			return;
   1.159 +		}
   1.160 +	
   1.161 +		if (DM_IsPlayerAlive(client))
   1.162 +		{
   1.163 +			return;
   1.164 +		}
   1.165 +	
   1.166 +		if (!player->will_respawn_on_class)
   1.167 +		{
   1.168 +			return;
   1.169 +		}
   1.170 +
   1.171 +		player->will_respawn_on_class = false;
   1.172 +
   1.173 +		/* Respawn! */
   1.174 +		DM_SchedRespawn(client);
   1.175 +	}
   1.176 +}
   1.177 +
   1.178 +void OnClientDropWeapons(CBaseEntity *pEntity)
   1.179 +{
   1.180 +	if (g_IsRunning)
   1.181 +	{
   1.182 +		/* Block them from having a defuse kit so they don't drop it. */
   1.183 +		DM_SetDefuseKit(pEntity, false);
   1.184 +	}
   1.185 +}
   1.186 +
   1.187 +void OnClientDroppedWeapon(CBaseEntity *pEntity, CBaseEntity *pWeapon)
   1.188 +{
   1.189 +	if (!g_IsRunning || !pWeapon)
   1.190 +	{
   1.191 +		return;
   1.192 +	}
   1.193 +
   1.194 +	if (DM_AllowC4())
   1.195 +	{
   1.196 +		edict_t *pEdict = gameents->BaseEntityToEdict(pWeapon);
   1.197 +		if (pEdict && strcmp(pEdict->GetClassName(), "weapon_c4") == 0)
   1.198 +		{
   1.199 +			return;
   1.200 +		}
   1.201 +	}
   1.202 +
   1.203 +	DM_RemoveEntity(pWeapon);
   1.204 +}
   1.205 +
   1.206 +#define IMPLEMENT_EVENT(name) \
   1.207 +	cls_event_##name g_cls_event_##name; \
   1.208 +	void cls_event_##name::FireGameEvent(IGameEvent *event)
   1.209 +
   1.210 +IMPLEMENT_EVENT(player_death)
   1.211 +{
   1.212 +	if (!g_IsRunning)
   1.213 +	{
   1.214 +		return;
   1.215 +	}
   1.216 +
   1.217 +	int userid = event->GetInt("userid");
   1.218 +
   1.219 +	int client = playerhelpers->GetClientOfUserId(userid);
   1.220 +	if (!client)
   1.221 +	{
   1.222 +		return;
   1.223 +	}
   1.224 +
   1.225 +	dm_player_t *player = DM_GetPlayer(client);
   1.226 +	if (!player)
   1.227 +	{
   1.228 +		return;
   1.229 +	}
   1.230 +
   1.231 +	player->is_spawned = false;
   1.232 +
   1.233 +	/* Remove ragdoll */
   1.234 +	unsigned int ragdollTime = DM_GetBodyStayTime();
   1.235 +	if (ragdollTime <= 20 && !g_InRoundRestart)
   1.236 +	{
   1.237 +		int serial;
   1.238 +		CBaseEntity *ragdoll = DM_GetAndClearRagdoll(player->pEntity, serial);
   1.239 +		if (ragdollTime == 0)
   1.240 +		{
   1.241 +			DM_RemoveEntity(ragdoll);
   1.242 +		} else {
   1.243 +			edict_t *pEdict = gameents->BaseEntityToEdict(ragdoll);
   1.244 +			if (pEdict)
   1.245 +			{
   1.246 +				int index = engine->IndexOfEdict(pEdict);
   1.247 +				IDataPack *pack = g_pSM->CreateDataPack();
   1.248 +				pack->PackCell(index);
   1.249 +				pack->PackCell(serial);
   1.250 +				ITimer *timer = timersys->CreateTimer(&s_RagdollRemover, ragdollTime, pack, 0);
   1.251 +				g_RagdollTimers.push_back(timer);
   1.252 +			}
   1.253 +		}
   1.254 +	}
   1.255 +
   1.256 +	if (!DM_OnClientDeath(client))
   1.257 +	{
   1.258 +		return;
   1.259 +	}
   1.260 +
   1.261 +	DM_SchedRespawn(client);
   1.262 +}
   1.263 +
   1.264 +IMPLEMENT_EVENT(player_team)
   1.265 +{
   1.266 +	if (!g_IsRunning)
   1.267 +	{
   1.268 +		return;
   1.269 +	}
   1.270 +
   1.271 +	int userid = event->GetInt("userid");
   1.272 +	int client = playerhelpers->GetClientOfUserId(userid);
   1.273 +	if (!client)
   1.274 +	{
   1.275 +		return;
   1.276 +	}
   1.277 +
   1.278 +	dm_player_t *player = DM_GetPlayer(client);
   1.279 +	if (!player || !player->pEntity)
   1.280 +	{
   1.281 +		return;
   1.282 +	}
   1.283 +
   1.284 +	if (DM_IsPlayerAlive(client) || event->GetBool("disconnect"))
   1.285 +	{
   1.286 +		return;
   1.287 +	}
   1.288 +
   1.289 +	int old_team = event->GetInt("oldteam");
   1.290 +	if (old_team == CS_TEAM_T || old_team == CS_TEAM_CT)
   1.291 +	{
   1.292 +		return;
   1.293 +	}
   1.294 +
   1.295 +	int new_team = event->GetInt("team");
   1.296 +	if (new_team != CS_TEAM_T && new_team != CS_TEAM_CT)
   1.297 +	{
   1.298 +		return;
   1.299 +	}
   1.300 +
   1.301 +	if (player->pBot)
   1.302 +	{
   1.303 +		DM_SchedRespawn(client);
   1.304 +	} else {
   1.305 +		player->will_respawn_on_class = true;
   1.306 +	}
   1.307 +}
   1.308 +
   1.309 +IMPLEMENT_EVENT(player_spawn)
   1.310 +{
   1.311 +	if (!g_IsRunning)
   1.312 +	{
   1.313 +		return;
   1.314 +	}
   1.315 +
   1.316 +	int userid = event->GetInt("userid");
   1.317 +	int client = playerhelpers->GetClientOfUserId(userid);
   1.318 +	if (!client)
   1.319 +	{
   1.320 +		return;
   1.321 +	}
   1.322 +
   1.323 +	dm_player_t *player = DM_GetPlayer(client);
   1.324 +	if (!player || !player->pEntity || (player->is_spawned && !g_InRoundRestart))
   1.325 +	{
   1.326 +		return;
   1.327 +	}
   1.328 +
   1.329 +	/* This can be fired before a player gets a team! */
   1.330 +	int team_idx = player->pInfo->GetTeamIndex();
   1.331 +	if (team_idx != CS_TEAM_T
   1.332 +		&& team_idx != CS_TEAM_CT)
   1.333 +	{
   1.334 +		return;
   1.335 +	}
   1.336 +
   1.337 +	bool old_value = player->is_spawned;
   1.338 +	player->is_spawned = true;
   1.339 +
   1.340 +	/* If we were waiting to respawn, cancel the timer */
   1.341 +	if (player->respawn_timer)
   1.342 +	{
   1.343 +		timersys->KillTimer(player->respawn_timer);
   1.344 +		player->respawn_timer = NULL;
   1.345 +	}
   1.346 +
   1.347 +	DM_OnClientSpawned(client, old_value);
   1.348 +}
   1.349 +
   1.350 +IMPLEMENT_EVENT(server_shutdown)
   1.351 +{
   1.352 +	g_IsInGlobalShutdown = true;
   1.353 +}
   1.354 +
   1.355 +IMPLEMENT_EVENT(round_start)
   1.356 +{
   1.357 +	g_InRoundRestart = false;
   1.358 +}
   1.359 +
   1.360 +IMPLEMENT_EVENT(round_end)
   1.361 +{
   1.362 +	g_InRoundRestart = true;
   1.363 +
   1.364 +	/* Clear our ragdoll timers to prevent a crash after map cleanup */
   1.365 +	List<ITimer *>::iterator iter = g_RagdollTimers.begin();
   1.366 +	while (iter != g_RagdollTimers.end())
   1.367 +	{
   1.368 +		timersys->KillTimer((*iter));
   1.369 +		iter = g_RagdollTimers.erase(iter);
   1.370 +	}
   1.371 +
   1.372 +	for (int i=1; i<=gpGlobals->maxClients; i++)
   1.373 +	{
   1.374 +		dm_player_t *player = DM_GetPlayer(i);
   1.375 +		player->is_spawned = false;
   1.376 +	}
   1.377 +}
   1.378 +
   1.379 +IMPLEMENT_EVENT(item_pickup)
   1.380 +{
   1.381 +	if (!g_IsRunning || DM_AllowC4())
   1.382 +	{
   1.383 +		return;
   1.384 +	}
   1.385 +
   1.386 +	const char *weapon = event->GetString("item");
   1.387 +	if (strcmp(weapon, "c4") != 0)
   1.388 +	{
   1.389 +		return;
   1.390 +	}
   1.391 +
   1.392 +	int userid = event->GetInt("userid");
   1.393 +	int client = playerhelpers->GetClientOfUserId(userid);
   1.394 +	if (!client)
   1.395 +	{
   1.396 +		return;
   1.397 +	}
   1.398 +
   1.399 +	dm_player_t *player = DM_GetPlayer(client);
   1.400 +	if (!player || !player->pEntity)
   1.401 +	{
   1.402 +		return;
   1.403 +	}
   1.404 +
   1.405 +	CBaseEntity *pWeapon = DM_GetWeaponFromSlot(player->pEntity, (int)WeaponType_C4);
   1.406 +	if (pWeapon)
   1.407 +	{
   1.408 +		DM_DropWeapon(player->pEntity, pWeapon);
   1.409 +	}
   1.410 +}
   1.411 +