summaryrefslogtreecommitdiff
path: root/game/server/tf2/order_killmortarguy.cpp
blob: ff2bdffe6f7908666e5a14b05ab5c9b584770827 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "order_helpers.h"
#include "order_killmortarguy.h"
#include "tf_team.h"


#define KILLMORTARGUY_DIST		3500


IMPLEMENT_SERVERCLASS_ST( COrderKillMortarGuy, DT_OrderKillMortarGuy )
END_SEND_TABLE()


static bool IsValidFn_DeployedBrianJacobsons( void *pUserData, int iClient )
{
	CBaseTFPlayer *pPlayer = (CBaseTFPlayer*)UTIL_PlayerByIndex( iClient+1 );
	if ( !pPlayer || pPlayer->IsClass( TFCLASS_UNDECIDED ) || !pPlayer->GetTeam() )
		return false;

	// Is this person on an enemy team?
	CSortBase *pSortBase = (CSortBase*)pUserData;
	CBaseTFPlayer *pMyPlayer = pSortBase->m_pPlayer;

	if( pPlayer->GetTeam()->GetTeamNumber() == pMyPlayer->GetTeam()->GetTeamNumber() )
		return false;

	// Is he alive?
	if( !pPlayer->IsAlive() )
		return false;

	// ROBIN: Removed mortar object. This needs to handle mortar vehicles instead.
	// Is he looking surly?
	//if( pPlayer->GetNumObjects( OBJ_MORTAR ) == 0 )
		//return false;

	// Is he close enough?
	if( pMyPlayer->GetAbsOrigin().DistTo( pPlayer->GetAbsOrigin() ) > KILLMORTARGUY_DIST )
		return false;

	// Is he visible to the tactical?
//	if( !pMyPlayer->GetTFTeam()->IsEntityVisibleToTactical( pPlayer ) )
//		return false;

	// KILL HIM!!!
	return true;
}


static int SortFn_PlayerEntsByDistance( void *pUserData, int a, int b )
{
	CBaseEntity *pEdictA = CBaseEntity::Instance( engine->PEntityOfEntIndex( a+1 ) );
	CBaseEntity *pEdictB = CBaseEntity::Instance( engine->PEntityOfEntIndex( b+1 ) );

	if ( !pEdictA || !pEdictB )
		return 1;

	CSortBase *pSortBase = (CSortBase*)pUserData;
	const Vector &v = pSortBase->m_pPlayer->GetAbsOrigin();

	return v.DistTo( pEdictA->GetAbsOrigin() ) < v.DistTo( pEdictB->GetAbsOrigin() );
}


bool COrderKillMortarGuy::CreateOrder( CPlayerClass *pClass )
{
	CSortBase info;
	info.m_pPlayer = pClass->GetPlayer();
	
	// Look for an enemy sniper visible to the 
	int supports[MAX_PLAYERS];
	int nSupports = BuildSortedActiveList( 
		supports,							// the sorted list
		MAX_PLAYERS,					
		SortFn_PlayerEntsByDistance,		// sort on distance
		IsValidFn_DeployedBrianJacobsons,	// only get deployed support guys
		&info,								// pUserData
		gpGlobals->maxClients				// how many players to look through
		);

	// Kill the closest punk.
	if( nSupports )
	{
		CBaseTFPlayer *pBrian = (CBaseTFPlayer*)UTIL_PlayerByIndex( supports[0]+1 );
		Assert( pBrian );

		COrderKillMortarGuy *pOrder = new COrderKillMortarGuy;
		pClass->GetTeam()->AddOrder( 
			ORDER_KILL,
			pBrian,
			pClass->GetPlayer(),
			1e24,
			60,
			pOrder
			);

		return true;
	}
	else
	{
		return false;
	}
}


bool COrderKillMortarGuy::UpdateOnEvent( COrderEvent_Base *pEvent )
{
	if ( pEvent->GetType() == ORDER_EVENT_PLAYER_KILLED )
	{
		COrderEvent_PlayerKilled *pKilled = (COrderEvent_PlayerKilled*)pEvent;
		if ( pKilled->m_pPlayer == GetTargetEntity() )
			return true;
	}

	return BaseClass::UpdateOnEvent( pEvent );
}