Skip to content

Commit 979c0d4

Browse files
authored
Merge pull request #236 from SourceDotNET/physconstraints
Constraints
2 parents 14b6b8c + 2726b24 commit 979c0d4

21 files changed

Lines changed: 3216 additions & 31 deletions

‎Game.Client/GarrysMod/LuaRender.cs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,35 @@ static int Clear(ILuaInterface lua) {
620620
return 0;
621621
}
622622

623+
[LuaFunction]
624+
static int SetMaterial(ILuaInterface lua) {
625+
IMaterial? material = (IMaterial?)LuaMaterial.LC_IMaterial.Get(1);
626+
if (material == null)
627+
return 0;
628+
using MatRenderContextPtr renderContext = new(materials);
629+
CurrentMaterial.Init(material);
630+
if (CurrentMaterial.IsNotNull)
631+
renderContext.Bind(CurrentMaterial.Get());
632+
return 0;
633+
}
634+
635+
static readonly MaterialReference CurrentMaterial = new();
636+
637+
#if CLIENT_DLL
638+
[LuaFunction]
639+
static int DrawSprite(ILuaInterface lua) {
640+
Vector3 pos = LuaVector.Get_Vector(1);
641+
ILuaObject? colorObj = lua.GetObject(4);
642+
Color color = new(255, 255, 255, 255);
643+
if (colorObj != null && colorObj.isTable())
644+
color = new((byte)(int)colorObj.GetMemberFloat("r", 255), (byte)(int)colorObj.GetMemberFloat("g", 255), (byte)(int)colorObj.GetMemberFloat("b", 255), (byte)(int)colorObj.GetMemberFloat("a", 255));
645+
float height = (float)lua.GetNumber(3);
646+
float width = (float)lua.GetNumber(2);
647+
BeamDraw.DrawSprite(pos, width, height, color);
648+
return 0;
649+
}
650+
#endif
651+
623652
[LuaFunction]
624653
static int ClearDepth(ILuaInterface lua) {
625654
bool stencil = lua.GetType(1) == (int)LuaType.Nil || lua.GetBool(1);

‎Game.Client/InMain.cs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,16 @@ public void CreateMove(int sequenceNumber, double inputSampleFrametime, bool act
295295

296296
cmd.RandomSeed = (int)(MD5Value.PseudoRandom((uint)sequenceNumber) & 0x7fffffff);
297297
#if GMOD_DLL
298+
int buttonsPressed = 0;
299+
for (ButtonCode code = ButtonCode.Key0; code < ButtonCode.Last; code++) {
300+
bool isMouse = code >= ButtonCode.MouseFirst && code <= ButtonCode.MouseLast;
301+
if (vguiInput.GetFocus() == null || isMouse) {
302+
if (inputsystem.IsButtonDown(code) || (isMouse && vguiInput.IsMouseDown(code)))
303+
cmd.ButtonsPressed[buttonsPressed++] = (byte)code;
304+
if (buttonsPressed > 4)
305+
break;
306+
}
307+
}
298308
MouseWheel = 0;
299309
#endif
300310
}

‎Game.Server/BaseAnimating.cs‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public Activity GetSequenceActivity(int sequence) {
379379
public bool IsModelScaled() => ModelScale > 1.0f + FLT_EPSILON || ModelScale < 1.0f - FLT_EPSILON;
380380
public float GetModelScale() => ModelScale;
381381

382-
StudioHdr? StudioHdr;
382+
protected StudioHdr? StudioHdr;
383383
public Model? GetModel() => modelinfo.GetModel(GetModelIndex());
384384

385385
// todo...
@@ -831,6 +831,26 @@ public bool GetAttachment(ReadOnlySpan<char> attachmentName, out Vector3 absOrig
831831
}
832832

833833

834+
public int GetAttachmentBone(int attachment) {
835+
StudioHdr? studioHdr = GetModelPtr();
836+
if (studioHdr == null)
837+
return 0;
838+
839+
if (attachment < 1 || attachment > studioHdr.GetNumAttachments())
840+
return 0;
841+
842+
return studioHdr.GetAttachmentBone(attachment - 1);
843+
}
844+
845+
public int GetPhysicsBone(int boneIndex) {
846+
StudioHdr? studioHdr = GetModelPtr();
847+
if (studioHdr != null) {
848+
if (boneIndex >= 0 && boneIndex < studioHdr.NumBones())
849+
return studioHdr.Bone(boneIndex).PhysicsBone;
850+
}
851+
return 0;
852+
}
853+
834854
public bool GetAttachment(int attachment, out Vector3 absOrigin, out QAngle absAngles) {
835855
Matrix3x4 attachmentToWorld;
836856

‎Game.Server/BaseEntity.cs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,9 @@ public virtual void VPhysicsFriction(IPhysicsObject obj, float energy, int surfa
17701770
public virtual bool PhysicsSplash(in Vector3 centerPoint, in Vector3 normal, float rawSpeed, float scaledSpeed) => false;
17711771

17721772
public IPhysicsObject? VPhysicsGetObject() => PhysicsObject;
1773+
#if GMOD_DLL
1774+
public virtual IPhysicsObject? VPhysicsGetElement(int index) => PhysicsObject;
1775+
#endif
17731776

17741777
public void VPhysicsSetObject(IPhysicsObject? physics) {
17751778
if (PhysicsObject != null && physics != null)
@@ -3012,6 +3015,14 @@ public virtual void Precache() {
30123015

30133016
}
30143017

3018+
public virtual void NotifySystemEvent(BaseEntity notify, NotifySystemEvent eventType, in NotifySystemEventParams parms) { }
3019+
3020+
#if GMOD_DLL
3021+
public virtual void SetPhysObject(int index, IPhysicsObject? obj) { }
3022+
public virtual void SetEntity(ReadOnlySpan<char> name, BaseEntity? entity) { }
3023+
public virtual void StartMotionController() { }
3024+
#endif
3025+
30153026
static bool _AllowPrecache;
30163027
public static bool IsPrecacheAllowed() => _AllowPrecache;
30173028
public static bool SetAllowPrecache(bool allow) => _AllowPrecache = allow;
@@ -3551,6 +3562,8 @@ struct TeleportListEntry
35513562

35523563
static void TeleportEntity(BaseEntity sourceEntity, ref TeleportListEntry entry, Vector3? newPosition, QAngle? newAngles, Vector3? newVelocity) {
35533564
BaseEntity teleport = entry.Entity;
3565+
Vector3 prevOrigin = entry.PrevAbsOrigin;
3566+
QAngle prevAngles = entry.PrevAbsAngles;
35543567

35553568
SolidFlags solidFlags = (SolidFlags)teleport.GetSolidFlags();
35563569
teleport.AddSolidFlags(SolidFlags.NotSolid);
@@ -3580,6 +3593,7 @@ static void TeleportEntity(BaseEntity sourceEntity, ref TeleportListEntry entry,
35803593
teleport.CalcAbsolutePosition();
35813594
}
35823595
IPhysicsObject? phys = teleport.VPhysicsGetObject();
3596+
bool rotatePhysics = false;
35833597

35843598
// handle physics objects / shadows
35853599
if (phys != null) {
@@ -3591,10 +3605,14 @@ static void TeleportEntity(BaseEntity sourceEntity, ref TeleportListEntry entry,
35913605
// don't rotate physics on players or bbox entities
35923606
if (teleport.IsPlayer() || teleport.GetSolid() == SolidType.BBox)
35933607
rotAngles = vec3_angle;
3608+
else
3609+
rotatePhysics = true;
35943610

35953611
phys.SetPosition(teleport.GetAbsOrigin(), rotAngles, true);
35963612
}
35973613

3614+
g_pNotify.ReportTeleportEvent(teleport, prevOrigin, prevAngles, rotatePhysics);
3615+
35983616
teleport.SetSolidFlags(solidFlags);
35993617
}
36003618

‎Game.Server/EntityList.cs‎

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace Game.Server;
1414

1515
public static class EntityListGlobals
1616
{
17+
public static readonly INotify g_pNotify = NotifyList.g_NotifyList;
18+
1719
[ConCommand("report_entities", "List all entities")]
1820
static void report_entities() {
1921
if (!Util.IsCommandIssuedByServerAdmin())
@@ -759,15 +761,93 @@ public void FrameUpdatePostEntityThink() {
759761
}
760762
}
761763

764+
public struct EntityNotify
765+
{
766+
public BaseEntity Notify;
767+
public BaseEntity Watched;
768+
}
769+
770+
public class NotifyList : INotify, IEntityListener
771+
{
772+
public static readonly NotifyList g_NotifyList = new();
773+
774+
readonly List<EntityNotify> List = [];
775+
776+
public void AddEntity(BaseEntity notify, BaseEntity watched) {
777+
watched.AddEFlags(EFL.Notify);
778+
List.Add(new() { Notify = notify, Watched = watched });
779+
}
780+
781+
public void RemoveEntity(BaseEntity notify, BaseEntity watched) {
782+
for (int i = List.Count; --i >= 0;) {
783+
if (List[i].Notify == notify && List[i].Watched == watched)
784+
FastRemove(i);
785+
}
786+
}
787+
788+
public void ReportNamedEvent(BaseEntity entity, ReadOnlySpan<char> inputName) {
789+
Variant_t emptyVariant = default;
790+
791+
if (!entity.IsEFlagSet(EFL.Notify))
792+
return;
793+
794+
for (int i = 0; i < List.Count; i++) {
795+
if (List[i].Watched == entity)
796+
List[i].Notify.AcceptInput(inputName, entity, entity, emptyVariant, 0);
797+
}
798+
}
799+
800+
public void LevelInitPreEntity() => gEntList.AddListenerEntity(this);
801+
802+
public void LevelShutdownPreEntity() {
803+
gEntList.RemoveListenerEntity(this);
804+
List.Clear();
805+
}
806+
807+
public void OnEntityCreated(BaseEntity entity) { }
808+
809+
public void OnEntityDeleted(BaseEntity entity) {
810+
((INotify)this).ReportDestroyEvent(entity);
811+
ClearEntity(entity);
812+
}
813+
814+
public void ClearEntity(BaseEntity notify) {
815+
for (int i = List.Count; --i >= 0;) {
816+
if (List[i].Notify == notify || List[i].Watched == notify)
817+
FastRemove(i);
818+
}
819+
}
820+
821+
public void ReportSystemEvent(BaseEntity entity, NotifySystemEvent eventType, in NotifySystemEventParams parms) {
822+
if (!entity.IsEFlagSet(EFL.Notify))
823+
return;
824+
825+
for (int i = 0; i < List.Count; i++) {
826+
if (List[i].Watched == entity)
827+
List[i].Notify.NotifySystemEvent(entity, eventType, parms);
828+
}
829+
}
830+
831+
void FastRemove(int i) {
832+
int last = List.Count - 1;
833+
List[i] = List[last];
834+
List.RemoveAt(last);
835+
}
836+
}
837+
762838
public class EntityListSystem : AutoGameSystemPerFrame
763839
{
764840
public static EntityListSystem g_EntityListSystem = new();
765841
public static void RespawnEntities() => g_EntityListSystem.RespawnAllEntities = true;
766842
bool RespawnAllEntities;
767843

768844
public override void LevelInitPreEntity() {
769-
SimThinkManager.g_SimThinkManager.LevelInitPreEntity();
845+
NotifyList.g_NotifyList.LevelInitPreEntity();
770846
EntityTouchManager.g_TouchManager.LevelInitPreEntity();
847+
SimThinkManager.g_SimThinkManager.LevelInitPreEntity();
848+
}
849+
public override void LevelShutdownPreEntity() {
850+
NotifyList.g_NotifyList.LevelShutdownPreEntity();
771851
}
772852
public override void LevelShutdownPostEntity() {
773853
SimThinkManager.g_SimThinkManager.LevelShutdownPostEntity();

‎Game.Server/LogicEntities.cs‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
using Source.Common.Physics;
77
using Source.Common.Server;
88

9+
#if GMOD_DLL
10+
using Game.Server.GarrysMod;
11+
#endif
12+
913
using System.Numerics;
1014

1115
namespace Game.Server;
@@ -1687,12 +1691,30 @@ public class LogicCollisionPair : LogicalEntity
16871691
string? NameAttach2;
16881692
bool Disabled;
16891693
bool Succeeded;
1694+
#if GMOD_DLL
1695+
InlineArray2<IPhysicsObject?> PhysObjects;
1696+
InlineArray2<EHANDLE> Entities;
1697+
InlineArray2<int> PhysIndices;
1698+
bool DisableOnRemove;
1699+
1700+
public LogicCollisionPair() {
1701+
Entities[0] = new();
1702+
Entities[1] = new();
1703+
}
1704+
#endif
16901705

16911706
public static readonly new DataMap DataDesc = new(typeof(LogicCollisionPair), LogicalEntity.DataDesc, [
16921707
DEFINE<LogicCollisionPair>.KEYFIELD(nameof(NameAttach1), FieldType.String, "attach1"),
16931708
DEFINE<LogicCollisionPair>.KEYFIELD(nameof(NameAttach2), FieldType.String, "attach2"),
16941709
DEFINE<LogicCollisionPair>.KEYFIELD(nameof(Disabled), FieldType.Boolean, "startdisabled"),
16951710
DEFINE<LogicCollisionPair>.FIELD(nameof(Succeeded), FieldType.Boolean),
1711+
#if GMOD_DLL
1712+
DEFINE<LogicCollisionPair>.KEYFIELD(nameof(DisableOnRemove), FieldType.Boolean, "disable_on_remove"),
1713+
DEFINE<LogicCollisionPair>.FIELD("Entities[0]", FieldType.EHandle),
1714+
DEFINE<LogicCollisionPair>.FIELD("Entities[1]", FieldType.EHandle),
1715+
DEFINE<LogicCollisionPair>.FIELD("PhysIndices[0]", FieldType.Integer),
1716+
DEFINE<LogicCollisionPair>.FIELD("PhysIndices[1]", FieldType.Integer),
1717+
#endif
16961718

16971719
// Inputs
16981720
DEFINE<LogicCollisionPair>.INPUTFUNC(FieldType.Void, "DisableCollisions", nameof(InputDisableCollisions), (INPUTFUNCPTR)((self, data) => ((LogicCollisionPair)self).InputDisableCollisions(data))),
@@ -1709,14 +1731,65 @@ public class LogicCollisionPair : LogicalEntity
17091731
return g_PhysWorldObject;
17101732

17111733
IPhysicsObject? physics = PhysicsHook.FindPhysicsObjectByName(name, errorEntity);
1712-
if (physics == null)
1734+
if (physics == null) {
17131735
DevWarning($"{errorEntity.GetClassname()}: can't find {name}\n");
1736+
#if GMOD_DLL
1737+
physics = g_PhysWorldObject;
1738+
#endif
1739+
}
17141740
return physics;
17151741
}
17161742

1743+
#if GMOD_DLL
1744+
public override void SetPhysObject(int index, IPhysicsObject? obj) {
1745+
PhysObjects[index] = obj;
1746+
BaseEntity? entity = (BaseEntity?)obj!.GetGameData();
1747+
Entities[index].Set(entity);
1748+
PhysIndices[index] = 0;
1749+
entity = Entities[index].Get();
1750+
if (entity != null) {
1751+
IPhysicsObject?[] list = new IPhysicsObject?[VPHYSICS_MAX_OBJECT_LIST_COUNT];
1752+
int count = entity.VPhysicsGetObjectList(list);
1753+
if (count > 1) {
1754+
for (int i = 0; i < count; i++) {
1755+
if (list[i] == obj)
1756+
PhysIndices[index] = i;
1757+
}
1758+
}
1759+
}
1760+
}
1761+
1762+
public override void OnRestore() {
1763+
BaseEntity? entity0 = Entities[0].Get();
1764+
if (entity0 != null)
1765+
PhysObjects[0] = entity0.VPhysicsGetElement(PhysIndices[0]);
1766+
BaseEntity? entity1 = Entities[1].Get();
1767+
if (entity1 != null)
1768+
PhysObjects[1] = entity1.VPhysicsGetElement(PhysIndices[1]);
1769+
Activate();
1770+
}
1771+
1772+
public override void UpdateOnRemove() {
1773+
if (DisableOnRemove && (!Succeeded || Disabled))
1774+
EnableCollisions(true);
1775+
base.UpdateOnRemove();
1776+
}
1777+
#endif
1778+
17171779
public void EnableCollisions(bool enable) {
1780+
#if GMOD_DLL
1781+
if (PhysObjects[0] != null || PhysObjects[1] != null) {
1782+
if (!LuaPhysObj.IsPhysicsObjectValid(PhysObjects[0]))
1783+
PhysObjects[0] = null;
1784+
if (!LuaPhysObj.IsPhysicsObjectValid(PhysObjects[1]))
1785+
PhysObjects[1] = null;
1786+
}
1787+
IPhysicsObject? physics0 = PhysObjects[0] ?? FindPhysicsObjectByNameOrWorld(NameAttach1, this);
1788+
IPhysicsObject? physics1 = PhysObjects[1] ?? FindPhysicsObjectByNameOrWorld(NameAttach2, this);
1789+
#else
17181790
IPhysicsObject? physics0 = FindPhysicsObjectByNameOrWorld(NameAttach1, this);
17191791
IPhysicsObject? physics1 = FindPhysicsObjectByNameOrWorld(NameAttach2, this);
1792+
#endif
17201793

17211794
// need two different objects to do anything
17221795
if (physics0 != null && physics1 != null && physics0 != physics1) {

0 commit comments

Comments
 (0)