diff --git a/Game.Client/C_BaseAnimating.cs b/Game.Client/C_BaseAnimating.cs index 4dd83a709..8e0606fae 100644 --- a/Game.Client/C_BaseAnimating.cs +++ b/Game.Client/C_BaseAnimating.cs @@ -457,7 +457,45 @@ public void RemoveBaseAnimatingInterpolatedVars() { RemoveVar(this, DA_Cycle, false); } - public bool ComputeHitboxSurroundingBox(out Vector3 vecWorldMins, out Vector3 vecWorldMaxs) => throw new NotImplementedException(); + memhandle_t HitboxBoneCacheHandle; + + public BoneCache GetBoneCache(StudioHdr? studioHdr) { + int boneMask = Studio.BONE_USED_BY_HITBOX; + BoneCache cache = Studio.GetBoneCache(HitboxBoneCacheHandle); + if (!cache.IsNull()) { + if (cache.IsValid(gpGlobals.CurTime, 0.0)) + return cache; + + if ((cache.BoneMask & boneMask) != boneMask) { + Studio.DestroyBoneCache(HitboxBoneCacheHandle); + HitboxBoneCacheHandle = 0; + cache = default; + } + } + + studioHdr ??= GetModelPtr(); + Assert(studioHdr != null); + + PushAllowBoneAccess(true, false, (BoneAccessTag)"GetBoneCache"); + SetupBones(null, -1, boneMask, gpGlobals.CurTime); + PopAllowBoneAccess((BoneAccessTag)"GetBoneCache"); + + if (!cache.IsNull()) + cache.UpdateBones(CachedBoneData.AsSpan(), studioHdr!.NumBones(), gpGlobals.CurTime); + else { + BoneCacheParams parms = new(); + parms.StudioHdr = studioHdr!; + parms.BoneToWorld = CachedBoneData.AsSpan(); + parms.CurTime = gpGlobals.CurTime; + parms.BoneMask = boneMask; + + HitboxBoneCacheHandle = Studio.CreateBoneCache(in parms); + cache = Studio.GetBoneCache(HitboxBoneCacheHandle); + } + + Assert(!cache.IsNull()); + return cache; + } public override bool GetSoundSpatialization(ref SpatializationInfo info) { using (AutoAllowBoneAccess boneaccess = new(true, false)) { @@ -951,6 +989,9 @@ private static void RecvProxy_Sequence(ref readonly RecvProxyData data, object i InvalidateBoneCache(); BoneMergeCache = null; + Studio.DestroyBoneCache(HitboxBoneCacheHandle); + HitboxBoneCacheHandle = 0; + if (CachedBoneData.Count != hdr.NumBones()) { CachedBoneData.SetSize(hdr.NumBones()); for (i = 0; i < hdr.NumBones(); i++) diff --git a/Game.Server/BaseAnimating.cs b/Game.Server/BaseAnimating.cs index f7328b2c2..af46cc54c 100644 --- a/Game.Server/BaseAnimating.cs +++ b/Game.Server/BaseAnimating.cs @@ -189,8 +189,6 @@ public void ResetSequence(int sequence) { ResetSequenceInfo(); } - public bool ComputeHitboxSurroundingBox(out Vector3 vecWorldMins, out Vector3 vecWorldMaxs) => throw new NotImplementedException(); - public const int NUM_POSEPAREMETERS = 24; public const int NUM_BONECTRLS = 4; diff --git a/Game.Server/Player.cs b/Game.Server/Player.cs index 6b613eae4..2511ab3ed 100644 --- a/Game.Server/Player.cs +++ b/Game.Server/Player.cs @@ -9,6 +9,7 @@ using Source.Common.Engine; using Source.Common.Formats.BSP; using Source.Common.Game.Server; +using Source.Common.Hashing; using Source.Common.Mathematics; using Source.Common.Physics; using Source.Engine; @@ -1468,11 +1469,48 @@ public override void PhysicsSimulate() { pi.NumCmds = commandsToRun; } } - else if (GetTimeSinceLastUserCommand() > 3.0f /*sv_player_usercommand_timeout.GetFloat()*/) - RunNullCommand(); gpGlobals.CurTime = savetime; gpGlobals.FrameTime = saveframetime; + + if (GetTimeSinceLastUserCommand() >= UserCommandTimeout) + RunLastCommand(); + } + + const float UserCommandTimeout = 0.5f; + + private void RunLastCommand() { + UserCmd cmd = LastCmd; + cmd.CommandNumber++; + cmd.TickCount++; + cmd.Forced = true; + cmd.RandomSeed = (int)(MD5Value.PseudoRandom((uint)cmd.CommandNumber) & 0x7fffffff); + cmd.MouseDeltaX = 0; + cmd.MouseDeltaY = 0; + cmd.Impulse = 0; + cmd.WeaponSelect = 0; + cmd.ScrollWheelSpeed = 0; + + TimeUnit_t oldFrameTime = gpGlobals.FrameTime; + TimeUnit_t oldCurTime = gpGlobals.CurTime; + + MoveHelperServer.s_MoveHelperServer.SetHost(this); + PlayerRunCommand(cmd, MoveHelperServer.s_MoveHelperServer); + + if (VecPlayerSimInfo.Count > 0) { + PlayerSimInfo pi = VecPlayerSimInfo[VecPlayerSimInfo.Count - 1]; + pi.Time = Platform.Time; + pi.AbsOrigin = GetAbsOrigin(); + pi.GameSimulationTime = gpGlobals.CurTime; + pi.NumCmds = 1; + } + + MoveHelperServer.s_MoveHelperServer.SetHost(null); + + LastCmd = cmd; + + gpGlobals.FrameTime = oldFrameTime; + gpGlobals.CurTime = oldCurTime; } private void RunNullCommand() { @@ -1553,7 +1591,7 @@ public virtual void PlayerRunCommand(UserCmd userCmd, IMoveHelper s_MoveHelperSe public bool IsPredictingWeapons() => false; // todo - TimeUnit_t GetTimeSinceLastUserCommand() => /*(!IsConnected() || IsFakeClient() || IsBot()) ? 0.0f :*/ gpGlobals.CurTime - LastUserCommandTime; + TimeUnit_t GetTimeSinceLastUserCommand() => (!IsConnected() || IsFakeClient() || IsBot()) ? 0.0f : gpGlobals.CurTime - LastUserCommandTime; public virtual void SetupVisibility(BaseEntity? viewEntity, byte[] pvs, int pvssize) { if (viewEntity != null) diff --git a/Game.Shared/BaseAnimatingShared.cs b/Game.Shared/BaseAnimatingShared.cs index c32579b7d..9abb166ff 100644 --- a/Game.Shared/BaseAnimatingShared.cs +++ b/Game.Shared/BaseAnimatingShared.cs @@ -9,6 +9,7 @@ using Source.Common.Mathematics; using System.Numerics; +using System.Runtime.CompilerServices; #if CLIENT_DLL namespace Game.Client @@ -339,6 +340,60 @@ public void CalculateIKLocks(TimeUnit_t currentTime) { partition.SuppressLists(curSuppressed, true); } #endif + + public bool ComputeHitboxSurroundingBox(out Vector3 vecWorldMins, out Vector3 vecWorldMaxs) { + vecWorldMins = default; + vecWorldMaxs = default; + +#if CLIENT_DLL + if (IsDynamicModelLoading()) + return false; +#endif + + StudioHdr? studioHdr = GetModelPtr(); + if (studioHdr == null) + return false; + + if (HitboxSet < 0 || HitboxSet >= studioHdr.NumHitboxSets()) { + Warning($"[UH-OH!] Invalid HitboxSet {HitboxSet} of {studioHdr.NumHitboxSets()} on model {studioHdr.Name()}.\n"); + return false; + } + + MStudioHitboxSet set = studioHdr.HitboxSet(HitboxSet); + if (set.NumHitboxes == 0) + return false; + +#if CLIENT_DLL + BoneCache cache = GetBoneCache(studioHdr); +#else + BoneCache cache = GetBoneCache(); +#endif + if (cache.IsNull()) + return false; + + vecWorldMins = new(float.MaxValue); + vecWorldMaxs = new(-float.MaxValue); + +#if !CLIENT_DLL + float modelScale = GetModelScale(); +#endif + for (int i = 0; i < set.NumHitboxes; i++) { + MStudioBBox box = set.Hitbox(i); + ref Matrix3x4 matrix = ref cache.GetCachedBone(box.Bone); + if (Unsafe.IsNullRef(ref matrix)) + continue; + +#if CLIENT_DLL + MathLib.TransformAABB(in matrix, box.BBMin, box.BBMax, out Vector3 boxAbsMins, out Vector3 boxAbsMaxs); +#else + MathLib.TransformAABB(in matrix, box.BBMin * modelScale, box.BBMax * modelScale, out Vector3 boxAbsMins, out Vector3 boxAbsMaxs); +#endif + vecWorldMins = Vector3.Min(vecWorldMins, boxAbsMins); + vecWorldMaxs = Vector3.Max(vecWorldMaxs, boxAbsMaxs); + } + + return true; + } } } #endif diff --git a/Source.Common/Hashing/MD5Value.cs b/Source.Common/Hashing/MD5Value.cs index b3ad06e33..2e49c6bb0 100644 --- a/Source.Common/Hashing/MD5Value.cs +++ b/Source.Common/Hashing/MD5Value.cs @@ -1,5 +1,6 @@ using CommunityToolkit.HighPerformance; +using System.Buffers.Binary; using System.Runtime.InteropServices; using FIELD_W_TYPE = System.UInt32; @@ -40,6 +41,14 @@ public static STRUCT_TYPE Compute(ReadOnlySpan data) { return FromBytes(hash); } + public static uint PseudoRandom(uint seed) { + Span data = stackalloc byte[sizeof(uint)]; + BinaryPrimitives.WriteUInt32LittleEndian(data, seed); + Span hash = stackalloc byte[SIZE_BYTES]; + HASH_ALGORITHM.HashData(data, hash); + return BinaryPrimitives.ReadUInt32LittleEndian(hash[6..]); + } + public readonly override string ToString() { Span chars = stackalloc char[SIZE_HEX_CHARACTERS]; ToString(chars); diff --git a/Source.Engine/Client/ClientState.cs b/Source.Engine/Client/ClientState.cs index 482ca8d79..e0f5e4e62 100644 --- a/Source.Engine/Client/ClientState.cs +++ b/Source.Engine/Client/ClientState.cs @@ -1097,6 +1097,28 @@ public void SetModel(int tableIndex) { p.SetModel(null); } + protected override bool ProcessFixAngle(SVC_FixAngle msg) { + if (msg.Angle.X > 180) + msg.Angle.X -= 360; + if (msg.Angle.Y > 180) + msg.Angle.Y -= 360; + if (msg.Angle.Z > 180) + msg.Angle.Z -= 360; + + if (msg.Relative) { + AddAngleTotal += msg.Angle.Y; + + AddAngle.Add(new() { + Total = AddAngleTotal, + StartTime = LastServerTickTime + }); + } + else + ViewAngles = msg.Angle; + + return true; + } + protected override bool ProcessCrosshairAngle(SVC_CrosshairAngle msg) { g_ClientDLL!.SetCrosshairAngle(msg.Angle); return true;