Skip to content

Commit 7995356

Browse files
authored
Merge pull request #244 from spacevx/various-fixes
Various fixes
2 parents b1a5f40 + 15dd1a5 commit 7995356

12 files changed

Lines changed: 291 additions & 6 deletions

File tree

‎Game.Client/C_BaseAnimating.cs‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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++;

‎Game.Client/C_BaseEntity.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,6 +2604,8 @@ public void SetDestroyedOnRecreateEntities() {
26042604
public virtual ref readonly BaseHandle GetRefEHandle() {
26052605
return ref RefEHandle;
26062606
}
2607+
2608+
public int GetSoundSourceIndex() => GetRefEHandle().GetEntryIndex();
26072609
public virtual void SetRefEHandle(in BaseHandle handle) {
26082610
RefEHandle.Index = handle.Index;
26092611
}

‎Game.Client/C_BaseViewModel.cs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ namespace Game.Client;
77

88
#if GMOD_DLL
99
using Game.Client.GarrysMod;
10+
using Game.Shared;
11+
12+
using System.Numerics;
1013
#endif
1114

1215
public partial class C_BaseViewModel
@@ -207,4 +210,18 @@ public override bool Interpolate(TimeUnit_t currentTime) {
207210
public override RenderGroup GetRenderGroup() {
208211
return RenderGroup.ViewModelOpaque;
209212
}
213+
214+
public override void FireEvent(in Vector3 origin, in QAngle angles, int ev, string options) {
215+
if (ev == (int)AnimEventId.AE_CL_PLAYSOUND || ev == CL_EVENT_SOUND) {
216+
C_BaseEntity? owner = GetOwner();
217+
if (owner != null) {
218+
LocalPlayerFilter filter = new();
219+
EmitSound(filter, owner.GetSoundSourceIndex(), options, in GetAbsOrigin(), out _);
220+
return;
221+
}
222+
}
223+
224+
if (GetOwningWeapon() != null)
225+
base.FireEvent(in origin, in angles, ev, options);
226+
}
210227
}

‎Game.Client/C_RecipientFilter.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ public PVSFilter(in Vector3 origin) : base() {
220220
public class LocalPlayerFilter : C_RecipientFilter
221221
{
222222
public LocalPlayerFilter() : base() {
223-
223+
if (C_BasePlayer.GetLocalPlayer() != null)
224+
AddRecipient(C_BasePlayer.GetLocalPlayer());
224225
}
225226
}
226227

‎Game.Shared/AmmoDef.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class AmmoDef
4646

4747
public static Ammo NULL = default;
4848

49-
public int AmmoIndex;
49+
public int AmmoIndex = 1;
5050
public Ammo[] AmmoType = new Ammo[MAX_AMMO_TYPES];
5151

5252
public ref Ammo GetAmmoOfIndex(int ammoIndex) {

‎Game.Shared/GarrysMod/WeaponPistol.cs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public partial class WeaponPistol : HL2MPMachineGun
4040
public const TimeUnit_t PISTOL_ACCURACY_SHOT_PENALTY_TIME = 0.2;// Applied amount of time each shot adds to the time we must recover from
4141
public const TimeUnit_t PISTOL_ACCURACY_MAXIMUM_PENALTY_TIME = 1.5;// Maximum penalty to deal out
4242

43+
public override bool Reload() {
44+
bool ret = DefaultReload(GetMaxClip1(), GetMaxClip2(), Activity.ACT_VM_RELOAD);
45+
if (ret) {
46+
WeaponSound(Shared.WeaponSound.Reload);
47+
AccuracyPenalty = 0.0f;
48+
}
49+
return ret;
50+
}
51+
4352
public override void ItemPostFrame() {
4453
base.ItemPostFrame();
4554

‎Game.Shared/GarrysMod/WeaponRPG.cs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ public WeaponRPG() {
4141
[NetworkVar("m_vecLaserDot")]
4242
public partial Vector3 LaserDot { get; set; }
4343
public override float GetFireRate() => 1f;
44+
45+
public override bool Reload() {
46+
BaseCombatCharacter? owner = GetOwner();
47+
if (owner == null)
48+
return false;
49+
50+
if (owner.GetAmmoCount(PrimaryAmmoType) <= 0)
51+
return false;
52+
53+
WeaponSound(Shared.WeaponSound.Reload);
54+
55+
SendWeaponAnim(Activity.ACT_VM_RELOAD);
56+
57+
return true;
58+
}
4459
}
4560

4661
[LinkEntityToClass("env_laserdot")]

‎Game.Shared/GarrysMod/WeaponShotgun.cs‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,66 @@ private bool StartReload() {
237237
return true;
238238
}
239239

240+
public override bool Reload() {
241+
if (!InReload)
242+
Warning("ERROR: Shotgun Reload called incorrectly!\n");
243+
244+
BaseCombatCharacter? owner = GetOwner();
245+
246+
if (owner == null)
247+
return false;
248+
249+
if (owner.GetAmmoCount(PrimaryAmmoType) <= 0)
250+
return false;
251+
252+
if (iClip1 >= GetMaxClip1())
253+
return false;
254+
255+
int j = Math.Min(1, owner.GetAmmoCount(PrimaryAmmoType));
256+
257+
if (j <= 0)
258+
return false;
259+
260+
FillClip();
261+
WeaponSound(Shared.WeaponSound.Reload);
262+
SendWeaponAnim(Activity.ACT_VM_RELOAD);
263+
264+
owner.NextAttack = gpGlobals.CurTime;
265+
NextPrimaryAttack = gpGlobals.CurTime + SequenceDuration();
266+
267+
return true;
268+
}
269+
270+
public override void FinishReload() {
271+
SetBodygroup(1, 1);
272+
273+
BaseCombatCharacter? owner = GetOwner();
274+
275+
if (owner == null)
276+
return;
277+
278+
InReload = false;
279+
280+
SendWeaponAnim(Activity.ACT_SHOTGUN_RELOAD_FINISH);
281+
282+
owner.NextAttack = gpGlobals.CurTime;
283+
NextPrimaryAttack = gpGlobals.CurTime + SequenceDuration();
284+
}
285+
286+
private void FillClip() {
287+
BaseCombatCharacter? owner = GetOwner();
288+
289+
if (owner == null)
290+
return;
291+
292+
if (owner.GetAmmoCount(PrimaryAmmoType) > 0) {
293+
if (Clip1() < GetMaxClip1()) {
294+
iClip1++;
295+
owner.RemoveAmmo(1, PrimaryAmmoType);
296+
}
297+
}
298+
}
299+
240300
public override void PrimaryAttack() {
241301
// Only the player fires this way so we can cast
242302
BasePlayer? player = ToBasePlayer(GetOwner());

‎Game.UI/OptionsSubAudio.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public class OptionsSubAudio : PropertyPage
1818
ComboBox SoundQualityCombo;
1919
CvarSlider SFXSlider;
2020
CvarSlider MusicSlider;
21+
#if GMOD_DLL
22+
CvarSlider GModSFXSlider;
23+
#endif
2124
ComboBox CloseCaptionCombo;
2225
bool RequireRestart;
2326
ComboBox SpokenLanguageCombo;
@@ -28,6 +31,9 @@ public class OptionsSubAudio : PropertyPage
2831
public OptionsSubAudio(Panel? parent, ReadOnlySpan<char> name) : base(parent, name) {
2932
SFXSlider = new(this, "SFXSlider", "#GameUI_SoundEffectVolume", 0.0f, 1.0f, "volume");
3033
MusicSlider = new(this, "MusicSlider", "#GameUI_MusicVolume", 0.0f, 1.0f, "Snd_MusicVolume");
34+
#if GMOD_DLL
35+
GModSFXSlider = new(this, "GModSFXSlider", "#GameUI_SoundFXVolume", 0.0f, 1.0f, "volume_sfx");
36+
#endif
3137

3238
CloseCaptionCombo = new(this, "CloseCaptionCheck", 6, false);
3339
CloseCaptionCombo.AddItem("#GameUI_NoClosedCaptions", null);
@@ -60,6 +66,9 @@ public override void OnResetData() {
6066
RequireRestart = false;
6167
SFXSlider.Reset();
6268
MusicSlider.Reset();
69+
#if GMOD_DLL
70+
GModSFXSlider.Reset();
71+
#endif
6372

6473
ConVarRef closecaption = new("closecaption");
6574
ConVarRef cc_subtitles = new("cc_subtitles");
@@ -103,6 +112,9 @@ public void OnControlModified(Panel sender) {
103112
public override void OnApplyChanges() {
104113
SFXSlider.ApplyChanges();
105114
MusicSlider.ApplyChanges();
115+
#if GMOD_DLL
116+
GModSFXSlider.ApplyChanges();
117+
#endif
106118

107119
// More to do
108120
}

0 commit comments

Comments
 (0)