forked from ValveSoftware/source-sdk-2013
-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathbaseflex.cpp
More file actions
3047 lines (2557 loc) · 85.7 KB
/
baseflex.cpp
File metadata and controls
3047 lines (2557 loc) · 85.7 KB
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:
//
//=============================================================================//
#include "cbase.h"
#include "animation.h"
#include "baseflex.h"
#include "filesystem.h"
#include "studio.h"
#include "choreoevent.h"
#include "choreoscene.h"
#include "choreoactor.h"
#include "vstdlib/random.h"
#include "engine/IEngineSound.h"
#include "tier1/strtools.h"
#include "KeyValues.h"
#include "ai_basenpc.h"
#include "ai_navigator.h"
#include "ai_moveprobe.h"
#include "sceneentity.h"
#include "ai_baseactor.h"
#include "datacache/imdlcache.h"
#include "tier1/byteswap.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
static ConVar scene_showlook( "scene_showlook", "0", FCVAR_ARCHIVE, "When playing back, show the directions of look events." );
static ConVar scene_showmoveto( "scene_showmoveto", "0", FCVAR_ARCHIVE, "When moving, show the end location." );
static ConVar scene_showunlock( "scene_showunlock", "0", FCVAR_ARCHIVE, "Show when a vcd is playing but normal AI is running." );
// static ConVar scene_checktagposition( "scene_checktagposition", "0", FCVAR_ARCHIVE, "When playing back a choreographed scene, check the current position of the tags relative to where they were authored." );
// Fake layer # to force HandleProcessSceneEvent to actually allocate the layer during npc think time instead of in between.
#define REQUEST_DEFERRED_LAYER_ALLOCATION -2
extern bool g_bClientFlex;
// ---------------------------------------------------------------------
//
// CBaseFlex -- physically simulated brush rectangular solid
//
// ---------------------------------------------------------------------
void* SendProxy_FlexWeights( const SendProp *pProp, const void *pStruct, const void *pVarData, CSendProxyRecipients *pRecipients, int objectID )
{
// Don't any flexweights to client unless scene_clientflex.GetBool() is false
if ( !g_bClientFlex )
return (void*)pVarData;
else
return NULL;
}
REGISTER_SEND_PROXY_NON_MODIFIED_POINTER( SendProxy_FlexWeights );
// SendTable stuff.
IMPLEMENT_SERVERCLASS_ST(CBaseFlex, DT_BaseFlex)
// Note we can't totally disabled flexweights transmission since some things like blink and eye tracking are still done by the server
SendPropArray3 (SENDINFO_ARRAY3(m_flexWeight), SendPropFloat(SENDINFO_ARRAY(m_flexWeight), 12, SPROP_ROUNDDOWN, 0.0f, 1.0f ) /*, SendProxy_FlexWeights*/ ),
SendPropInt (SENDINFO(m_blinktoggle), 1, SPROP_UNSIGNED ),
SendPropVector (SENDINFO(m_viewtarget), -1, SPROP_COORD),
#ifdef HL2_DLL
SendPropFloat ( SENDINFO_VECTORELEM(m_vecViewOffset, 0), 0, SPROP_NOSCALE ),
SendPropFloat ( SENDINFO_VECTORELEM(m_vecViewOffset, 1), 0, SPROP_NOSCALE ),
SendPropFloat ( SENDINFO_VECTORELEM(m_vecViewOffset, 2), 0, SPROP_NOSCALE ),
SendPropVector ( SENDINFO(m_vecLean), -1, SPROP_COORD ),
SendPropVector ( SENDINFO(m_vecShift), -1, SPROP_COORD ),
#endif
END_SEND_TABLE()
BEGIN_DATADESC( CBaseFlex )
// m_blinktoggle
DEFINE_ARRAY( m_flexWeight, FIELD_FLOAT, MAXSTUDIOFLEXCTRL ),
DEFINE_FIELD( m_viewtarget, FIELD_POSITION_VECTOR ),
// m_SceneEvents
// m_FileList
DEFINE_FIELD( m_flAllowResponsesEndTime, FIELD_TIME ),
// m_ActiveChoreoScenes
// DEFINE_FIELD( m_LocalToGlobal, CUtlRBTree < FS_LocalToGlobal_t , unsigned short > ),
// m_bUpdateLayerPriorities
DEFINE_FIELD( m_flLastFlexAnimationTime, FIELD_TIME ),
#ifdef HL2_DLL
//DEFINE_FIELD( m_vecPrevOrigin, FIELD_POSITION_VECTOR ),
//DEFINE_FIELD( m_vecPrevVelocity, FIELD_VECTOR ),
DEFINE_FIELD( m_vecLean, FIELD_VECTOR ),
DEFINE_FIELD( m_vecShift, FIELD_VECTOR ),
#endif
END_DATADESC()
#ifdef MAPBASE_VSCRIPT
BEGIN_ENT_SCRIPTDESC( CBaseFlex, CBaseAnimatingOverlay, "Animated characters who have vertex flex capability." )
#else
BEGIN_ENT_SCRIPTDESC( CBaseFlex, CBaseAnimating, "Animated characters who have vertex flex capability." )
#endif
DEFINE_SCRIPTFUNC_NAMED( ScriptGetOldestScene, "GetCurrentScene", "Returns the instance of the oldest active scene entity (if any)." )
DEFINE_SCRIPTFUNC_NAMED( ScriptGetSceneByIndex, "GetSceneByIndex", "Returns the instance of the scene entity at the specified index." )
#ifdef MAPBASE
DEFINE_SCRIPTFUNC( SetViewtarget, "Sets the entity's eye target." )
#endif
END_SCRIPTDESC();
LINK_ENTITY_TO_CLASS( funCBaseFlex, CBaseFlex ); // meaningless independant class!!
CBaseFlex::CBaseFlex( void ) :
m_LocalToGlobal( 0, 0, FlexSettingLessFunc )
{
#ifdef _DEBUG
// default constructor sets the viewtarget to NAN
m_viewtarget.Init();
#endif
m_bUpdateLayerPriorities = true;
m_flLastFlexAnimationTime = 0.0;
}
CBaseFlex::~CBaseFlex( void )
{
m_LocalToGlobal.RemoveAll();
Assert( m_SceneEvents.Count() == 0 );
}
void CBaseFlex::SetModel( const char *szModelName )
{
MDLCACHE_CRITICAL_SECTION();
BaseClass::SetModel( szModelName );
for (LocalFlexController_t i = LocalFlexController_t(0); i < GetNumFlexControllers(); i++)
{
SetFlexWeight( i, 0.0f );
}
}
void CBaseFlex::SetViewtarget( const Vector &viewtarget )
{
m_viewtarget = viewtarget; // bah
}
void CBaseFlex::SetFlexWeight( LocalFlexController_t index, float value )
{
if (index >= 0 && index < GetNumFlexControllers())
{
CStudioHdr *pstudiohdr = GetModelPtr( );
if (! pstudiohdr)
return;
mstudioflexcontroller_t *pflexcontroller = pstudiohdr->pFlexcontroller( index );
if (pflexcontroller->max != pflexcontroller->min)
{
value = (value - pflexcontroller->min) / (pflexcontroller->max - pflexcontroller->min);
value = clamp( value, 0.0f, 1.0f );
}
m_flexWeight.Set( index, value );
}
}
float CBaseFlex::GetFlexWeight( LocalFlexController_t index )
{
if (index >= 0 && index < GetNumFlexControllers())
{
CStudioHdr *pstudiohdr = GetModelPtr( );
if (! pstudiohdr)
return 0;
mstudioflexcontroller_t *pflexcontroller = pstudiohdr->pFlexcontroller( index );
if (pflexcontroller->max != pflexcontroller->min)
{
return m_flexWeight[index] * (pflexcontroller->max - pflexcontroller->min) + pflexcontroller->min;
}
return m_flexWeight[index];
}
return 0.0;
}
LocalFlexController_t CBaseFlex::FindFlexController( const char *szName )
{
for (LocalFlexController_t i = LocalFlexController_t(0); i < GetNumFlexControllers(); i++)
{
if (stricmp( GetFlexControllerName( i ), szName ) == 0)
{
return i;
}
}
// AssertMsg( 0, UTIL_VarArgs( "flexcontroller %s couldn't be mapped!!!\n", szName ) );
return LocalFlexController_t(0);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBaseFlex::StartChoreoScene( CChoreoScene *scene )
{
if ( m_ActiveChoreoScenes.Find( scene ) != m_ActiveChoreoScenes.InvalidIndex() )
{
return;
}
m_ActiveChoreoScenes.AddToTail( scene );
m_bUpdateLayerPriorities = true;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBaseFlex::RemoveChoreoScene( CChoreoScene *scene, bool canceled )
{
// Assert( m_ActiveChoreoScenes.Find( scene ) != m_ActiveChoreoScenes.InvalidIndex() );
m_ActiveChoreoScenes.FindAndRemove( scene );
m_bUpdateLayerPriorities = true;
if (canceled)
{
CAI_BaseNPC *myNpc = MyNPCPointer( );
if ( myNpc )
{
myNpc->ClearSceneLock( );
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CBaseFlex::GetScenePriority( CChoreoScene *scene )
{
int iPriority = 0;
int c = m_ActiveChoreoScenes.Count();
// count number of channels in scenes older than current
for ( int i = 0; i < c; i++ )
{
CChoreoScene *pScene = m_ActiveChoreoScenes[ i ];
if ( !pScene )
{
continue;
}
if ( pScene == scene )
{
break;
}
iPriority += pScene->GetNumChannels( );
}
return iPriority;
}
//-----------------------------------------------------------------------------
// Purpose: Remove all active SceneEvents
//-----------------------------------------------------------------------------
void CBaseFlex::ClearSceneEvents( CChoreoScene *scene, bool canceled )
{
if ( !scene )
{
m_SceneEvents.RemoveAll();
return;
}
for ( int i = m_SceneEvents.Count() - 1; i >= 0; i-- )
{
CSceneEventInfo *info = &m_SceneEvents[ i ];
Assert( info );
Assert( info->m_pScene );
Assert( info->m_pEvent );
if ( info->m_pScene != scene )
continue;
if ( !ClearSceneEvent( info, false, canceled ))
{
// unknown expression to clear!!
Assert( 0 );
}
// Free this slot
info->m_pEvent = NULL;
info->m_pScene = NULL;
info->m_bStarted = false;
m_SceneEvents.Remove( i );
}
}
//-----------------------------------------------------------------------------
// Purpose: Stop specifics of expression
//-----------------------------------------------------------------------------
bool CBaseFlex::ClearSceneEvent( CSceneEventInfo *info, bool fastKill, bool canceled )
{
Assert( info );
Assert( info->m_pScene );
Assert( info->m_pEvent );
// FIXME: this code looks duplicated
switch ( info->m_pEvent->GetType() )
{
case CChoreoEvent::GESTURE:
case CChoreoEvent::SEQUENCE:
{
if (info->m_iLayer >= 0)
{
if ( fastKill )
{
FastRemoveLayer( info->m_iLayer );
}
else if (info->m_pEvent->GetType() == CChoreoEvent::GESTURE)
{
if (canceled)
{
// remove slower if interrupted
RemoveLayer( info->m_iLayer, 0.5 );
}
else
{
RemoveLayer( info->m_iLayer, 0.1 );
}
}
else
{
RemoveLayer( info->m_iLayer, 0.3 );
}
}
}
return true;
case CChoreoEvent::MOVETO:
{
CAI_BaseNPC *myNpc = MyNPCPointer( );
if (!myNpc)
return true;
// cancel moveto if it's distance based, of if the event was part of a canceled vcd
if (IsMoving() && (canceled || info->m_pEvent->GetDistanceToTarget() > 0.0))
{
if (!info->m_bHasArrived)
{
if (info->m_pScene)
{
Scene_Printf( "%s : %8.2f: MOVETO canceled but actor %s not at goal\n", info->m_pScene->GetFilename(), info->m_pScene->GetTime(), info->m_pEvent->GetActor()->GetName() );
}
}
myNpc->GetNavigator()->StopMoving( false ); // Stop moving
}
}
return true;
case CChoreoEvent::FACE:
case CChoreoEvent::FLEXANIMATION:
case CChoreoEvent::EXPRESSION:
case CChoreoEvent::LOOKAT:
case CChoreoEvent::GENERIC:
{
// no special rules
}
return true;
case CChoreoEvent::SPEAK:
{
// Tracker 15420: Issue stopsound if we need to cut this short...
if ( canceled )
{
StopSound( info->m_pEvent->GetParameters() );
#ifdef HL2_EPISODIC
// If we were holding the semaphore because of this speech, release it
CAI_BaseActor *pBaseActor = dynamic_cast<CAI_BaseActor*>(this);
if ( pBaseActor )
{
pBaseActor->GetExpresser()->ForceNotSpeaking();
}
#endif
}
}
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Add string indexed scene/expression/duration to list of active SceneEvents
// Input : scenefile -
// expression -
// duration -
//-----------------------------------------------------------------------------
#ifdef MAPBASE
void CBaseFlex::AddSceneEvent( CChoreoScene *scene, CChoreoEvent *event, CBaseEntity *pTarget, CSceneEntity *pSceneEnt )
#else
void CBaseFlex::AddSceneEvent( CChoreoScene *scene, CChoreoEvent *event, CBaseEntity *pTarget )
#endif
{
if ( !scene || !event )
{
Msg( "CBaseFlex::AddSceneEvent: scene or event was NULL!!!\n" );
return;
}
CChoreoActor *actor = event->GetActor();
if ( !actor )
{
Msg( "CBaseFlex::AddSceneEvent: event->GetActor() was NULL!!!\n" );
return;
}
CSceneEventInfo info;
memset( (void *)&info, 0, sizeof( info ) );
info.m_pEvent = event;
info.m_pScene = scene;
info.m_hTarget = pTarget;
info.m_bStarted = false;
info.m_hSceneEntity = pSceneEnt;
#ifdef MAPBASE
if (StartSceneEvent( &info, scene, event, actor, pTarget, pSceneEnt ))
#else
if (StartSceneEvent( &info, scene, event, actor, pTarget ))
#endif
{
m_SceneEvents.AddToTail( info );
}
else
{
Scene_Printf( "CBaseFlex::AddSceneEvent: event failed\n" );
// Assert( 0 ); // expression failed to start
}
}
//-----------------------------------------------------------------------------
// Starting various expression types
//-----------------------------------------------------------------------------
bool CBaseFlex::RequestStartSequenceSceneEvent( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event, CChoreoActor *actor, CBaseEntity *pTarget )
{
info->m_nSequence = LookupSequence( event->GetParameters() );
// make sure sequence exists
if (info->m_nSequence < 0)
{
Warning( "CSceneEntity %s :\"%s\" unable to find sequence \"%s\"\n", STRING(GetEntityName()), actor->GetName(), event->GetParameters() );
return false;
}
// This is a bit of a hack, but we need to defer the actual allocation until Process which will sync the layer allocation
// to the NPCs think/m_flAnimTime instead of some arbitrary tick
info->m_iLayer = REQUEST_DEFERRED_LAYER_ALLOCATION;
info->m_pActor = actor;
return true;
}
bool CBaseFlex::RequestStartGestureSceneEvent( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event, CChoreoActor *actor, CBaseEntity *pTarget )
{
info->m_nSequence = LookupSequence( event->GetParameters() );
// make sure sequence exists
if (info->m_nSequence < 0)
{
Warning( "CSceneEntity %s :\"%s\" unable to find gesture \"%s\"\n", STRING(GetEntityName()), actor->GetName(), event->GetParameters() );
return false;
}
// This is a bit of a hack, but we need to defer the actual allocation until Process which will sync the layer allocation
// to the NPCs think/m_flAnimTime instead of some arbitrary tick
info->m_iLayer = REQUEST_DEFERRED_LAYER_ALLOCATION;
info->m_pActor = actor;
return true;
}
bool CBaseFlex::HandleStartSequenceSceneEvent( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event, CChoreoActor *actor )
{
Assert( info->m_iLayer == REQUEST_DEFERRED_LAYER_ALLOCATION );
info->m_nSequence = LookupSequence( event->GetParameters() );
info->m_iLayer = -1;
if (info->m_nSequence < 0)
{
Warning( "CSceneEntity %s :\"%s\" unable to find sequence \"%s\"\n", STRING(GetEntityName()), actor->GetName(), event->GetParameters() );
return false;
}
if (!EnterSceneSequence( scene, event ))
{
if (!event->GetPlayOverScript())
{
// this has failed to start
Warning( "CSceneEntity %s :\"%s\" failed to start sequence \"%s\"\n", STRING(GetEntityName()), actor->GetName(), event->GetParameters() );
return false;
}
// Start anyways, just use normal no-movement, must be in IDLE rules
}
info->m_iPriority = actor->FindChannelIndex( event->GetChannel() );
info->m_iLayer = AddLayeredSequence( info->m_nSequence, info->m_iPriority + GetScenePriority( scene ) );
SetLayerNoRestore( info->m_iLayer, true );
SetLayerWeight( info->m_iLayer, 0.0 );
bool looping = ((GetSequenceFlags( GetModelPtr(), info->m_nSequence ) & STUDIO_LOOPING) != 0);
if (!looping)
{
// figure out the animtime when this was frame 0
float dt = scene->GetTime() - event->GetStartTime();
float seq_duration = SequenceDuration( info->m_nSequence );
float flCycle = dt / seq_duration;
flCycle = flCycle - (int)flCycle; // loop
SetLayerCycle( info->m_iLayer, flCycle, flCycle );
SetLayerPlaybackRate( info->m_iLayer, 0.0 );
}
else
{
SetLayerPlaybackRate( info->m_iLayer, 1.0 );
}
if (IsMoving())
{
info->m_flWeight = 0.0;
}
else
{
info->m_flWeight = 1.0;
}
return true;
}
bool CBaseFlex::HandleStartGestureSceneEvent( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event, CChoreoActor *actor )
{
Assert( info->m_iLayer == REQUEST_DEFERRED_LAYER_ALLOCATION );
info->m_nSequence = LookupSequence( event->GetParameters() );
info->m_iLayer = -1;
if (info->m_nSequence < 0)
{
Warning( "CSceneEntity %s :\"%s\" unable to find gesture \"%s\"\n", STRING(GetEntityName()), actor->GetName(), event->GetParameters() );
return false;
}
// FIXME: this seems like way too much code
info->m_bIsGesture = false;
KeyValues *seqKeyValues = GetSequenceKeyValues( info->m_nSequence );
if (seqKeyValues)
{
// Do we have a build point section?
KeyValues *pkvAllFaceposer = seqKeyValues->FindKey("faceposer");
if ( pkvAllFaceposer )
{
KeyValues *pkvType = pkvAllFaceposer->FindKey("type");
if (pkvType)
{
info->m_bIsGesture = (stricmp( pkvType->GetString(), "gesture" ) == 0) ? true : false;
}
}
// FIXME: fixup tags that should be set as "linear", should be done in faceposer
char szStartLoop[CEventAbsoluteTag::MAX_EVENTTAG_LENGTH] = { "loop" };
char szEndLoop[CEventAbsoluteTag::MAX_EVENTTAG_LENGTH] = { "end" };
// check in the tag indexes
KeyValues *pkvFaceposer;
for ( pkvFaceposer = pkvAllFaceposer->GetFirstSubKey(); pkvFaceposer; pkvFaceposer = pkvFaceposer->GetNextKey() )
{
if (!stricmp( pkvFaceposer->GetName(), "startloop" ))
{
V_strcpy_safe( szStartLoop, pkvFaceposer->GetString() );
}
else if (!stricmp( pkvFaceposer->GetName(), "endloop" ))
{
V_strcpy_safe( szEndLoop, pkvFaceposer->GetString() );
}
}
CEventAbsoluteTag *ptag;
ptag = event->FindAbsoluteTag( CChoreoEvent::ORIGINAL, szStartLoop );
if (ptag)
{
ptag->SetLinear( true );
}
ptag = event->FindAbsoluteTag( CChoreoEvent::PLAYBACK, szStartLoop );
if (ptag)
{
ptag->SetLinear( true );
}
ptag = event->FindAbsoluteTag( CChoreoEvent::ORIGINAL, szEndLoop );
if (ptag)
{
ptag->SetLinear( true );
}
ptag = event->FindAbsoluteTag( CChoreoEvent::PLAYBACK, szEndLoop );
if (ptag)
{
ptag->SetLinear( true );
}
if ( pkvAllFaceposer )
{
CStudioHdr *pstudiohdr = GetModelPtr();
mstudioseqdesc_t &seqdesc = pstudiohdr->pSeqdesc( info->m_nSequence );
mstudioanimdesc_t &animdesc = pstudiohdr->pAnimdesc( pstudiohdr->iRelativeAnim( info->m_nSequence, seqdesc.anim(0,0) ) );
// check in the tag indexes
KeyValues *pkvFaceposer;
for ( pkvFaceposer = pkvAllFaceposer->GetFirstSubKey(); pkvFaceposer; pkvFaceposer = pkvFaceposer->GetNextKey() )
{
if (!stricmp( pkvFaceposer->GetName(), "tags" ))
{
KeyValues *pkvTags;
for ( pkvTags = pkvFaceposer->GetFirstSubKey(); pkvTags; pkvTags = pkvTags->GetNextKey() )
{
int maxFrame = animdesc.numframes - 2; // FIXME: this is off by one!
if ( maxFrame > 0)
{
float percentage = (float)pkvTags->GetInt() / maxFrame;
CEventAbsoluteTag *ptag = event->FindAbsoluteTag( CChoreoEvent::ORIGINAL, pkvTags->GetName() );
if (ptag)
{
if (fabs(ptag->GetPercentage() - percentage) > 0.05)
{
DevWarning("%s repositioned tag: %s : %.3f -> %.3f (%s:%s:%s)\n", scene->GetFilename(), pkvTags->GetName(), ptag->GetPercentage(), percentage, scene->GetFilename(), actor->GetName(), event->GetParameters() );
// reposition tag
ptag->SetPercentage( percentage );
}
}
}
}
}
}
if (!event->VerifyTagOrder())
{
DevWarning("out of order tags : %s : (%s:%s:%s)\n", scene->GetFilename(), actor->GetName(), event->GetName(), event->GetParameters() );
}
}
seqKeyValues->deleteThis();
}
// initialize posture suppression
// FIXME: move priority of base animation so that layers can be inserted before
// FIXME: query stopping, post idle layer to figure out correct weight
// GetIdleLayerWeight()?
if (!info->m_bIsGesture && IsMoving())
{
info->m_flWeight = 0.0;
}
else
{
info->m_flWeight = 1.0;
}
// this happens before StudioFrameAdvance()
info->m_iPriority = actor->FindChannelIndex( event->GetChannel() );
info->m_iLayer = AddLayeredSequence( info->m_nSequence, info->m_iPriority + GetScenePriority( scene ) );
SetLayerNoRestore( info->m_iLayer, true );
SetLayerDuration( info->m_iLayer, event->GetDuration() );
SetLayerWeight( info->m_iLayer, 0.0 );
bool looping = ((GetSequenceFlags( GetModelPtr(), info->m_nSequence ) & STUDIO_LOOPING) != 0);
if ( looping )
{
DevMsg( 1, "vcd error, gesture %s of model %s is marked as STUDIO_LOOPING!\n",
event->GetParameters(), STRING(GetModelName()) );
}
SetLayerLooping( info->m_iLayer, false ); // force to not loop
float duration = event->GetDuration( );
// figure out the animtime when this was frame 0
float flEventCycle = (scene->GetTime() - event->GetStartTime()) / duration;
float flCycle = event->GetOriginalPercentageFromPlaybackPercentage( flEventCycle );
SetLayerCycle( info->m_iLayer, flCycle, 0.0 );
SetLayerPlaybackRate( info->m_iLayer, 0.0 );
return true;
}
bool CBaseFlex::StartFacingSceneEvent( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event, CChoreoActor *actor, CBaseEntity *pTarget )
{
if ( pTarget )
{
// Don't allow FACE commands while sitting in the vehicle
CAI_BaseNPC *myNpc = MyNPCPointer();
if ( myNpc && myNpc->IsInAVehicle() )
return false;
info->m_bIsMoving = false;
return true;
}
return false;
}
bool CBaseFlex::StartMoveToSceneEvent( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event, CChoreoActor *actor, CBaseEntity *pTarget )
{
if (pTarget)
{
info->m_bIsMoving = false;
info->m_bHasArrived = false;
CAI_BaseNPC *myNpc = MyNPCPointer( );
if (!myNpc)
{
return false;
}
EnterSceneSequence( scene, event, true );
// If they're already moving, stop them
//
// Don't stop them during restore because that will set a stopping path very
// nearby, causing us to signal arrival prematurely in CheckSceneEventCompletion.
// BEWARE: the behavior of this bug depended on the order in which the entities were restored!!
if ( myNpc->IsMoving() && !scene->IsRestoring() )
{
myNpc->GetNavigator()->StopMoving( false );
}
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
#ifdef MAPBASE
bool CBaseFlex::StartSceneEvent( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event, CChoreoActor *actor, CBaseEntity *pTarget, CSceneEntity *pSceneEnt )
#else
bool CBaseFlex::StartSceneEvent( CSceneEventInfo *info, CChoreoScene *scene, CChoreoEvent *event, CChoreoActor *actor, CBaseEntity *pTarget )
#endif
{
switch ( event->GetType() )
{
case CChoreoEvent::SEQUENCE:
return RequestStartSequenceSceneEvent( info, scene, event, actor, pTarget );
case CChoreoEvent::GESTURE:
return RequestStartGestureSceneEvent( info, scene, event, actor, pTarget );
case CChoreoEvent::FACE:
return StartFacingSceneEvent( info, scene, event, actor, pTarget );
// FIXME: move this to an CBaseActor
case CChoreoEvent::MOVETO:
return StartMoveToSceneEvent( info, scene, event, actor, pTarget );
case CChoreoEvent::LOOKAT:
info->m_hTarget = pTarget;
return true;
case CChoreoEvent::FLEXANIMATION:
info->InitWeight( this );
return true;
case CChoreoEvent::SPEAK:
return true;
case CChoreoEvent::EXPRESSION: // These are handled client-side
return true;
#ifdef MAPBASE
case CChoreoEvent::GENERIC:
{
// This is handled in CBaseFlex so that any flex entity--including players--could use this text.
if (stricmp(event->GetParameters(), "AI_GAMETEXT") == 0)
{
// game_text-based lines, for placeholders
if ( event->GetParameters2() )
{
info->m_nType = 12; // SCENE_AI_GAMETEXT
hudtextparms_t textParams;
textParams.holdTime = event->GetDuration();
textParams.fadeinTime = 0.5f;
textParams.fadeoutTime = 0.5f;
// Push text farther down if the previous one is still active
static float g_flLastTextStartTime = 0.0f;
static float g_flLastTextEndTime = 0.0f;
static int g_nLastTextActor = 0;
static int g_nTextSlot = 0;
// Which actor am I?
int nActor = 0;
for ( int i = 0; i < scene->GetNumActors(); i++ )
{
if (scene->GetActor( i ) == actor)
{
nActor = i;
break;
}
}
float flStartTime = event->GetStartTime();
if ( g_flLastTextStartTime > flStartTime )
{
// New scene, reset slot
g_nTextSlot = 0;
}
else if ( nActor != g_nLastTextActor )
{
if ( g_flLastTextEndTime > flStartTime )
{
// Put it on another slot
g_nTextSlot++;
}
else
{
g_nTextSlot = 0;
}
}
g_flLastTextStartTime = flStartTime;
g_flLastTextEndTime = event->GetEndTime() + textParams.fadeoutTime;
g_nLastTextActor = nActor;
const int nMaxActors = 2;
textParams.channel = 2 + ( g_nTextSlot % nMaxActors );
textParams.x = -1;
textParams.y = 0.6 + (( g_nTextSlot % nMaxActors ) * 0.1);
textParams.effect = 0;
textParams.r1 = 255;
textParams.g1 = 255;
textParams.b1 = 255;
if ( DispatchGetGameTextSpeechParams( textParams ) )
{
CRecipientFilter filter;
filter.AddAllPlayers();
filter.MakeReliable();
UserMessageBegin( filter, "HudMsg" );
WRITE_BYTE ( textParams.channel & 0xFF );
WRITE_FLOAT( textParams.x );
WRITE_FLOAT( textParams.y );
WRITE_BYTE ( textParams.r1 );
WRITE_BYTE ( textParams.g1 );
WRITE_BYTE ( textParams.b1 );
WRITE_BYTE ( textParams.a1 );
WRITE_BYTE ( textParams.r2 );
WRITE_BYTE ( textParams.g2 );
WRITE_BYTE ( textParams.b2 );
WRITE_BYTE ( textParams.a2 );
WRITE_BYTE ( textParams.effect );
WRITE_FLOAT( textParams.fadeinTime );
WRITE_FLOAT( textParams.fadeoutTime );
WRITE_FLOAT( textParams.holdTime );
WRITE_FLOAT( textParams.fxTime );
WRITE_STRING( event->GetParameters2() );
WRITE_STRING( "" ); // No custom font
WRITE_BYTE ( Q_strlen( event->GetParameters2() ) );
MessageEnd();
}
return true;
}
}
return false;
}
#endif
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Remove expression
// Input : scenefile -
// expression -
//-----------------------------------------------------------------------------
void CBaseFlex::RemoveSceneEvent( CChoreoScene *scene, CChoreoEvent *event, bool fastKill )
{
Assert( event );
for ( int i = 0 ; i < m_SceneEvents.Count(); i++ )
{
CSceneEventInfo *info = &m_SceneEvents[ i ];
Assert( info );
Assert( info->m_pEvent );
if ( info->m_pScene != scene )
continue;
if ( info->m_pEvent != event)
continue;
if (ClearSceneEvent( info, fastKill, false ))
{
// Free this slot
info->m_pEvent = NULL;
info->m_pScene = NULL;
info->m_bStarted = false;
m_SceneEvents.Remove( i );
return;
}
}
// many events refuse to start due to bogus parameters
}
//-----------------------------------------------------------------------------
// Purpose: Checks to see if the event should be considered "completed"
//-----------------------------------------------------------------------------
bool CBaseFlex::CheckSceneEvent( float currenttime, CChoreoScene *scene, CChoreoEvent *event )
{
for ( int i = 0 ; i < m_SceneEvents.Count(); i++ )
{
CSceneEventInfo *info = &m_SceneEvents[ i ];
Assert( info );
Assert( info->m_pEvent );
if ( info->m_pScene != scene )
continue;
if ( info->m_pEvent != event)
continue;
return CheckSceneEventCompletion( info, currenttime, scene, event );
}
return true;
}
bool CBaseFlex::CheckSceneEventCompletion( CSceneEventInfo *info, float currenttime, CChoreoScene *scene, CChoreoEvent *event )
{
switch ( event->GetType() )
{
case CChoreoEvent::MOVETO:
{
CAI_BaseNPC *npc = MyNPCPointer( );
if (npc)
{
// check movement, check arrival
if (npc->GetNavigator()->IsGoalActive())
{
const Task_t *pCurTask = npc->GetTask();
if ( pCurTask && (pCurTask->iTask == TASK_PLAY_SCENE || pCurTask->iTask == TASK_WAIT_FOR_MOVEMENT ) )
{
float preload = event->GetEndTime() - currenttime;
if (preload < 0)
{
//Msg("%.1f: no preload\n", currenttime );
return false;
}
float t = npc->GetTimeToNavGoal();
// Msg("%.1f: preload (%s:%.1f) %.1f %.1f\n", currenttime, event->GetName(), event->GetEndTime(), preload, t );
// FIXME: t is zero if no path can be built!
if (t > 0.0f && t <= preload)
{
return true;
}
return false;
}
}
else if (info->m_bHasArrived)
{