summaryrefslogtreecommitdiff
path: root/game/server/hl1/hl1_weaponbox.cpp
blob: 07a745edc7c00c84d9e15480aeaccb26cace2697 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//


#include "cbase.h"
#include "hl1_items.h"
#include "ammodef.h"


#define WEAPONBOX_MODEL "models/w_weaponbox.mdl"


class CWeaponBox : public CHL1Item
{
public:
	DECLARE_CLASS( CWeaponBox, CHL1Item );

	void Spawn( void );
	void Precache( void );
	bool KeyValue( const char *szKeyName, const char *szValue );
	void BoxTouch( CBaseEntity *pPlayer );

	DECLARE_DATADESC();

private:
	bool		PackAmmo( char *szName, int iCount );
	int			GiveAmmo( int iCount, char *szName, int iMax, int *pIndex = NULL );
	
	int			m_cAmmoTypes;	// how many ammo types packed into this box (if packed by a level designer)
	string_t	m_rgiszAmmo[MAX_AMMO_SLOTS];	// ammo names
	int			m_rgAmmo[MAX_AMMO_SLOTS];		// ammo quantities
};
LINK_ENTITY_TO_CLASS(weaponbox, CWeaponBox);
PRECACHE_REGISTER(weaponbox);

BEGIN_DATADESC( CWeaponBox )
	DEFINE_ARRAY( m_rgiszAmmo, FIELD_STRING, MAX_AMMO_SLOTS ),
	DEFINE_ARRAY( m_rgAmmo, FIELD_INTEGER, MAX_AMMO_SLOTS ),
	DEFINE_FIELD( m_cAmmoTypes, FIELD_INTEGER ),

	DEFINE_ENTITYFUNC( BoxTouch ),
END_DATADESC()


bool CWeaponBox::KeyValue( const char *szKeyName, const char *szValue )
{
	if ( m_cAmmoTypes < MAX_AMMO_SLOTS )
	{
		if ( PackAmmo( (char *)szKeyName, atoi( szValue ) ) )
		{
			m_cAmmoTypes++;// count this new ammo type.

			return true;
		}
	}
	else
	{
		Warning( "WeaponBox too full! only %d ammotypes allowed\n", MAX_AMMO_SLOTS );
	}

	return BaseClass::KeyValue( szKeyName, szValue );
}

void CWeaponBox::Spawn( void )
{ 
	Precache();
	SetModel( WEAPONBOX_MODEL );
	BaseClass::Spawn();

	PrecacheScriptSound( "Item.Pickup" );

	SetTouch( &CWeaponBox::BoxTouch );
}


void CWeaponBox::Precache( void )
{
	PrecacheModel( WEAPONBOX_MODEL );
}


void CWeaponBox::BoxTouch( CBaseEntity *pOther )
{
	if ( !( GetFlags() & FL_ONGROUND ) )
	{
		return;
	}

	if ( !pOther->IsPlayer() )
	{
		// only players may touch a weaponbox.
		return;
	}

	if ( !pOther->IsAlive() )
	{
		// no dead guys.
		return;
	}

	CBasePlayer *pPlayer = (CBasePlayer *)pOther;
	int i;

// dole out ammo
	for ( i = 0 ; i < MAX_AMMO_SLOTS ; i++ )
	{
		if ( m_rgiszAmmo[ i ] != NULL_STRING )
		{
			// there's some ammo of this type. 
			pPlayer->GiveAmmo( m_rgAmmo[ i ], (char *)STRING( m_rgiszAmmo[ i ] ) );

			// now empty the ammo from the weaponbox since we just gave it to the player
			m_rgiszAmmo[ i ]	= NULL_STRING;
			m_rgAmmo[ i ]		= 0;
		}
	}

	CPASAttenuationFilter filter( pOther, "Item.Pickup" );
	EmitSound( filter, pOther->entindex(), "Item.Pickup" );

	SetTouch(NULL);
	if ( g_pGameRules->ItemShouldRespawn( this ) == GR_ITEM_RESPAWN_NO )
	{
		UTIL_Remove( this );
	}
}


bool CWeaponBox::PackAmmo( char *szName, int iCount )
{
	char szConvertedName[ 32 ];

	if ( FStrEq( szName, "" ) )
	{
		// error here
		Warning( "NULL String in PackAmmo!\n" );
		return false;
	}
	
	Q_snprintf( szConvertedName, sizeof( szConvertedName ), "%s", szName );
	if ( !stricmp( szName, "bolts" ) )
	{
		Q_snprintf( szConvertedName, sizeof( szConvertedName ), "XBowBolt" );
	}
	if ( !stricmp( szName, "uranium" ) )
	{
		Q_snprintf( szConvertedName, sizeof( szConvertedName ), "Uranium" );
	}
	if ( !stricmp( szName, "9mm" ) )
	{
		Q_snprintf( szConvertedName, sizeof( szConvertedName ), "9mmRound" );
	}
	if ( !stricmp( szName, "Hand Grenade" ) )
	{
		Q_snprintf( szConvertedName, sizeof( szConvertedName ), "Grenade" );
	}
	if ( !stricmp( szName, "Hornets" ) )
	{
		Q_snprintf( szConvertedName, sizeof( szConvertedName ), "Hornet" );
	}
	if ( !stricmp( szName, "ARgrenades" ) )
	{
		Q_snprintf( szConvertedName, sizeof( szConvertedName ), "MP5_Grenade" );
	}
	if ( !stricmp( szName, "357" ) )
	{
		Q_snprintf( szConvertedName, sizeof( szConvertedName ), "357Round" );
	}
	if ( !stricmp( szName, "rockets" ) )
	{
		Q_snprintf( szConvertedName, sizeof( szConvertedName ), "RPG_Rocket" );
	}
	if ( !stricmp( szName, "Satchel Charge" ) )
	{
		Q_snprintf( szConvertedName, sizeof( szConvertedName ), "Satchel" );
	}
	if ( !stricmp( szName, "buckshot" ) )
	{
		Q_snprintf( szConvertedName, sizeof( szConvertedName ), "Buckshot" );
	}
	if ( !stricmp( szName, "Snarks" ) )
	{
		Q_snprintf( szConvertedName, sizeof( szConvertedName ), "Snark" );
	}
	if ( !stricmp( szName, "Trip Mine" ) )
	{
		Q_snprintf( szConvertedName, sizeof( szConvertedName ), "TripMine" );
	}

	int iMaxCarry = GetAmmoDef()->MaxCarry( GetAmmoDef()->Index( szConvertedName ) );

	if ( iMaxCarry > 0 && iCount > 0 )
	{
		//ALERT ( at_console, "Packed %d rounds of %s\n", iCount, STRING(iszName) );
		GiveAmmo( iCount, szConvertedName, iMaxCarry );
		return true;
	}

	return false;
}

//=========================================================
// CWeaponBox - GiveAmmo
//=========================================================
int CWeaponBox::GiveAmmo( int iCount, char *szName, int iMax, int *pIndex )
{
	int i;

	for ( i = 1; ( i < MAX_AMMO_SLOTS ) && ( m_rgiszAmmo[i] != NULL_STRING ); i++ )
	{
		if ( stricmp( szName, STRING( m_rgiszAmmo[i] ) ) == 0 )
		{
			if (pIndex)
				*pIndex = i;

			int iAdd = MIN( iCount, iMax - m_rgAmmo[i]);
			if (iCount == 0 || iAdd > 0)
			{
				m_rgAmmo[i] += iAdd;

				return i;
			}
			return -1;
		}
	}

	if (i < MAX_AMMO_SLOTS)
	{
		if (pIndex)
			*pIndex = i;

		m_rgiszAmmo[i]	= AllocPooledString( szName );
		m_rgAmmo[i]		= iCount;

		return i;
	}
	Warning( "out of named ammo slots\n");
	return i;
}