Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Game.Client/C_BasePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ public override void Dispose() {
public float GetStepSize() => Local.StepSize;

float OldPlayerZ;
float OldPlayerViewOffsetZ;
bool WasFreezeFraming;
Vector3 FreezeFrameStart;
TimeUnit_t FreezeFrameStartTime;
Expand Down
2 changes: 2 additions & 0 deletions Game.Server/AI_BaseNPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9085,6 +9085,8 @@ public void UpdateEfficiency(bool inPVS) {
SetEfficiency((AI_Efficiency)Math.Clamp((int)efficiency, (int)minEfficiency, (int)maxEfficiency));
}

public virtual void PlayerPenetratingVPhysics() => PerformAvoidance = true;

public void GetPlayerAvoidBounds(out Vector3 mins, out Vector3 maxs) {
mins = WorldAlignMins();
maxs = WorldAlignMaxs();
Expand Down
58 changes: 58 additions & 0 deletions Game.Server/BaseCombatCharacter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Source.Common.Engine;
using Source.Common.Formats.BSP;
using Source.Common.Mathematics;
using Source.Common.Physics;

using System.Numerics;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -354,6 +355,63 @@ public virtual int IRelationPriority(BaseEntity? target) {

public void SetImpactEnergyScale(float scale) => ImpactEnergyScale = scale;

static readonly ConVar phys_stressbodyweights = new("phys_stressbodyweights", "5.0");

static readonly float MaxStressDamageForce = BitConverter.Int32BitsToSingle(0x5f0ede49);

public override void VPhysicsUpdate(IPhysicsObject physics) {
ApplyStressDamage(physics, false);
base.VPhysicsUpdate(physics);
}

public virtual float CalculatePhysicsStressDamage(out VPhysicsObjectStress stressOut, IPhysicsObject physics) {
float mass = physics.GetMass();
CalculateObjectStress(physics, this, out stressOut);
float stress = (stressOut.ReceivedStress * ImpactEnergyScale) / mass;

if (stressOut.HasNonStaticStress && stress > phys_stressbodyweights.GetFloat()) {
if ((physics.GetGameFlags() & PhysicsFlags.Penetrating) == 0)
return 200;
}

return 0;
}

public virtual void ApplyStressDamage(IPhysicsObject physics, bool requireLargeObject) {
if (Classify() == Class_T.PlayerAlly || Classify() == Class_T.PlayerAllyVital) {
if (hl2_episodic.GetBool())
return;
}

float damage = CalculatePhysicsStressDamage(out VPhysicsObjectStress stressOut, physics);
if (damage > 0) {
if (requireLargeObject && !stressOut.HasLargeObjectContact)
return;

TakeDamageInfo dmgInfo = new(GetWorldEntity(), GetWorldEntity(), vec3_origin, vec3_origin, damage, DamageType.Crush);
float force = -stressOut.ReceivedStress * GetCurrentGravity() * (float)gpGlobals.FrameTime;
if (force > -MaxStressDamageForce && force < MaxStressDamageForce)
dmgInfo.SetDamageForce(new Vector3(0, 0, force));
dmgInfo.SetDamagePosition(GetAbsOrigin());

if (stressOut.StressObject?.GetGameData() is BaseEntity stressEntity) {
dmgInfo.SetInflictor(stressEntity);
dmgInfo.SetAttacker(stressEntity);

BaseEntity? owner = stressEntity.GetOwnerEntity();
if (owner != null)
dmgInfo.SetAttacker(owner);
else {
BasePlayer? physicsAttacker = stressEntity.HasPhysicsAttacker(1.0);
if (physicsAttacker != null)
dmgInfo.SetAttacker(physicsAttacker);
}
}

TakeDamage(dmgInfo);
}
}

public AI_HullType GetHullType() => Hull;
public void SetHullType(AI_HullType hullType) => Hull = hullType;

Expand Down
16 changes: 15 additions & 1 deletion Game.Server/BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ public static void EmitSound<IRF>(scoped in IRF filter, int entIndex, scoped in
public int GetTeamNumber() => TeamNum;

IPhysicsObject? PhysicsObject = null!;
public void VPhysicsUpdate(IPhysicsObject physics) {
public virtual void VPhysicsUpdate(IPhysicsObject physics) {
switch (GetMoveType()) {
case Source.MoveType.VPhysics: {
if (GetMoveParent() != null) {
Expand Down Expand Up @@ -1431,6 +1431,10 @@ public void PhysicsRelinkChildren(TimeUnit_t dt) {

public virtual void VPhysicsShadowCollision(int index, ref GameVCollisionEvent ev) { }

public virtual void VPhysicsShadowUpdate(IPhysicsObject physics) { }

public virtual BasePlayer? HasPhysicsAttacker(TimeUnit_t dt) => null;

public virtual void VPhysicsCollision(int index, ref GameVCollisionEvent ev) {
int otherIndex = index == 0 ? 1 : 0;
BaseEntity hitEntity = ev.Entities[otherIndex]!;
Expand Down Expand Up @@ -1474,6 +1478,16 @@ public void VPhysicsSetObject(IPhysicsObject? physics) {
PhysicsObject = physics;
}

public void VPhysicsSwapObject(IPhysicsObject? swap) {
if (swap == null)
PhysRemoveShadow(this);

if (PhysicsObject == null)
Warning($"Bad vphysics swap for {GetClassname()}\n");

PhysicsObject = swap;
}

bool VPhysicsInitSetup() {
// don't support logical ents
if (Edict() == null || IsMarkedForDeletion())
Expand Down
2 changes: 1 addition & 1 deletion Game.Server/EntityList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal class EntItem
public class EntityList
{
int NumItems;
EntItem? ItemList;
internal EntItem? ItemList;

public void AddEntity(BaseEntity ent) {
// check if it's already in the list; if not, add it
Expand Down
20 changes: 20 additions & 0 deletions Game.Server/Physics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public static class PhysicsHookGlobals {

public const float VPHYSICS_LARGE_OBJECT_MASS = 500.0f;

public static readonly ConVar phys_timescale = new("phys_timescale", "1", FCvar.Replicated, "Scale time for physics", callback: TimescaleChanged);
public static float g_GMOD_PhysTimeScale = 1.0f;

static void TimescaleChanged(IConVar var, in ConVarChangeContext ctx) => physenv?.ResetSimulationClock();

public static float GMOD_PhysicsGetTotalTimeScale() => phys_timescale.GetFloat() * g_GMOD_PhysTimeScale;

static PhysicsHookGlobals(){
SetPhysicsGameSystem(g_PhysicsHook);
}
Expand Down Expand Up @@ -251,6 +258,7 @@ public static bool PhysIsInCallback(){

public static void PhysAddShadow(BaseEntity entity) => g_ShadowEntities!.AddEntity(entity);
public static void PhysRemoveShadow(BaseEntity entity) => g_ShadowEntities!.DeleteEntity(entity);
public static void PhysGetListOfPenetratingEntities(BaseEntity search, List<BaseEntity> list) => g_Collisions.GetListOfPenetratingEntities(search, list);

public static void PhysOnCleanupDeleteList() {
g_Collisions.FlushQueuedOperations();
Expand Down Expand Up @@ -440,6 +448,18 @@ void PhysFrame(TimeUnit_t deltaTime) {
ArrayPool<IPhysicsObject>.Shared.Return(activeList, true);
}

for (EntItem? item = g_ShadowEntities!.ItemList; item != null; item = item.Next) {
BaseEntity? entity = item.Ent.Get();
if (entity == null) {
Msg("Dangling pointer to physics entity!!!\n");
continue;
}

IPhysicsObject? physics = entity.VPhysicsGetObject();
if (physics != null && !physics.IsAsleep())
entity.VPhysicsShadowUpdate(physics);
}

g_Collisions.BufferTouchEvents(false);
g_Collisions.FrameUpdate();
}
Expand Down
194 changes: 194 additions & 0 deletions Game.Server/PhysicsImpactDamage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
global using static Game.Server.PhysicsImpactDamage;

using Game.Shared;

using Source;
using Source.Common.Mathematics;
using Source.Common.Physics;

using System.Numerics;

namespace Game.Server;

public struct VPhysicsObjectStress
{
public float ExertedStress;
public float ReceivedStress;
public bool HasNonStaticStress;
public bool HasLargeObjectContact;
public IPhysicsObject? StressObject;
}

public static class PhysicsImpactDamage
{
static bool IsPhysicallyControlled(BaseEntity entity, IPhysicsObject physics) {
bool isPhysical = false;
if (entity.GetMoveType() == Source.MoveType.VPhysics)
isPhysical = true;
else {
IPhysicsShadowController? shadow = physics.GetShadowController();
if (shadow != null)
isPhysical = shadow.IsPhysicallyControlled();
}
return isPhysical;
}

public static float CalculateObjectStress(IPhysicsObject obj, BaseEntity inputOwnerEntity, out VPhysicsObjectStress output) {
List<BaseEntity?> objectList = [];
List<Vector3> objectForce = [];
bool hasLargeObject = false;
IPhysicsObject? stressObject = null;

objectList.Add(null);
objectForce.Add(vec3_origin);
objectList.Add(null);
objectForce.Add(vec3_origin);

BaseCombatCharacter? bcc = inputOwnerEntity.MyCombatCharacterPointer();

IPhysicsFrictionSnapshot snapshot = obj.CreateFrictionSnapshot();
float objMass = obj.GetMass();
while (snapshot.IsValid()) {
float force = snapshot.GetNormalForce();
if (force > 0.0f) {
IPhysicsObject other = snapshot.GetObject(1)!;
BaseEntity? otherEntity = other.GetGameData() as BaseEntity;
otherEntity ??= GetWorldEntity()!;

BaseEntity otherOwner = otherEntity;
if (otherEntity.GetOwnerEntity() != null)
otherOwner = otherEntity.GetOwnerEntity()!;

int outIndex = 0;
if (!other.IsMoveable())
outIndex = 0;
else if (bcc != null && (bcc.IRelationType(otherOwner) == Disposition.LI || otherEntity.IsNavIgnored()))
outIndex = 1;
else if ((other.GetGameFlags() & PhysicsFlags.PlayerHeld) != 0)
outIndex = 1;
else {
if (other.GetMass() >= VPHYSICS_LARGE_OBJECT_MASS) {
if (inputOwnerEntity.GetGroundEntity() != otherEntity)
hasLargeObject = true;
}

outIndex = objectList.Count;
for (int i = objectList.Count - 1; i >= 2; --i) {
if (objectList[i] == otherOwner) {
outIndex = i;
break;
}
}

if (outIndex == objectList.Count) {
objectList.Add(otherOwner);
objectForce.Add(vec3_origin);
stressObject ??= other;
}
}

if (outIndex != 0 && inputOwnerEntity.GetMoveType() != Source.MoveType.VPhysics && !IsPhysicallyControlled(otherEntity, other))
force = 0.0f;

snapshot.GetSurfaceNormal(out Vector3 normal);
objectForce[outIndex] += normal * force;
}
snapshot.NextFrictionData();
}
obj.DestroyFrictionSnapshot(snapshot);

objectForce[1] = vec3_origin;

float sum = 0;
Vector3 negativeForce = vec3_origin;
Vector3 positiveForce = vec3_origin;

Assert(objectList.Count == objectForce.Count);
for (int objectIndex = objectList.Count - 1; objectIndex >= 0; --objectIndex) {
Vector3 force = objectForce[objectIndex];
sum += force.Length();
for (int i = 0; i < 3; i++) {
if (force[i] < 0)
negativeForce[i] -= force[i];
else
positiveForce[i] += force[i];
}
}

Vector3 internalForce = vec3_origin;
Vector3 externalForce = vec3_origin;

for (int i = 0; i < 3; i++) {
if (negativeForce[i] < positiveForce[i]) {
internalForce[i] = positiveForce[i] - negativeForce[i];
externalForce[i] = negativeForce[i];
}
else {
internalForce[i] = negativeForce[i] - positiveForce[i];
externalForce[i] = positiveForce[i];
}
}

physenv.GetGravity(out Vector3 gravVector);
float gravity = gravVector.Length();
if (inputOwnerEntity.GetMoveType() != Source.MoveType.VPhysics && obj.IsMoveable()) {
Vector3 lastVel = vec3_origin;
IPhysicsShadowController? shadow = obj.GetShadowController();
if (shadow != null)
shadow.GetLastImpulse(out lastVel);
else {
if ((obj.GetCallbackFlags() & CallbackFlags.IsPlayerController) != 0) {
BasePlayer? player = ToBasePlayer(inputOwnerEntity);
IPhysicsPlayerController? controller = player?.GetPhysicsController();
controller?.GetLastImpulse(out lastVel);
}
}

float inputLen = lastVel.Length() * (1.0f / (float)physenv.GetSimulationTimestep()) * objMass;
if (inputLen > 0.0f) {
float internalLen = internalForce.Length();
if (internalLen < inputLen) {
float ratio = internalLen / inputLen;
Vector3 delta = internalForce * (1.0f - ratio);
internalForce += delta;
float deltaLen = delta.Length();
sum -= deltaLen;
float extLen = MathLib.VectorNormalize(ref externalForce) - deltaLen;
if (extLen < 0)
extLen = 0;

externalForce *= extLen;
}
}
}

float invGravity = gravity;
if (invGravity <= 0)
invGravity = 1.0f;
else
invGravity = 1.0f / invGravity;

sum *= invGravity;
internalForce *= invGravity;
externalForce *= invGravity;
if (!obj.IsMoveable()) {
externalForce = internalForce;
internalForce = vec3_origin;

if (!obj.IsStatic())
sum += objMass;
}
else {
if (sum > objMass)
sum = objMass + (sum - objMass) * 0.5f;
}

output.ExertedStress = internalForce.Length();
output.ReceivedStress = externalForce.Length();
output.HasNonStaticStress = objectList.Count > 2;
output.HasLargeObjectContact = hasLargeObject;
output.StressObject = stressObject;

return sum;
}
}
Loading
Loading