extensions/tf2/teleporter.cpp
author GoD-Tony <aangione@gmail.com>
Sun Aug 04 10:03:40 2013 -0400 (2013-08-04)
changeset 3971 d2e8100b2814
parent 3533 f35b66137b06
permissions -rw-r--r--
Added ISDKHooks interface with entity listeners (bug 5602, r=psychonic).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Team Fortress 2 Extension
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "teleporter.h"
    33 
    34 CDetour *canPlayerTeleportDetour = NULL;
    35 
    36 IForward *g_teleportForward = NULL;
    37 
    38 class CTFPlayer;
    39 
    40 DETOUR_DECL_MEMBER1(CanPlayerBeTeleported, bool, CTFPlayer *, pPlayer)
    41 {
    42 	bool origCanTeleport = DETOUR_MEMBER_CALL(CanPlayerBeTeleported)(pPlayer);
    43 
    44 	cell_t teleporterCell = gamehelpers->EntityToBCompatRef((CBaseEntity *)this);
    45 	cell_t playerCell = gamehelpers->EntityToBCompatRef((CBaseEntity *)pPlayer);
    46 
    47 	if (!g_teleportForward)
    48 	{
    49 		g_pSM->LogMessage(myself, "Teleport forward is invalid");
    50 		return origCanTeleport;
    51 	}
    52 
    53 	cell_t returnValue = origCanTeleport ? 1 : 0;
    54 
    55 	g_teleportForward->PushCell(playerCell); // client
    56 	g_teleportForward->PushCell(teleporterCell); // teleporter
    57 	g_teleportForward->PushCellByRef(&returnValue); // return value
    58 
    59 	cell_t result = 0;
    60 
    61 	g_teleportForward->Execute(&result);
    62 
    63 	if (result > Pl_Continue)
    64 	{
    65 		// plugin wants to override the game (returned something other than Plugin_Continue)
    66 		return returnValue == 1;
    67 	}
    68 	else
    69 	{
    70 		return origCanTeleport; // let the game decide
    71 	}
    72 }
    73 
    74 bool InitialiseTeleporterDetour()
    75 {
    76 	canPlayerTeleportDetour = DETOUR_CREATE_MEMBER(CanPlayerBeTeleported, "CanPlayerTeleport");
    77 
    78 	if (canPlayerTeleportDetour != NULL)
    79 	{
    80 		canPlayerTeleportDetour->EnableDetour();
    81 		return true;
    82 	}
    83 
    84 	g_pSM->LogError(myself, "Teleport forward could not be initialized - Disabled hook");
    85 
    86 	return false;
    87 }
    88 
    89 void RemoveTeleporterDetour()
    90 {
    91 	if (canPlayerTeleportDetour != NULL)
    92 	{
    93 		canPlayerTeleportDetour->Destroy();
    94 		canPlayerTeleportDetour = NULL;
    95 	}
    96 }