Skip to content

Commit 7166fd1

Browse files
authored
Merge pull request #294 from ZehMatt/lua-ports
Couple lua bindings and fixes
2 parents 777767f + cb70440 commit 7166fd1

11 files changed

Lines changed: 203 additions & 7 deletions

File tree

‎Game.Client/HUD/Hud.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Game.Shared;
88

99
using Source;
10+
using Source.Common.Commands;
1011
using Source.Common.Filesystem;
1112
using Source.Common.Formats.Keyvalues;
1213
using Source.Common.GUI;
@@ -154,6 +155,8 @@ public enum ProgressBarType
154155
[EngineComponent]
155156
public class Hud(HudElementHelper HudElementHelper)
156157
{
158+
static readonly ConVar hidehud = new("hidehud", "0", FCvar.Cheat);
159+
157160
readonly HudTextureDict Icons = [];
158161
public readonly List<IHudElement> HudList = [];
159162
internal InButtons KeyBits;
@@ -267,7 +270,8 @@ internal bool IsHidden(HideHudBits hudFlags) {
267270
return true;
268271

269272
HideHudBits hideHud = (HideHudBits)player.Local.HideHUD;
270-
// todo: hidehud convar
273+
if (hidehud.GetInt() != 0)
274+
hideHud = (HideHudBits)hidehud.GetInt();
271275

272276
if ((hideHud & HideHudBits.All) != 0)
273277
return true;

‎Game.Server/GarrysMod/GMOD_Player.cs‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,43 @@ public void Kill() {
7373
Event_Dying(new TakeDamageInfo(this, this, 0, DamageType.NeverGib));
7474
}
7575

76+
public void KillSilent() {
77+
if (gGM != null && gGM.CallWithArgs((int)LUA_POOLEDSTRING.PlayerSilentDeath)) {
78+
LuaEntity.Push_Entity(this);
79+
gGM.CallFinish(1);
80+
}
81+
82+
// todo: LeaveVehicle();
83+
// todo: SetDriving(null);
84+
Health = 0;
85+
ClearUseEntity();
86+
// todo: DetonateTripmines();
87+
88+
GetActiveWeapon()?.Holster();
89+
90+
SetViewOffset(VEC_DEAD_VIEWHEIGHT_SCALED(this));
91+
LifeState = (int)Source.LifeState.Dying;
92+
pl.DeadFlag = true;
93+
AddSolidFlags(SolidFlags.NotSolid);
94+
SetMoveType(Source.MoveType.None);
95+
SetGroundEntity(null);
96+
SetSuitUpdate(null, false, 0);
97+
SetFOV(this, 0);
98+
DeathTime = gpGlobals.CurTime;
99+
FlashlightTurnOff();
100+
LifeState = (int)Source.LifeState.Dead;
101+
RemoveEffects(EntityEffects.NoDraw);
102+
StopZooming();
103+
RemoveAllItems(true);
104+
UnforceButtons((InButtons)0x3FFFFFF);
105+
EnableButtons((InButtons)0x3FFFFFF);
106+
107+
if (gGM != null && gGM.CallWithArgs((int)LUA_POOLEDSTRING.PostPlayerDeath)) {
108+
LuaEntity.Push_Entity(this);
109+
gGM.CallFinish(1);
110+
}
111+
}
112+
76113
public ReadOnlySpan<char> GetUniqueID() {
77114
if (gpGlobals.MaxClients == 1) {
78115
if (NeedsUniqueID) {

‎Game.Server/GarrysMod/LuaNPC.cs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Game.Shared;
2+
3+
using Source.Common.GarrysMod.Lua;
4+
5+
namespace Game.Server.GarrysMod;
6+
7+
public static partial class LuaNPC
8+
{
9+
[LuaClass]
10+
static readonly LuaEntityClass LC_NPC = LuaEntity.LC_NPC;
11+
12+
public static AI_BaseNPC? Get_NPC(int stackPos, bool allowNull) {
13+
BaseEntity? ent = LuaEntity.Get_Entity(stackPos, allowNull);
14+
if (ent == null)
15+
return null;
16+
17+
AI_BaseNPC? npc = ent.MyNPCPointer();
18+
if (npc == null && !allowNull)
19+
g_Lua!.Error("Tried to use a NULL NPC!");
20+
return npc;
21+
}
22+
23+
[LuaMethod]
24+
static int NPC__SetCurrentWeaponProficiency(ILuaInterface lua) {
25+
AI_BaseNPC? npc = Get_NPC(1, false);
26+
if (npc == null)
27+
return 0;
28+
29+
npc.SetCurrentWeaponProficiency((WeaponProficiency)Math.Clamp((int)lua.CheckNumber(2), (int)WeaponProficiency.Poor, (int)WeaponProficiency.Perfect));
30+
return 0;
31+
}
32+
33+
[LuaMethod]
34+
static int NPC__GetCurrentWeaponProficiency(ILuaInterface lua) {
35+
AI_BaseNPC? npc = Get_NPC(1, false);
36+
if (npc == null)
37+
return 0;
38+
39+
lua.PushNumber((int)npc.GetCurrentWeaponProficiency());
40+
return 1;
41+
}
42+
}

‎Game.Server/Triggers.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace Game.Server;
1515
public static class TriggerGlobals
1616
{
1717
public static readonly ConVar showtriggers = new("showtriggers", "0", FCvar.Cheat, "Shows trigger brushes");
18+
public static readonly ConVar g_debug_transitions = new("g_debug_transitions", "0", FCvar.None, "Set to 1 and restart the map to be warned if the map has no trigger_transition volumes. Set to 2 to see a dump of all entities & associated results during a transition.");
1819

1920
// Global list of triggers that care about weapon fire
2021
// Doesn't need saving, the triggers re-add themselves on restore.

‎Game.Shared/GarrysMod/GarrysMod.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ public class GarrysMod : IGarrysMod
4343
{
4444
public static readonly ConVar lua_strict = new("lua_strict", "0", FCvar.Replicated | FCvar.Notify, "Enable extra checks for Lua API, such as argument type checking, errors that normally would be silent, etc. Useful to catch bugs in code when developing addons.");
4545
public static readonly ConVar sv_allowcslua = new("sv_allowcslua", "0", FCvar.Archive | FCvar.Notify | FCvar.Replicated, "Allow clients to run clientside addons. This will override any gamemode setting!");
46+
public static readonly ConVar gmod_suit = new("gmod_suit", "0", FCvar.Replicated, "Set to non zero to enable Half-Life 2 aux suit power stuff.");
4647

4748
public void DLLInit(IServiceCollection services) {
4849
#if CLIENT_DLL

‎Game.Shared/GarrysMod/LuaEntity.cs‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,24 @@ static int Entity__IsFlagSet(ILuaInterface lua) {
18151815
return 1;
18161816
}
18171817

1818+
[LuaMethod]
1819+
static int Entity__AddFlags(ILuaInterface lua) {
1820+
BaseEntity ent = Get_Entity(1, false)!;
1821+
if (IsQueuedForRemoval(ent) || ent.IsEFlagSet(EFL.KillMe))
1822+
return 0;
1823+
ent.AddFlag((EntityFlags)(int)lua.CheckNumber(2));
1824+
return 0;
1825+
}
1826+
1827+
[LuaMethod]
1828+
static int Entity__RemoveFlags(ILuaInterface lua) {
1829+
BaseEntity ent = Get_Entity(1, false)!;
1830+
if (IsQueuedForRemoval(ent) || ent.IsEFlagSet(EFL.KillMe))
1831+
return 0;
1832+
ent.RemoveFlag((EntityFlags)(int)lua.CheckNumber(2));
1833+
return 0;
1834+
}
1835+
18181836
[LuaMethod]
18191837
static int Entity__GetParent(ILuaInterface lua) {
18201838
BaseEntity ent = Get_Entity(1, false)!;
@@ -2094,6 +2112,25 @@ static int Entity__EmitSound(ILuaInterface lua) {
20942112
return 0;
20952113
}
20962114

2115+
[LuaMethod]
2116+
static int Entity__StopSound(ILuaInterface lua) {
2117+
BaseEntity ent = Get_Entity(1, false)!;
2118+
string soundName = lua.CheckString(2);
2119+
2120+
if (ent.IsBaseCombatWeapon() && ent is BaseCombatWeapon weapon && weapon.GetOwner() is BaseEntity owner) {
2121+
BaseEntity.StopSound(owner.EntIndex(), (int)SoundEntityChannel.Weapon, soundName);
2122+
return 0;
2123+
}
2124+
2125+
if (!stristr(soundName, ".wav").IsEmpty || !stristr(soundName, ".mp3").IsEmpty || !stristr(soundName, ".ogg").IsEmpty || soundName.StartsWith('!')) {
2126+
for (SoundEntityChannel channel = SoundEntityChannel.Replace; channel <= SoundEntityChannel.Voice2; channel++)
2127+
BaseEntity.StopSound(ent.EntIndex(), (int)channel, soundName);
2128+
}
2129+
else
2130+
ent.StopSound(soundName);
2131+
return 0;
2132+
}
2133+
20972134
[LuaMethod]
20982135
static int Entity__ViewModelIndex(ILuaInterface lua) {
20992136
BaseEntity ent = Get_Entity(1, false)!;
@@ -3070,6 +3107,29 @@ static int Entity__SetCollisionGroup(ILuaInterface lua) {
30703107
}
30713108

30723109
#if GAME_DLL
3110+
[LuaMethod]
3111+
static int Entity__SetLagCompensated(ILuaInterface lua) {
3112+
BaseEntity ent = Get_Entity(1, false)!;
3113+
if (ent.GetBaseAnimating() == null || ent.IsPlayer())
3114+
return 0;
3115+
if (lua.GetBool(2))
3116+
lagcompensation.AddAdditionalEntity(ent);
3117+
else
3118+
lagcompensation.RemoveAdditionalEntity(ent);
3119+
return 0;
3120+
}
3121+
3122+
[LuaMethod]
3123+
static int Entity__IsLagCompensated(ILuaInterface lua) {
3124+
BaseEntity ent = Get_Entity(1, false)!;
3125+
if (ent.GetBaseAnimating() == null) {
3126+
lua.PushBool(false);
3127+
return 1;
3128+
}
3129+
lua.PushBool(ent.IsPlayer() || lagcompensation.IsAdditionalEntity(ent));
3130+
return 1;
3131+
}
3132+
30733133
[LuaMethod]
30743134
static int Entity__HasBoneManipulations(ILuaInterface lua) {
30753135
BaseEntity ent = Get_Entity(1, false)!;

‎Game.Shared/GarrysMod/LuaGame.cs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,47 @@ static int SetSkillLevel(ILuaInterface lua) {
9898
rules.SkillLevel = (int)g_Lua!.CheckNumber(1);
9999
return 0;
100100
}
101+
102+
[LuaFunction]
103+
static int GetGlobalState(ILuaInterface lua) {
104+
int index = GlobalEntity.GetIndex(g_Lua!.CheckString(1));
105+
g_Lua.PushNumber(index < 0 ? (int)GlobalEState.Dead : (int)GlobalEntity.GetState(index));
106+
return 1;
107+
}
108+
109+
[LuaFunction]
110+
static int SetGlobalState(ILuaInterface lua) {
111+
string name = g_Lua!.CheckString(1);
112+
int index = GlobalEntity.GetIndex(name);
113+
GlobalEState state = (GlobalEState)Math.Clamp((int)g_Lua.CheckNumber(2), (int)GlobalEState.Off, (int)GlobalEState.Dead);
114+
if (index < 0) {
115+
GlobalEntity.Add(name, gpGlobals.MapName ?? "", state);
116+
GlobalEntity.SetCounter(GlobalEntity.GetIndex(name), 0);
117+
}
118+
else
119+
GlobalEntity.SetState(index, state);
120+
return 0;
121+
}
122+
123+
[LuaFunction]
124+
static int GetGlobalCounter(ILuaInterface lua) {
125+
int index = GlobalEntity.GetIndex(g_Lua!.CheckString(1));
126+
g_Lua.PushNumber(index < 0 ? 0 : GlobalEntity.GetCounter(index));
127+
return 1;
128+
}
129+
130+
[LuaFunction]
131+
static int SetGlobalCounter(ILuaInterface lua) {
132+
string name = g_Lua!.CheckString(1);
133+
int index = GlobalEntity.GetIndex(name);
134+
int counter = (int)g_Lua.CheckNumber(2);
135+
if (index < 0) {
136+
GlobalEntity.Add(name, gpGlobals.MapName ?? "", GlobalEState.Off);
137+
index = GlobalEntity.GetIndex(name);
138+
}
139+
GlobalEntity.SetCounter(index, counter);
140+
return 0;
141+
}
101142
#endif
102143

103144
[LuaFunction]

‎Game.Shared/GarrysMod/LuaPlayer.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ static int Player__Kill(ILuaInterface lua) {
152152
player.Kill();
153153
return 0;
154154
}
155+
156+
[LuaMethod]
157+
static int Player__KillSilent(ILuaInterface lua) {
158+
GMOD_Player player = (GMOD_Player)Get_Player(1, false)!;
159+
player.KillSilent();
160+
return 0;
161+
}
155162
#endif
156163

157164
[LuaMethod]

‎Game.Shared/SoundEmitterSystem.cs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,15 @@ public partial class
403403
{
404404
public static void StopSound(int entIndex, int channel, ReadOnlySpan<char> sample) => g_SoundEmitterSystem.StopSound(entIndex, channel, sample);
405405
public static void StopSound(int entIndex, ReadOnlySpan<char> soundname) => g_SoundEmitterSystem.StopSound(entIndex, soundname);
406-
#if GAME_DLL
407-
public void StopSound(ReadOnlySpan<char> soundname) => StopSound(EntIndex(), soundname);
406+
public void StopSound(ReadOnlySpan<char> soundname) {
407+
#if CLIENT_DLL
408+
if (EntIndex() == -1) {
409+
StopSound(GetSoundSourceIndex(), soundname);
410+
return;
411+
}
408412
#endif
413+
StopSound(EntIndex(), soundname);
414+
}
409415

410416
public static SoundLevel LookupSoundLevel(ReadOnlySpan<char> soundname) {
411417
return soundemitterbase.LookupSoundLevel(soundname);

‎Source.Engine/Precache.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class PrecacheItem
2323

2424
public const int PRECACHE_USER_DATA_NUMBITS = 2;
2525

26-
public const int MAX_MODEL_INDEX_BITS = 12;
26+
public const int MAX_MODEL_INDEX_BITS = 13;
2727
public const int MAX_MODELS = (1 << MAX_MODEL_INDEX_BITS);
2828

2929
public const int MAX_GENERIC_INDEX_BITS = 9;

0 commit comments

Comments
 (0)