forked from sears2424/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.cpp
1038 lines (850 loc) · 26 KB
/
animation.cpp
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
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#include "cbase.h"
#include "studio.h"
#include "activitylist.h"
#include "engine/IEngineSound.h"
#include "ai_activity.h"
#include "animation.h"
#include "bone_setup.h"
#include "scriptevent.h"
#include "npcevent.h"
#include "eventlist.h"
#include "tier0/vprof.h"
#if !defined( CLIENT_DLL ) && !defined( MAKEXVCD )
#include "util.h"
#include "enginecallback.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#pragma warning( disable : 4244 )
#define iabs(i) (( (i) >= 0 ) ? (i) : -(i) )
int ExtractBbox( CStudioHdr *pstudiohdr, int sequence, Vector& mins, Vector& maxs )
{
if (! pstudiohdr)
return 0;
if (!pstudiohdr->SequencesAvailable())
return 0;
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc( sequence );
mins = seqdesc.bbmin;
maxs = seqdesc.bbmax;
return 1;
}
//-----------------------------------------------------------------------------
// Purpose:
//
// Input : *pstudiohdr -
// iSequence -
//
// Output : mstudioseqdesc_t
//-----------------------------------------------------------------------------
extern int g_nActivityListVersion;
extern int g_nEventListVersion;
void SetEventIndexForSequence( mstudioseqdesc_t &seqdesc )
{
if ( &seqdesc == NULL )
return;
seqdesc.flags |= STUDIO_EVENT;
if ( seqdesc.numevents == 0 )
return;
for ( int index = 0; index < (int)seqdesc.numevents; index++ )
{
mstudioevent_t *pevent = seqdesc.pEvent( index );
if ( !pevent )
continue;
if ( pevent->type & AE_TYPE_NEWEVENTSYSTEM )
{
const char *pEventName = pevent->pszEventName();
int iEventIndex = EventList_IndexForName( pEventName );
if ( iEventIndex == -1 )
{
pevent->event = EventList_RegisterPrivateEvent( pEventName );
}
else
{
pevent->event = iEventIndex;
pevent->type |= EventList_GetEventType( iEventIndex );
}
}
}
}
mstudioevent_t *GetEventIndexForSequence( mstudioseqdesc_t &seqdesc )
{
if (!(seqdesc.flags & STUDIO_EVENT))
{
SetEventIndexForSequence( seqdesc );
}
return seqdesc.pEvent( 0 );
}
void BuildAllAnimationEventIndexes( CStudioHdr *pstudiohdr )
{
if ( !pstudiohdr )
return;
if( pstudiohdr->GetEventListVersion() != g_nEventListVersion )
{
for ( int i = 0 ; i < pstudiohdr->GetNumSeq() ; i++ )
{
SetEventIndexForSequence( pstudiohdr->pSeqdesc( i ) );
}
pstudiohdr->SetEventListVersion( g_nEventListVersion );
}
}
//-----------------------------------------------------------------------------
// Purpose: Ensures that activity / index relationship is recalculated
// Input :
// Output :
//-----------------------------------------------------------------------------
void ResetEventIndexes( CStudioHdr *pstudiohdr )
{
if (! pstudiohdr)
return;
pstudiohdr->SetEventListVersion( g_nEventListVersion - 1 );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void SetActivityForSequence( CStudioHdr *pstudiohdr, int i )
{
int iActivityIndex;
const char *pszActivityName;
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc( i );
seqdesc.flags |= STUDIO_ACTIVITY;
pszActivityName = GetSequenceActivityName( pstudiohdr, i );
if ( pszActivityName[0] != '\0' )
{
iActivityIndex = ActivityList_IndexForName( pszActivityName );
if ( iActivityIndex == -1 )
{
// Allow this now. Animators can create custom activities that are referenced only on the client or by scripts, etc.
//Warning( "***\nModel %s tried to reference unregistered activity: %s \n***\n", pstudiohdr->name, pszActivityName );
//Assert(0);
// HACK: the client and server don't share the private activity list so registering it on the client would hose the server
#ifdef CLIENT_DLL
seqdesc.flags &= ~STUDIO_ACTIVITY;
#else
seqdesc.activity = ActivityList_RegisterPrivateActivity( pszActivityName );
#endif
}
else
{
seqdesc.activity = iActivityIndex;
}
}
}
//=========================================================
// IndexModelSequences - set activity and event indexes for all model
// sequences that have them.
//=========================================================
void IndexModelSequences( CStudioHdr *pstudiohdr )
{
int i;
if (! pstudiohdr)
return;
if (!pstudiohdr->SequencesAvailable())
return;
for ( i = 0 ; i < pstudiohdr->GetNumSeq() ; i++ )
{
SetActivityForSequence( pstudiohdr, i );
SetEventIndexForSequence( pstudiohdr->pSeqdesc( i ) );
}
pstudiohdr->SetActivityListVersion( g_nActivityListVersion );
}
//-----------------------------------------------------------------------------
// Purpose: Ensures that activity / index relationship is recalculated
// Input :
// Output :
//-----------------------------------------------------------------------------
void ResetActivityIndexes( CStudioHdr *pstudiohdr )
{
if (! pstudiohdr)
return;
pstudiohdr->SetActivityListVersion( g_nActivityListVersion - 1 );
}
void VerifySequenceIndex( CStudioHdr *pstudiohdr )
{
if ( !pstudiohdr )
{
return;
}
if( pstudiohdr->GetActivityListVersion( ) != g_nActivityListVersion )
{
// this model's sequences have not yet been indexed by activity
IndexModelSequences( pstudiohdr );
}
}
#if !defined( MAKEXVCD )
bool IsInPrediction()
{
return CBaseEntity::GetPredictionPlayer() != NULL;
}
int SelectWeightedSequence( CStudioHdr *pstudiohdr, int activity, int curSequence )
{
VPROF( "SelectWeightedSequence" );
if (! pstudiohdr)
return 0;
if (!pstudiohdr->SequencesAvailable())
return 0;
VerifySequenceIndex( pstudiohdr );
#if STUDIO_SEQUENCE_ACTIVITY_LOOKUPS_ARE_SLOW
int weighttotal = 0;
int seq = ACTIVITY_NOT_AVAILABLE;
int weight = 0;
for (int i = 0; i < pstudiohdr->GetNumSeq(); i++)
{
int curActivity = GetSequenceActivity( pstudiohdr, i, &weight );
if (curActivity == activity)
{
if ( curSequence == i && weight < 0 )
{
seq = i;
break;
}
weighttotal += iabs(weight);
int randomValue;
if ( IsInPrediction() )
randomValue = SharedRandomInt( "SelectWeightedSequence", 0, weighttotal - 1, i );
else
randomValue = RandomInt( 0, weighttotal - 1 );
if (!weighttotal || randomValue < iabs(weight))
seq = i;
}
}
return seq;
#else
return pstudiohdr->SelectWeightedSequence( activity, curSequence );
#endif
}
// Pick a sequence for the given activity. If the current sequence is appropriate for the
// current activity, and its stored weight is negative (whatever that means), always select
// it. Otherwise perform a weighted selection -- imagine a large roulette wheel, with each
// sequence having a number of spaces corresponding to its weight.
int CStudioHdr::CActivityToSequenceMapping::SelectWeightedSequence( CStudioHdr *pstudiohdr, int activity, int curSequence )
{
if (!ValidateAgainst(pstudiohdr))
{
AssertMsg1(false, "CStudioHdr %s has changed its vmodel pointer without reinitializing its activity mapping! Now performing emergency reinitialization.", pstudiohdr->pszName());
ExecuteOnce(DebuggerBreakIfDebugging());
Reinitialize(pstudiohdr);
}
// a null m_pSequenceTuples just means that this studio header has no activities.
if (!m_pSequenceTuples)
return ACTIVITY_NOT_AVAILABLE;
// is the current sequence appropriate?
if (curSequence >= 0)
{
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc( curSequence );
if (seqdesc.activity == activity && seqdesc.actweight < 0)
return curSequence;
}
// get the data for the given activity
HashValueType dummy( activity, 0, 0, 0 );
UtlHashHandle_t handle = m_ActToSeqHash.Find(dummy);
if (!m_ActToSeqHash.IsValidHandle(handle))
{
return ACTIVITY_NOT_AVAILABLE;
}
const HashValueType * __restrict actData = &m_ActToSeqHash[handle];
int weighttotal = actData->totalWeight;
// generate a random number from 0 to the total weight
int randomValue;
if ( IsInPrediction() )
{
randomValue = SharedRandomInt( "SelectWeightedSequence", 0, weighttotal - 1 );
}
else
{
randomValue = RandomInt( 0, weighttotal - 1 );
}
// chug through the entries in the list (they are sequential therefore cache-coherent)
// until we run out of random juice
SequenceTuple * __restrict sequenceInfo = m_pSequenceTuples + actData->startingIdx;
const SequenceTuple *const stopHere = sequenceInfo + actData->count; // this is a backup
// in case the weights are somehow miscalculated -- we don't read or write through
// it (because it aliases the restricted pointer above); it's only here for
// the comparison.
while (randomValue >= sequenceInfo->weight && sequenceInfo < stopHere)
{
randomValue -= sequenceInfo->weight;
++sequenceInfo;
}
return sequenceInfo->seqnum;
}
int CStudioHdr::CActivityToSequenceMapping::SelectWeightedSequenceFromModifiers( CStudioHdr *pstudiohdr, int activity, CUtlSymbol *pActivityModifiers, int iModifierCount )
{
if ( !pstudiohdr->SequencesAvailable() )
{
return ACTIVITY_NOT_AVAILABLE;
}
VerifySequenceIndex( pstudiohdr );
if ( pstudiohdr->GetNumSeq() == 1 )
{
return ( ::GetSequenceActivity( pstudiohdr, 0, NULL ) == activity ) ? 0 : ACTIVITY_NOT_AVAILABLE;
}
if (!ValidateAgainst(pstudiohdr))
{
AssertMsg1(false, "CStudioHdr %s has changed its vmodel pointer without reinitializing its activity mapping! Now performing emergency reinitialization.", pstudiohdr->pszName());
ExecuteOnce(DebuggerBreakIfDebugging());
Reinitialize(pstudiohdr);
}
// a null m_pSequenceTuples just means that this studio header has no activities.
if (!m_pSequenceTuples)
return ACTIVITY_NOT_AVAILABLE;
// get the data for the given activity
HashValueType dummy( activity, 0, 0, 0 );
UtlHashHandle_t handle = m_ActToSeqHash.Find(dummy);
if (!m_ActToSeqHash.IsValidHandle(handle))
{
return ACTIVITY_NOT_AVAILABLE;
}
const HashValueType * __restrict actData = &m_ActToSeqHash[handle];
// go through each sequence and give it a score
int top_score = -1;
CUtlVector<int> topScoring( actData->count, actData->count );
for ( int i = 0; i < actData->count; i++ )
{
SequenceTuple * __restrict sequenceInfo = m_pSequenceTuples + actData->startingIdx + i;
int score = 0;
// count matching activity modifiers
for ( int m = 0; m < iModifierCount; m++ )
{
int num_modifiers = sequenceInfo->iNumActivityModifiers;
for ( int k = 0; k < num_modifiers; k++ )
{
if ( sequenceInfo->pActivityModifiers[ k ] == pActivityModifiers[ m ] )
{
score++;
break;
}
}
}
if ( score > top_score )
{
topScoring.RemoveAll();
topScoring.AddToTail( sequenceInfo->seqnum );
top_score = score;
}
}
// randomly pick between the highest scoring sequences ( NOTE: this method of selecting a sequence ignores activity weights )
if ( IsInPrediction() )
{
return topScoring[ SharedRandomInt( "SelectWeightedSequence", 0, topScoring.Count() - 1 ) ];
}
return topScoring[ RandomInt( 0, topScoring.Count() - 1 ) ];
}
#endif
int SelectHeaviestSequence( CStudioHdr *pstudiohdr, int activity )
{
if ( !pstudiohdr )
return 0;
VerifySequenceIndex( pstudiohdr );
int maxweight = 0;
int seq = ACTIVITY_NOT_AVAILABLE;
int weight = 0;
for (int i = 0; i < pstudiohdr->GetNumSeq(); i++)
{
int curActivity = GetSequenceActivity( pstudiohdr, i, &weight );
if (curActivity == activity)
{
if ( iabs(weight) > maxweight )
{
maxweight = iabs(weight);
seq = i;
}
}
}
return seq;
}
void GetEyePosition ( CStudioHdr *pstudiohdr, Vector &vecEyePosition )
{
if ( !pstudiohdr )
{
Warning( "GetEyePosition() Can't get pstudiohdr ptr!\n" );
return;
}
vecEyePosition = pstudiohdr->eyeposition();
}
//-----------------------------------------------------------------------------
// Purpose: Looks up an activity by name.
// Input : label - Name of the activity to look up, ie "ACT_IDLE"
// Output : Activity index or ACT_INVALID if not found.
//-----------------------------------------------------------------------------
int LookupActivity( CStudioHdr *pstudiohdr, const char *label )
{
VPROF( "LookupActivity" );
if ( !pstudiohdr )
{
return 0;
}
for ( int i = 0; i < pstudiohdr->GetNumSeq(); i++ )
{
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc( i );
if ( stricmp( seqdesc.pszActivityName(), label ) == 0 )
{
return seqdesc.activity;
}
}
return ACT_INVALID;
}
#if !defined( MAKEXVCD )
//-----------------------------------------------------------------------------
// Purpose: Looks up a sequence by sequence name first, then by activity name.
// Input : label - The sequence name or activity name to look up.
// Output : Returns the sequence index of the matching sequence, or ACT_INVALID.
//-----------------------------------------------------------------------------
int LookupSequence( CStudioHdr *pstudiohdr, const char *label )
{
VPROF( "LookupSequence" );
if (! pstudiohdr)
return 0;
if (!pstudiohdr->SequencesAvailable())
return 0;
//
// Look up by sequence name.
//
for (int i = 0; i < pstudiohdr->GetNumSeq(); i++)
{
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc( i );
if (stricmp( seqdesc.pszLabel(), label ) == 0)
return i;
}
//
// Not found, look up by activity name.
//
int nActivity = LookupActivity( pstudiohdr, label );
if (nActivity != ACT_INVALID )
{
return SelectWeightedSequence( pstudiohdr, nActivity );
}
return ACT_INVALID;
}
void GetSequenceLinearMotion( CStudioHdr *pstudiohdr, int iSequence, const float poseParameter[], Vector *pVec )
{
if ( !pstudiohdr)
{
ExecuteNTimes( 20, Msg( "Bad pstudiohdr in GetSequenceLinearMotion()!\n" ) );
return;
}
if (!pstudiohdr->SequencesAvailable())
return;
if( iSequence < 0 || iSequence >= pstudiohdr->GetNumSeq() )
{
// Don't spam on bogus model
if ( pstudiohdr->GetNumSeq() > 0 )
{
ExecuteNTimes( 20, Msg( "Bad sequence (%i out of %i max) in GetSequenceLinearMotion() for model '%s'!\n", iSequence, pstudiohdr->GetNumSeq(), pstudiohdr->pszName() ) );
}
pVec->Init();
return;
}
QAngle vecAngles;
Studio_SeqMovement( pstudiohdr, iSequence, 0, 1.0, poseParameter, (*pVec), vecAngles );
}
#endif
const char *GetSequenceName( CStudioHdr *pstudiohdr, int iSequence )
{
if( !pstudiohdr || iSequence < 0 || iSequence >= pstudiohdr->GetNumSeq() )
{
if ( pstudiohdr )
{
Msg( "Bad sequence in GetSequenceName() for model '%s'!\n", pstudiohdr->pszName() );
}
return "Unknown";
}
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc( iSequence );
return seqdesc.pszLabel();
}
const char *GetSequenceActivityName( CStudioHdr *pstudiohdr, int iSequence )
{
if( !pstudiohdr || iSequence < 0 || iSequence >= pstudiohdr->GetNumSeq() )
{
if ( pstudiohdr )
{
Msg( "Bad sequence in GetSequenceActivityName() for model '%s'!\n", pstudiohdr->pszName() );
}
return "Unknown";
}
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc( iSequence );
return seqdesc.pszActivityName( );
}
int GetSequenceFlags( CStudioHdr *pstudiohdr, int sequence )
{
if ( !pstudiohdr ||
!pstudiohdr->SequencesAvailable() ||
sequence < 0 ||
sequence >= pstudiohdr->GetNumSeq() )
{
return 0;
}
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc( sequence );
return seqdesc.flags;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pstudiohdr -
// sequence -
// type -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool HasAnimationEventOfType( CStudioHdr *pstudiohdr, int sequence, int type )
{
if ( !pstudiohdr || sequence >= pstudiohdr->GetNumSeq() )
return false;
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc( sequence );
if ( !&seqdesc )
return false;
mstudioevent_t *pevent = GetEventIndexForSequence( seqdesc );
if ( !pevent )
return false;
if (seqdesc.numevents == 0 )
return false;
int index;
for ( index = 0; index < (int)seqdesc.numevents; index++ )
{
if ( pevent[ index ].event == type )
{
return true;
}
}
return false;
}
int GetAnimationEvent( CStudioHdr *pstudiohdr, int sequence, animevent_t *pNPCEvent, float flStart, float flEnd, int index )
{
if ( !pstudiohdr || sequence >= pstudiohdr->GetNumSeq() || !pNPCEvent )
return 0;
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc( sequence );
if (seqdesc.numevents == 0 || index >= (int)seqdesc.numevents )
return 0;
// Msg( "flStart %f flEnd %f (%d) %s\n", flStart, flEnd, seqdesc.numevents, seqdesc.label );
mstudioevent_t *pevent = GetEventIndexForSequence( seqdesc );
for (; index < (int)seqdesc.numevents; index++)
{
// Don't send client-side events to the server AI
if ( pevent[index].type & AE_TYPE_NEWEVENTSYSTEM )
{
if ( !(pevent[index].type & AE_TYPE_SERVER) )
continue;
}
else if ( pevent[index].event >= EVENT_CLIENT ) //Adrian - Support the old event system
continue;
bool bOverlapEvent = false;
if (pevent[index].cycle >= flStart && pevent[index].cycle < flEnd)
{
bOverlapEvent = true;
}
// FIXME: doesn't work with animations being played in reverse
else if ((seqdesc.flags & STUDIO_LOOPING) && flEnd < flStart)
{
if (pevent[index].cycle >= flStart || pevent[index].cycle < flEnd)
{
bOverlapEvent = true;
}
}
if (bOverlapEvent)
{
pNPCEvent->pSource = NULL;
pNPCEvent->cycle = pevent[index].cycle;
#if !defined( MAKEXVCD )
pNPCEvent->eventtime = gpGlobals->curtime;
#else
pNPCEvent->eventtime = 0.0f;
#endif
pNPCEvent->event = pevent[index].event;
pNPCEvent->options = pevent[index].pszOptions();
pNPCEvent->type = pevent[index].type;
return index + 1;
}
}
return 0;
}
int FindTransitionSequence( CStudioHdr *pstudiohdr, int iCurrentSequence, int iGoalSequence, int *piDir )
{
if ( !pstudiohdr )
return iGoalSequence;
if ( !pstudiohdr->SequencesAvailable() )
return iGoalSequence;
if ( ( iCurrentSequence < 0 ) || ( iCurrentSequence >= pstudiohdr->GetNumSeq() ) )
return iGoalSequence;
if ( ( iGoalSequence < 0 ) || ( iGoalSequence >= pstudiohdr->GetNumSeq() ) )
{
// asking for a bogus sequence. Punt.
Assert( 0 );
return iGoalSequence;
}
// bail if we're going to or from a node 0
if (pstudiohdr->EntryNode( iCurrentSequence ) == 0 || pstudiohdr->EntryNode( iGoalSequence ) == 0)
{
*piDir = 1;
return iGoalSequence;
}
int iEndNode;
// Msg( "from %d to %d: ", pEndNode->iEndNode, pGoalNode->iStartNode );
// check to see if we should be going forward or backward through the graph
if (*piDir > 0)
{
iEndNode = pstudiohdr->ExitNode( iCurrentSequence );
}
else
{
iEndNode = pstudiohdr->EntryNode( iCurrentSequence );
}
// if both sequences are on the same node, just go there
if (iEndNode == pstudiohdr->EntryNode( iGoalSequence ))
{
*piDir = 1;
return iGoalSequence;
}
int iInternNode = pstudiohdr->GetTransition( iEndNode, pstudiohdr->EntryNode( iGoalSequence ) );
// if there is no transitionial node, just go to the goal sequence
if (iInternNode == 0)
return iGoalSequence;
int i;
// look for someone going from the entry node to next node it should hit
// this may be the goal sequences node or an intermediate node
for (i = 0; i < pstudiohdr->GetNumSeq(); i++)
{
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc(i );
if (pstudiohdr->EntryNode( i ) == iEndNode && pstudiohdr->ExitNode( i ) == iInternNode)
{
*piDir = 1;
return i;
}
if (seqdesc.nodeflags)
{
if (pstudiohdr->ExitNode( i ) == iEndNode && pstudiohdr->EntryNode( i ) == iInternNode)
{
*piDir = -1;
return i;
}
}
}
// this means that two parts of the node graph are not connected.
DevMsg( 2, "error in transition graph: %s to %s\n", pstudiohdr->pszNodeName( iEndNode ), pstudiohdr->pszNodeName( pstudiohdr->EntryNode( iGoalSequence ) ));
// Go ahead and jump to the goal sequence
return iGoalSequence;
}
bool GotoSequence( CStudioHdr *pstudiohdr, int iCurrentSequence, float flCurrentCycle, float flCurrentRate, int iGoalSequence, int &nNextSequence, float &flNextCycle, int &iNextDir )
{
if ( !pstudiohdr )
return false;
if ( !pstudiohdr->SequencesAvailable() )
return false;
if ( ( iCurrentSequence < 0 ) || ( iCurrentSequence >= pstudiohdr->GetNumSeq() ) )
return false;
if ( ( iGoalSequence < 0 ) || ( iGoalSequence >= pstudiohdr->GetNumSeq() ) )
{
// asking for a bogus sequence. Punt.
Assert( 0 );
return false;
}
// bail if we're going to or from a node 0
if (pstudiohdr->EntryNode( iCurrentSequence ) == 0 || pstudiohdr->EntryNode( iGoalSequence ) == 0)
{
iNextDir = 1;
flNextCycle = 0.0;
nNextSequence = iGoalSequence;
return true;
}
int iEndNode = pstudiohdr->ExitNode( iCurrentSequence );
// Msg( "from %d to %d: ", pEndNode->iEndNode, pGoalNode->iStartNode );
// if we're in a transition sequence
if (pstudiohdr->EntryNode( iCurrentSequence ) != pstudiohdr->ExitNode( iCurrentSequence ))
{
// are we done with it?
if (flCurrentRate > 0.0 && flCurrentCycle >= 0.999)
{
iEndNode = pstudiohdr->ExitNode( iCurrentSequence );
}
else if (flCurrentRate < 0.0 && flCurrentCycle <= 0.001)
{
iEndNode = pstudiohdr->EntryNode( iCurrentSequence );
}
else
{
// nope, exit
return false;
}
}
// if both sequences are on the same node, just go there
if (iEndNode == pstudiohdr->EntryNode( iGoalSequence ))
{
iNextDir = 1;
flNextCycle = 0.0;
nNextSequence = iGoalSequence;
return true;
}
int iInternNode = pstudiohdr->GetTransition( iEndNode, pstudiohdr->EntryNode( iGoalSequence ) );
// if there is no transitionial node, just go to the goal sequence
if (iInternNode == 0)
{
iNextDir = 1;
flNextCycle = 0.0;
nNextSequence = iGoalSequence;
return true;
}
int i;
// look for someone going from the entry node to next node it should hit
// this may be the goal sequences node or an intermediate node
for (i = 0; i < pstudiohdr->GetNumSeq(); i++)
{
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc(i );
if (pstudiohdr->EntryNode( i ) == iEndNode && pstudiohdr->ExitNode( i ) == iInternNode)
{
iNextDir = 1;
flNextCycle = 0.0;
nNextSequence = i;
return true;
}
if (seqdesc.nodeflags)
{
if (pstudiohdr->ExitNode( i ) == iEndNode && pstudiohdr->EntryNode( i ) == iInternNode)
{
iNextDir = -1;
flNextCycle = 0.999;
nNextSequence = i;
return true;
}
}
}
// this means that two parts of the node graph are not connected.
DevMsg( 2, "error in transition graph: %s to %s\n", pstudiohdr->pszNodeName( iEndNode ), pstudiohdr->pszNodeName( pstudiohdr->EntryNode( iGoalSequence ) ));
return false;
}
void SetBodygroup( CStudioHdr *pstudiohdr, int& body, int iGroup, int iValue )
{
if (! pstudiohdr)
return;
if (iGroup >= pstudiohdr->numbodyparts())
return;
mstudiobodyparts_t *pbodypart = pstudiohdr->pBodypart( iGroup );
if (iValue >= pbodypart->nummodels)
return;
int iCurrent = (body / pbodypart->base) % pbodypart->nummodels;
body = (body - (iCurrent * pbodypart->base) + (iValue * pbodypart->base));
}
int GetBodygroup( CStudioHdr *pstudiohdr, int body, int iGroup )
{
if (! pstudiohdr)
return 0;
if (iGroup >= pstudiohdr->numbodyparts())
return 0;
mstudiobodyparts_t *pbodypart = pstudiohdr->pBodypart( iGroup );
if (pbodypart->nummodels <= 1)
return 0;
int iCurrent = (body / pbodypart->base) % pbodypart->nummodels;
return iCurrent;
}
const char *GetBodygroupName( CStudioHdr *pstudiohdr, int iGroup )
{
if ( !pstudiohdr)
return "";
if (iGroup >= pstudiohdr->numbodyparts())
return "";
mstudiobodyparts_t *pbodypart = pstudiohdr->pBodypart( iGroup );
return pbodypart->pszName();
}
int FindBodygroupByName( CStudioHdr *pstudiohdr, const char *name )
{
if ( !pstudiohdr || !pstudiohdr->IsValid() )
return -1;
int group;
for ( group = 0; group < pstudiohdr->numbodyparts(); group++ )
{
mstudiobodyparts_t *pbodypart = pstudiohdr->pBodypart( group );
if ( !Q_strcasecmp( name, pbodypart->pszName() ) )
{
return group;
}
}
return -1;
}
int GetBodygroupCount( CStudioHdr *pstudiohdr, int iGroup )
{
if ( !pstudiohdr )
return 0;
if (iGroup >= pstudiohdr->numbodyparts())
return 0;
mstudiobodyparts_t *pbodypart = pstudiohdr->pBodypart( iGroup );
return pbodypart->nummodels;
}
int GetNumBodyGroups( CStudioHdr *pstudiohdr )
{
if ( !pstudiohdr )
return 0;
return pstudiohdr->numbodyparts();
}
int GetSequenceActivity( CStudioHdr *pstudiohdr, int sequence, int *pweight )
{
if (!pstudiohdr || !pstudiohdr->SequencesAvailable() )
{
if (pweight)
*pweight = 0;
return 0;
}
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc( sequence );
if (!(seqdesc.flags & STUDIO_ACTIVITY))
{
SetActivityForSequence( pstudiohdr, sequence );
}
if (pweight)
*pweight = seqdesc.actweight;
return seqdesc.activity;
}
void GetAttachmentLocalSpace( CStudioHdr *pstudiohdr, int attachIndex, matrix3x4_t &pLocalToWorld )
{
if ( attachIndex >= 0 )
{
const mstudioattachment_t &pAttachment = pstudiohdr->pAttachment(attachIndex);
MatrixCopy( pAttachment.local, pLocalToWorld );
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *pstudiohdr -
// *name -
// Output : int
//-----------------------------------------------------------------------------
int FindHitboxSetByName( CStudioHdr *pstudiohdr, const char *name )
{
if ( !pstudiohdr )
return -1;
for ( int i = 0; i < pstudiohdr->numhitboxsets(); i++ )
{
mstudiohitboxset_t *set = pstudiohdr->pHitboxSet( i );
if ( !set )
continue;