diff --git a/Game.Server/PhysObj.cs b/Game.Server/PhysObj.cs index cdb525228..429106a02 100644 --- a/Game.Server/PhysObj.cs +++ b/Game.Server/PhysObj.cs @@ -1,6 +1,15 @@ using Game.Shared; +using Source; using Source.Common; +using Source.Common.Commands; +using Source.Common.Formats.BSP; +using Source.Common.Mathematics; +using Source.Common.Physics; + +using System.Numerics; + +using DEFINE = Source.DEFINE; namespace Game.Server; @@ -11,3 +20,96 @@ public class PhysBox : Breakable public static readonly SendTable DT_PhysBox = new(DT_BaseEntity, []); public static readonly new ServerClass ServerClass = new ServerClass(DT_PhysBox); } + +[LinkEntityToClass("env_physimpact")] +public class PhysImpact : PointEntity +{ + public const int bitsPHYSIMPACT_NOFALLOFF = 0x00000001; + public const int bitsPHYSIMPACT_INFINITE_LENGTH = 0x00000002; + public const int bitsPHYSIMPACT_IGNORE_MASS = 0x00000004; + public const int bitsPHYSIMPACT_IGNORE_NORMAL = 0x00000008; + + public const float DEFAULT_EXPLODE_DISTANCE = 256; + + public static readonly ConVar debug_physimpact = new("debug_physimpact", "0"); + + public static readonly new DataMap DataDesc = new(typeof(PhysImpact), PointEntity.DataDesc, [ + DEFINE.KEYFIELD(nameof(Damage), FieldType.Float, "magnitude"), + DEFINE.KEYFIELD(nameof(Distance), FieldType.Float, "distance"), + DEFINE.KEYFIELD(nameof(DirectionEntityName), FieldType.String, "directionentityname"), + + DEFINE.INPUTFUNC(FieldType.Void, "Impact", nameof(InputImpact), (INPUTFUNCPTR)((self, data) => ((PhysImpact)self).InputImpact(data))), + ]); + public override DataMap? GetDataDescMap() => DataDesc; + + float Damage; + float Distance; + string? DirectionEntityName; + + public override void Activate() { + base.Activate(); + } + + public override void Spawn() { + SetMoveType(Source.MoveType.None); + SetSolid(SolidType.None); + SetModelName(null); + + if (Distance == 0) + Distance = DEFAULT_EXPLODE_DISTANCE; + } + + void PointAtEntity() { + if (string.IsNullOrEmpty(DirectionEntityName)) + return; + + Util.PointAtNamedEntity(this, DirectionEntityName); + } + + public void InputImpact(InputData inputdata) { + if (!string.IsNullOrEmpty(DirectionEntityName)) + PointAtEntity(); + + MathLib.AngleVectors(GetAbsAngles(), out Vector3 dir); + + float dist = HasSpawnFlags(bitsPHYSIMPACT_INFINITE_LENGTH) ? MAX_TRACE_LENGTH : Distance; + Vector3 start = GetAbsOrigin(); + Vector3 end = start + (dir * dist); + + Util.TraceLine(start, end, Mask.Shot, this, Source.CollisionGroup.None, out Trace trace); + if (trace.StartSolid) { + Vector3 startOffset = start + (dir * 0.1f); + Util.TraceLine(startOffset, end, Mask.Shot, this, Source.CollisionGroup.None, out trace); + } + + if (trace.Fraction != 1.0) { + if (trace.StartSolid) + trace.Plane.Normal = -dir; + BaseEntity? ent = (BaseEntity?)trace.Ent; + + IPhysicsObject? physics = ent?.VPhysicsGetObject(); + if (ent != null && physics != null) { + TakeDamageInfo info = new(); + info.SetAttacker(this); + info.SetInflictor(this); + info.SetDamage(0); + info.SetDamageForce(vec3_origin); + info.SetDamageType(DamageType.Generic); + + ent.DispatchTraceAttack(info, dir, ref trace); + ApplyMultiDamage(); + + float damage = HasSpawnFlags(bitsPHYSIMPACT_NOFALLOFF | bitsPHYSIMPACT_INFINITE_LENGTH) ? + Damage : (Damage * (1.0f - trace.Fraction)); + + if (HasSpawnFlags(bitsPHYSIMPACT_IGNORE_MASS)) + damage *= physics.GetMass(); + + if (!HasSpawnFlags(bitsPHYSIMPACT_IGNORE_NORMAL)) + dir = -trace.Plane.Normal; + + physics.ApplyForceOffset(damage * dir * phys_pushscale.GetFloat(), trace.EndPos); + } + } + } +} diff --git a/Game.Server/Util.cs b/Game.Server/Util.cs index e506c3919..0d98dd632 100644 --- a/Game.Server/Util.cs +++ b/Game.Server/Util.cs @@ -11,6 +11,7 @@ using Source.Common.Engine; using Source.Common.Formats.BSP; using Source.Common.Mathematics; +using Source.Common.Physics; using Source.Engine.Server; using System; @@ -227,6 +228,34 @@ public static void ClearTrace(ref Trace trace) { trace.FractionLeftSolid = 0; } + public static bool PointAtEntity(BaseEntity? dest, BaseEntity? target) { + if (dest == null || target == null) + return false; + + Vector3 dir = target.GetAbsOrigin() - dest.GetAbsOrigin(); + + MathLib.VectorNormalize(ref dir); + + MathLib.VectorAngles(dir, out QAngle angles); + dest.SetLocalAngles(angles); + dest.SetAbsAngles(angles); + +#if GMOD_DLL + IPhysicsObject? physicsObject = dest.VPhysicsGetObject(); + if (physicsObject != null) { + physicsObject.GetPosition(out Vector3 worldPos, out _); + physicsObject.SetPosition(worldPos, angles, true); + } +#endif + + return true; + } + + public static void PointAtNamedEntity(BaseEntity dest, string? target) { + if (!PointAtEntity(dest, gEntList.FindEntityByName(null, target))) + DevMsg(1, $"{dest.GetClassname()} ({dest.GetDebugName()}) was unable to point at an entity named: {target}\n"); + } + public static int DropToFloor(BaseEntity entity, Mask mask) { entity.SetGroundEntity(null); diff --git a/Game.Shared/TakeDamageInfo.cs b/Game.Shared/TakeDamageInfo.cs index 1979bc163..1fd8c048c 100644 --- a/Game.Shared/TakeDamageInfo.cs +++ b/Game.Shared/TakeDamageInfo.cs @@ -104,7 +104,7 @@ public static void AddMultiDamage(in TakeDamageInfo info, BaseEntity? entity) { } } - static readonly ConVar phys_pushscale = new( "phys_pushscale", "1", FCvar.Replicated); + public static readonly ConVar phys_pushscale = new( "phys_pushscale", "1", FCvar.Replicated);