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