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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#include "cbase.h"
#include "attribute_manager.h"
#include "gamestringpool.h"
#include "saverestore.h"
#include "saverestore_utlvector.h"
#include "fmtstr.h"
#include "KeyValues.h"
#include "econ_item_system.h"
#if defined( TF_DLL ) || defined( TF_CLIENT_DLL )
#include "tf_gamerules.h" // attribute cache flushing; can be generalized if/when Dota needs similar functionality
#endif // defined( TF_DLL ) || defined( TF_CLIENT_DLL )
#define PROVIDER_PARITY_BITS 6
#define PROVIDER_PARITY_MASK ((1<<PROVIDER_PARITY_BITS)-1)
//==================================================================================================================
// ATTRIBUTE MANAGER SAVE/LOAD & NETWORKING
//===================================================================================================================
BEGIN_DATADESC_NO_BASE( CAttributeManager )
DEFINE_UTLVECTOR( m_Providers, FIELD_EHANDLE ),
DEFINE_UTLVECTOR( m_Receivers, FIELD_EHANDLE ),
DEFINE_FIELD( m_iReapplyProvisionParity, FIELD_INTEGER ),
DEFINE_FIELD( m_hOuter, FIELD_EHANDLE ),
// DEFINE_FIELD( m_bPreventLoopback, FIELD_BOOLEAN ), // Don't need to save
DEFINE_FIELD( m_ProviderType, FIELD_INTEGER ),
END_DATADESC()
BEGIN_DATADESC( CAttributeContainer )
DEFINE_EMBEDDED( m_Item ),
END_DATADESC()
#ifndef DOTA_DLL
BEGIN_DATADESC( CAttributeContainerPlayer )
END_DATADESC()
#endif
#ifndef CLIENT_DLL
EXTERN_SEND_TABLE( DT_ScriptCreatedItem );
#else
EXTERN_RECV_TABLE( DT_ScriptCreatedItem );
#endif
BEGIN_NETWORK_TABLE_NOBASE( CAttributeManager, DT_AttributeManager )
#ifndef CLIENT_DLL
SendPropEHandle( SENDINFO(m_hOuter) ),
SendPropInt( SENDINFO(m_ProviderType), 4, SPROP_UNSIGNED ),
SendPropInt( SENDINFO(m_iReapplyProvisionParity), PROVIDER_PARITY_BITS, SPROP_UNSIGNED ),
#else
RecvPropEHandle( RECVINFO(m_hOuter) ),
RecvPropInt( RECVINFO(m_ProviderType) ),
RecvPropInt( RECVINFO(m_iReapplyProvisionParity) ),
#endif
END_NETWORK_TABLE()
BEGIN_NETWORK_TABLE_NOBASE( CAttributeContainer, DT_AttributeContainer )
#ifndef CLIENT_DLL
SendPropEHandle( SENDINFO(m_hOuter) ),
SendPropInt( SENDINFO(m_ProviderType), 4, SPROP_UNSIGNED ),
SendPropInt( SENDINFO(m_iReapplyProvisionParity), PROVIDER_PARITY_BITS, SPROP_UNSIGNED ),
SendPropDataTable(SENDINFO_DT(m_Item), &REFERENCE_SEND_TABLE(DT_ScriptCreatedItem)),
#else
RecvPropEHandle( RECVINFO(m_hOuter) ),
RecvPropInt( RECVINFO(m_ProviderType) ),
RecvPropInt( RECVINFO(m_iReapplyProvisionParity) ),
RecvPropDataTable(RECVINFO_DT(m_Item), 0, &REFERENCE_RECV_TABLE(DT_ScriptCreatedItem)),
#endif
END_NETWORK_TABLE()
#ifndef DOTA_DLL
BEGIN_NETWORK_TABLE_NOBASE( CAttributeContainerPlayer, DT_AttributeContainerPlayer )
#ifndef CLIENT_DLL
SendPropEHandle( SENDINFO(m_hOuter) ),
SendPropInt( SENDINFO(m_ProviderType), 4, SPROP_UNSIGNED ),
SendPropInt( SENDINFO(m_iReapplyProvisionParity), PROVIDER_PARITY_BITS, SPROP_UNSIGNED ),
SendPropEHandle( SENDINFO(m_hPlayer) ),
#else
RecvPropEHandle( RECVINFO(m_hOuter) ),
RecvPropInt( RECVINFO(m_ProviderType) ),
RecvPropInt( RECVINFO(m_iReapplyProvisionParity) ),
RecvPropEHandle( RECVINFO( m_hPlayer ) ),
#endif
END_NETWORK_TABLE()
#endif
template< class T > T AttributeConvertFromFloat( float flValue )
{
return static_cast<T>( flValue );
}
template<> float AttributeConvertFromFloat<float>( float flValue )
{
return flValue;
}
template<> int AttributeConvertFromFloat<int>( float flValue )
{
return RoundFloatToInt( flValue );
}
//-----------------------------------------------------------------------------
// All fields in the object are all initialized to 0.
//-----------------------------------------------------------------------------
void *CAttributeManager::operator new( size_t stAllocateBlock )
{
// call into engine to get memory
Assert( stAllocateBlock != 0 );
void *pMem = malloc( stAllocateBlock );
memset( pMem, 0, stAllocateBlock );
return pMem;
};
void *CAttributeManager::operator new( size_t stAllocateBlock, int nBlockUse, const char *pFileName, int nLine )
{
// call into engine to get memory
Assert( stAllocateBlock != 0 );
void *pMem = malloc( stAllocateBlock );
memset( pMem, 0, stAllocateBlock );
return pMem;
}
CAttributeManager::CAttributeManager()
{
m_nCalls = 0;
m_nCurrentTick = 0;
}
#ifdef CLIENT_DLL
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAttributeManager::OnPreDataChanged( DataUpdateType_t updateType )
{
m_iOldReapplyProvisionParity = m_iReapplyProvisionParity;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAttributeManager::OnDataChanged( DataUpdateType_t updateType )
{
if ( m_iReapplyProvisionParity != m_iOldReapplyProvisionParity )
{
// We've changed who we're providing to in some way. Reapply it.
IHasAttributes *pAttribInterface = GetAttribInterface( GetOuter() );
if ( pAttribInterface )
{
pAttribInterface->ReapplyProvision();
}
ClearCache();
m_iOldReapplyProvisionParity = m_iReapplyProvisionParity.Get();
}
}
#endif // CLIENT_DLL
//-----------------------------------------------------------------------------
// Purpose: Call this inside your entity's Spawn()
//-----------------------------------------------------------------------------
void CAttributeManager::InitializeAttributes( CBaseEntity *pEntity )
{
Assert( GetAttribInterface( pEntity ) );
m_hOuter = pEntity;
m_bPreventLoopback = false;
}
//=====================================================================================================
// ATTRIBUTE PROVIDERS
//=====================================================================================================
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAttributeManager::ProvideTo( CBaseEntity *pProvider )
{
IHasAttributes *pOwnerAttribInterface = GetAttribInterface( pProvider );
if ( pOwnerAttribInterface )
{
pOwnerAttribInterface->GetAttributeManager()->AddProvider( m_hOuter.Get() );
#ifndef CLIENT_DLL
m_iReapplyProvisionParity = (m_iReapplyProvisionParity + 1) & PROVIDER_PARITY_MASK;
NetworkStateChanged();
#endif
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAttributeManager::StopProvidingTo( CBaseEntity *pProvider )
{
IHasAttributes *pOwnerAttribInterface = GetAttribInterface( pProvider );
if ( pOwnerAttribInterface )
{
pOwnerAttribInterface->GetAttributeManager()->RemoveProvider( m_hOuter.Get() );
#ifndef CLIENT_DLL
m_iReapplyProvisionParity = (m_iReapplyProvisionParity + 1) & PROVIDER_PARITY_MASK;
NetworkStateChanged();
#endif
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAttributeManager::AddProvider( CBaseEntity *pProvider )
{
// Make sure he's not already in our list, and prevent circular provision
Assert( !IsBeingProvidedToBy(pProvider) );
Assert( !IsProvidingTo(pProvider) );
// Ensure he's allowed to provide
IHasAttributes *pProviderAttrInterface = GetAttribInterface( pProvider );
Assert( pProviderAttrInterface );
m_Providers.AddToTail( pProvider );
pProviderAttrInterface->GetAttributeManager()->m_Receivers.AddToTail( GetOuter() );
ClearCache();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAttributeManager::RemoveProvider( CBaseEntity *pProvider )
{
Assert( pProvider );
IHasAttributes *pProviderAttrInterface = GetAttribInterface( pProvider );
Assert( pProviderAttrInterface );
if ( !IsBeingProvidedToBy( pProvider ) )
return;
Assert( pProviderAttrInterface->GetAttributeManager()->IsProvidingTo( GetOuter() ) );
Assert( pProviderAttrInterface->GetAttributeManager()->m_Receivers.Find( GetOuter() ) != pProviderAttrInterface->GetAttributeManager()->m_Receivers.InvalidIndex() );
m_Providers.FindAndFastRemove( pProvider );
pProviderAttrInterface->GetAttributeManager()->m_Receivers.FindAndFastRemove( GetOuter() );
ClearCache();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CAttributeManager::ClearCache( void )
{
if ( m_bPreventLoopback )
return;
m_CachedResults.Purge();
m_bPreventLoopback = true;
// Tell all providers relying on me that they need to wipe their cache too
FOR_EACH_VEC( m_Receivers, i )
{
IHasAttributes *pAttribInterface = GetAttribInterface( m_Receivers[i].Get() );
if ( pAttribInterface )
{
pAttribInterface->GetAttributeManager()->ClearCache();
}
}
// Tell our owner that he needs to clear his too, in case he has attributes affecting him
IHasAttributes *pMyAttribInterface = GetAttribInterface( m_hOuter.Get().Get() );
if ( pMyAttribInterface )
{
pMyAttribInterface->GetAttributeManager()->ClearCache();
}
m_bPreventLoopback = false;
#ifndef CLIENT_DLL
// Force out client to clear their cache as well
m_iReapplyProvisionParity = (m_iReapplyProvisionParity + 1) & PROVIDER_PARITY_MASK;
NetworkStateChanged();
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CAttributeManager::GetGlobalCacheVersion() const
{
#if defined( TF_DLL ) || defined( TF_CLIENT_DLL )
return TFGameRules() ? TFGameRules()->GetGlobalAttributeCacheVersion() : 0;
#else
return 0;
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Return true if this entity is providing attributes to the specified entity
//-----------------------------------------------------------------------------
bool CAttributeManager::IsProvidingTo( CBaseEntity *pEntity ) const
{
IHasAttributes *pAttribInterface = GetAttribInterface( pEntity );
if ( pAttribInterface )
{
if ( pAttribInterface->GetAttributeManager()->IsBeingProvidedToBy( GetOuter() ) )
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Return true if this entity is being provided attributes by the specified entity
//-----------------------------------------------------------------------------
bool CAttributeManager::IsBeingProvidedToBy( CBaseEntity *pEntity ) const
{
return ( m_Providers.Find( pEntity ) != m_Providers.InvalidIndex() );
}
//=====================================================================================================
// ATTRIBUTE HOOKS
//=====================================================================================================
//-----------------------------------------------------------------------------
// Purpose: Wrapper that checks to see if we've already got the result in our cache
//-----------------------------------------------------------------------------
float CAttributeManager::ApplyAttributeFloatWrapper( float flValue, CBaseEntity *pInitiator, string_t iszAttribHook, CUtlVector<CBaseEntity*> *pItemList )
{
VPROF_BUDGET( "CAttributeManager::ApplyAttributeFloatWrapper", VPROF_BUDGETGROUP_ATTRIBUTES );
#ifdef DEBUG
AssertMsg1( m_nCalls != 5000, "%d calls for attributes in a single tick. This is slow and bad.", m_nCalls );
if( m_nCurrentTick != gpGlobals->tickcount )
{
m_nCalls = 0;
m_nCurrentTick = gpGlobals->tickcount;
}
++m_nCalls;
#endif
// Have we requested a global attribute cache flush?
const int iGlobalCacheVersion = GetGlobalCacheVersion();
if ( m_iCacheVersion != iGlobalCacheVersion )
{
ClearCache();
m_iCacheVersion = iGlobalCacheVersion;
}
// We can't cache off item references so if we asked for them we need to execute the whole slow path.
if ( !pItemList )
{
int iCount = m_CachedResults.Count();
for ( int i = iCount-1; i >= 0; i-- )
{
if ( m_CachedResults[i].iAttribHook == iszAttribHook )
{
if ( m_CachedResults[i].in.fl == flValue )
return m_CachedResults[i].out.fl;
// We've got a cached result for a different flIn value. Remove the cached result to
// prevent stacking up entries for different requests (i.e. crit chance)
m_CachedResults.Remove(i);
break;
}
}
}
// Wasn't in cache, or we need item references. Do the work.
float flResult = ApplyAttributeFloat( flValue, pInitiator, iszAttribHook, pItemList );
// Add it to our cache if we didn't ask for item references. We could add the result value here
// even if we did but we'd need to walk the cache to search for an old entry to overwrite first.
if ( !pItemList )
{
int iIndex = m_CachedResults.AddToTail();
m_CachedResults[iIndex].in.fl = flValue;
m_CachedResults[iIndex].out.fl = flResult;
m_CachedResults[iIndex].iAttribHook = iszAttribHook;
}
return flResult;
}
//-----------------------------------------------------------------------------
// Purpose: Wrapper that checks to see if we've already got the result in our cache
//-----------------------------------------------------------------------------
string_t CAttributeManager::ApplyAttributeStringWrapper( string_t iszValue, CBaseEntity *pInitiator, string_t iszAttribHook, CUtlVector<CBaseEntity*> *pItemList /*= NULL*/ )
{
// Have we requested a global attribute cache flush?
const int iGlobalCacheVersion = GetGlobalCacheVersion();
if ( m_iCacheVersion != iGlobalCacheVersion )
{
ClearCache();
m_iCacheVersion = iGlobalCacheVersion;
}
// We can't cache off item references so if we asked for them we need to execute the whole slow path.
if ( !pItemList )
{
int iCount = m_CachedResults.Count();
for ( int i = iCount-1; i >= 0; i-- )
{
if ( m_CachedResults[i].iAttribHook == iszAttribHook )
{
if ( m_CachedResults[i].in.isz == iszValue )
{
return m_CachedResults[i].out.isz;
}
// We've got a cached result for a different flIn value. Remove the cached result to
// prevent stacking up entries for different requests (i.e. crit chance)
m_CachedResults.Remove(i);
break;
}
}
}
// Wasn't in cache, or we need item references. Do the work.
string_t iszOut = ApplyAttributeString( iszValue, pInitiator, iszAttribHook, pItemList );
// Add it to our cache if we didn't ask for item references. We could add the result value here
// even if we did but we'd need to walk the cache to search for an old entry to overwrite first.
if ( !pItemList )
{
int iIndex = m_CachedResults.AddToTail();
m_CachedResults[iIndex].in.isz = iszValue;
m_CachedResults[iIndex].out.isz = iszOut;
m_CachedResults[iIndex].iAttribHook = iszAttribHook;
}
return iszOut;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
float CAttributeManager::ApplyAttributeFloat( float flValue, CBaseEntity *pInitiator, string_t iszAttribHook, CUtlVector<CBaseEntity*> *pItemList )
{
VPROF_BUDGET( "CAttributeManager::ApplyAttributeFloat", VPROF_BUDGETGROUP_ATTRIBUTES );
if ( m_bPreventLoopback || !GetOuter() )
return flValue;
// We need to prevent loopback between two items both providing to the same entity.
m_bPreventLoopback = true;
IHasAttributes *pInitiatorAttribInterface = GetAttribInterface( pInitiator );
// See if we have any providers. If we do, tell them to apply.
FOR_EACH_VEC( m_Providers, iHook )
{
CBaseEntity *pProvider = m_Providers[iHook].Get();
if ( !pProvider )
continue;
if ( pProvider == pInitiator )
continue;
IHasAttributes *pAttribInterface = GetAttribInterface( pProvider );
Assert( pAttribInterface );
// Don't allow weapons to provide to other weapons being carried by the same person
if ( pInitiatorAttribInterface &&
pAttribInterface->GetAttributeManager()->GetProviderType() == PROVIDER_WEAPON &&
pInitiatorAttribInterface->GetAttributeManager()->GetProviderType() == PROVIDER_WEAPON )
{
continue;
}
flValue = pAttribInterface->GetAttributeManager()->ApplyAttributeFloat( flValue, pInitiator, iszAttribHook, pItemList );
}
// Then see if our owner has any attributes he wants to apply as well.
// i.e. An aura is providing attributes to this weapon's carrier.
IHasAttributes *pMyAttribInterface = GetAttribInterface( m_hOuter.Get().Get() );
Assert( pMyAttribInterface );
if ( pMyAttribInterface && pMyAttribInterface->GetAttributeOwner() )
{
IHasAttributes *pOwnerAttribInterface = GetAttribInterface( pMyAttribInterface->GetAttributeOwner() );
if ( pOwnerAttribInterface )
{
flValue = pOwnerAttribInterface->GetAttributeManager()->ApplyAttributeFloat( flValue, pInitiator, iszAttribHook, pItemList );
}
}
m_bPreventLoopback = false;
return flValue;
}
string_t CAttributeManager::ApplyAttributeString( string_t iszValue, CBaseEntity *pInitiator, string_t iszAttribHook /*= NULL_STRING*/, CUtlVector<CBaseEntity*> *pItemList /*= NULL*/ )
{
VPROF_BUDGET( "CAttributeManager::ApplyAttributeString", VPROF_BUDGETGROUP_ATTRIBUTES );
if ( m_bPreventLoopback || !GetOuter() )
return iszValue;
// We need to prevent loopback between two items both providing to the same entity.
m_bPreventLoopback = true;
IHasAttributes *pInitiatorAttribInterface = GetAttribInterface( pInitiator );
// See if we have any providers. If we do, tell them to apply.
FOR_EACH_VEC( m_Providers, iHook )
{
CBaseEntity *pProvider = m_Providers[iHook].Get();
if ( !pProvider )
continue;
if ( pProvider == pInitiator )
continue;
IHasAttributes *pAttribInterface = GetAttribInterface( pProvider );
Assert( pAttribInterface );
// Don't allow weapons to provide to other weapons being carried by the same person
if ( pInitiatorAttribInterface &&
pAttribInterface->GetAttributeManager()->GetProviderType() == PROVIDER_WEAPON &&
pInitiatorAttribInterface->GetAttributeManager()->GetProviderType() == PROVIDER_WEAPON )
{
continue;
}
iszValue = pAttribInterface->GetAttributeManager()->ApplyAttributeString( iszValue, pInitiator, iszAttribHook, pItemList );
}
// Then see if our owner has any attributes he wants to apply as well.
// i.e. An aura is providing attributes to this weapon's carrier.
IHasAttributes *pMyAttribInterface = GetAttribInterface( m_hOuter.Get().Get() );
Assert( pMyAttribInterface );
if ( pMyAttribInterface->GetAttributeOwner() )
{
IHasAttributes *pOwnerAttribInterface = GetAttribInterface( pMyAttribInterface->GetAttributeOwner() );
if ( pOwnerAttribInterface )
{
iszValue = pOwnerAttribInterface->GetAttributeManager()->ApplyAttributeString( iszValue, pInitiator, iszAttribHook, pItemList );
}
}
m_bPreventLoopback = false;
return iszValue;
}
//=====================================================================================================
// ATTRIBUTE CONTAINER
//=====================================================================================================
//-----------------------------------------------------------------------------
// Purpose: Call this inside your entity's Spawn()
//-----------------------------------------------------------------------------
void CAttributeContainer::InitializeAttributes( CBaseEntity *pEntity )
{
BaseClass::InitializeAttributes( pEntity );
#ifndef CLIENT_DLL
/*
if ( !m_Item.IsValid() )
{
Warning("Item '%s' not setup correctly. Attempting to create attributes on an unitialized item.\n", m_hOuter.Get()->GetDebugName() );
}
*/
#endif
m_Item.GetAttributeList()->SetManager( this );
OnAttributeValuesChanged();
}
static void ApplyAttribute( const CEconItemAttributeDefinition *pAttributeDef, float& flValue, const float flValueModifier )
{
Assert( pAttributeDef );
Assert( pAttributeDef->GetAttributeType() );
AssertMsg1( pAttributeDef->GetAttributeType()->BSupportsGameplayModificationAndNetworking(), "Attempt to hook the value of attribute '%s' which doesn't support hooking! Pull the value of the attribute directly using FindAttribute()!", pAttributeDef->GetDefinitionName() );
const int iAttrDescFormat = pAttributeDef->GetDescriptionFormat();
switch ( iAttrDescFormat )
{
case ATTDESCFORM_VALUE_IS_PERCENTAGE:
case ATTDESCFORM_VALUE_IS_INVERTED_PERCENTAGE:
{
flValue *= flValueModifier;
}
break;
case ATTDESCFORM_VALUE_IS_ADDITIVE:
case ATTDESCFORM_VALUE_IS_ADDITIVE_PERCENTAGE:
case ATTDESCFORM_VALUE_IS_PARTICLE_INDEX:
{
flValue += flValueModifier;
}
break;
case ATTDESCFORM_VALUE_IS_KILLSTREAK_IDLEEFFECT_INDEX:
case ATTDESCFORM_VALUE_IS_KILLSTREAKEFFECT_INDEX:
case ATTDESCFORM_VALUE_IS_FROM_LOOKUP_TABLE:
{
flValue = flValueModifier;
}
break;
case ATTDESCFORM_VALUE_IS_OR:
{
int iTmp = flValue;
iTmp |= (int)flValueModifier;
flValue = iTmp;
}
break;
case ATTDESCFORM_VALUE_IS_DATE:
Assert( !"Attempt to apply date attribute in ApplyAttribute()." ); // No-one should be hooking date descriptions
break;
default:
// Unknown value format.
AssertMsg1( false, "Unknown attribute value type %i in ApplyAttribute().", iAttrDescFormat );
break;
}
}
//-----------------------------------------------------------------------------
// Purpose: Given two attributes, return a collated value.
//-----------------------------------------------------------------------------
float CollateAttributeValues( const CEconItemAttributeDefinition *pAttrDef1, const float flAttribValue1, const CEconItemAttributeDefinition *pAttrDef2, const float flAttribValue2 )
{
Assert( pAttrDef1 );
Assert( pAttrDef2 );
AssertMsg2( !Q_stricmp( pAttrDef1->GetAttributeClass(), pAttrDef2->GetAttributeClass() ), "We can only collate attributes of matching definitions: mismatch between '%s' / '%s'!", pAttrDef1->GetAttributeClass(), pAttrDef2->GetAttributeClass() );
AssertMsg2( pAttrDef1->GetDescriptionFormat() == pAttrDef2->GetDescriptionFormat(), "We can only collate attributes of matching description format: mismatch between '%u' / '%u'!", pAttrDef1->GetDescriptionFormat(), pAttrDef2->GetDescriptionFormat() );
const int iAttrDescFormat = pAttrDef1->GetDescriptionFormat();
float flValue = 0;
switch ( iAttrDescFormat )
{
case ATTDESCFORM_VALUE_IS_PERCENTAGE:
case ATTDESCFORM_VALUE_IS_INVERTED_PERCENTAGE:
{
flValue = 1.0;
}
break;
case ATTDESCFORM_VALUE_IS_ADDITIVE:
case ATTDESCFORM_VALUE_IS_ADDITIVE_PERCENTAGE:
case ATTDESCFORM_VALUE_IS_FROM_LOOKUP_TABLE:
case ATTDESCFORM_VALUE_IS_OR:
{
flValue = 0;
}
break;
case ATTDESCFORM_VALUE_IS_DATE:
Assert( !"Attempt to apply date attribute in CollateAttributeValues()." ); // No-one should be hooking date descriptions
break;
default:
// Unknown value format.
AssertMsg1( false, "Unknown attribute value type %i in ApplyAttribute().", iAttrDescFormat );
break;
}
ApplyAttribute( pAttrDef1, flValue, flAttribValue1 );
ApplyAttribute( pAttrDef2, flValue, flAttribValue2 );
return flValue;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
class CEconItemAttributeIterator_ApplyAttributeFloat : public CEconItemSpecificAttributeIterator
{
public:
CEconItemAttributeIterator_ApplyAttributeFloat( CBaseEntity *pOuter, float flInitialValue, string_t iszAttribHook, CUtlVector<CBaseEntity *> *pItemList )
: m_pOuter( pOuter )
, m_flValue( flInitialValue )
, m_iszAttribHook( iszAttribHook )
, m_pItemList( pItemList )
{
Assert( pOuter );
}
virtual bool OnIterateAttributeValue( const CEconItemAttributeDefinition *pAttrDef, attrib_value_t value )
{
COMPILE_TIME_ASSERT( sizeof( value ) == sizeof( float ) );
Assert( pAttrDef );
if ( pAttrDef->GetCachedClass() != m_iszAttribHook )
return true;
if ( m_pItemList && !m_pItemList->HasElement( m_pOuter ) )
{
m_pItemList->AddToTail( m_pOuter );
}
ApplyAttribute( pAttrDef, m_flValue, *reinterpret_cast<float *>( &value ) );
// We assume that each attribute can only be in the attribute list for a single item once, but we're
// iterating over attribute *classes* here, not unique attribute types, so we carry on looking.
return true;
}
float GetResultValue() const
{
return m_flValue;
}
private:
CBaseEntity *m_pOuter;
float m_flValue;
string_t m_iszAttribHook;
CUtlVector<CBaseEntity *> *m_pItemList;
};
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
float CAttributeContainer::ApplyAttributeFloat( float flValue, CBaseEntity *pInitiator, string_t iszAttribHook, CUtlVector<CBaseEntity*> *pItemList )
{
if ( m_bPreventLoopback || !GetOuter() )
return flValue;
// We need to prevent loopback between two items both providing to the same entity.
m_bPreventLoopback = true;
// ...
CEconItemAttributeIterator_ApplyAttributeFloat it( GetOuter(), flValue, iszAttribHook, pItemList );
m_Item.IterateAttributes( &it );
m_bPreventLoopback = false;
return BaseClass::ApplyAttributeFloat( it.GetResultValue(), pInitiator, iszAttribHook, pItemList );
}
#ifndef DOTA_DLL
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
float CAttributeContainerPlayer::ApplyAttributeFloat( float flValue, CBaseEntity *pInitiator, string_t iszAttribHook, CUtlVector<CBaseEntity*> *pItemList )
{
if ( m_bPreventLoopback || !GetOuter() )
return flValue;
m_bPreventLoopback = true;
CEconItemAttributeIterator_ApplyAttributeFloat it( GetOuter(), flValue, iszAttribHook, pItemList );
CBasePlayer *pPlayer = GetPlayer();
if ( pPlayer )
{
pPlayer->m_AttributeList.IterateAttributes( &it );
}
m_bPreventLoopback = false;
return BaseClass::ApplyAttributeFloat( it.GetResultValue(), pInitiator, iszAttribHook, pItemList );
}
#endif
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
class CEconItemAttributeIterator_ApplyAttributeString : public CEconItemSpecificAttributeIterator
{
public:
CEconItemAttributeIterator_ApplyAttributeString( CBaseEntity *pOuter, string_t iszInitialValue, string_t iszAttribHook, CUtlVector<CBaseEntity *> *pItemList )
: m_pOuter( pOuter )
, m_iszValue( iszInitialValue )
, m_iszAttribHook( iszAttribHook )
, m_pItemList( pItemList )
, m_bFoundString( false )
{
Assert( pOuter );
}
virtual bool OnIterateAttributeValue( const CEconItemAttributeDefinition *pAttrDef, attrib_value_t value )
{
COMPILE_TIME_ASSERT( sizeof( value ) == sizeof( float ) );
// Do we want to process attribute of this type?
Assert( pAttrDef );
Assert( pAttrDef->GetCachedClass() != m_iszAttribHook );
//AssertMsg( 0, "OnIterateAttributeValue of type CAttribute_String, we shouldn't get here." );
return true;
}
virtual bool OnIterateAttributeValue( const CEconItemAttributeDefinition *pAttrDef, const CAttribute_String& value )
{
Assert( pAttrDef );
if ( pAttrDef->GetCachedClass() != m_iszAttribHook )
return true;
if ( FoundString() )
return true;
m_iszValue = AllocPooledString( value.value().c_str() );
m_bFoundString = true;
return true;
}
string_t GetResultValue()
{
return m_iszValue;
}
private:
bool FoundString()
{
// Implement something for the case where there's more than one of the same attribute
AssertMsg( !m_bFoundString, "Already found a string attribute with %s class, return the first attribute found.", STRING( m_iszAttribHook ) );
return m_bFoundString;
}
CBaseEntity *m_pOuter;
string_t m_iszValue;
string_t m_iszAttribHook;
CUtlVector<CBaseEntity *> *m_pItemList;
bool m_bFoundString;
};
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
string_t CAttributeContainer::ApplyAttributeString( string_t iszValue, CBaseEntity *pInitiator, string_t iszAttribHook /*= NULL_STRING*/, CUtlVector<CBaseEntity*> *pItemList /*= NULL*/ )
{
if ( m_bPreventLoopback || !GetOuter() )
return iszValue;
// We need to prevent loopback between two items both providing to the same entity.
m_bPreventLoopback = true;
// ...
CEconItemAttributeIterator_ApplyAttributeString it( GetOuter(), iszValue, iszAttribHook, pItemList );
m_Item.IterateAttributes( &it );
m_bPreventLoopback = false;
return BaseClass::ApplyAttributeString( it.GetResultValue(), pInitiator, iszAttribHook, pItemList );
}
string_t CAttributeContainerPlayer::ApplyAttributeString( string_t iszValue, CBaseEntity *pInitiator, string_t iszAttribHook /*= NULL_STRING*/, CUtlVector<CBaseEntity*> *pItemList /*= NULL*/ )
{
if ( m_bPreventLoopback || !GetOuter() )
return iszValue;
m_bPreventLoopback = true;
CEconItemAttributeIterator_ApplyAttributeString it( GetOuter(), iszValue, iszAttribHook, pItemList );
CBasePlayer *pPlayer = GetPlayer();
if ( pPlayer )
{
pPlayer->m_AttributeList.IterateAttributes( &it );
}
m_bPreventLoopback = false;
return BaseClass::ApplyAttributeString( it.GetResultValue(), pInitiator, iszAttribHook, pItemList );
}
|