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
29 changes: 29 additions & 0 deletions Game.Client/GarrysMod/LuaRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,35 @@ static int Clear(ILuaInterface lua) {
return 0;
}

[LuaFunction]
static int SetMaterial(ILuaInterface lua) {
IMaterial? material = (IMaterial?)LuaMaterial.LC_IMaterial.Get(1);
if (material == null)
return 0;
using MatRenderContextPtr renderContext = new(materials);
CurrentMaterial.Init(material);
if (CurrentMaterial.IsNotNull)
renderContext.Bind(CurrentMaterial.Get());
return 0;
}

static readonly MaterialReference CurrentMaterial = new();

#if CLIENT_DLL
[LuaFunction]
static int DrawSprite(ILuaInterface lua) {
Vector3 pos = LuaVector.Get_Vector(1);
ILuaObject? colorObj = lua.GetObject(4);
Color color = new(255, 255, 255, 255);
if (colorObj != null && colorObj.isTable())
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));
float height = (float)lua.GetNumber(3);
float width = (float)lua.GetNumber(2);
BeamDraw.DrawSprite(pos, width, height, color);
return 0;
}
#endif

[LuaFunction]
static int ClearDepth(ILuaInterface lua) {
bool stencil = lua.GetType(1) == (int)LuaType.Nil || lua.GetBool(1);
Expand Down
10 changes: 10 additions & 0 deletions Game.Client/InMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,16 @@ public void CreateMove(int sequenceNumber, double inputSampleFrametime, bool act

cmd.RandomSeed = (int)(MD5Value.PseudoRandom((uint)sequenceNumber) & 0x7fffffff);
#if GMOD_DLL
int buttonsPressed = 0;
for (ButtonCode code = ButtonCode.Key0; code < ButtonCode.Last; code++) {
bool isMouse = code >= ButtonCode.MouseFirst && code <= ButtonCode.MouseLast;
if (vguiInput.GetFocus() == null || isMouse) {
if (inputsystem.IsButtonDown(code) || (isMouse && vguiInput.IsMouseDown(code)))
cmd.ButtonsPressed[buttonsPressed++] = (byte)code;
if (buttonsPressed > 4)
break;
}
}
MouseWheel = 0;
#endif
}
Expand Down
22 changes: 21 additions & 1 deletion Game.Server/BaseAnimating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public Activity GetSequenceActivity(int sequence) {
public bool IsModelScaled() => ModelScale > 1.0f + FLT_EPSILON || ModelScale < 1.0f - FLT_EPSILON;
public float GetModelScale() => ModelScale;

StudioHdr? StudioHdr;
protected StudioHdr? StudioHdr;
public Model? GetModel() => modelinfo.GetModel(GetModelIndex());

// todo...
Expand Down Expand Up @@ -831,6 +831,26 @@ public bool GetAttachment(ReadOnlySpan<char> attachmentName, out Vector3 absOrig
}


public int GetAttachmentBone(int attachment) {
StudioHdr? studioHdr = GetModelPtr();
if (studioHdr == null)
return 0;

if (attachment < 1 || attachment > studioHdr.GetNumAttachments())
return 0;

return studioHdr.GetAttachmentBone(attachment - 1);
}

public int GetPhysicsBone(int boneIndex) {
StudioHdr? studioHdr = GetModelPtr();
if (studioHdr != null) {
if (boneIndex >= 0 && boneIndex < studioHdr.NumBones())
return studioHdr.Bone(boneIndex).PhysicsBone;
}
return 0;
}

public bool GetAttachment(int attachment, out Vector3 absOrigin, out QAngle absAngles) {
Matrix3x4 attachmentToWorld;

Expand Down
18 changes: 18 additions & 0 deletions Game.Server/BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,9 @@ public virtual void VPhysicsFriction(IPhysicsObject obj, float energy, int surfa
public virtual bool PhysicsSplash(in Vector3 centerPoint, in Vector3 normal, float rawSpeed, float scaledSpeed) => false;

public IPhysicsObject? VPhysicsGetObject() => PhysicsObject;
#if GMOD_DLL
public virtual IPhysicsObject? VPhysicsGetElement(int index) => PhysicsObject;
#endif

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

}

public virtual void NotifySystemEvent(BaseEntity notify, NotifySystemEvent eventType, in NotifySystemEventParams parms) { }

#if GMOD_DLL
public virtual void SetPhysObject(int index, IPhysicsObject? obj) { }
public virtual void SetEntity(ReadOnlySpan<char> name, BaseEntity? entity) { }
public virtual void StartMotionController() { }
#endif

static bool _AllowPrecache;
public static bool IsPrecacheAllowed() => _AllowPrecache;
public static bool SetAllowPrecache(bool allow) => _AllowPrecache = allow;
Expand Down Expand Up @@ -3551,6 +3562,8 @@ struct TeleportListEntry

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

SolidFlags solidFlags = (SolidFlags)teleport.GetSolidFlags();
teleport.AddSolidFlags(SolidFlags.NotSolid);
Expand Down Expand Up @@ -3580,6 +3593,7 @@ static void TeleportEntity(BaseEntity sourceEntity, ref TeleportListEntry entry,
teleport.CalcAbsolutePosition();
}
IPhysicsObject? phys = teleport.VPhysicsGetObject();
bool rotatePhysics = false;

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

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

g_pNotify.ReportTeleportEvent(teleport, prevOrigin, prevAngles, rotatePhysics);

teleport.SetSolidFlags(solidFlags);
}

Expand Down
82 changes: 81 additions & 1 deletion Game.Server/EntityList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace Game.Server;

public static class EntityListGlobals
{
public static readonly INotify g_pNotify = NotifyList.g_NotifyList;

[ConCommand("report_entities", "List all entities")]
static void report_entities() {
if (!Util.IsCommandIssuedByServerAdmin())
Expand Down Expand Up @@ -759,15 +761,93 @@ public void FrameUpdatePostEntityThink() {
}
}

public struct EntityNotify
{
public BaseEntity Notify;
public BaseEntity Watched;
}

public class NotifyList : INotify, IEntityListener
{
public static readonly NotifyList g_NotifyList = new();

readonly List<EntityNotify> List = [];

public void AddEntity(BaseEntity notify, BaseEntity watched) {
watched.AddEFlags(EFL.Notify);
List.Add(new() { Notify = notify, Watched = watched });
}

public void RemoveEntity(BaseEntity notify, BaseEntity watched) {
for (int i = List.Count; --i >= 0;) {
if (List[i].Notify == notify && List[i].Watched == watched)
FastRemove(i);
}
}

public void ReportNamedEvent(BaseEntity entity, ReadOnlySpan<char> inputName) {
Variant_t emptyVariant = default;

if (!entity.IsEFlagSet(EFL.Notify))
return;

for (int i = 0; i < List.Count; i++) {
if (List[i].Watched == entity)
List[i].Notify.AcceptInput(inputName, entity, entity, emptyVariant, 0);
}
}

public void LevelInitPreEntity() => gEntList.AddListenerEntity(this);

public void LevelShutdownPreEntity() {
gEntList.RemoveListenerEntity(this);
List.Clear();
}

public void OnEntityCreated(BaseEntity entity) { }

public void OnEntityDeleted(BaseEntity entity) {
((INotify)this).ReportDestroyEvent(entity);
ClearEntity(entity);
}

public void ClearEntity(BaseEntity notify) {
for (int i = List.Count; --i >= 0;) {
if (List[i].Notify == notify || List[i].Watched == notify)
FastRemove(i);
}
}

public void ReportSystemEvent(BaseEntity entity, NotifySystemEvent eventType, in NotifySystemEventParams parms) {
if (!entity.IsEFlagSet(EFL.Notify))
return;

for (int i = 0; i < List.Count; i++) {
if (List[i].Watched == entity)
List[i].Notify.NotifySystemEvent(entity, eventType, parms);
}
}

void FastRemove(int i) {
int last = List.Count - 1;
List[i] = List[last];
List.RemoveAt(last);
}
}

public class EntityListSystem : AutoGameSystemPerFrame
{
public static EntityListSystem g_EntityListSystem = new();
public static void RespawnEntities() => g_EntityListSystem.RespawnAllEntities = true;
bool RespawnAllEntities;

public override void LevelInitPreEntity() {
SimThinkManager.g_SimThinkManager.LevelInitPreEntity();
NotifyList.g_NotifyList.LevelInitPreEntity();
EntityTouchManager.g_TouchManager.LevelInitPreEntity();
SimThinkManager.g_SimThinkManager.LevelInitPreEntity();
}
public override void LevelShutdownPreEntity() {
NotifyList.g_NotifyList.LevelShutdownPreEntity();
}
public override void LevelShutdownPostEntity() {
SimThinkManager.g_SimThinkManager.LevelShutdownPostEntity();
Expand Down
75 changes: 74 additions & 1 deletion Game.Server/LogicEntities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
using Source.Common.Physics;
using Source.Common.Server;

#if GMOD_DLL
using Game.Server.GarrysMod;
#endif

using System.Numerics;

namespace Game.Server;
Expand Down Expand Up @@ -1687,12 +1691,30 @@ public class LogicCollisionPair : LogicalEntity
string? NameAttach2;
bool Disabled;
bool Succeeded;
#if GMOD_DLL
InlineArray2<IPhysicsObject?> PhysObjects;
InlineArray2<EHANDLE> Entities;
InlineArray2<int> PhysIndices;
bool DisableOnRemove;

public LogicCollisionPair() {
Entities[0] = new();
Entities[1] = new();
}
#endif

public static readonly new DataMap DataDesc = new(typeof(LogicCollisionPair), LogicalEntity.DataDesc, [
DEFINE<LogicCollisionPair>.KEYFIELD(nameof(NameAttach1), FieldType.String, "attach1"),
DEFINE<LogicCollisionPair>.KEYFIELD(nameof(NameAttach2), FieldType.String, "attach2"),
DEFINE<LogicCollisionPair>.KEYFIELD(nameof(Disabled), FieldType.Boolean, "startdisabled"),
DEFINE<LogicCollisionPair>.FIELD(nameof(Succeeded), FieldType.Boolean),
#if GMOD_DLL
DEFINE<LogicCollisionPair>.KEYFIELD(nameof(DisableOnRemove), FieldType.Boolean, "disable_on_remove"),
DEFINE<LogicCollisionPair>.FIELD("Entities[0]", FieldType.EHandle),
DEFINE<LogicCollisionPair>.FIELD("Entities[1]", FieldType.EHandle),
DEFINE<LogicCollisionPair>.FIELD("PhysIndices[0]", FieldType.Integer),
DEFINE<LogicCollisionPair>.FIELD("PhysIndices[1]", FieldType.Integer),
#endif

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

IPhysicsObject? physics = PhysicsHook.FindPhysicsObjectByName(name, errorEntity);
if (physics == null)
if (physics == null) {
DevWarning($"{errorEntity.GetClassname()}: can't find {name}\n");
#if GMOD_DLL
physics = g_PhysWorldObject;
#endif
}
return physics;
}

#if GMOD_DLL
public override void SetPhysObject(int index, IPhysicsObject? obj) {
PhysObjects[index] = obj;
BaseEntity? entity = (BaseEntity?)obj!.GetGameData();
Entities[index].Set(entity);
PhysIndices[index] = 0;
entity = Entities[index].Get();
if (entity != null) {
IPhysicsObject?[] list = new IPhysicsObject?[VPHYSICS_MAX_OBJECT_LIST_COUNT];
int count = entity.VPhysicsGetObjectList(list);
if (count > 1) {
for (int i = 0; i < count; i++) {
if (list[i] == obj)
PhysIndices[index] = i;
}
}
}
}

public override void OnRestore() {
BaseEntity? entity0 = Entities[0].Get();
if (entity0 != null)
PhysObjects[0] = entity0.VPhysicsGetElement(PhysIndices[0]);
BaseEntity? entity1 = Entities[1].Get();
if (entity1 != null)
PhysObjects[1] = entity1.VPhysicsGetElement(PhysIndices[1]);
Activate();
}

public override void UpdateOnRemove() {
if (DisableOnRemove && (!Succeeded || Disabled))
EnableCollisions(true);
base.UpdateOnRemove();
}
#endif

public void EnableCollisions(bool enable) {
#if GMOD_DLL
if (PhysObjects[0] != null || PhysObjects[1] != null) {
if (!LuaPhysObj.IsPhysicsObjectValid(PhysObjects[0]))
PhysObjects[0] = null;
if (!LuaPhysObj.IsPhysicsObjectValid(PhysObjects[1]))
PhysObjects[1] = null;
}
IPhysicsObject? physics0 = PhysObjects[0] ?? FindPhysicsObjectByNameOrWorld(NameAttach1, this);
IPhysicsObject? physics1 = PhysObjects[1] ?? FindPhysicsObjectByNameOrWorld(NameAttach2, this);
#else
IPhysicsObject? physics0 = FindPhysicsObjectByNameOrWorld(NameAttach1, this);
IPhysicsObject? physics1 = FindPhysicsObjectByNameOrWorld(NameAttach2, this);
#endif

// need two different objects to do anything
if (physics0 != null && physics1 != null && physics0 != physics1) {
Expand Down
Loading
Loading