summaryrefslogtreecommitdiff
path: root/game/server/dod/dod_ammo_box.cpp
blob: 19cc22d7918c71311cf427e2ac5a0c75e59bd412 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//

#include "cbase.h"
#include "dod_ammo_box.h"
#include "dod_player.h"
#include "dod_gamerules.h"

BEGIN_DATADESC( CAmmoBox )
	DEFINE_THINKFUNC( FlyThink ),
	DEFINE_ENTITYFUNC( BoxTouch ),
END_DATADESC();

LINK_ENTITY_TO_CLASS( dod_ammo_box, CAmmoBox );

void CAmmoBox::Spawn( void )
{
	Precache( );
	SetModel( "models/ammo/ammo_us.mdl" );
	BaseClass::Spawn();

	SetNextThink( gpGlobals->curtime + 0.75f );
	SetThink( &CAmmoBox::FlyThink );

	SetTouch( &CAmmoBox::BoxTouch );

	m_hOldOwner = GetOwnerEntity();
}

void CAmmoBox::Precache( void )
{
	PrecacheModel( "models/ammo/ammo_axis.mdl" );
	PrecacheModel( "models/ammo/ammo_us.mdl" );
}

CAmmoBox *CAmmoBox::Create( const Vector &vecOrigin, const QAngle &vecAngles, CBaseEntity *pOwner, int team )
{
	CAmmoBox *p = static_cast<CAmmoBox *> ( CBaseAnimating::Create( "dod_ammo_box", vecOrigin, vecAngles, pOwner ) );

	p->SetAmmoTeam( team );

	return p;
}

void CAmmoBox::SetAmmoTeam( int team )
{
	switch( team )
	{
	case TEAM_ALLIES:
		{
			SetModel( "models/ammo/ammo_us.mdl" );
		}
		break;
	case TEAM_AXIS:
		{
			SetModel( "models/ammo/ammo_axis.mdl" );
		}
		break;
	default:
		Assert(0);
		break;
	}

	m_iAmmoTeam = team;
}

void CAmmoBox::FlyThink( void )
{
	SetOwnerEntity( NULL );	//so our owner can pick it back up
}

void CAmmoBox::BoxTouch( CBaseEntity *pOther )
{
	Assert( pOther );

	if( !pOther->IsPlayer() )
		return;

	if( !pOther->IsAlive() )
		return;

	//Don't let the person who threw this ammo pick it up until it hits the ground.
	//This way we can throw ammo to people, but not touch it as soon as we throw it ourselves
	if( GetOwnerEntity() == pOther )
		return;

	CDODPlayer *pPlayer = ToDODPlayer( pOther );

	Assert( pPlayer );

	if( pPlayer->GetTeamNumber() != m_iAmmoTeam )
		return;

	if( pPlayer == m_hOldOwner )
	{
		//don't give ammo, just give him his drop again
		pPlayer->ReturnGenericAmmo();	
		UTIL_Remove(this);
	}
	else
	{
		//See if they can use some ammo, if so, remove the box
		if( pPlayer->GiveGenericAmmo() )
			UTIL_Remove(this);
	}	
}

bool CAmmoBox::MyTouch( CBasePlayer *pBasePlayer )
{
	if ( !pBasePlayer )
	{
		Assert( false );
		return false;
	}

	if( !pBasePlayer->IsAlive() )
		return false;

	if( pBasePlayer->GetTeamNumber() != m_iAmmoTeam )
		return false;

	CDODPlayer *pPlayer = ToDODPlayer( pBasePlayer );

	if( pPlayer == m_hOldOwner )
	{
		//don't give ammo, just give him his drop again
		pPlayer->ReturnGenericAmmo();	
		UTIL_Remove(this);
	}
	else
	{
		//See if they can use some ammo, if so, remove the box
		if( pPlayer->GiveGenericAmmo() )
			UTIL_Remove(this);
	}

	return true;
}