@@ -305,8 +305,128 @@ public void InvalidateBoneCache() {
305305 MostRecentModelBoneCounter = ModelBoneCounter - 1 ;
306306 LastBoneSetupTime = - TimeUnit_t . MaxValue ;
307307 }
308+ public const int CL_EVENT_SOUND = 5004 ;
309+
310+ static bool IsClientAnimEvent ( MStudioEvent ev ) {
311+ if ( ( ev . Type & ( int ) AnimEventType . NewEventSystem ) != 0 )
312+ return ( ev . Type & ( int ) AnimEventType . Client ) != 0 ;
313+
314+ return ev . Event >= 5000 ;
315+ }
316+
317+ static string EventOptions ( MStudioEvent ev ) => System . Text . Encoding . ASCII . GetString ( ( ( ReadOnlySpan < byte > ) ev . Options ( ) ) . SliceNullTerminatedString ( ) ) ;
318+
308319 public virtual void DoAnimationEvents ( StudioHdr hdr ) {
320+ if ( hdr == null )
321+ return ;
322+
323+ if ( GetSequence ( ) == - 1 )
324+ return ;
325+
326+ float eventCycle = ( float ) GetCycle ( ) ;
327+
328+ if ( ! IsVisible ( ) && ! IsViewModel ( ) )
329+ return ;
330+
331+ int numSeq = hdr . GetNumSeq ( ) ;
332+ if ( numSeq < 1 ) {
333+ Warning ( $ "{ GetDebugName ( ) } [{ EntIndex ( ) } ]: no sequences?\n ") ;
334+ return ;
335+ }
336+
337+ int seqNum = GetSequence ( ) ;
338+ if ( seqNum >= numSeq ) {
339+ Warning ( $ "{ GetDebugName ( ) } [{ EntIndex ( ) } ]: Playing sequence { seqNum } but there's only { numSeq } in total?\n ") ;
340+ return ;
341+ }
342+
343+ MStudioSeqDesc seqdesc = hdr . Seqdesc ( seqNum ) ;
344+ if ( seqdesc . NumEvents == 0 )
345+ return ;
346+
347+ Animation . GetEventIndexForSequence ( seqdesc ) ;
348+
349+ bool resetEvents = ResetEventsParity != PrevResetEventsParity ;
350+ PrevResetEventsParity = ResetEventsParity ;
351+
352+ if ( EventSequence != GetSequence ( ) || resetEvents ) {
353+ EventSequence = GetSequence ( ) ;
354+ eventCycle = 0.0f ;
355+ PrevEventCycle = - 0.01f ;
356+ }
357+
358+ if ( eventCycle == PrevEventCycle )
359+ return ;
309360
361+ bool looped = false ;
362+ if ( eventCycle <= PrevEventCycle ) {
363+ if ( PrevEventCycle - eventCycle > 0.5 )
364+ looped = true ;
365+ else
366+ return ;
367+ }
368+
369+ if ( looped ) {
370+ for ( int i = 0 ; i < ( int ) seqdesc . NumEvents ; i ++ ) {
371+ MStudioEvent ev = seqdesc . Event ( i ) ;
372+ if ( ! IsClientAnimEvent ( ev ) )
373+ continue ;
374+
375+ if ( ev . Cycle <= PrevEventCycle )
376+ continue ;
377+
378+ FireEvent ( in GetAbsOrigin ( ) , in GetAbsAngles ( ) , ev . Event , EventOptions ( ev ) ) ;
379+ }
380+
381+ PrevEventCycle = eventCycle - 0.001f ;
382+ }
383+
384+ for ( int i = 0 ; i < ( int ) seqdesc . NumEvents ; i ++ ) {
385+ MStudioEvent ev = seqdesc . Event ( i ) ;
386+ if ( ! IsClientAnimEvent ( ev ) )
387+ continue ;
388+
389+ if ( ev . Cycle > PrevEventCycle && ev . Cycle <= eventCycle )
390+ FireEvent ( in GetAbsOrigin ( ) , in GetAbsAngles ( ) , ev . Event , EventOptions ( ev ) ) ;
391+ }
392+
393+ PrevEventCycle = eventCycle ;
394+ }
395+
396+ public virtual void FireEvent ( in Vector3 origin , in QAngle angles , int ev , string options ) {
397+ switch ( ev ) {
398+ case ( int ) AnimEventId . AE_CL_PLAYSOUND : {
399+ LocalPlayerFilter filter = new ( ) ;
400+ if ( Attachments . Count > 0 ) {
401+ GetAttachment ( 1 , out Vector3 attachOrigin , out _ ) ;
402+ EmitSound ( filter , GetSoundSourceIndex ( ) , options , in attachOrigin , out _ ) ;
403+ }
404+ else
405+ EmitSound ( filter , GetSoundSourceIndex ( ) , options , in GetAbsOrigin ( ) , out _ ) ;
406+ }
407+ break ;
408+ case ( int ) AnimEventId . AE_CL_STOPSOUND :
409+ StopSound ( GetSoundSourceIndex ( ) , options ) ;
410+ break ;
411+ case CL_EVENT_SOUND :
412+ FireObsoleteEvent ( in origin , in angles , ev , options ) ;
413+ break ;
414+ }
415+ }
416+
417+ public virtual void FireObsoleteEvent ( in Vector3 origin , in QAngle angles , int ev , string options ) {
418+ switch ( ev ) {
419+ case CL_EVENT_SOUND : {
420+ LocalPlayerFilter filter = new ( ) ;
421+ if ( Attachments . Count > 0 ) {
422+ GetAttachment ( 1 , out Vector3 attachOrigin , out _ ) ;
423+ EmitSound ( filter , GetSoundSourceIndex ( ) , options , in attachOrigin , out _ ) ;
424+ }
425+ else
426+ EmitSound ( filter , GetSoundSourceIndex ( ) , options ) ;
427+ }
428+ break ;
429+ }
310430 }
311431 public bool IsBoneCacheValid ( ) => MostRecentModelBoneCounter == ModelBoneCounter ;
312432 public static void InvalidateBoneCaches ( ) => ModelBoneCounter ++ ;
0 commit comments