Skip to content
Merged

Lua #231

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
10 changes: 8 additions & 2 deletions Game.Client/C_BaseAnimating.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public void SetBodygroup(int group, int value) {
Animation.SetBodygroup(GetModelPtr(), ref Body, group, value);
}

public int GetBodygroup(int group) => Animation.GetBodygroup(GetModelPtr(), Body, group);
public ReadOnlySpan<char> GetBodygroupName(int group) => Animation.GetBodygroupName(GetModelPtr(), group);
public int GetBodygroupCount(int group) => Animation.GetBodygroupCount(GetModelPtr(), group);

float GroundSpeed;
const TimeUnit_t MAX_ANIMTIME_INTERVAL = 0.2;
public TimeUnit_t GetAnimTimeInterval() => Math.Min(gpGlobals.CurTime - AnimTime, MAX_ANIMTIME_INTERVAL);
Expand Down Expand Up @@ -179,7 +183,7 @@ public void StudioFrameAdvance() {
GroundSpeed = (float)GetSequenceGroundSpeed(hdr, GetSequence()) * GetModelScale();
}

public override int VPhysicsGetObjectList(Span<IPhysicsObject?> list){
public override int VPhysicsGetObjectList(Span<IPhysicsObject?> list) {
if (IsRagdoll()) {
int i;
for (i = 0; i < Ragdoll.RagdollBoneCount(); ++i) {
Expand Down Expand Up @@ -845,7 +849,7 @@ public string GetSequenceName(int sequence) {
return Animation.GetSequenceName(GetModelPtr(), sequence);
}

public ClientRagdoll? Ragdoll = null;
public ClientRagdoll? Ragdoll = null;

public override void OnDataChanged(DataUpdateType updateType) {
if (Ragdoll != null && GetSequence() != PrevSequence) {
Expand Down Expand Up @@ -1043,6 +1047,8 @@ public static class SequenceProxy
for (i = 0; i < hdr.GetNumPoseParameters(); i++) {
MStudioPoseParamDesc Pose = hdr.PoseParameter(i);
iv_flPoseParameter.SetLooping(Pose.Loop != 0.0f, i);
if (!IsServerEntity())
SetPoseParameter(hdr, i, 0.0f);
}

EyeAttachment = LookupAttachment("eyes");
Expand Down
31 changes: 29 additions & 2 deletions Game.Client/C_BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,7 @@ public C_BaseEntity() {
public int Health;
[NetworkVar("m_iMaxHealth")]
public int MaxHealth;
public virtual int GetMaxHealth() => MaxHealth;
[NetworkVar("m_spawnflags")]
public int SpawnFlags;
[NetworkVar("m_iGModFlags")]
Expand Down Expand Up @@ -1246,7 +1247,29 @@ public bool SetModel(ReadOnlySpan<char> modelName) {

public virtual ref readonly Vector3 GetRenderOrigin() => ref GetAbsOrigin();
public virtual ref readonly QAngle GetRenderAngles() => ref GetAbsAngles();
#if GMOD_DLL
Vector3 RenderBoundsOverrideMins;
Vector3 RenderBoundsOverrideMaxs;
bool HasRenderBoundsOverride;

public virtual void SetRenderBounds(in Vector3 mins, in Vector3 maxs) {
RenderBoundsOverrideMins = mins;
RenderBoundsOverrideMaxs = maxs;
HasRenderBoundsOverride = true;
}

public virtual bool GetRenderBoundsOverride(out Vector3 mins, out Vector3 maxs) {
mins = RenderBoundsOverrideMins;
maxs = RenderBoundsOverrideMaxs;
return HasRenderBoundsOverride;
}
#endif

public void GetRenderBounds(out Vector3 mins, out Vector3 maxs) {
#if GMOD_DLL
if (GetRenderBoundsOverride(out mins, out maxs))
return;
#endif
ModelType nModelType = modelinfo.GetModelType(Model);
if (nModelType == ModelType.Studio || nModelType == ModelType.Brush)
modelinfo.GetModelRenderBounds(GetModel(), out mins, out maxs);
Expand Down Expand Up @@ -1274,6 +1297,10 @@ public virtual bool IsTransparent() {
}

public virtual void UpdateOnRemove() {
#if GMOD_DLL
if (GetRefEHandle().Index != Constants.INVALID_EHANDLE_INDEX)
garrysmod.EntityRemoved(this, DestroyedOnRecreateEntities);
#endif
VPhysicsDestroyObject();

Assert(GetMoveParent() == null);
Expand Down Expand Up @@ -2536,7 +2563,7 @@ public void MoveToAimEnt() {
public bool IsDormant() => IsServerEntity() && Dormant;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool IsServerEntity() => Index != -1;
public bool IsServerEntity() => Index != -1;

public int EntIndex() {
return Index;
Expand Down Expand Up @@ -3538,7 +3565,7 @@ public bool GetShadowCastDistance(ref float dist, ShadowType shadowType) {

EHANDLE ShadowDirUseOtherEntity = new();

public bool GetShadowCastDirection(ref Vector3 direction, ShadowType shadowType) {
public virtual bool GetShadowCastDirection(ref Vector3 direction, ShadowType shadowType) {
if (ShadowDirUseOtherEntity.Get() != null)
return ShadowDirUseOtherEntity.Get()!.GetShadowCastDirection(ref direction, shadowType);
return false;
Expand Down
8 changes: 7 additions & 1 deletion Game.Client/C_BasePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ public bool GetSteamID(out CSteamID id) {
}
return false;
}
public int GetUserID() {
if (!engine.GetPlayerInfo(EntIndex(), out PlayerInfo pi))
return -1;
return pi.UserID;
}
PlayerInfo NetworkIDInfo;
InlineArray33<char> NetworkIDString;
public ReadOnlySpan<char> GetNetworkIDString() {
Expand All @@ -123,7 +128,7 @@ public ReadOnlySpan<char> GetNetworkIDString() {
return "NULL";
}
public bool IsHLTV() => IsLocalPlayer() && engine.IsHLTV();
public bool IsBot() => false; // TODO, gmod
public bool IsBot() => (GetFlags() & EntityFlags.FakeClient) != 0;
public static bool ShouldDrawLocalPlayer() {
return input.CAM_IsThirdPerson(); // todo
}
Expand Down Expand Up @@ -621,6 +626,7 @@ public void SetLocalViewAngles(in QAngle angles) {
EHANDLE TonemapController = new();
[NetworkVar("m_hViewEntity")]
EHANDLE ViewEntity = new();
public virtual C_BaseEntity? GetViewEntity() => ViewEntity.Get();
[NetworkVar("m_hViewModel")]
InlineArrayNewMaxViewmodels<Handle<C_BaseViewModel>> ViewModel = new();
[NetworkVar("m_bDisableWorldClicking")]
Expand Down
26 changes: 26 additions & 0 deletions Game.Client/C_PlayerResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,32 @@ public bool IsConnected(int index) {
return Connected[index];
}

public int GetPlayerScore(int index) {
if (!IsConnected(index))
return 0;
return Score[index];
}

public int GetPing(int index) {
if (!IsConnected(index))
return 0;
return Ping[index];
}

public int GetDeaths(int index) {
if (!IsConnected(index))
return 0;
return Deaths[index];
}

public int GetFrags(int index) => GetPlayerScore(index);

public int GetArmor(int index) {
if (!IsConnected(index))
return 0;
return Armor[index];
}

public ReadOnlySpan<char> GetPlayerName(int index) {
if (index < 1 || index > Constants.MAX_PLAYERS) {
Assert(false);
Expand Down
148 changes: 148 additions & 0 deletions Game.Client/GarrysMod/AvatarImage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using Source;
using Source.Common.GUI;
using Source.GUI.Controls;

using Steamworks;

namespace Game.Client.GarrysMod;

public class AvatarImage : Panel
{
static readonly List<TextureID>[] FreeTextures = [[], [], []];

TextureID TextureID;
ulong SteamID;
int Size;
readonly IImage? DefaultImage;

readonly Callback<AvatarImageLoaded_t> AvatarImageLoaded;
readonly Callback<PersonaStateChange_t> PersonaStateChange;

public AvatarImage(Panel? parent, ReadOnlySpan<char> name) : base(parent, name) {
SteamID = 0;
Size = 0;
AvatarImageLoaded = Callback<AvatarImageLoaded_t>.Create(OnAvatarImageLoaded);
PersonaStateChange = Callback<PersonaStateChange_t>.Create(OnPersonaStateChange);
TextureID = TextureID.INVALID;
DefaultImage = SchemeManager.GetImage("avatar_default", true);
}

public override void Dispose() {
ClearTexture();
PersonaStateChange.Dispose();
AvatarImageLoaded.Dispose();
base.Dispose();
}

void OnAvatarImageLoaded(AvatarImageLoaded_t info) {
if (info.m_steamID.m_SteamID == SteamID)
LoadAvatar();
}

void OnPersonaStateChange(PersonaStateChange_t info) {
if (info.m_ulSteamID == SteamID && (info.m_nChangeFlags & EPersonaChange.k_EPersonaChangeAvatar) != 0)
LoadAvatar();
}

void ClearTexture() {
SteamID = 0;
if (TextureID == TextureID.INVALID) {
TextureID = TextureID.INVALID;
return;
}

FreeTextures[Size].Add(TextureID);
TextureID = TextureID.INVALID;
}

void LoadAvatar() {
CSteamID steamID = new(SteamID);
if (SteamFriends.RequestUserInformation(steamID, false))
return;

int avatar;
if (Size == 0)
avatar = SteamFriends.GetSmallFriendAvatar(steamID);
else if (Size == 1)
avatar = SteamFriends.GetMediumFriendAvatar(steamID);
else if (Size == 2)
avatar = SteamFriends.GetLargeFriendAvatar(steamID);
else
return;

if ((uint)(avatar + 1) <= 1)
return;

if (!SteamUtils.GetImageSize(avatar, out uint wide, out uint tall))
return;

int size = (int)(wide * tall * 4);
byte[] rgba = new byte[size];
if (!SteamUtils.GetImageRGBA(avatar, rgba, size))
return;

if (TextureID == TextureID.INVALID) {
List<TextureID> free = FreeTextures[Size];
if (free.Count < 1)
TextureID = Surface.CreateNewTextureID(true);
else {
TextureID = free[^1];
free.RemoveAt(free.Count - 1);
}
}

Surface.DrawSetTextureRGBA(TextureID, rgba, (int)wide, (int)tall, 0, true);
}

public void SetSteamID(ulong steamID, int size) {
if (size < 0)
size = 0;
else if (2 < size)
size = 2;

if (SteamID != steamID || size != Size) {
if (size != Size)
ClearTexture();
SteamID = steamID;
Size = size;
LoadAvatar();
}
}

public void SetPlayer(C_BasePlayer? player, int size) {
if (player == null || !player.GetSteamID(out CSteamID id)) {
ClearTexture();
return;
}

ulong steamID = id.m_SteamID;
int clamped = 0;
if (-1 < size) {
clamped = size;
if (2 < size)
clamped = 2;
}

if (SteamID == steamID && clamped == Size)
return;

if (clamped != Size)
ClearTexture();

SteamID = steamID;
Size = clamped;
LoadAvatar();
}

public override void Paint() {
if (TextureID == TextureID.INVALID) {
DefaultImage!.SetSize(GetWide(), GetTall());
DefaultImage.Paint();
return;
}

Surface.DrawSetTexture(TextureID);
Surface.DrawSetColor(new Color(255, 255, 255, 255));
Surface.DrawTexturedRect(0, 0, GetWide(), GetTall());
}
}
Loading
Loading