Skip to content
Merged

Lua #239

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
107 changes: 107 additions & 0 deletions Game.Client/GarrysMod/LuaBfRead.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using Game.Shared;

using Source.Common;
using Source.Common.Bitbuffers;
using Source.Common.GarrysMod.Lua;
using Source.Common.Mathematics;

using System.Numerics;

namespace Game.Client.GarrysMod;

public static partial class LuaBfRead
{
[LuaClass(typeof(bf_read))]
public static readonly LuaClass LC_bf_read = new("bf_read", LuaType.UserMsg, null, null);

public static void Push_bf_read(bf_read? buf) => LC_bf_read.Push(buf);

[LuaMethod]
static int bf_read__ReadAngle(ILuaInterface lua) {
bf_read buf = (bf_read)LC_bf_read.Get(1)!;
buf.ReadBitAngles(out QAngle angles);
LuaAngle.Push_Angle(angles);
return 1;
}

[LuaMethod]
static int bf_read__ReadBool(ILuaInterface lua) {
bf_read buf = (bf_read)LC_bf_read.Get(1)!;
lua.PushBool(buf.ReadOneBit() != 0);
return 1;
}

[LuaMethod]
static int bf_read__ReadChar(ILuaInterface lua) {
bf_read buf = (bf_read)LC_bf_read.Get(1)!;
lua.PushNumber(buf.ReadChar());
return 1;
}

[LuaMethod]
static int bf_read__ReadEntity(ILuaInterface lua) {
bf_read buf = (bf_read)LC_bf_read.Get(1)!;
uint handle = LuaNetworkedVars.ReadEHandle(buf);
if (handle == 0xFFFFFFFF)
return 0;
if (handle == 0) {
LuaEntity.Push_Entity(null);
return 1;
}
LuaEntity.Push_Entity(new EHANDLE(handle).Get());
return 1;
}

[LuaMethod]
static int bf_read__ReadFloat(ILuaInterface lua) {
bf_read buf = (bf_read)LC_bf_read.Get(1)!;
lua.PushNumber(buf.ReadFloat());
return 1;
}

[LuaMethod]
static int bf_read__ReadLong(ILuaInterface lua) {
bf_read buf = (bf_read)LC_bf_read.Get(1)!;
lua.PushNumber(buf.ReadLong());
return 1;
}

[LuaMethod]
static int bf_read__ReadShort(ILuaInterface lua) {
bf_read buf = (bf_read)LC_bf_read.Get(1)!;
lua.PushNumber(buf.ReadShort());
return 1;
}

[LuaMethod]
static int bf_read__ReadString(ILuaInterface lua) {
bf_read buf = (bf_read)LC_bf_read.Get(1)!;
Span<byte> str = stackalloc byte[128];
buf.ReadString(str, false, out int numChars);
lua.PushString(str[..numChars]);
return 1;
}

[LuaMethod]
static int bf_read__ReadVector(ILuaInterface lua) {
bf_read buf = (bf_read)LC_bf_read.Get(1)!;
buf.ReadBitVec3Coord(out Vector3 vec);
LuaVector.Push_Vector(vec);
return 1;
}

[LuaMethod]
static int bf_read__ReadVectorNormal(ILuaInterface lua) {
bf_read buf = (bf_read)LC_bf_read.Get(1)!;
buf.ReadBitVec3Normal(out Vector3 vec);
LuaVector.Push_Vector(vec);
return 1;
}

[LuaMethod]
static int bf_read__Reset(ILuaInterface lua) {
bf_read buf = (bf_read)LC_bf_read.Get(1)!;
buf.Seek(0);
return 0;
}
}
128 changes: 128 additions & 0 deletions Game.Client/GarrysMod/LuaDLight.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using Source;
using Source.Common;
using Source.Common.GarrysMod.Lua;

namespace Game.Client.GarrysMod;

public static partial class LuaDLight
{
[LuaClass(typeof(DLight), NullError = "Tried to use a NULL dlight_t!")]
public static readonly LuaClass LC_dlight_t = new("dlight_t", LuaType.DLight, null, null);

[LuaGlobal]
static int DynamicLight(ILuaInterface lua) {
int key = (int)lua.CheckNumber(1);
DLight? light;
if (lua.GetType(2) != LuaType.Nil && lua.GetBool(2))
light = effects.AllocElight(key);
else
light = effects.AllocDlight(key);
if (light == null)
return 0;
LC_dlight_t.Push(light);
return 1;
}

[LuaMethod]
static int dlight_t____tostring(ILuaInterface lua) {
DLight? light = (DLight?)LC_dlight_t.Get(1);
if (light == null) {
lua.Error("Tried to use a NULL dlight_t!");
return 0;
}
lua.PushString($"dlight_t [{light.Key}]");
return 1;
}

[LuaMethod]
static int dlight_t____newindex(ILuaInterface lua) {
DLight? light = (DLight?)LC_dlight_t.Get(1);
if (light == null) {
lua.Error("Tried to use a NULL dlight_t!");
return 0;
}

string key = lua.CheckString(2);
if (key.Length == 0)
return 0;

if (stricmp(key, "pos") == 0 || stricmp(key, "position") == 0) {
light.Origin = LuaVector.Get_Vector(3);
return 0;
}
if (stricmp(key, "noworld") == 0) {
if (lua.GetBool(3))
light.Flags |= DLightFlags.NoWorldIllumination;
else
light.Flags &= ~DLightFlags.NoWorldIllumination;
return 0;
}
if (stricmp(key, "nomodel") == 0) {
if (lua.GetBool(3))
light.Flags |= DLightFlags.NoModelIllumination;
else
light.Flags &= ~DLightFlags.NoModelIllumination;
return 0;
}
if (stricmp(key, "dir") == 0 || stricmp(key, "direction") == 0) {
light.Direction = LuaVector.Get_Vector(3);
return 0;
}
if (stricmp(key, "radius") == 0 || stricmp(key, "size") == 0) {
float radius = (float)lua.GetNumber(3);
if (radius <= 0.0f)
radius = 0.0f;
if (2048.0f <= radius)
radius = 2048.0f;
light.Radius = radius;
return 0;
}
if (stricmp(key, "die") == 0 || stricmp(key, "dietime") == 0) {
light.Die = lua.GetNumber(3);
return 0;
}
if (stricmp(key, "decay") == 0 || stricmp(key, "fade") == 0) {
light.Decay = (float)lua.GetNumber(3);
return 0;
}
if (stricmp(key, "minlight") == 0) {
light.MinLight = (float)lua.GetNumber(3);
return 0;
}
if (stricmp(key, "style") == 0) {
light.Style = (int)lua.GetNumber(3);
return 0;
}
if (stricmp(key, "key") == 0) {
light.Key = (int)lua.GetNumber(3);
return 0;
}
if (stricmp(key, "InnerAngle") == 0) {
light.InnerAngle = (float)lua.GetNumber(3);
return 0;
}
if (stricmp(key, "OuterAngle") == 0) {
light.OuterAngle = (float)lua.GetNumber(3);
return 0;
}
if (stricmp(key, "r") == 0) {
light.Color.R = (byte)(int)lua.GetNumber(3);
return 0;
}
if (stricmp(key, "g") == 0) {
light.Color.G = (byte)(int)lua.GetNumber(3);
return 0;
}
if (stricmp(key, "b") == 0) {
light.Color.B = (byte)(int)lua.GetNumber(3);
return 0;
}
if (stricmp(key, "Brightness") == 0) {
light.Color.Exponent = (sbyte)(int)lua.GetNumber(3);
return 0;
}

lua.ErrorNoHalt($"Tried to set wrong key '{key}' on Dynamic Light object.\n Valid keys: Pos, Dir, Size, DieTime, Brightness, Decay, MinLight, Style, Key, InnerAngle, OuterAngle, r, g, b, nomodel, noworld.\n");
return 0;
}
}
Loading
Loading