Skip to content
Merged

Lua #162

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
55 changes: 54 additions & 1 deletion Game.Client/C_BaseAnimating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Source.Common;
using Source.Common.Commands;
using Source.Common.Engine;
using Source.Common.Formats.Keyvalues;
using Source.Common.Mathematics;

using System;
Expand Down Expand Up @@ -179,6 +180,28 @@ public TimeUnit_t GetSequenceMoveDist(StudioHdr? studioHdr, int sequence) {
return vecReturn.Length();
}
public TimeUnit_t GetSequenceGroundSpeed(int sequence) => GetSequenceGroundSpeed(GetModelPtr(), sequence);
static string? Studio_GetKeyValueText(StudioHdr? studioHdr, int sequence) {
if (studioHdr != null && studioHdr.SequencesAvailable()) {
if (sequence >= 0 && sequence < studioHdr.GetNumSeq()) {
MStudioSeqDesc seqdesc = studioHdr.Seqdesc(sequence);
if (seqdesc.KeyValueSize != 0)
return System.Text.Encoding.ASCII.GetString(((ReadOnlySpan<byte>)seqdesc.Data.Span[seqdesc.KeyValueIndex..]).SliceNullTerminatedString());
}
}
return null;
}

public KeyValues? GetSequenceKeyValues(int sequence) {
string? text = Studio_GetKeyValueText(GetModelPtr(), sequence);

if (text != null) {
KeyValues seqKeyValues = new("");
if (seqKeyValues.LoadFromBuffer(modelinfo.GetModelName(GetModel()), text))
return seqKeyValues;
}
return null;
}

public TimeUnit_t GetSequenceGroundSpeed(StudioHdr? studioHdr, int sequence) {
TimeUnit_t t = SequenceDuration(studioHdr, sequence);
if (t > 0)
Expand Down Expand Up @@ -1283,7 +1306,9 @@ public double FrameAdvance(double interval) {
return flInterval;
}

public virtual void UpdateClientSideAnimation() {
public virtual void UpdateClientSideAnimation() => UpdateBaseClientSideAnimation();

protected void UpdateBaseClientSideAnimation() {
if (ClientSideAnimation) {
Assert(ClientSideAnimationListHandle != INVALID_CLIENTSIDEANIMATION_LIST_HANDLE);
if (GetSequence() != -1) {
Expand Down Expand Up @@ -1733,6 +1758,9 @@ void DisableMuzzleFlash() {

int PrevNewSequenceParity;
int PrevResetEventsParity;
#if GMOD_DLL
public virtual void ClearPrevResetEventsParity() => PrevResetEventsParity = -1;
#endif

public int GetSequence() => Sequence;
public override void UpdateVisibility() {
Expand Down Expand Up @@ -1807,6 +1835,31 @@ public override void GetAimEntOrigin(C_BaseEntity attachedTo, out Vector3 origin
public EHANDLE BoneManipulator = new();
[NetworkName("m_pFlexManipulator")]
public EHANDLE FlexManipulator = new();
public EHANDLE ClientFlexManipulator = new();

public virtual C_BaseEntity? GetFlexManipulator(bool create) {
if (FlexManipulator.IsValid())
return FlexManipulator.Get();

if (ClientFlexManipulator.Get() != null)
return ClientFlexManipulator.Get();

if (create) {
C_FlexManipulate manipulator = new();
if (!manipulator.InitializeAsClientEntity(null, RenderGroup.OpaqueEntity))
return null;

manipulator.SetParent(this, 0);
manipulator.SetOwnerEntity(this);
manipulator.SetLocalOrigin(default);
manipulator.Spawn();
ClientFlexManipulator.Set(manipulator);
return ClientFlexManipulator.Get();
}

return null;
}

[NetworkName("m_OverrideViewTarget")]
public Vector3 OverrideViewTarget;
[NetworkName("m_fadeMinDist")]
Expand Down
14 changes: 1 addition & 13 deletions Game.Client/C_BaseAnimatingOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private static void ResizeAnimationLayerCallback(object instance, object list, i
[NetworkName("m_AnimOverlay")]
readonly List<AnimationLayerRef> AnimOverlay = [];
readonly List<InterpolatedVar<AnimationLayer>> iv_AnimOverlay = [];
readonly float[] OverlayPrevEventCycle = new float[MAX_OVERLAYS];
public readonly float[] OverlayPrevEventCycle = new float[MAX_OVERLAYS];

public AnimationLayerRef GetAnimOverlay(int i) => AnimOverlay[i];

Expand Down Expand Up @@ -200,16 +200,4 @@ public override void DoAnimationEvents(StudioHdr hdr) {
}
return hdr;
}

public void EstimateAbsVelocity(out Vector3 vel) {
if (this == C_BasePlayer.GetLocalPlayer()) {
// This is interpolated and networked
vel = GetAbsVelocity();
return;
}
vel = default;
using InterpolationContext context = new();
InterpolationContext.EnableExtrapolation(true);
IV_Origin.GetDerivative_SmoothVelocity(new Span<Vector3>(ref vel), gpGlobals.CurTime);
}
}
16 changes: 16 additions & 0 deletions Game.Client/C_BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2755,6 +2755,22 @@ public ref readonly Vector3 GetAbsVelocity() {
CalcAbsoluteVelocity();
return ref AbsVelocity;
}

public virtual void EstimateAbsVelocity(out Vector3 vel) {
#if GMOD_DLL
if (this != C_BasePlayer.GetLocalPlayer() && GetMoveType() != Source.MoveType.Fly && GetMoveType() != Source.MoveType.FlyGravity && (!IsPlayer() || GetMoveType() == Source.MoveType.None)) {
#else
if (this != C_BasePlayer.GetLocalPlayer()) {
#endif
vel = default;
using InterpolationContext context = new();
InterpolationContext.EnableExtrapolation(true);
IV_Origin.GetDerivative_SmoothVelocity(new Span<Vector3>(ref vel), gpGlobals.CurTime);
return;
}

vel = GetAbsVelocity();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to beg Rubat for weeks to just return the real thing, the stuff above is still a workaround for some addons that apparently break 😢

}
public void SetAbsVelocity(in Vector3 absVelocity) {
if (AbsVelocity == absVelocity)
return;
Expand Down
20 changes: 20 additions & 0 deletions Game.Client/C_BaseFlex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,26 @@ public bool CheckSceneEvent(TimeUnit_t currenttime, ChoreoScene? scene, ChoreoEv

public virtual bool CheckSceneEventCompletion(SceneEventInfo info, TimeUnit_t currenttime, ChoreoScene scene, ChoreoEvent ev) => true;

#if GMOD_DLL
public void SetFlexWeight(LocalFlexController index, float value, bool clamp) {
if (index >= 0 && index < GetNumFlexControllers() && (int)index < 96) {
StudioHdr? studioHdr = GetModelPtr();
if (studioHdr == null)
return;

MStudioFlexController flexcontroller = studioHdr.FlexController(index);

if (flexcontroller.Max != flexcontroller.Min) {
value = (value - flexcontroller.Min) / (flexcontroller.Max - flexcontroller.Min);
if (clamp)
value = Math.Clamp(value, 0.0f, 1.0f);
}

FlexWeight[(int)index] = value;
}
}
#endif

public void SetFlexWeight(LocalFlexController index, float value) {
if (index >= 0 && index < GetNumFlexControllers()) {
StudioHdr? studioHdr = GetModelPtr();
Expand Down
4 changes: 4 additions & 0 deletions Game.Client/C_BasePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ public virtual void PreThink() {
}
public C_BaseEntity? GetVehicleEntity() => Vehicle.Get();
public bool IsInAVehicle() => Vehicle.Get() != null;
#if GMOD_DLL
public virtual bool RestrictPlayerPitch() => false;
#endif
public virtual void SetAnimation(PlayerAnim playerAnim) { } // todo

public virtual Vector3 GetAutoaimVector(float scale) {
Expand Down Expand Up @@ -571,6 +574,7 @@ public void SetLocalViewAngles(in QAngle angles) {
EHANDLE ColorCorrectionCtrl = new();
[NetworkName("m_bUseWeaponsInVehicle")]
bool UseWeaponsInVehicle;
public bool GetAllowWeaponsInVehicle() => UseWeaponsInVehicle;
public bool OnTarget;
[NetworkName("m_flDeathTime")]
public double DeathTime;
Expand Down
5 changes: 5 additions & 0 deletions Game.Client/C_FlexManipulate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ public class C_FlexManipulate : C_BaseEntity
public Vector3 EyesLocalTarget;
[NetworkName("m_iFlexWeights")]
public InlineArray96<float> FlexWeights;

public void SetFlexWeight(byte index, float weight) {
if (index < 96 && FlexWeights[index] != weight)
FlexWeights[index] = weight;
}
}
17 changes: 17 additions & 0 deletions Game.Client/GarrysMod/C_GMOD_Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Source;
using Source.Common;
using Source.Common.Commands;
using Source.Common.GarrysMod.Lua;
using Source.Common.Mathematics;

using System.Numerics;

Expand Down Expand Up @@ -79,4 +81,19 @@ public partial class C_GMOD_Player() : C_HL2MP_Player()
public float Gravity;
[NetworkName("m_bSprintEnabled")]
public bool SprintEnabled;

public override void UpdateClientSideAnimation() {
if (Driving.Get() != null) {
if (gGM != null && gGM.CallWithArgs((int)LUA_POOLEDSTRING.PlayerDriveAnimate)) {
LuaEntity.Push_Entity(this);
gGM.CallNoReturns(1);
}
}
else {
QAngle eyeAngles = EyeAngles();
PlayerAnimState.Update(eyeAngles[YAW], eyeAngles[PITCH]);
}

UpdateBaseClientSideAnimation();
}
}
8 changes: 8 additions & 0 deletions Game.Client/HL2MP/C_HL2MP_Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,18 @@ public override void PostThink() {
AngEyeAngles = EyeAngles();
}

#if GMOD_DLL
public override void UpdateClientSideAnimation() {
QAngle eyeAngles = EyeAngles();
PlayerAnimState.Update(eyeAngles[YAW], eyeAngles[PITCH]);
UpdateBaseClientSideAnimation();
}
#else
public override void ClientThink() {
base.ClientThink();
PlayerAnimState.Update(AngEyeAngles[YAW], AngEyeAngles[PITCH]);
}
#endif

public override ShadowType ShadowCastType() {
if (!IsVisible())
Expand Down
12 changes: 11 additions & 1 deletion Game.Client/HLClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,18 @@ public void GMOD_DoSnapshots() {
throw new NotImplementedException();
}

public static InlineArray128<float> g_VoiceVolume;

public void GMOD_VoiceVolume(uint playerID, float volume) {
throw new NotImplementedException();
if (playerID == 512) {
C_BasePlayer? localPlayer = C_BasePlayer.GetLocalPlayer();
if (localPlayer == null)
return;
playerID = (uint)localPlayer.EntIndex();
}

if (playerID - 1U < 128)
g_VoiceVolume[(int)playerID - 1] = volume;
}

public void GMOD_OnDrawSkybox() {
Expand Down
25 changes: 25 additions & 0 deletions Game.Server/BaseAnimating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ public partial class BaseAnimating : BaseEntity
public EHANDLE BoneManipulator = new();
[NetworkName("m_pFlexManipulator")]
public EHANDLE FlexManipulator = new();

public virtual void SetFlexManipulator(BaseEntity? manipulator) => FlexManipulator.Set(manipulator);

public virtual BaseEntity? GetFlexManipulator(bool create) {
if (FlexManipulator.Get() == null && create) {
BaseEntity? manipulator = CreateEntityByName("manipulate_flex");
if (manipulator == null)
return null;

manipulator.SetParent(this, -1);
manipulator.SetLocalOrigin(default);
manipulator.Spawn();
SetFlexManipulator(manipulator);
}

return FlexManipulator.Get();
}

[NetworkName("m_fadeMinDist")]
public float FadeMinDist;
[NetworkName("m_fadeMaxDist")]
Expand Down Expand Up @@ -925,4 +943,11 @@ public void SetBodygroup(int group, int value) {
public bool IsUsingClientSideAnimation() {
return ClientSideAnimation;
}

public void UseClientSideAnimation(bool use) {
if (ClientSideAnimation != use) {
NetworkStateChanged();
ClientSideAnimation = use;
}
}
}
7 changes: 7 additions & 0 deletions Game.Server/FlexManipulate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ public class FlexManipulate : BaseEntity
public Vector3 EyesLocalTarget;
[NetworkName("m_iFlexWeights")]
public InlineArray96<float> FlexWeights;

public void SetFlexWeight(byte index, float weight) {
if (index < 96 && FlexWeights[index] != weight) {
NetworkStateChanged();
FlexWeights[index] = weight;
}
}
}
21 changes: 21 additions & 0 deletions Game.Server/GarrysMod/GMOD_Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Source;
using Source.Common;
using Source.Common.Engine;
using Source.Common.GarrysMod.Lua;

using System.Numerics;

Expand Down Expand Up @@ -55,11 +56,31 @@ public partial class GMOD_Player : HL2MP_Player
]);
public static readonly new ServerClass ServerClass = new ServerClass(DT_GMOD_Player);

public bool Transition;

public override void InitialSpawn() {
SetModel("models/player.mdl");
base.InitialSpawn();

if (gGM != null && gGM.CallWithArgs((int)LUA_POOLEDSTRING.PlayerInitialSpawn)) {
LuaEntity.Push_Entity(this);
g_Lua!.PushBool(Transition);
gGM.CallNoReturns(2);
}

LuaNetworkedVars.g_LuaNetworkedVars!.PlayerInsert(this);
}

public override void Spawn() {
base.Spawn();

if (gGM != null && gGM.CallWithArgs((int)LUA_POOLEDSTRING.PlayerSpawn)) {
LuaEntity.Push_Entity(this);
g_Lua!.PushBool(Transition);
gGM.CallNoReturns(2);
}
}

public override BaseEntity EntSelectSpawnPoint() {
BaseEntity? spot = null;
while ((spot = gEntList.FindEntityByClassname(spot, "info_player_start")) != null) {
Expand Down
11 changes: 10 additions & 1 deletion Game.Server/HL2MP/HL2MP_Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public HL2MP_Player() {
AngEyeAngles.Init();

base.ChangeTeam(0);

#if GMOD_DLL
UseClientSideAnimation(true);
#endif
}

public override void UpdateOnRemove() {
Expand Down Expand Up @@ -522,6 +526,7 @@ public override bool BumpWeapon(BaseCombatWeapon weapon) {
return true;
}

#if !GMOD_DLL
public override void ChangeTeam(int team, bool autoTeam = false, bool silent = false, bool autoBalance = false) {
bool kill = false;

Expand Down Expand Up @@ -550,6 +555,7 @@ public override void ChangeTeam(int team, bool autoTeam = false, bool silent = f
if (kill == true)
CommitSuicide();
}
#endif

bool HandleCommand_JoinTeam(int team) {
throw new NotImplementedException();
Expand Down Expand Up @@ -721,7 +727,10 @@ bool StartObserverMode(int mode) {
throw new NotImplementedException();
}

void StopObserverMode() { }
public override void StopObserverMode() {
// todo
base.StopObserverMode();
}

void State_Enter_OBSERVER_MODE() { }

Expand Down
Loading
Loading