summaryrefslogtreecommitdiff
path: root/game/shared/tf/tf_dropped_weapon.cpp
blob: 305df02cba668bca5582510f71db9562f3ddc041 (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"

#include "tf_dropped_weapon.h"

#ifdef CLIENT_DLL
#include "c_tf_player.h"
#include "model_types.h"
#endif // CLIENT_DLL

#ifdef GAME_DLL
#include "tf_player.h"
#include "tf_weaponbase.h"
#include "tf_weapon_medigun.h"
#include "tf_gamerules.h"
#include "tf_weapon_bottle.h"
#endif // GAME_DLL


#ifdef GAME_DLL
ConVar tf_dropped_weapon_lifetime( "tf_dropped_weapon_lifetime", "30", FCVAR_CHEAT ); 

EXTERN_SEND_TABLE( DT_ScriptCreatedItem );

LINK_ENTITY_TO_CLASS( tf_dropped_weapon, CTFDroppedWeapon );

PRECACHE_REGISTER( tf_dropped_weapon );
#else
EXTERN_RECV_TABLE( DT_ScriptCreatedItem );
#endif

IMPLEMENT_NETWORKCLASS_ALIASED( TFDroppedWeapon, DT_TFDroppedWeapon );

BEGIN_NETWORK_TABLE( CTFDroppedWeapon, DT_TFDroppedWeapon )
#if !defined( CLIENT_DLL )
	SendPropDataTable( SENDINFO_DT(m_Item), &REFERENCE_SEND_TABLE(DT_ScriptCreatedItem) ),
	SendPropFloat( SENDINFO( m_flChargeLevel ) ),
#else
	RecvPropDataTable( RECVINFO_DT(m_Item), 0, &REFERENCE_RECV_TABLE(DT_ScriptCreatedItem) ),
	RecvPropFloat( RECVINFO( m_flChargeLevel ) ),
#endif
END_NETWORK_TABLE()

IMPLEMENT_AUTO_LIST( IDroppedWeaponAutoList );

//-----------------------------------------------------------------------------
CTFDroppedWeapon::CTFDroppedWeapon()
#ifdef GAME_DLL
	: m_nClip( 0 )
	, m_nAmmo( 0 )
	, m_nDetonated( 0 )
	, m_flEnergy( 0.f )
	, m_flEffectBarRegenTime( 0.f )
#endif
{
#ifdef CLIENT_DLL
	m_pGlowEffect = NULL;
	m_bShouldGlowForLocalPlayer = false;
	m_flOldChargeLevel = 0.f;
#endif // CLIENT_DLL

	m_flChargeLevel.Set( 0.f );
}

//-----------------------------------------------------------------------------
CTFDroppedWeapon::~CTFDroppedWeapon()
{
#ifdef CLIENT_DLL
	if ( m_worldmodelStatTrakAddon )
	{
		m_worldmodelStatTrakAddon->Remove();
	}

	if ( m_effect )
	{
		ParticleProp()->StopEmission( m_effect );
		m_effect = NULL;
	}

	DestroyGlowEffect();
#endif // CLIENT_DLL
}

//-----------------------------------------------------------------------------
void CTFDroppedWeapon::Spawn()
{
#ifdef GAME_DLL
	SetModel( STRING( GetModelName() ) );

	SetMoveType( MOVETYPE_FLYGRAVITY );
	SetSolid( SOLID_BBOX );
	SetBlocksLOS( false );
	AddEFlags( EFL_NO_ROTORWASH_PUSH );

	// This will make them not collide with the player, but will collide
	// against other items + weapons
	SetCollisionGroup( COLLISION_GROUP_DEBRIS );
	CollisionProp()->UseTriggerBounds( true, ITEM_PICKUP_BOX_BLOAT );

	// Create the object in the physics system
	int nSolidFlags = GetSolidFlags() | FSOLID_NOT_STANDABLE;

	if ( VPhysicsInitNormal( SOLID_VPHYSICS, nSolidFlags, false ) == NULL )
	{
		SetSolid( SOLID_BBOX );
		AddSolidFlags( nSolidFlags );

		// If it's not physical, drop it to the floor
		if ( UTIL_DropToFloor( this, MASK_SOLID ) == 0 )
		{
			Warning( "Item %s fell out of level at %f,%f,%f\n", GetClassname(), GetAbsOrigin().x, GetAbsOrigin().y, GetAbsOrigin().z);
			UTIL_Remove( this );
			return;
		}
	}

#endif // GAME_DLL
	BaseClass::Spawn();

#ifdef GAME_DLL
	SetContextThink( &CTFDroppedWeapon::SUB_Remove, gpGlobals->curtime + tf_dropped_weapon_lifetime.GetFloat(), "RemoveThink" );
#endif // GAME_DLL
}


#ifdef CLIENT_DLL
//-----------------------------------------------------------------------------
void CTFDroppedWeapon::OnPreDataChanged( DataUpdateType_t updateType )
{
	BaseClass::OnPreDataChanged( updateType );

	m_flOldChargeLevel = m_flChargeLevel;
}

//-----------------------------------------------------------------------------
void CTFDroppedWeapon::OnDataChanged( DataUpdateType_t updateType )
{
	BaseClass::OnDataChanged( updateType );

	if ( updateType == DATA_UPDATE_CREATED )
	{
		// if its startrak attach a model to it
		if ( m_Item.GetItemID() != INVALID_ITEM_ID )
		{
			int iStrangeType = -1;
			for ( int i = 0; i < GetKillEaterAttrCount(); i++ )
			{
				if ( m_Item.FindAttribute( GetKillEaterAttr_Score( i ) ) )
				{
					iStrangeType = i;
					break;
				}
			}

			// It's strange, does it have module as well?
			if ( iStrangeType != -1 )
			{
				CAttribute_String attrModule;
				static CSchemaAttributeDefHandle pAttr_module( "weapon_uses_stattrak_module" );
				if ( m_Item.FindAttribute( pAttr_module, &attrModule ) && attrModule.has_value() )
				{
					static CSchemaAttributeDefHandle pAttr_moduleScale( "weapon_stattrak_module_scale" );
					// Does it have a stat track module
					float flScale = 1.0f;
					uint32 unFloatAsUint32 = 1;
					if ( m_Item.FindAttribute( pAttr_moduleScale, &unFloatAsUint32 ) )
					{
						flScale = (float&)unFloatAsUint32;
					}

					C_BaseAnimating *pStatTrakEnt = new class C_BaseAnimating;
					if ( pStatTrakEnt && pStatTrakEnt->InitializeAsClientEntity( "models/weapons/c_models/stattrack.mdl", RENDER_GROUP_OPAQUE_ENTITY ) )
					{
						pStatTrakEnt->AddEffects( EF_BONEMERGE );
						pStatTrakEnt->AddEffects( EF_BONEMERGE_FASTCULL );

						m_worldmodelStatTrakAddon = pStatTrakEnt;
						pStatTrakEnt->SetParent( this );
						pStatTrakEnt->SetLocalOrigin( vec3_origin );
						pStatTrakEnt->UpdatePartitionListEntry();
						pStatTrakEnt->CollisionProp()->MarkPartitionHandleDirty();
						pStatTrakEnt->SetModelScale( flScale );
						pStatTrakEnt->UpdateVisibility();

						pStatTrakEnt->SetBodygroup( 1, 1 );

						pStatTrakEnt->m_nSkin = m_Item.GetTeamNumber();	// Use the "Sad" skin

						//pStatTrakEnt->SetModelScale( 2.0f );
						//	//if ( !cl_righthand.GetBool() )
						//	//{
						//	//	pStatTrakEnt->SetBodygroup( 0, 1 ); // use a special mirror-image stattrak module that appears correct for lefties
						//	//}

						RemoveEffects( EF_NODRAW );
					}
				}
			}

			// Normal Attached models (ie festive lights)
			const CEconItemDefinition *pItemDef = m_Item.GetItemDefinition();
			if ( pItemDef )
			{
				// Update the state of additional model attachments
				m_vecAttachedModels.Purge();
				int iTeamNumber = m_Item.GetTeamNumber();
				int iAttachedModels = pItemDef->GetNumAttachedModels( iTeamNumber );
				for ( int i = 0; i < iAttachedModels; i++ )
				{
					attachedmodel_t	*pModel = pItemDef->GetAttachedModelData( iTeamNumber, i );

					int iModelIndex = modelinfo->GetModelIndex( pModel->m_pszModelName );
					if ( iModelIndex >= 0 )
					{
						AttachedModelData_t attachedModelData;
						attachedModelData.m_pModel = modelinfo->GetModel( iModelIndex );
						attachedModelData.m_iModelDisplayFlags = pModel->m_iModelDisplayFlags;
						m_vecAttachedModels.AddToTail( attachedModelData );
					}
				}

				// Festive
				{
					static CSchemaAttributeDefHandle pAttr_is_festivized( "is_festivized" );
					if ( pAttr_is_festivized && m_Item.FindAttribute( pAttr_is_festivized ) )
					{
						int iAttachedFestiveModels = pItemDef->GetNumAttachedModelsFestivized( iTeamNumber );
						if ( iAttachedFestiveModels )
						{

							for ( int i = 0; i < iAttachedFestiveModels; i++ )
							{
								attachedmodel_t	*pModel = pItemDef->GetAttachedModelDataFestivized( iTeamNumber, i );

								int iModelIndex = modelinfo->GetModelIndex( pModel->m_pszModelName );
								if ( iModelIndex >= 0 )
								{
									AttachedModelData_t attachedModelData;
									attachedModelData.m_pModel = modelinfo->GetModel( iModelIndex );
									attachedModelData.m_iModelDisplayFlags = pModel->m_iModelDisplayFlags;
									m_vecAttachedModels.AddToTail( attachedModelData );
								}

							}
						}
					}
				}
			}

			SetupParticleEffect();
		}

		SetNextClientThink( CLIENT_THINK_ALWAYS );
	}

	if ( m_flOldChargeLevel != m_flChargeLevel )
	{
		float flRem = fmod( m_flChargeLevel, 0.1f );
		if ( flRem < 0.01f )
		{
			ParticleProp()->Create( "drain_effect", PATTACH_POINT_FOLLOW, LookupAttachment( "muzzle" ) );
			EmitSound( "Medigun.DrainCharge" );
		}
	}
}

//-----------------------------------------------------------------------------
void CTFDroppedWeapon::ModifyEmitSoundParams( EmitSound_t &params )
{
	params.m_nPitch = RemapVal( m_flChargeLevel, 0.f, 1.f, 90, 180 );
	params.m_nFlags |= SND_CHANGE_PITCH;
}

//-----------------------------------------------------------------------------
bool CTFDroppedWeapon::OnInternalDrawModel( ClientModelRenderInfo_t *pInfo )
{
	if ( !BaseClass::OnInternalDrawModel( pInfo ) )
		return false;

	// Draw Attached Models
	// Draw our attached models as well
	for ( int i = 0; i < m_vecAttachedModels.Size(); i++ )
	{
		const AttachedModelData_t& attachedModel = m_vecAttachedModels[i];

		if ( attachedModel.m_pModel && ( attachedModel.m_iModelDisplayFlags & kAttachedModelDisplayFlag_WorldModel ) )
		{
			ClientModelRenderInfo_t infoAttached = *pInfo;

			infoAttached.pRenderable = this;
			infoAttached.instance = MODEL_INSTANCE_INVALID;
			infoAttached.entity_index = this->index;
			infoAttached.pModel = attachedModel.m_pModel;

			infoAttached.pModelToWorld = &infoAttached.modelToWorld;

			// Turns the origin + angles into a matrix
			AngleMatrix( infoAttached.angles, infoAttached.origin, infoAttached.modelToWorld );

			DrawModelState_t state;
			matrix3x4_t *pBoneToWorld;
			bool bMarkAsDrawn = modelrender->DrawModelSetup( infoAttached, &state, NULL, &pBoneToWorld );
			DoInternalDrawModel( &infoAttached, ( bMarkAsDrawn && ( infoAttached.flags & STUDIO_RENDER ) ) ? &state : NULL, pBoneToWorld );
		}
	}

	return true;
}
//-----------------------------------------------------------------------------
// Purpose: Get an econ material override for the given team.
// Returns: NULL if there is no override. 
//-----------------------------------------------------------------------------
IMaterial *CTFDroppedWeapon::GetEconWeaponMaterialOverride( int iTeam )
{
	CEconItemView *pItemView = GetItem();
	if ( !pItemView )
		return NULL;

	return pItemView->GetMaterialOverride( iTeam );
}

//-----------------------------------------------------------------------------
void CTFDroppedWeapon::SetupParticleEffect()
{
	attachedparticlesystem_t *pParticleSystem = NULL;

	// do community_sparkle effect if this is a community item?
	const int iQualityParticleType = m_Item.GetQualityParticleType();
	if ( iQualityParticleType > 0 )
	{
		pParticleSystem = GetItemSchema()->GetAttributeControlledParticleSystem( iQualityParticleType );
	}

	if ( !pParticleSystem )
	{
		// does this hat even have a particle effect
		static CSchemaAttributeDefHandle pAttrDef_AttachParticleEffect( "attach particle effect" );
		uint32 iValue = 0;
		if ( !m_Item.FindAttribute( pAttrDef_AttachParticleEffect, &iValue ) )
		{
			return;
		}

		const float& value_as_float = (float&)iValue;
		pParticleSystem = GetItemSchema()->GetAttributeControlledParticleSystem( value_as_float );
	}

	// failed to find any particle effect
	if ( !pParticleSystem )
	{
		return;
	}

	// Team Color
	if ( GetTeamNumber() == TF_TEAM_BLUE && V_stristr( pParticleSystem->pszSystemName, "_teamcolor_red" ))
	{
		static char pBlue[256];
		V_StrSubst( pParticleSystem->pszSystemName, "_teamcolor_red", "_teamcolor_blue", pBlue, 256 );
		pParticleSystem = GetItemSchema()->FindAttributeControlledParticleSystem( pBlue );
		if ( !pParticleSystem )
		{
			return;
		}
	}

	// World model effect
	// Stop it on both the viewmodel & the world model, because it may be removed due to first/thirdperson switch
	static char pszTempName[256];
	const char* pszSystemName = pParticleSystem->pszSystemName;

	// Weapon Remap for a Base Effect to be used on a specific weapon
	if ( pParticleSystem->bUseSuffixName && m_Item.GetItemDefinition()->GetParticleSuffix() )
	{
		V_strcpy_safe( pszTempName, pszSystemName );
		V_strcat_safe( pszTempName, "_" );
		V_strcat_safe( pszTempName, m_Item.GetItemDefinition()->GetParticleSuffix() );
		pszSystemName = pszTempName;
	}

	m_effect = ParticleProp()->Create( pszSystemName, PATTACH_ABSORIGIN_FOLLOW );
	if ( m_effect )
	{
		for ( int i=0; i<ARRAYSIZE( pParticleSystem->pszControlPoints ); ++i )
		{
			const char *pszAttachmentName = pParticleSystem->pszControlPoints[i];
			if ( pszAttachmentName && pszAttachmentName[0] != '\0' )
			{
				ParticleProp()->AddControlPoint( m_effect, i, this, PATTACH_POINT_FOLLOW, pParticleSystem->pszControlPoints[i] );
			}
		}
	}
}

//-----------------------------------------------------------------------------
void CTFDroppedWeapon::ClientThink()
{
	C_TFPlayer *pTFPlayer = C_TFPlayer::GetLocalTFPlayer();
	bool bShouldGlowForLocalPlayer = pTFPlayer && pTFPlayer->IsAlive() && pTFPlayer->CanPickupDroppedWeapon( this ) && pTFPlayer->IsLineOfSightClear( this );
	if ( bShouldGlowForLocalPlayer )
	{
		// ignore the item that the player's equipped
		int iLoadoutSlot = 0;
		CTFItemDefinition *pItemDef = m_Item.GetStaticData();
		if ( pItemDef )
		{
			int iClass = pTFPlayer->GetPlayerClass()->GetClassIndex();
			iLoadoutSlot = pItemDef->GetLoadoutSlot( iClass );
			CTFWeaponBase *pWeapon = dynamic_cast< CTFWeaponBase* >( pTFPlayer->GetEntityForLoadoutSlot( iLoadoutSlot ) );
			if ( pWeapon && *pWeapon->GetAttributeContainer()->GetItem() == m_Item )
			{
				bShouldGlowForLocalPlayer = false;
			}
		}
	}

	if ( m_bShouldGlowForLocalPlayer != bShouldGlowForLocalPlayer )
	{
		m_bShouldGlowForLocalPlayer = bShouldGlowForLocalPlayer;
		UpdateGlowEffect();
	}
}

//-----------------------------------------------------------------------------
void CTFDroppedWeapon::UpdateGlowEffect( void )
{
	// destroy the existing effect
	if ( m_pGlowEffect )
	{
		DestroyGlowEffect();
	}

	// create a new effect if we have a cart
	if ( m_bShouldGlowForLocalPlayer )
	{
		Vector color = Vector( 0.745f, 0.773f, 0.157f );
		m_pGlowEffect = new CGlowObject( this, color, 1.0, true );
	}
}

//-----------------------------------------------------------------------------
void CTFDroppedWeapon::DestroyGlowEffect( void )
{
	if ( m_pGlowEffect )
	{
		delete m_pGlowEffect;
		m_pGlowEffect = NULL;
	}
}

//-----------------------------------------------------------------------------
bool CTFDroppedWeapon::IsVisibleToTargetID( void ) const
{
	C_TFPlayer *pLocalPlayer = C_TFPlayer::GetLocalTFPlayer();
	if ( !pLocalPlayer )
		return false;
	return pLocalPlayer->CanPickupDroppedWeapon( this );
}

#endif // CLIENT_DLL

#ifdef GAME_DLL
#define MAX_DROPPED_WEAPON_COUNT	32
//-----------------------------------------------------------------------------
CTFDroppedWeapon *CTFDroppedWeapon::Create( CTFPlayer *pLastOwner, const Vector &vecOrigin, const QAngle &vecAngles, const char *pszModelName, const CEconItemView *pItem )
{
	// don't drop weapon in MVM
	if ( TFGameRules()->IsMannVsMachineMode() )
		return NULL;

	int nNumRemoved = 0;

	// make sure we clean up the same item that was dropped before dropping a new one
	for ( int i=0; i<CTFDroppedWeapon::AutoList().Count(); ++i )
	{
		CTFDroppedWeapon *pDroppedWeapon = static_cast< CTFDroppedWeapon* >( CTFDroppedWeapon::AutoList()[i] );
		if ( pDroppedWeapon->m_Item.GetItemID() == pItem->GetItemID() && pDroppedWeapon->m_Item.GetItemDefIndex() == pItem->GetItemDefIndex() && pDroppedWeapon->m_hPlayer.Get() == pLastOwner )
		{
			UTIL_Remove( pDroppedWeapon );
			nNumRemoved++;
		}
	}

	// if we're still going over max dropped weapon count, remove more items
	int nNumToRemove = CTFDroppedWeapon::AutoList().Count() - nNumRemoved - MAX_DROPPED_WEAPON_COUNT;
	for ( int i=0; i<CTFDroppedWeapon::AutoList().Count() && nNumToRemove > 0; ++i )
	{
		CTFDroppedWeapon *pDroppedWeapon = static_cast< CTFDroppedWeapon* >( CTFDroppedWeapon::AutoList()[i] );

		// skip item that we already marked for deletion
		if ( pDroppedWeapon->IsMarkedForDeletion() )
			continue;

		UTIL_Remove( pDroppedWeapon );
		nNumToRemove--;
	}

	CTFDroppedWeapon *pDroppedWeapon = static_cast<CTFDroppedWeapon*>( CBaseAnimating::CreateNoSpawn( "tf_dropped_weapon", vecOrigin, vecAngles ) );
	if ( pDroppedWeapon )
	{
		pDroppedWeapon->SetModelName( AllocPooledString( pszModelName ) );
		pDroppedWeapon->SetItem( pItem );
		DispatchSpawn( pDroppedWeapon );
	}

	return pDroppedWeapon;
}

//-----------------------------------------------------------------------------
void CTFDroppedWeapon::InitDroppedWeapon( CTFPlayer *pPlayer, CTFWeaponBase *pWeapon,  bool bSwap, bool bIsSuicide /*= false*/ )
{
	m_hPlayer = pPlayer;

	// Calculate the initial impulse on the weapon.
	Vector vecImpulse( 0.0f, 0.0f, 0.0f );
	float flImpulseScale = 0.f;
	if ( bSwap && pPlayer )
	{
		Vector vecForward, vecUp;
		AngleVectors( pPlayer->EyeAngles(), &vecForward, NULL, &vecUp );
		vecImpulse += Vector(0,0,1.5); //vecUp * 0.5f;
		vecImpulse += vecForward * 1.0f;
		flImpulseScale = 250.f;
	}
	else
	{
		Vector vecRight, vecUp;
		AngleVectors( EyeAngles(), NULL, &vecRight, &vecUp );
		vecImpulse += vecUp * random->RandomFloat( -0.25, 0.25 );
		vecImpulse += vecRight * random->RandomFloat( -0.25, 0.25 );
		flImpulseScale = random->RandomFloat( 100.f, 150.f );
	}
	VectorNormalize( vecImpulse );
	vecImpulse *= flImpulseScale;
	vecImpulse += GetAbsVelocity();

	if ( VPhysicsGetObject() )
	{
		// We can probably remove this when the mass on the weapons is correct!
		VPhysicsGetObject()->SetMass( 25.0f );
		AngularImpulse angImpulse( 0, random->RandomFloat( 0, 100 ), 0 );
		VPhysicsGetObject()->SetVelocityInstantaneous( &vecImpulse, &angImpulse );
	}

	m_nSkin = pWeapon->GetSkin();
	
	m_nClip = pWeapon->IsEnergyWeapon() ? pWeapon->GetMaxClip1() : pWeapon->Clip1();
	m_nAmmo = pPlayer->GetAmmoCount( pWeapon->GetPrimaryAmmoType() );
	m_flEnergy = pWeapon->Energy_GetEnergy();
	m_flNextPrimaryAttack = pWeapon->m_flNextPrimaryAttack;
	m_flNextSecondaryAttack = pWeapon->m_flNextSecondaryAttack;

	if ( bIsSuicide )
	{
		m_flChargeLevel = 0.f;
	}
	else
	{
		CWeaponMedigun *pMedigun = dynamic_cast< CWeaponMedigun* >( pWeapon );
		if ( pMedigun )
		{
			m_flChargeLevel.Set( pMedigun->GetChargeLevel() );
			if ( m_flChargeLevel > 0.f )
			{
				SetContextThink( &CTFDroppedWeapon::ChargeLevelDegradeThink, gpGlobals->curtime + 0.1f, "ChargeLevelDegradeThink" );
			}
		}
	}

	CTFStickBomb *pStickBomb = dynamic_cast< CTFStickBomb* >( pWeapon );
	if ( pStickBomb )
	{
		m_nDetonated = pStickBomb->GetDetonated();
	}

	// Capture bar regen (Jarate, base ball)
	m_flEffectBarRegenTime = pWeapon->m_flEffectBarRegenTime;

	//DevMsg( "Dropped weapon with: clip[%d] ammo[%d] energy[%f]\n", m_nClip, m_nAmmo, m_flEnergy );
}

//-----------------------------------------------------------------------------
void CTFDroppedWeapon::InitPickedUpWeapon( CTFPlayer *pPlayer, CTFWeaponBase *pWeapon )
{
	// clear the context think
	SetContextThink( NULL, 0, "ChargeLevelDegradeThink" );

	// preserve the ammo
	int nCurrentMetal = pPlayer->GetAmmoCount( TF_AMMO_METAL );
	pWeapon->m_iClip1 = m_nClip;
	if ( pWeapon->GetPrimaryAmmoType() != -1 )
	{
		pPlayer->SetAmmoCount( m_nAmmo, pWeapon->GetPrimaryAmmoType() );
	}
	// SetAmmoCount can override metal for some weapon
	// Make sure engineer don't gain metal by picking up weapon
	pPlayer->SetAmmoCount( nCurrentMetal, TF_AMMO_METAL );
	pWeapon->Energy_SetEnergy( m_flEnergy );

	CWeaponMedigun *pMedigun = dynamic_cast< CWeaponMedigun* >( pWeapon );
	if ( pMedigun )
	{
		pMedigun->SetChargeLevel( m_flChargeLevel );
	}

	CTFStickBomb *pStickBomb = dynamic_cast< CTFStickBomb* >( pWeapon );
	if ( pStickBomb )
	{
		pStickBomb->SetDetonated( m_nDetonated );
	}

	// stomp the team color
	if ( pWeapon->GetAttributeContainer() && pWeapon->GetAttributeContainer()->GetItem() )
	{
		pWeapon->GetAttributeContainer()->GetItem()->SetTeamNumber( GetItem()->GetTeamNumber() );
	}

	pWeapon->m_flEffectBarRegenTime = m_flEffectBarRegenTime;
 	pWeapon->m_flNextPrimaryAttack = m_flNextPrimaryAttack;
 	pWeapon->m_flNextSecondaryAttack = m_flNextSecondaryAttack;
}

//-----------------------------------------------------------------------------
void CTFDroppedWeapon::ChargeLevelDegradeThink()
{
	m_flChargeLevel.Set( m_flChargeLevel - 0.01f );

	if ( m_flChargeLevel < 0.f )
	{
		m_flChargeLevel.Set( 0.f );
		SetContextThink( NULL, 0, "ChargeLevelDegradeThink" );
		return;
	}

	SetContextThink( &CTFDroppedWeapon::ChargeLevelDegradeThink, gpGlobals->curtime + 0.1f, "ChargeLevelDegradeThink" );
}

//-----------------------------------------------------------------------------
void CTFDroppedWeapon::SetItem( const CEconItemView *pItem )
{
	if ( pItem )
		m_Item.CopyFrom( *pItem );
}
#endif // GAME_DLL