From 1132313f52b5c45a79f121c70bdb36b0da41718b Mon Sep 17 00:00:00 2001 From: callumok2004 Date: Sun, 11 Oct 2026 05:57:09 +0100 Subject: [PATCH 1/4] LoadReplacementMaterials --- Source.MaterialSystem/MaterialSystem.cs | 43 +++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/Source.MaterialSystem/MaterialSystem.cs b/Source.MaterialSystem/MaterialSystem.cs index 4d5e6be6a..2ce179f37 100644 --- a/Source.MaterialSystem/MaterialSystem.cs +++ b/Source.MaterialSystem/MaterialSystem.cs @@ -58,6 +58,7 @@ public class MaterialSystem : IMaterialSystemInternal, IShaderUtil public IShaderAPI ShaderAPI; public readonly IMeshMgr MeshMgr; public readonly IMaterialSystemHardwareConfig HardwareConfig; + public readonly ICommandLine CommandLine; public readonly MaterialSystem_Config Config; readonly static ConVar mat_vsync = new("mat_vsync", "0", 0, "Force sync to vertical retrace", 0, 1); @@ -123,6 +124,7 @@ public MaterialSystem(IServiceProvider services) { HardwareConfig = services.GetRequiredService(); // todo: interface ShaderSystem = services.GetRequiredService(); Config = services.GetRequiredService()!; + CommandLine = services.GetRequiredService()!; HardwareRenderContext = new(this); // Link up @@ -1098,7 +1100,7 @@ void ScanDirForReplacements(ReadOnlySpan pathName) { while (!findFileName.IsEmpty) { if (FileSystem.FindIsDirectory(findHandle)) { - if (!findFileName.SequenceEqual(".") && !findFileName.SequenceEqual("..")) + if (strcmp(findFileName, ".") != 0 && strcmp(findFileName, "..") != 0) ScanDirForReplacements($"{pathName}/{findFileName}"); } @@ -1108,10 +1110,47 @@ void ScanDirForReplacements(ReadOnlySpan pathName) { FileSystem.FindClose(findHandle); } + void LoadReplacementMaterials() { + const string location = "materials"; + + if (CommandLine.FindParm("-matscan") != 0) + ScanDirForReplacements(location); + else + InitReplacementsFromFile(location); + } + + void InitReplacementsFromFile(ReadOnlySpan pathName) { + List replacementFiles = []; + + ReadListFromFile(replacementFiles, $"{pathName}/replacements.txt"); + foreach (string file in replacementFiles) { + string baseName = $"{pathName}/{file}/replacements.vmt"; + if (FileSystem.FileExists(baseName)) { + KeyValues? kv = FileSystem.LoadKeyValues(IFileSystem.KeyValuesPreloadType.VMT, baseName); + if (kv != null) + Replacements.TryAdd($"{pathName}/{file}/", kv); + } + } + } + + int ReadListFromFile(List outReplacementMaterials, string pathName) { + long size = FileSystem.Size(pathName); + if (size <= 0) + return 0; + + char[] fileContents = new char[size]; + if (!FileSystem.ReadFile(pathName, null, fileContents, 0)) + return 0; + + outReplacementMaterials.AddRange(new string(fileContents).Split(["\r\n", "\r", "\n"], StringSplitOptions.RemoveEmptyEntries)); + + return outReplacementMaterials.Count; + } + void PreloadReplacements() { Replacements.Clear(); - ScanDirForReplacements("materials"); + LoadReplacementMaterials(); ReplacementFilesValid = true; } From 90ad348031a1486809522c34112b9bd96fb6b5d1 Mon Sep 17 00:00:00 2001 From: callumok2004 Date: Sun, 11 Oct 2026 06:24:02 +0100 Subject: [PATCH 2/4] Animation hooks, more lua api --- Game.Client/C_BaseAnimating.cs | 28 +++ Game.Client/C_BaseAnimatingOverlay.cs | 14 +- Game.Client/C_BaseEntity.cs | 16 ++ Game.Client/C_BaseFlex.cs | 20 +++ Game.Client/C_BasePlayer.cs | 4 + Game.Client/C_FlexManipulate.cs | 5 + Game.Client/HLClient.cs | 12 +- Game.Server/BaseAnimating.cs | 18 ++ Game.Server/FlexManipulate.cs | 7 + Game.Server/Player.cs | 4 + Game.Shared/GarrysMod/LuaEntity.cs | 206 ++++++++++++++++++++++ Game.Shared/GarrysMod/LuaPlayer.cs | 120 +++++++++++++ Game.Shared/HL2MP/HL2MPPlayerAnimState.cs | 162 ++++++++++++++++- Game.Shared/MultiplayerAnimState.cs | 94 +++++++++- Source.Common/Exts.cs | 1 + 15 files changed, 692 insertions(+), 19 deletions(-) diff --git a/Game.Client/C_BaseAnimating.cs b/Game.Client/C_BaseAnimating.cs index 5a1e78fd4..8fa06aa74 100644 --- a/Game.Client/C_BaseAnimating.cs +++ b/Game.Client/C_BaseAnimating.cs @@ -1733,6 +1733,9 @@ void DisableMuzzleFlash() { int PrevNewSequenceParity; int PrevResetEventsParity; +#if GMOD_DLL + public virtual void ClearPrevResetEventsParity() => PrevResetEventsParity = -1; +#endif public int GetSequence() => Sequence; public override void UpdateVisibility() { @@ -1807,6 +1810,31 @@ public override void GetAimEntOrigin(C_BaseEntity attachedTo, out Vector3 origin public EHANDLE BoneManipulator = new(); [NetworkName("m_pFlexManipulator")] public EHANDLE FlexManipulator = new(); + public EHANDLE ClientFlexManipulator = new(); + + public virtual C_BaseEntity? GetFlexManipulator(bool create) { + if (FlexManipulator.IsValid()) + return FlexManipulator.Get(); + + if (ClientFlexManipulator.Get() != null) + return ClientFlexManipulator.Get(); + + if (create) { + C_FlexManipulate manipulator = new(); + if (!manipulator.InitializeAsClientEntity(null, RenderGroup.OpaqueEntity)) + return null; + + manipulator.SetParent(this, 0); + manipulator.SetOwnerEntity(this); + manipulator.SetLocalOrigin(default); + manipulator.Spawn(); + ClientFlexManipulator.Set(manipulator); + return ClientFlexManipulator.Get(); + } + + return null; + } + [NetworkName("m_OverrideViewTarget")] public Vector3 OverrideViewTarget; [NetworkName("m_fadeMinDist")] diff --git a/Game.Client/C_BaseAnimatingOverlay.cs b/Game.Client/C_BaseAnimatingOverlay.cs index 11961f80a..501afc2a5 100644 --- a/Game.Client/C_BaseAnimatingOverlay.cs +++ b/Game.Client/C_BaseAnimatingOverlay.cs @@ -79,7 +79,7 @@ private static void ResizeAnimationLayerCallback(object instance, object list, i [NetworkName("m_AnimOverlay")] readonly List AnimOverlay = []; readonly List> iv_AnimOverlay = []; - readonly float[] OverlayPrevEventCycle = new float[MAX_OVERLAYS]; + public readonly float[] OverlayPrevEventCycle = new float[MAX_OVERLAYS]; public AnimationLayerRef GetAnimOverlay(int i) => AnimOverlay[i]; @@ -200,16 +200,4 @@ public override void DoAnimationEvents(StudioHdr hdr) { } return hdr; } - - public void EstimateAbsVelocity(out Vector3 vel) { - if (this == C_BasePlayer.GetLocalPlayer()) { - // This is interpolated and networked - vel = GetAbsVelocity(); - return; - } - vel = default; - using InterpolationContext context = new(); - InterpolationContext.EnableExtrapolation(true); - IV_Origin.GetDerivative_SmoothVelocity(new Span(ref vel), gpGlobals.CurTime); - } } diff --git a/Game.Client/C_BaseEntity.cs b/Game.Client/C_BaseEntity.cs index 02f774afe..185324da9 100644 --- a/Game.Client/C_BaseEntity.cs +++ b/Game.Client/C_BaseEntity.cs @@ -2755,6 +2755,22 @@ public ref readonly Vector3 GetAbsVelocity() { CalcAbsoluteVelocity(); return ref AbsVelocity; } + + public virtual void EstimateAbsVelocity(out Vector3 vel) { +#if GMOD_DLL + if (this != C_BasePlayer.GetLocalPlayer() && GetMoveType() != Source.MoveType.Fly && GetMoveType() != Source.MoveType.FlyGravity && (!IsPlayer() || GetMoveType() == Source.MoveType.None)) { +#else + if (this != C_BasePlayer.GetLocalPlayer()) { +#endif + vel = default; + using InterpolationContext context = new(); + InterpolationContext.EnableExtrapolation(true); + IV_Origin.GetDerivative_SmoothVelocity(new Span(ref vel), gpGlobals.CurTime); + return; + } + + vel = GetAbsVelocity(); + } public void SetAbsVelocity(in Vector3 absVelocity) { if (AbsVelocity == absVelocity) return; diff --git a/Game.Client/C_BaseFlex.cs b/Game.Client/C_BaseFlex.cs index 5a816b8ca..d9fa6a4dd 100644 --- a/Game.Client/C_BaseFlex.cs +++ b/Game.Client/C_BaseFlex.cs @@ -933,6 +933,26 @@ public bool CheckSceneEvent(TimeUnit_t currenttime, ChoreoScene? scene, ChoreoEv public virtual bool CheckSceneEventCompletion(SceneEventInfo info, TimeUnit_t currenttime, ChoreoScene scene, ChoreoEvent ev) => true; +#if GMOD_DLL + public void SetFlexWeight(LocalFlexController index, float value, bool clamp) { + if (index >= 0 && index < GetNumFlexControllers() && (int)index < 96) { + StudioHdr? studioHdr = GetModelPtr(); + if (studioHdr == null) + return; + + MStudioFlexController flexcontroller = studioHdr.FlexController(index); + + if (flexcontroller.Max != flexcontroller.Min) { + value = (value - flexcontroller.Min) / (flexcontroller.Max - flexcontroller.Min); + if (clamp) + value = Math.Clamp(value, 0.0f, 1.0f); + } + + FlexWeight[(int)index] = value; + } + } +#endif + public void SetFlexWeight(LocalFlexController index, float value) { if (index >= 0 && index < GetNumFlexControllers()) { StudioHdr? studioHdr = GetModelPtr(); diff --git a/Game.Client/C_BasePlayer.cs b/Game.Client/C_BasePlayer.cs index ab7310dfb..1cd4669e3 100644 --- a/Game.Client/C_BasePlayer.cs +++ b/Game.Client/C_BasePlayer.cs @@ -253,6 +253,9 @@ public virtual void PreThink() { } public C_BaseEntity? GetVehicleEntity() => Vehicle.Get(); public bool IsInAVehicle() => Vehicle.Get() != null; +#if GMOD_DLL + public virtual bool RestrictPlayerPitch() => false; +#endif public virtual void SetAnimation(PlayerAnim playerAnim) { } // todo public virtual Vector3 GetAutoaimVector(float scale) { @@ -571,6 +574,7 @@ public void SetLocalViewAngles(in QAngle angles) { EHANDLE ColorCorrectionCtrl = new(); [NetworkName("m_bUseWeaponsInVehicle")] bool UseWeaponsInVehicle; + public bool GetAllowWeaponsInVehicle() => UseWeaponsInVehicle; public bool OnTarget; [NetworkName("m_flDeathTime")] public double DeathTime; diff --git a/Game.Client/C_FlexManipulate.cs b/Game.Client/C_FlexManipulate.cs index 7f26b9cbe..c294b73d4 100644 --- a/Game.Client/C_FlexManipulate.cs +++ b/Game.Client/C_FlexManipulate.cs @@ -21,4 +21,9 @@ public class C_FlexManipulate : C_BaseEntity public Vector3 EyesLocalTarget; [NetworkName("m_iFlexWeights")] public InlineArray96 FlexWeights; + + public void SetFlexWeight(byte index, float weight) { + if (index < 96 && FlexWeights[index] != weight) + FlexWeights[index] = weight; + } } diff --git a/Game.Client/HLClient.cs b/Game.Client/HLClient.cs index 62b53e9c4..db68396a2 100644 --- a/Game.Client/HLClient.cs +++ b/Game.Client/HLClient.cs @@ -760,8 +760,18 @@ public void GMOD_DoSnapshots() { throw new NotImplementedException(); } + public static InlineArray128 g_VoiceVolume; + public void GMOD_VoiceVolume(uint playerID, float volume) { - throw new NotImplementedException(); + if (playerID == 512) { + C_BasePlayer? localPlayer = C_BasePlayer.GetLocalPlayer(); + if (localPlayer == null) + return; + playerID = (uint)localPlayer.EntIndex(); + } + + if (playerID - 1U < 128) + g_VoiceVolume[(int)playerID - 1] = volume; } public void GMOD_OnDrawSkybox() { diff --git a/Game.Server/BaseAnimating.cs b/Game.Server/BaseAnimating.cs index 53028d31f..b4f401e41 100644 --- a/Game.Server/BaseAnimating.cs +++ b/Game.Server/BaseAnimating.cs @@ -128,6 +128,24 @@ public partial class BaseAnimating : BaseEntity public EHANDLE BoneManipulator = new(); [NetworkName("m_pFlexManipulator")] public EHANDLE FlexManipulator = new(); + + public virtual void SetFlexManipulator(BaseEntity? manipulator) => FlexManipulator.Set(manipulator); + + public virtual BaseEntity? GetFlexManipulator(bool create) { + if (FlexManipulator.Get() == null && create) { + BaseEntity? manipulator = CreateEntityByName("manipulate_flex"); + if (manipulator == null) + return null; + + manipulator.SetParent(this, -1); + manipulator.SetLocalOrigin(default); + manipulator.Spawn(); + SetFlexManipulator(manipulator); + } + + return FlexManipulator.Get(); + } + [NetworkName("m_fadeMinDist")] public float FadeMinDist; [NetworkName("m_fadeMaxDist")] diff --git a/Game.Server/FlexManipulate.cs b/Game.Server/FlexManipulate.cs index 0c1ca5d23..b1961bfe7 100644 --- a/Game.Server/FlexManipulate.cs +++ b/Game.Server/FlexManipulate.cs @@ -21,4 +21,11 @@ public class FlexManipulate : BaseEntity public Vector3 EyesLocalTarget; [NetworkName("m_iFlexWeights")] public InlineArray96 FlexWeights; + + public void SetFlexWeight(byte index, float weight) { + if (index < 96 && FlexWeights[index] != weight) { + NetworkStateChanged(); + FlexWeights[index] = weight; + } + } } diff --git a/Game.Server/Player.cs b/Game.Server/Player.cs index 7f99f0491..7fbaf525d 100644 --- a/Game.Server/Player.cs +++ b/Game.Server/Player.cs @@ -523,6 +523,9 @@ public void SetBodyPitch(float pitch) { public IServerVehicle? GetVehicle() => Vehicle.Get()?.GetServerVehicle(); public override BaseEntity? GetVehicleEntity() => Vehicle.Get(); public override bool IsInAVehicle() => Vehicle.Get() != null; +#if GMOD_DLL + public virtual bool RestrictPlayerPitch() => false; +#endif public float GetStepSize() => Local.StepSize; public BaseEntity? GetViewEntity() => ViewEntity.Get(); @@ -577,6 +580,7 @@ public void EnableControl(bool control) { EHANDLE ColorCorrectionCtrl = new(); [NetworkName("m_bUseWeaponsInVehicle")] bool UseWeaponsInVehicle; + public bool GetAllowWeaponsInVehicle() => UseWeaponsInVehicle; public bool OnTarget; [NetworkName("m_flDeathTime")] public TimeUnit_t DeathTime; diff --git a/Game.Shared/GarrysMod/LuaEntity.cs b/Game.Shared/GarrysMod/LuaEntity.cs index 93026c82c..68dfd5186 100644 --- a/Game.Shared/GarrysMod/LuaEntity.cs +++ b/Game.Shared/GarrysMod/LuaEntity.cs @@ -57,6 +57,17 @@ public static partial class LuaEntity public static readonly LuaEntityClass LC_Vehicle = new("Vehicle", LuaType.Entity, null, "Entity"); public static readonly LuaEntityClass LC_Weapon = new("Weapon", LuaType.Entity, null, "Entity"); + // todo + static readonly List> RemoveList = []; + + static bool IsQueuedForRemoval(BaseEntity ent) { + foreach (Handle handle in RemoveList) { + if (handle.Get() == ent) + return true; + } + return false; + } + static bool bWarning; public static BaseEntity? GetEntityFromHandle(uint handle) { @@ -445,6 +456,201 @@ static int Entity__GetPos(ILuaInterface lua) { return 1; } + [LuaMethod] + static int Entity__GetMoveType(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + lua.PushNumber((int)ent.GetMoveType()); + return 1; + } + + [LuaMethod] + static int Entity__WaterLevel(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + if (!ent.IsPlayer()) + ent.UpdateWaterState(); + lua.PushNumber((int)ent.GetWaterLevel()); + return 1; + } + + [LuaMethod] + [LuaMethod("IsOnGround")] + static int Entity__OnGround(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + lua.PushBool((ent.GetFlags() & EntityFlags.OnGround) != 0); + return 1; + } + + [LuaMethod] + static int Entity__IsFlagSet(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + lua.PushBool(((int)ent.GetFlags() & (int)lua.CheckNumber(2)) != 0); + return 1; + } + + [LuaMethod] + static int Entity__GetParent(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + Push_Entity(ent.GetMoveParent()); + return 1; + } + + [LuaMethod] + static int Entity__GetAngles(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + LuaAngle.Push_Angle(ent.GetAbsAngles()); + return 1; + } + + [LuaMethod] + static int Entity__GetUp(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + ent.GetVectors(out _, out _, out Vector3 up); + LuaVector.Push_Vector(up); + return 1; + } + + [LuaMethod] + static int Entity__GetVelocity(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + Vector3 velocity; +#if CLIENT_DLL + ent.EstimateAbsVelocity(out velocity); + if (ent.GetMoveType() == MoveType.VPhysics && ent.VPhysicsGetObject() != null) + ent.VPhysicsGetObject()!.GetVelocity(out velocity, out _); +#else + if (ent.GetMoveType() == MoveType.VPhysics) { + velocity = default; + ent.VPhysicsGetObject()?.GetVelocity(out velocity, out _); + } + else + velocity = ent.GetAbsVelocity(); +#endif + LuaVector.Push_Vector(velocity); + return 1; + } + + [LuaMethod] + static int Entity__LookupSequence(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + BaseAnimating? animating = ent.GetBaseAnimating(); + if (animating == null || ent.IsMarkedForDeletion()) + return 0; + + int sequence = animating.LookupSequence(lua.CheckString(2)); + lua.PushNumber(sequence); + if (sequence == -1) { + lua.PushNil(); + return 2; + } + + lua.PushNumber((float)animating.SequenceDuration(sequence)); + return 2; + } + + static int GetPoseParameterIndex(ILuaInterface lua, BaseAnimating animating) { + if (lua.GetType(2) == LuaType.Number) + return (int)lua.CheckNumber(2); + return animating.LookupPoseParameter(lua.CheckString(2)); + } + + [LuaMethod] + static int Entity__GetPoseParameter(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + BaseAnimating? animating = ent.GetBaseAnimating(); + if (animating == null || ent.IsMarkedForDeletion()) + return 0; + + lua.PushNumber(animating.GetPoseParameter(GetPoseParameterIndex(lua, animating))); + return 1; + } + + [LuaMethod] + static int Entity__SetPoseParameter(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + BaseAnimating? animating = ent.GetBaseAnimating(); + if (animating == null || IsQueuedForRemoval(ent) || ent.IsMarkedForDeletion()) + return 0; + + int index = GetPoseParameterIndex(lua, animating); + animating.SetPoseParameter(index, (float)lua.CheckNumber(3)); + return 0; + } + + [LuaMethod] + static int Entity__SetPlaybackRate(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + BaseAnimating? animating = ent.GetBaseAnimating(); + if (animating == null || IsQueuedForRemoval(ent) || ent.IsMarkedForDeletion()) + return 0; + + float rate = (float)lua.CheckNumber(2); + if (!float.IsFinite(rate)) + rate = 1.0f; + animating.SetPlaybackRate(rate); + return 0; + } + +#if CLIENT_DLL + [LuaMethod] + static int Entity__SetIK(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + if (lua.GetBool(2)) + ent.EntClientFlags &= ~EntClientFlags.DontUseIK; + else + ent.EntClientFlags |= EntClientFlags.DontUseIK; + return 0; + } +#else + // todo: Entity:SetIK +#endif + + [LuaMethod] + static int Entity__GetFlexIDByName(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + BaseAnimating? animating = ent.GetBaseAnimating(); + if (animating == null || ent.IsMarkedForDeletion()) + return 0; + + StudioHdr? studioHdr = animating.GetModelPtr(); + if (studioHdr == null) + return 0; + + string name = lua.CheckString(2); + for (int i = 0; i < (int)studioHdr.NumFlexControllers(); i++) { + if (strnicmp(studioHdr.FlexController((LocalFlexController)i).Name(), name, name.Length) == 0) { + lua.PushNumber(i); + return 1; + } + } + + return 0; + } + + [LuaMethod] + static int Entity__SetFlexWeight(ILuaInterface lua) { + BaseEntity ent = Get_Entity(1, false)!; + BaseAnimating? animating = ent.GetBaseAnimating(); + if (animating == null || IsQueuedForRemoval(ent) || ent.IsMarkedForDeletion()) + return 0; + + int index = Math.Clamp((int)lua.CheckNumber(2), 0, 95); + float weight = (float)lua.CheckNumber(3); + +#if CLIENT_DLL + if (animating.GetFlexManipulator(false) == null && animating is C_BaseFlex flex) { + flex.SetFlexWeight((LocalFlexController)index, weight, false); + return 0; + } + + if (animating.GetFlexManipulator(true) is C_FlexManipulate manipulator) + manipulator.SetFlexWeight((byte)index, weight); +#else + if (animating.GetFlexManipulator(true) is FlexManipulate manipulator) + manipulator.SetFlexWeight((byte)index, weight); +#endif + return 0; + } + [LuaMethod] static int Entity__GetForward(ILuaInterface lua) { BaseEntity ent = Get_Entity(1, false)!; diff --git a/Game.Shared/GarrysMod/LuaPlayer.cs b/Game.Shared/GarrysMod/LuaPlayer.cs index 0077914f5..08391c411 100644 --- a/Game.Shared/GarrysMod/LuaPlayer.cs +++ b/Game.Shared/GarrysMod/LuaPlayer.cs @@ -1,10 +1,14 @@ #if CLIENT_DLL || GAME_DLL +using Game.Shared; + using Source.Common.GarrysMod.Lua; using Source.Common.Mathematics; using System.Numerics; #if CLIENT_DLL +using GMOD_Player = Game.Client.GarrysMod.C_GMOD_Player; + namespace Game.Client.GarrysMod; #else namespace Game.Server.GarrysMod; @@ -107,6 +111,122 @@ static int Player__GetActiveWeapon(ILuaInterface lua) { return 1; } + [LuaMethod] + static int Player__TranslateWeaponActivity(ILuaInterface lua) { + BasePlayer player = Get_Player(1, false)!; + int act = (int)lua.CheckNumber(2); + BaseCombatWeapon? weapon = player.GetActiveWeapon(); + if (weapon != null) { + bool required = false; + act = (int)weapon.ActivityOverride((Activity)act, ref required); + } + lua.PushNumber(act); + return 1; + } + + [LuaMethod] + static int Player__GetAllowWeaponsInVehicle(ILuaInterface lua) { + BasePlayer player = Get_Player(1, false)!; + lua.PushBool(player.GetAllowWeaponsInVehicle()); + return 1; + } + + [LuaMethod] + static int Player__IsPlayingTaunt(ILuaInterface lua) { + BasePlayer player = Get_Player(1, false)!; + lua.PushBool(((GMOD_Player)player).GestureEndTime > gpGlobals.CurTime); + return 1; + } + + [LuaMethod] + static int Player__IsTyping(ILuaInterface lua) { + BasePlayer player = Get_Player(1, false)!; + lua.PushBool(((GMOD_Player)player).IsTyping); + return 1; + } + + [LuaMethod] + static int Player__IsSpeaking(ILuaInterface lua) { + BasePlayer player = Get_Player(1, false)!; +#if CLIENT_DLL + if (player.IsLocalPlayer()) + lua.PushBool(GetClientVoiceMgr()!.IsLocalPlayerSpeaking()); + else + lua.PushBool(GetClientVoiceMgr()!.IsPlayerSpeaking(player.EntIndex())); +#else + lua.PushBool(gpGlobals.CurTime - player.LastTimePlayerTalked() < 0.1); +#endif + return 1; + } + +#if CLIENT_DLL + [LuaMethod] + static int Player__VoiceVolume(ILuaInterface lua) { + BasePlayer player = Get_Player(1, false)!; + int index = player.EntIndex(); + if ((uint)(index - 1) < 128) { + lua.PushNumber(HLClient.g_VoiceVolume[index - 1]); + return 1; + } + return 0; + } +#endif + + [LuaMethod] + static int Player__InVehicle(ILuaInterface lua) { + BasePlayer player = Get_Player(1, false)!; + lua.PushBool(player.IsInAVehicle()); + return 1; + } + + [LuaMethod] + static int Player__AnimResetGestureSlot(ILuaInterface lua) { + BasePlayer player = Get_Player(1, false)!; + HL2MPPlayerAnimState? animState = (player as HL2MP_Player)?.PlayerAnimState; + if (animState == null) + return 0; + + animState.ResetGestureSlot((int)lua.CheckNumber(2)); + return 0; + } + + [LuaMethod] + static int Player__AnimRestartGesture(ILuaInterface lua) { + BasePlayer player = Get_Player(1, false)!; + HL2MPPlayerAnimState? animState = (player as HL2MP_Player)?.PlayerAnimState; + if (animState == null) + return 0; + + animState.RestartGesture((int)lua.CheckNumber(2), (Activity)(int)lua.CheckNumber(3), lua.GetBool(4)); + return 0; + } + + [LuaMethod] + static int Player__AnimRestartMainSequence(ILuaInterface lua) { + BasePlayer player = Get_Player(1, false)!; + HL2MPPlayerAnimState? animState = (player as HL2MP_Player)?.PlayerAnimState; + if (animState == null) + return 0; + + animState.RestartMainSequence(); + return 0; + } + + [LuaMethod] + static int Player__AnimSetGestureWeight(ILuaInterface lua) { + BasePlayer player = Get_Player(1, false)!; + HL2MPPlayerAnimState? animState = (player as HL2MP_Player)?.PlayerAnimState; + if (animState == null) + return 0; + + AnimationLayerRef? layer = animState.GetGestureSlotLayer((int)lua.CheckNumber(2)); + if (layer == null) + return 0; + + layer.Weight = (float)lua.CheckNumber(3); + return 0; + } + [LuaMethod] static int Player__HasWeapon(ILuaInterface lua) { BasePlayer player = Get_Player(1, false)!; diff --git a/Game.Shared/HL2MP/HL2MPPlayerAnimState.cs b/Game.Shared/HL2MP/HL2MPPlayerAnimState.cs index ec21d109c..3fbccef36 100644 --- a/Game.Shared/HL2MP/HL2MPPlayerAnimState.cs +++ b/Game.Shared/HL2MP/HL2MPPlayerAnimState.cs @@ -4,6 +4,15 @@ using Game.Client; #endif +#if GMOD_DLL +#if CLIENT_DLL +using Game.Client.GarrysMod; +#else +using Game.Server.GarrysMod; +#endif +using Source.Common.GarrysMod.Lua; +#endif + using Source; using Source.Common; using Source.Common.Mathematics; @@ -44,7 +53,38 @@ public static HL2MPPlayerAnimState CreateHL2MPPlayerAnimState(HL2MP_Player playe public override void ClearAnimationState() => base.ClearAnimationState(); +#if GMOD_DLL + public override Activity CalcMainActivity() { + if (gGM != null && gGM.CallWithArgs((int)LUA_POOLEDSTRING.CalcMainActivity)) { + GetOuterAbsVelocity(out Vector3 velocity); + LuaEntity.Push_Entity(HL2MPPlayer); + LuaVector.Push_Vector(velocity); + if (gGM.CallReturns(2, 2)) { + ILuaObject ideal = g_Lua!.GetReturn(1); + ILuaObject seqOverride = g_Lua.GetReturn(0); + if (!ideal.isNil() && !seqOverride.isNil()) { + SpecificMainSequence = seqOverride.GetInt(); + ShowDebugInfo(); + return (Activity)ideal.GetInt(); + } + } + } + return 0; + } +#endif + public override Activity TranslateActivity(Activity actDesired) { +#if GMOD_DLL + if (gGM != null && gGM.CallWithArgs((int)LUA_POOLEDSTRING.TranslateActivity)) { + LuaEntity.Push_Entity(GetHL2MPPlayer()); + g_Lua!.PushNumber((int)actDesired); + if (gGM.CallReturns(2, 1)) { + ILuaObject ret = g_Lua.GetReturn(0); + if (!ret.isNil()) + return (Activity)ret.GetInt(); + } + } +#endif Activity translateActivity = actDesired; bool required = false; @@ -79,12 +119,132 @@ public override void Update(float eyeYaw, float eyePitch) { ComputePoseParam_AimYaw(studioHdr); } +#if GMOD_DLL +#if CLIENT_DLL + hl2mpPlayer.UpdateLookAt(); +#endif + if (gGM != null && gGM.CallWithArgs((int)LUA_POOLEDSTRING.UpdateAnimation)) { + BasePlayer player = GetBasePlayer(); + float maxSeqGroundSpeed = (float)player.GetSequenceGroundSpeed(player.GetModelPtr(), player.GetSequence()); + GetOuterAbsVelocity(out Vector3 velocity); + LuaEntity.Push_Entity(HL2MPPlayer); + LuaVector.Push_Vector(velocity); + g_Lua!.PushNumber(maxSeqGroundSpeed); + gGM.CallNoReturns(3); + } + + if (hl2mpPlayer.GetMoveParent() == null && hl2mpPlayer.RestrictPlayerPitch()) { + AngRender[PITCH] = 0; + AngRender[ROLL] = 0; + } + else + AngRender = hl2mpPlayer.GetAbsAngles(); +#else #if CLIENT_DLL if (C_BasePlayer.ShouldDrawLocalPlayer()) HL2MPPlayer!.SetPlaybackRate(1.0f); +#endif #endif } - public override void DoAnimationEvent(PlayerAnimEvent evt, int data = 0) => throw new NotImplementedException(); + + public override void DoAnimationEvent(PlayerAnimEvent evt, int data = 0) { + Activity gestureActivity = Activity.ACT_INVALID; + +#if GMOD_DLL + if (gGM != null && gGM.CallWithArgs((int)LUA_POOLEDSTRING.DoAnimationEvent)) { + LuaEntity.Push_Entity(HL2MPPlayer); + g_Lua!.PushNumber((int)evt); + g_Lua.PushNumber(data); + if (gGM.CallReturns(3, 1)) { + ILuaObject ret = g_Lua.GetReturn(0); + if (!ret.isNil()) { +#if CLIENT_DLL + PlayWeaponGesture((Activity)ret.GetInt()); +#endif + return; + } + } + } +#endif + + switch (evt) { + case PlayerAnimEvent.AttackPrimary: + if ((HL2MPPlayer!.GetFlags() & EntityFlags.Ducking) != 0) + RestartGesture((int)GestureSlotIndex.AttackAndReload, Activity.ACT_MP_ATTACK_CROUCH_PRIMARYFIRE); + else + RestartGesture((int)GestureSlotIndex.AttackAndReload, Activity.ACT_MP_ATTACK_STAND_PRIMARYFIRE); + + gestureActivity = Activity.ACT_VM_PRIMARYATTACK; + break; + case PlayerAnimEvent.VoiceCommandGesture: + if (!IsGestureSlotActive((int)GestureSlotIndex.AttackAndReload)) + RestartGesture((int)GestureSlotIndex.AttackAndReload, (Activity)data); + break; + case PlayerAnimEvent.AttackSecondary: + if ((HL2MPPlayer!.GetFlags() & EntityFlags.Ducking) != 0) + RestartGesture((int)GestureSlotIndex.AttackAndReload, Activity.ACT_MP_ATTACK_CROUCH_SECONDARYFIRE); + else + RestartGesture((int)GestureSlotIndex.AttackAndReload, Activity.ACT_MP_ATTACK_STAND_SECONDARYFIRE); + + gestureActivity = Activity.ACT_VM_PRIMARYATTACK; + break; + case PlayerAnimEvent.AttackPre: + if ((HL2MPPlayer!.GetFlags() & EntityFlags.Ducking) != 0) + gestureActivity = Activity.ACT_MP_ATTACK_CROUCH_PREFIRE; + else + gestureActivity = Activity.ACT_MP_ATTACK_STAND_PREFIRE; + + RestartGesture((int)GestureSlotIndex.AttackAndReload, gestureActivity, false); + break; + case PlayerAnimEvent.AttackPost: + RestartGesture((int)GestureSlotIndex.AttackAndReload, Activity.ACT_MP_ATTACK_STAND_POSTFIRE); + break; + case PlayerAnimEvent.Reload: + if ((GetBasePlayer().GetFlags() & EntityFlags.Ducking) != 0) + RestartGesture((int)GestureSlotIndex.AttackAndReload, Activity.ACT_MP_RELOAD_CROUCH); + else + RestartGesture((int)GestureSlotIndex.AttackAndReload, Activity.ACT_MP_RELOAD_STAND); + break; + case PlayerAnimEvent.ReloadLoop: + if ((GetBasePlayer().GetFlags() & EntityFlags.Ducking) != 0) + RestartGesture((int)GestureSlotIndex.AttackAndReload, Activity.ACT_MP_RELOAD_CROUCH_LOOP); + else + RestartGesture((int)GestureSlotIndex.AttackAndReload, Activity.ACT_MP_RELOAD_STAND_LOOP); + break; + case PlayerAnimEvent.ReloadEnd: + if ((GetBasePlayer().GetFlags() & EntityFlags.Ducking) != 0) + RestartGesture((int)GestureSlotIndex.AttackAndReload, Activity.ACT_MP_RELOAD_CROUCH_END); + else + RestartGesture((int)GestureSlotIndex.AttackAndReload, Activity.ACT_MP_RELOAD_STAND_END); + break; + default: + base.DoAnimationEvent(evt, data); + break; + } + +#if CLIENT_DLL + PlayWeaponGesture(gestureActivity); +#endif + } + +#if CLIENT_DLL + void PlayWeaponGesture(Activity gestureActivity) { +#if GMOD_DLL + if (GetHL2MPPlayer().EntIndex() == engine.GetLocalPlayer() && !C_BasePlayer.ShouldDrawLocalPlayer()) + return; +#endif + if (gestureActivity != Activity.ACT_INVALID) { + BaseCombatWeapon? weapon = GetHL2MPPlayer().GetActiveWeapon(); + if (weapon != null) { + weapon.SendWeaponAnim(gestureActivity); +#if GMOD_DLL + weapon.ClearPrevResetEventsParity(); +#endif + weapon.DoAnimationEvents(weapon.GetModelPtr()!); + } + } + } +#endif protected override bool HandleSwimming(ref Activity idealActivity) { bool inWater = base.HandleSwimming(ref idealActivity); diff --git a/Game.Shared/MultiplayerAnimState.cs b/Game.Shared/MultiplayerAnimState.cs index 26fd780fb..6a2327a22 100644 --- a/Game.Shared/MultiplayerAnimState.cs +++ b/Game.Shared/MultiplayerAnimState.cs @@ -384,15 +384,101 @@ protected bool IsGestureSlotPlaying(int gestureSlot, Activity gestureActivity) { return GestureSlots[gestureSlot].Activity == gestureActivity; } - protected virtual void RestartGesture(int gestureSlot, Activity gestureActivity, bool autoKill = true) => throw new NotImplementedException(); - protected void AddToGestureSlot(int gestureSlot, Activity gestureActivity, bool autoKill) => throw new NotImplementedException(); + public virtual void RestartGesture(int gestureSlot, Activity gestureActivity, bool autoKill = true) { + Assert(gestureSlot >= 0 && gestureSlot < (int)GestureSlotIndex.Count); + + if (!VerifyAnimLayerInSlot(gestureSlot)) + return; + + if (!IsGestureSlotPlaying(gestureSlot, gestureActivity)) { +#if CLIENT_DLL + if (IsGestureSlotActive(gestureSlot)) { + GestureSlot gesture = GestureSlots[gestureSlot]; + if (gesture != null && gesture.AnimLayer != null) { + gesture.AnimLayer.Cycle = 1.0; + RunGestureSlotAnimEventsToCompletion(GestureSlots[gestureSlot]); + } + } +#endif + + Activity idealGestureActivity = TranslateActivity(gestureActivity); + AddToGestureSlot(gestureSlot, idealGestureActivity, autoKill); + return; + } + + GestureSlots[gestureSlot].AnimLayer!.Cycle = 0.0f; + GestureSlots[gestureSlot].AnimLayer!.PrevCycle = 0.0f; + } + + protected void AddToGestureSlot(int gestureSlot, Activity gestureActivity, bool autoKill) { + Assert(gestureSlot >= 0 && gestureSlot < (int)GestureSlotIndex.Count); + + BasePlayer player = GetBasePlayer(); + if (player == null) + return; + + if (GestureSlots[gestureSlot].AnimLayer == null) + return; + + if (!VerifyAnimLayerInSlot(gestureSlot)) + return; + + int gestureSequence = player.SelectWeightedSequence(gestureActivity); + if (gestureSequence <= 0) + return; + + GestureSlot slot = GestureSlots[gestureSlot]; + AnimationLayerRef layer = slot.AnimLayer!; + +#if CLIENT_DLL + slot.GestureSlotIndex = gestureSlot; + slot.Activity = gestureActivity; + slot.AutoKill = autoKill; + slot.Active = true; + layer.Sequence = gestureSequence; + layer.Order = gestureSlot; + layer.Weight = 1.0f; + layer.PlaybackRate = 1.0f; + layer.Cycle = 0.0f; + layer.PrevCycle = 0.0f; + layer.LayerAnimtime = 0.0f; + layer.LayerFadeOuttime = 0.0f; + + player.OverlayPrevEventCycle[gestureSlot] = -1.0f; +#else + slot.GestureSlotIndex = gestureSlot; + slot.Activity = gestureActivity; + slot.AutoKill = autoKill; + slot.Active = true; + layer.Activity = gestureActivity; + layer.Order = gestureSlot; + layer.Priority = 0; + layer.Cycle = 0.0f; + layer.PrevCycle = 0.0f; + layer.PlaybackRate = 1.0f; + layer.Activity = gestureActivity; + layer.Sequence = gestureSequence; + layer.Weight = 1.0f; + layer.BlendIn = 0.0f; + layer.BlendOut = 0.0f; + layer.SequenceFinished = false; + layer.LastEventCheck = 0.0f; + layer.LastEventCheck = gpGlobals.CurTime; + layer.Looping = false; + if (autoKill) + layer.Flags |= AnimLayerFlags.AutoKill; + else + layer.Flags &= ~AnimLayerFlags.AutoKill; + layer.Flags |= AnimLayerFlags.Active; +#endif + } public void AddVCDSequenceToGestureSlot(int gestureSlot, int gestureSequence, TimeUnit_t cycle = 0.0f, bool autoKill = true) => throw new NotImplementedException(); - public AnimationLayerRef? GetGestureSlotLayer(int gestureSlot) => throw new NotImplementedException(); + public AnimationLayerRef? GetGestureSlotLayer(int gestureSlot) => GestureSlots[gestureSlot].AnimLayer; public virtual void ShowDebugInfo() { if (anim_showstate.GetInt() == GetBasePlayer().EntIndex()) DebugShowAnimStateForPlayer(GetBasePlayer().IsServer()); } - protected virtual void RestartMainSequence() { + public virtual void RestartMainSequence() { BaseAnimatingOverlay player = GetBasePlayer(); if (player != null) { player.AnimTime = gpGlobals.CurTime; diff --git a/Source.Common/Exts.cs b/Source.Common/Exts.cs index 8d9d9b9e9..0cd88f9c2 100644 --- a/Source.Common/Exts.cs +++ b/Source.Common/Exts.cs @@ -1141,6 +1141,7 @@ public static void FileInfo(this ReadOnlySpan inStr, Span slashSepa case '/': case '\\': lastSlashIdx = i; + lastDotIdx = -1; if (!slashSeparators.IsEmpty && slashSepIdx < slashSeparators.Length) { slashSeparators[slashSepIdx++] = new(lastMetRange, i - 1); lastMetRange = i + 1; From db7572f97e68facc114e32509fc59711029982ed Mon Sep 17 00:00:00 2001 From: callumok2004 Date: Sun, 11 Oct 2026 06:36:38 +0100 Subject: [PATCH 3/4] Remaining anim stuff --- Game.Client/C_BaseAnimating.cs | 27 +- Game.Client/GarrysMod/C_GMOD_Player.cs | 17 ++ Game.Client/HL2MP/C_HL2MP_Player.cs | 8 + Game.Server/BaseAnimating.cs | 7 + Game.Server/HL2MP/HL2MP_Player.cs | 4 + Game.Shared/HL2MP/HL2MPPlayerAnimState.cs | 2 + Game.Shared/MultiplayerAnimState.cs | 317 +++++++++++++++++++++- 7 files changed, 377 insertions(+), 5 deletions(-) diff --git a/Game.Client/C_BaseAnimating.cs b/Game.Client/C_BaseAnimating.cs index 8fa06aa74..34ad032f0 100644 --- a/Game.Client/C_BaseAnimating.cs +++ b/Game.Client/C_BaseAnimating.cs @@ -8,6 +8,7 @@ using Source.Common; using Source.Common.Commands; using Source.Common.Engine; +using Source.Common.Formats.Keyvalues; using Source.Common.Mathematics; using System; @@ -179,6 +180,28 @@ public TimeUnit_t GetSequenceMoveDist(StudioHdr? studioHdr, int sequence) { return vecReturn.Length(); } public TimeUnit_t GetSequenceGroundSpeed(int sequence) => GetSequenceGroundSpeed(GetModelPtr(), sequence); + static string? Studio_GetKeyValueText(StudioHdr? studioHdr, int sequence) { + if (studioHdr != null && studioHdr.SequencesAvailable()) { + if (sequence >= 0 && sequence < studioHdr.GetNumSeq()) { + MStudioSeqDesc seqdesc = studioHdr.Seqdesc(sequence); + if (seqdesc.KeyValueSize != 0) + return System.Text.Encoding.ASCII.GetString(((ReadOnlySpan)seqdesc.Data.Span[seqdesc.KeyValueIndex..]).SliceNullTerminatedString()); + } + } + return null; + } + + public KeyValues? GetSequenceKeyValues(int sequence) { + string? text = Studio_GetKeyValueText(GetModelPtr(), sequence); + + if (text != null) { + KeyValues seqKeyValues = new(""); + if (seqKeyValues.LoadFromBuffer(modelinfo.GetModelName(GetModel()), text)) + return seqKeyValues; + } + return null; + } + public TimeUnit_t GetSequenceGroundSpeed(StudioHdr? studioHdr, int sequence) { TimeUnit_t t = SequenceDuration(studioHdr, sequence); if (t > 0) @@ -1283,7 +1306,9 @@ public double FrameAdvance(double interval) { return flInterval; } - public virtual void UpdateClientSideAnimation() { + public virtual void UpdateClientSideAnimation() => UpdateBaseClientSideAnimation(); + + protected void UpdateBaseClientSideAnimation() { if (ClientSideAnimation) { Assert(ClientSideAnimationListHandle != INVALID_CLIENTSIDEANIMATION_LIST_HANDLE); if (GetSequence() != -1) { diff --git a/Game.Client/GarrysMod/C_GMOD_Player.cs b/Game.Client/GarrysMod/C_GMOD_Player.cs index 56404f3c0..a9e3b7129 100644 --- a/Game.Client/GarrysMod/C_GMOD_Player.cs +++ b/Game.Client/GarrysMod/C_GMOD_Player.cs @@ -4,6 +4,8 @@ using Source; using Source.Common; using Source.Common.Commands; +using Source.Common.GarrysMod.Lua; +using Source.Common.Mathematics; using System.Numerics; @@ -79,4 +81,19 @@ public partial class C_GMOD_Player() : C_HL2MP_Player() public float Gravity; [NetworkName("m_bSprintEnabled")] public bool SprintEnabled; + + public override void UpdateClientSideAnimation() { + if (Driving.Get() != null) { + if (gGM != null && gGM.CallWithArgs((int)LUA_POOLEDSTRING.PlayerDriveAnimate)) { + LuaEntity.Push_Entity(this); + gGM.CallNoReturns(1); + } + } + else { + QAngle eyeAngles = EyeAngles(); + PlayerAnimState.Update(eyeAngles[YAW], eyeAngles[PITCH]); + } + + UpdateBaseClientSideAnimation(); + } } diff --git a/Game.Client/HL2MP/C_HL2MP_Player.cs b/Game.Client/HL2MP/C_HL2MP_Player.cs index 9b6651bbf..d6250d39a 100644 --- a/Game.Client/HL2MP/C_HL2MP_Player.cs +++ b/Game.Client/HL2MP/C_HL2MP_Player.cs @@ -102,10 +102,18 @@ public override void PostThink() { AngEyeAngles = EyeAngles(); } +#if GMOD_DLL + public override void UpdateClientSideAnimation() { + QAngle eyeAngles = EyeAngles(); + PlayerAnimState.Update(eyeAngles[YAW], eyeAngles[PITCH]); + UpdateBaseClientSideAnimation(); + } +#else public override void ClientThink() { base.ClientThink(); PlayerAnimState.Update(AngEyeAngles[YAW], AngEyeAngles[PITCH]); } +#endif public override ShadowType ShadowCastType() { if (!IsVisible()) diff --git a/Game.Server/BaseAnimating.cs b/Game.Server/BaseAnimating.cs index b4f401e41..65acc1d43 100644 --- a/Game.Server/BaseAnimating.cs +++ b/Game.Server/BaseAnimating.cs @@ -943,4 +943,11 @@ public void SetBodygroup(int group, int value) { public bool IsUsingClientSideAnimation() { return ClientSideAnimation; } + + public void UseClientSideAnimation(bool use) { + if (ClientSideAnimation != use) { + NetworkStateChanged(); + ClientSideAnimation = use; + } + } } diff --git a/Game.Server/HL2MP/HL2MP_Player.cs b/Game.Server/HL2MP/HL2MP_Player.cs index 88a2b8a0f..cb0d92d81 100644 --- a/Game.Server/HL2MP/HL2MP_Player.cs +++ b/Game.Server/HL2MP/HL2MP_Player.cs @@ -112,6 +112,10 @@ public HL2MP_Player() { AngEyeAngles.Init(); base.ChangeTeam(0); + +#if GMOD_DLL + UseClientSideAnimation(true); +#endif } public override void UpdateOnRemove() { diff --git a/Game.Shared/HL2MP/HL2MPPlayerAnimState.cs b/Game.Shared/HL2MP/HL2MPPlayerAnimState.cs index 3fbccef36..e485c5362 100644 --- a/Game.Shared/HL2MP/HL2MPPlayerAnimState.cs +++ b/Game.Shared/HL2MP/HL2MPPlayerAnimState.cs @@ -309,6 +309,7 @@ protected override bool HandleJumping(ref Activity idealActivity) { return false; } +#if !GMOD_DLL new bool SetupPoseParameters(StudioHdr? studioHdr) { if (PoseParameterInit) return true; @@ -473,6 +474,7 @@ protected override float GetCurrentMaxGroundSpeed() { return speed; } +#endif public HL2MP_Player GetHL2MPPlayer() => HL2MPPlayer!; } diff --git a/Game.Shared/MultiplayerAnimState.cs b/Game.Shared/MultiplayerAnimState.cs index 6a2327a22..0f30b5348 100644 --- a/Game.Shared/MultiplayerAnimState.cs +++ b/Game.Shared/MultiplayerAnimState.cs @@ -10,6 +10,7 @@ using Source.Common; using Source.Common.Commands; using Source.Common.Mathematics; +using Source.Common.Formats.Keyvalues; using System.Numerics; @@ -605,6 +606,41 @@ public virtual Activity TranslateActivity(Activity actDesired) { return actDesired; } +#if GMOD_DLL + protected virtual float GetCurrentMaxGroundSpeed() { + StudioHdr? studioHdr = GetBasePlayer().GetModelPtr(); + + if (studioHdr == null) + return 1.0f; + + int moveX = GetBasePlayer().LookupPoseParameter(studioHdr, "move_x"); + int moveY = GetBasePlayer().LookupPoseParameter(studioHdr, "move_y"); + + float prevX = GetBasePlayer().GetPoseParameter(moveX); + float prevY = GetBasePlayer().GetPoseParameter(moveY); + + float d = Math.Max(MathF.Abs(prevX), MathF.Abs(prevY)); + float newX = 1.0f, newY = 0.0f; + if (d != 0.0) { + newX = prevX / d; + newY = prevY / d; + } + + if (moveX >= 0) + GetBasePlayer().SetPoseParameter(studioHdr, moveX, newX); + if (moveY >= 0) + GetBasePlayer().SetPoseParameter(studioHdr, moveY, newY); + + float speed = (float)GetBasePlayer().GetSequenceGroundSpeed(studioHdr, GetBasePlayer().GetSequence()); + + if (moveX >= 0) + GetBasePlayer().SetPoseParameter(studioHdr, moveX, prevX); + if (moveY >= 0) + GetBasePlayer().SetPoseParameter(studioHdr, moveY, prevY); + + return speed; + } +#else protected virtual float GetCurrentMaxGroundSpeed() { StudioHdr? studioHdr = GetBasePlayer().GetModelPtr(); @@ -635,7 +671,19 @@ protected virtual float GetCurrentMaxGroundSpeed() { return speed; } - protected virtual float CalcMovementSpeed(out bool isMoving) => throw new NotImplementedException(); +#endif + protected virtual float CalcMovementSpeed(out bool isMoving) { + GetOuterAbsVelocity(out Vector3 velocity); + float speed = velocity.Length2D(); + + if (speed > MOVING_MINIMUM_SPEED) { + isMoving = true; + return speed; + } + + isMoving = false; + return 0.0f; + } protected virtual float CalcMovementPlaybackRate(out bool isMoving) => throw new NotImplementedException(); protected float GetInterpolatedGroundSpeed() => throw new NotImplementedException(); protected virtual void ComputeSequences(StudioHdr? studioHdr) { @@ -656,6 +704,15 @@ protected void ComputeMainSequence() { CurrentMainSequenceActivity = idealActivity; +#if GMOD_DLL + if (SpecificMainSequence >= 0) { + if (player.GetSequence() != SpecificMainSequence) { + player.ResetSequence(SpecificMainSequence); + ResetGroundSpeed(); + } + return; + } +#else if (SpecificMainSequence >= 0) { if (player.GetSequence() != SpecificMainSequence) { player.ResetSequence(SpecificMainSequence); @@ -670,6 +727,7 @@ protected void ComputeMainSequence() { RestartMainSequence(); ResetGroundSpeed(); } +#endif int animDesired = SelectWeightedSequence(TranslateActivity(idealActivity)); if (player.GetSequenceActivity(player.GetSequence()) == player.GetSequenceActivity(animDesired)) @@ -750,7 +808,11 @@ protected void UpdateGestureLayer(StudioHdr? studioHdr, GestureSlot gesture) { return; } else +#if GMOD_DLL + gesture.AnimLayer.Cycle = 0.0; +#else gesture.AnimLayer.Cycle = 1.0; +#endif } #else if (gesture.Activity != Activity.ACT_INVALID && gesture.AnimLayer!.Activity == Activity.ACT_INVALID) @@ -800,6 +862,22 @@ protected virtual bool ShouldUpdateAnimState() { return GetBasePlayer().IsAlive() || Dying; } +#if GMOD_DLL + protected bool SetupPoseParameters(StudioHdr? studioHdr) { + if (PoseParameterInit) + return true; + + if (studioHdr == null) + return false; + + PoseParameterInit = true; + + PoseParameterData.AimPitch = GetBasePlayer().LookupPoseParameter(studioHdr, "aim_pitch"); + PoseParameterData.AimYaw = GetBasePlayer().LookupPoseParameter(studioHdr, "aim_yaw"); + + return true; + } +#else protected bool SetupPoseParameters(StudioHdr? studioHdr) { if (PoseParameterInit) return true; @@ -820,13 +898,244 @@ protected bool SetupPoseParameters(StudioHdr? studioHdr) { return true; } - protected void DoMovementTest(StudioHdr? studioHdr, float x, float y) => throw new NotImplementedException(); - protected void DoMovementTest(StudioHdr? studioHdr) => throw new NotImplementedException(); - protected void GetMovementFlags(StudioHdr? studioHdr) => throw new NotImplementedException(); +#endif +#if GMOD_DLL + protected void DoMovementTest(StudioHdr? studioHdr, float x, float y) { + GetBasePlayer().SetPoseParameter(studioHdr, GetBasePlayer().LookupPoseParameter(GetBasePlayer().GetModelPtr(), "move_x"), x); + GetBasePlayer().SetPoseParameter(studioHdr, GetBasePlayer().LookupPoseParameter(GetBasePlayer().GetModelPtr(), "move_y"), y); + } +#else + protected void DoMovementTest(StudioHdr? studioHdr, float x, float y) { + GetBasePlayer().SetPoseParameter(studioHdr, PoseParameterData.MoveX, x); + GetBasePlayer().SetPoseParameter(studioHdr, PoseParameterData.MoveY, y); + } +#endif + + protected void DoMovementTest(StudioHdr? studioHdr) { + if (LegAnimType == LegAnimType.Anim9Way) { + DoMovementTest(studioHdr, -1.0f, -1.0f); + DoMovementTest(studioHdr, -1.0f, 0.0f); + DoMovementTest(studioHdr, -1.0f, 1.0f); + DoMovementTest(studioHdr, 0.0f, -1.0f); + DoMovementTest(studioHdr, 0.0f, 1.0f); + DoMovementTest(studioHdr, 1.0f, -1.0f); + DoMovementTest(studioHdr, 1.0f, 0.0f); + DoMovementTest(studioHdr, 1.0f, 1.0f); + } + } + + protected void GetMovementFlags(StudioHdr? studioHdr) { + if (MovementSequence == GetBasePlayer().GetSequence()) + return; + + MovementSequence = GetBasePlayer().GetSequence(); + LegAnimType = LegAnimType.Anim9Way; + + KeyValues? seqKeyValues = GetBasePlayer().GetSequenceKeyValues(MovementSequence); + if (seqKeyValues != null) { + KeyValues? kvMovement = seqKeyValues.FindKey("movement"); + if (kvMovement != null) { + ReadOnlySpan style = kvMovement.GetString(); + if (stricmp(style, "robot2") == 0) + LegAnimType = LegAnimType.Anim8Way; + } + } + + if (MovementSequence < 0 || (GetBasePlayer().GetFlags() & EntityFlags.OnGround) == 0 || studioHdr!.Seqdesc(MovementSequence).GroupSize[0] == 1) + return; + + DoMovementTest(studioHdr); + } + + static float SnapYawTo(float value) { + float sign = 1.0f; + if (value < 0.0f) { + sign = -1.0f; + value = -value; + } + + if (value < 23.0f) + value = 0.0f; + else if (value < 67.0f) + value = 45.0f; + else if (value < 113.0f) + value = 90.0f; + else if (value < 157) + value = 135.0f; + else + value = 180.0f; + + return value * sign; + } + +#if GMOD_DLL + protected virtual void ComputePoseParam_MoveYaw(StudioHdr? studioHdr) { + EstimateYaw(); + + float angle = MathLib.AngleNormalize(EyeYaw); + + float yaw = angle - PoseParameterData.EstimateYaw; + yaw = MathLib.AngleNormalize(-yaw); + + float speed = CalcMovementSpeed(out bool isMoving); + + BasePlayer player = GetBasePlayer(); + Vector2 currentMoveYaw = default; + if (!isMoving) { + int moveX = player.LookupPoseParameter(player.GetModelPtr(), "move_x"); + int moveY = player.LookupPoseParameter(player.GetModelPtr(), "move_y"); + if (moveX >= 0) + player.SetPoseParameter(player.GetModelPtr(), moveX, 0); + if (moveY >= 0) + player.SetPoseParameter(player.GetModelPtr(), moveY, 0); + } + else { + GetMovementFlags(studioHdr); + + if (mp_slammoveyaw.GetBool()) + yaw = SnapYawTo(yaw); + + if (LegAnimType == LegAnimType.Anim9Way) { + currentMoveYaw.X = MathF.Cos(MathLib.DEG2RAD(yaw)); + currentMoveYaw.Y = -MathF.Sin(MathLib.DEG2RAD(yaw)); + float invScale = Math.Max(MathF.Abs(currentMoveYaw.X), MathF.Abs(currentMoveYaw.Y)); + if (invScale != 0.0f) { + currentMoveYaw.X /= invScale; + currentMoveYaw.Y /= invScale; + } + + int moveX = player.LookupPoseParameter(player.GetModelPtr(), "move_x"); + int moveY = player.LookupPoseParameter(player.GetModelPtr(), "move_y"); + if (moveX >= 0) + player.SetPoseParameter(moveX, currentMoveYaw.X); + if (moveY >= 0) + player.SetPoseParameter(moveY, currentMoveYaw.Y); + + float maxSpeed = (float)player.GetSequenceGroundSpeed(player.GetModelPtr(), player.GetSequence()); + if (speed < maxSpeed) { + currentMoveYaw.X *= speed / maxSpeed; + currentMoveYaw.Y *= speed / maxSpeed; + } + + if (moveX >= 0) + player.SetPoseParameter(moveX, currentMoveYaw.X); + if (moveY >= 0) + player.SetPoseParameter(moveY, currentMoveYaw.Y); + } + else { + int moveYaw = player.LookupPoseParameter(player.GetModelPtr(), "move_yaw"); + int moveScale = player.LookupPoseParameter(player.GetModelPtr(), "move_scale"); + if (moveYaw >= 0) + player.SetPoseParameter(moveYaw, yaw); + if (moveScale >= 0) + player.SetPoseParameter(moveScale, 1.0f); + + float maxSpeed = (float)player.GetSequenceGroundSpeed(player.GetModelPtr(), player.GetSequence()); + if (speed < maxSpeed && moveScale >= 0) + player.SetPoseParameter(moveScale, speed / maxSpeed); + } + } + + DebugAnimData.MoveYaw = currentMoveYaw; + } + + protected virtual void EstimateYaw() { + TimeUnit_t deltaTime = gpGlobals.FrameTime; + if (deltaTime == 0.0f) + return; + + GetOuterAbsVelocity(out Vector3 estVelocity); + QAngle angles = GetBasePlayer().GetLocalAngles(); + + if (estVelocity.X == 0.0f && estVelocity.Y == 0.0f) { + float yawDelta = angles[YAW] - PoseParameterData.EstimateYaw; + yawDelta = MathLib.AngleNormalize(yawDelta); + + if (deltaTime < 0.25f) + yawDelta *= (float)(deltaTime * 4.0f); + else + yawDelta *= (float)deltaTime; + + PoseParameterData.EstimateYaw += yawDelta; + } + else { + PoseParameterData.EstimateYaw = (float)(Math.Atan2(estVelocity.Y, estVelocity.X) * 180.0f / Math.PI); + PoseParameterData.EstimateYaw = Math.Clamp(PoseParameterData.EstimateYaw, -180.0f, 180.0f); + } + } + + protected virtual void ComputePoseParam_AimPitch(StudioHdr? studioHdr) { + float aimPitch = EyePitch; + + BasePlayer player = GetBasePlayer(); + player.SetPoseParameter(studioHdr, PoseParameterData.AimPitch, aimPitch); + int aimPitchParam = player.LookupPoseParameter(player.GetModelPtr(), "aim_pitch"); + if (aimPitchParam >= 0) + player.SetPoseParameter(player.GetModelPtr(), aimPitchParam, aimPitch); + + DebugAnimData.AimPitch = aimPitch; + } + + protected virtual void ComputePoseParam_AimYaw(StudioHdr? studioHdr) { + GetOuterAbsVelocity(out Vector3 velocity); + + bool moving = velocity.Length() > 1.0f; + + if (moving || ForceAimYaw) + GoalFeetYaw = EyeYaw; + else { + if (PoseParameterData.LastAimTurnTime <= 0.0f) { + GoalFeetYaw = EyeYaw; + CurrentFeetYaw = EyeYaw; + PoseParameterData.LastAimTurnTime = (float)gpGlobals.CurTime; + } + else { + float yawDelta = MathLib.AngleNormalize(GoalFeetYaw - EyeYaw); + + if (MathF.Abs(yawDelta) > 45.0f) { + float side = (yawDelta > 0.0f) ? -1.0f : 1.0f; + GoalFeetYaw += 45.0f * side; + } + } + } + + GoalFeetYaw = MathLib.AngleNormalize(GoalFeetYaw); + if (GoalFeetYaw != CurrentFeetYaw) { + if (ForceAimYaw) + CurrentFeetYaw = GoalFeetYaw; + else { + ConvergeYawAngles(GoalFeetYaw, 720.0f, gpGlobals.FrameTime, ref CurrentFeetYaw); + LastAimTurnTime = gpGlobals.CurTime; + } + } + + AngRender[YAW] = CurrentFeetYaw; + + float aimYaw = EyeYaw - CurrentFeetYaw; + aimYaw = MathLib.AngleNormalize(aimYaw); + + BasePlayer player = GetBasePlayer(); + player.SetPoseParameter(studioHdr, PoseParameterData.AimYaw, aimYaw); + int aimYawParam = player.LookupPoseParameter(player.GetModelPtr(), "aim_yaw"); + if (aimYawParam >= 0) + player.SetPoseParameter(player.GetModelPtr(), aimYawParam, aimYaw); + + ForceAimYaw = false; + DebugAnimData.AimYaw = aimYaw; + +#if !CLIENT_DLL + QAngle angle = GetBasePlayer().GetAbsAngles(); + angle[YAW] = CurrentFeetYaw; + + GetBasePlayer().SetAbsAngles(angle); +#endif + } +#else protected virtual void ComputePoseParam_MoveYaw(StudioHdr? studioHdr) => throw new NotImplementedException(); protected virtual void EstimateYaw() => throw new NotImplementedException(); protected virtual void ComputePoseParam_AimPitch(StudioHdr? studioHdr) => throw new NotImplementedException(); protected virtual void ComputePoseParam_AimYaw(StudioHdr? studioHdr) => throw new NotImplementedException(); +#endif protected void ConvergeYawAngles(float goalYaw, float yawRate, TimeUnit_t deltaTime, ref float currentYaw) { const float FADE_TURN_DEGREES = 60.0f; From 1fab4f6fb6100c11b18a4cc3ede2247c286f5ae7 Mon Sep 17 00:00:00 2001 From: callumok2004 Date: Sun, 11 Oct 2026 07:03:44 +0100 Subject: [PATCH 4/4] Player spawn hooks --- Game.Server/GarrysMod/GMOD_Player.cs | 21 ++++++++++++++++ Game.Server/HL2MP/HL2MP_Player.cs | 7 +++++- Game.Server/Player.cs | 4 ++++ Game.Server/Util.cs | 8 +++++++ Game.Shared/GarrysMod/LuaPlayer.cs | 36 ++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 1 deletion(-) diff --git a/Game.Server/GarrysMod/GMOD_Player.cs b/Game.Server/GarrysMod/GMOD_Player.cs index dece38a48..3b7298c4f 100644 --- a/Game.Server/GarrysMod/GMOD_Player.cs +++ b/Game.Server/GarrysMod/GMOD_Player.cs @@ -6,6 +6,7 @@ using Source; using Source.Common; using Source.Common.Engine; +using Source.Common.GarrysMod.Lua; using System.Numerics; @@ -55,11 +56,31 @@ public partial class GMOD_Player : HL2MP_Player ]); public static readonly new ServerClass ServerClass = new ServerClass(DT_GMOD_Player); + public bool Transition; + public override void InitialSpawn() { + SetModel("models/player.mdl"); base.InitialSpawn(); + + if (gGM != null && gGM.CallWithArgs((int)LUA_POOLEDSTRING.PlayerInitialSpawn)) { + LuaEntity.Push_Entity(this); + g_Lua!.PushBool(Transition); + gGM.CallNoReturns(2); + } + LuaNetworkedVars.g_LuaNetworkedVars!.PlayerInsert(this); } + public override void Spawn() { + base.Spawn(); + + if (gGM != null && gGM.CallWithArgs((int)LUA_POOLEDSTRING.PlayerSpawn)) { + LuaEntity.Push_Entity(this); + g_Lua!.PushBool(Transition); + gGM.CallNoReturns(2); + } + } + public override BaseEntity EntSelectSpawnPoint() { BaseEntity? spot = null; while ((spot = gEntList.FindEntityByClassname(spot, "info_player_start")) != null) { diff --git a/Game.Server/HL2MP/HL2MP_Player.cs b/Game.Server/HL2MP/HL2MP_Player.cs index cb0d92d81..9505fca14 100644 --- a/Game.Server/HL2MP/HL2MP_Player.cs +++ b/Game.Server/HL2MP/HL2MP_Player.cs @@ -526,6 +526,7 @@ public override bool BumpWeapon(BaseCombatWeapon weapon) { return true; } +#if !GMOD_DLL public override void ChangeTeam(int team, bool autoTeam = false, bool silent = false, bool autoBalance = false) { bool kill = false; @@ -554,6 +555,7 @@ public override void ChangeTeam(int team, bool autoTeam = false, bool silent = f if (kill == true) CommitSuicide(); } +#endif bool HandleCommand_JoinTeam(int team) { throw new NotImplementedException(); @@ -725,7 +727,10 @@ bool StartObserverMode(int mode) { throw new NotImplementedException(); } - void StopObserverMode() { } + public override void StopObserverMode() { + // todo + base.StopObserverMode(); + } void State_Enter_OBSERVER_MODE() { } diff --git a/Game.Server/Player.cs b/Game.Server/Player.cs index 7fbaf525d..2f9146e10 100644 --- a/Game.Server/Player.cs +++ b/Game.Server/Player.cs @@ -581,6 +581,10 @@ public void EnableControl(bool control) { [NetworkName("m_bUseWeaponsInVehicle")] bool UseWeaponsInVehicle; public bool GetAllowWeaponsInVehicle() => UseWeaponsInVehicle; + + public virtual void StopObserverMode() { + // todo + } public bool OnTarget; [NetworkName("m_flDeathTime")] public TimeUnit_t DeathTime; diff --git a/Game.Server/Util.cs b/Game.Server/Util.cs index e506c3919..65f9f816c 100644 --- a/Game.Server/Util.cs +++ b/Game.Server/Util.cs @@ -695,8 +695,16 @@ internal static void SetOrigin(BaseEntity entity, in Vector3 origin, bool fireTr internal static void SetModel(BaseEntity baseEntity, ReadOnlySpan modelName) { int i = modelinfo.GetModelIndex(modelName); +#if GMOD_DLL + if (i == -1) { + i = BaseEntity.PrecacheModel(modelName, true); + if (i < 0) + return; + } +#else if (i == -1) Error($"{baseEntity.EntIndex()}/{baseEntity/*.GetEntityName()*/} - {baseEntity.GetClassname()}: Util.SetModel: not precached: {modelName}\n"); +#endif BaseAnimating? animating = baseEntity.GetBaseAnimating(); animating?.ForceBone = 0; diff --git a/Game.Shared/GarrysMod/LuaPlayer.cs b/Game.Shared/GarrysMod/LuaPlayer.cs index 08391c411..284fe6236 100644 --- a/Game.Shared/GarrysMod/LuaPlayer.cs +++ b/Game.Shared/GarrysMod/LuaPlayer.cs @@ -1,3 +1,4 @@ +using Source; #if CLIENT_DLL || GAME_DLL using Game.Shared; @@ -124,6 +125,41 @@ static int Player__TranslateWeaponActivity(ILuaInterface lua) { return 1; } +#if GAME_DLL + [LuaMethod] + static int Player__UnSpectate(ILuaInterface lua) { + BasePlayer player = Get_Player(1, false)!; + player.StopObserverMode(); + player.ResetObserverMode(); + player.RemoveEFlags(EFL.NoClipActive); + player.SetMoveType(MoveType.Walk); + return 0; + } +#endif + + [LuaMethod] + static int Player__SetClassID(ILuaInterface lua) { + BasePlayer player = Get_Player(1, false)!; + int classID = (int)lua.CheckNumber(2); + if (gpGlobals.MaxClients > 1 && (uint)(classID + 16384) > 32767) + lua.ErrorNoHalt($"Out of bounds value {classID} for networking in range {-16384} to {16383}\n"); + + GMOD_Player gmodPlayer = (GMOD_Player)player; +#if GAME_DLL + if (gmodPlayer.PlayerClass != classID) + gmodPlayer.NetworkStateChanged(); +#endif + gmodPlayer.PlayerClass = classID; + return 1; + } + + [LuaMethod] + static int Player__GetClassID(ILuaInterface lua) { + BasePlayer player = Get_Player(1, false)!; + lua.PushNumber(((GMOD_Player)player).PlayerClass); + return 1; + } + [LuaMethod] static int Player__GetAllowWeaponsInVehicle(ILuaInterface lua) { BasePlayer player = Get_Player(1, false)!;