Skip to content

Commit f4aa2e7

Browse files
committed
Sun
1 parent a718e40 commit f4aa2e7

5 files changed

Lines changed: 162 additions & 6 deletions

File tree

‎Game.Client/C_Sun.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ public partial class C_Sun : C_BaseEntity
4242
public int OverlayMaterial;
4343
// todo: RecvProxy_HDRColorScale
4444
[NetworkVar("HDRColorScale")]
45-
public int HDRColorScale;
45+
public float HDRColorScale;
4646
}

‎Game.Server/Sun.cs‎

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
using Source;
44
using Source.Common;
5+
using Source.Common.Engine;
6+
using Source.Common.Mathematics;
57

68
using System.Numerics;
79

@@ -24,6 +26,98 @@ public partial class Sun : BaseEntity
2426
new(nameof(HDRColorScale)) { Encoding = NetEncoding.Float, Bits = 0, Flags = PropFlags.NoScale, Low = 0.0f, High = 100.0f },
2527
];
2628

29+
public static readonly new DataMap DataDesc = new(typeof(Sun), BaseEntity.DataDesc, [
30+
DEFINE<Sun>.FIELD(nameof(Direction), FieldType.Vector),
31+
32+
DEFINE<Sun>.KEYFIELD(nameof(UseAngles), FieldType.Integer, "use_angles"),
33+
DEFINE<Sun>.KEYFIELD(nameof(Pitch), FieldType.Float, "pitch"),
34+
DEFINE<Sun>.KEYFIELD(nameof(Yaw), FieldType.Float, "angle"),
35+
DEFINE<Sun>.KEYFIELD(nameof(Size), FieldType.Integer, "size"),
36+
DEFINE<Sun>.KEYFIELD(nameof(Overlay), FieldType.Color32, "overlaycolor"),
37+
DEFINE<Sun>.KEYFIELD(nameof(OverlaySize), FieldType.Integer, "overlaysize"),
38+
DEFINE<Sun>.KEYFIELD(nameof(StrMaterial), FieldType.String, "material"),
39+
DEFINE<Sun>.KEYFIELD(nameof(StrOverlayMaterial), FieldType.String, "overlaymaterial"),
40+
41+
DEFINE<Sun>.FIELD(nameof(On), FieldType.Boolean),
42+
43+
DEFINE<Sun>.INPUTFUNC(FieldType.Void, "TurnOn", nameof(InputTurnOn), (INPUTFUNCPTR)((self, data) => ((Sun)self).InputTurnOn(data))),
44+
DEFINE<Sun>.INPUTFUNC(FieldType.Void, "TurnOff", nameof(InputTurnOff), (INPUTFUNCPTR)((self, data) => ((Sun)self).InputTurnOff(data))),
45+
DEFINE<Sun>.INPUTFUNC(FieldType.Color32, "SetColor", nameof(InputSetColor), (INPUTFUNCPTR)((self, data) => ((Sun)self).InputSetColor(data))),
46+
47+
DEFINE<Sun>.KEYFIELD(nameof(HDRColorScale), FieldType.Float, "HDRColorScale"),
48+
]);
49+
public override DataMap? GetDataDescMap() => DataDesc;
50+
51+
public Sun() {
52+
Direction = new(0, 0, 1);
53+
54+
UseAngles = 0;
55+
Pitch = 0;
56+
Yaw = 0;
57+
Size = 16;
58+
59+
On = true;
60+
AddEFlags(EFL.ForceCheckTransmit);
61+
62+
StrMaterial = null;
63+
StrOverlayMaterial = null;
64+
OverlaySize = -1;
65+
}
66+
67+
public override void Activate() {
68+
base.Activate();
69+
70+
if (UseAngles != 0) {
71+
MapUtils.SetupLightNormalFromProps(GetAbsAngles(), Yaw, Pitch, out Vector3 direction);
72+
Direction = -direction;
73+
}
74+
else {
75+
BaseEntity? ent = gEntList.FindEntityByName(null, Target);
76+
if (ent != null) {
77+
Vector3 direction = GetAbsOrigin() - ent.GetAbsOrigin();
78+
MathLib.VectorNormalize(ref direction);
79+
Direction = direction;
80+
}
81+
}
82+
83+
if (OverlaySize == -1)
84+
OverlaySize = Size;
85+
86+
if (StrMaterial == null)
87+
StrMaterial = "sprites/light_glow02_add_noz.vmt";
88+
else if (StrMaterial.AsSpan().GetFileExtension().IsEmpty)
89+
StrMaterial += ".vmt";
90+
91+
if (StrOverlayMaterial == null)
92+
StrOverlayMaterial = "sprites/light_glow02_add_noz.vmt";
93+
else if (StrOverlayMaterial.AsSpan().GetFileExtension().IsEmpty)
94+
StrOverlayMaterial += ".vmt";
95+
96+
Material = PrecacheModel(StrMaterial);
97+
OverlayMaterial = PrecacheModel(StrOverlayMaterial);
98+
}
99+
100+
void InputTurnOn(InputData inputdata) {
101+
if (!On)
102+
On = true;
103+
}
104+
105+
void InputTurnOff(InputData inputdata) {
106+
if (On)
107+
On = false;
108+
}
109+
110+
void InputSetColor(InputData inputdata) => Render = inputdata.Value.Color32();
111+
112+
public override EdictFlags UpdateTransmitState() => SetTransmitState(EdictFlags.Always);
113+
114+
public string? StrMaterial;
115+
public string? StrOverlayMaterial;
116+
117+
public int UseAngles;
118+
public float Pitch;
119+
public float Yaw;
120+
27121
[NetworkVar("m_clrRender")]
28122
public partial Color Render { get; set; }
29123
[NetworkVar("m_clrOverlay")]
@@ -41,5 +135,5 @@ public partial class Sun : BaseEntity
41135
[NetworkVar("m_nOverlayMaterial")]
42136
public partial int OverlayMaterial { get; set; }
43137
[NetworkVar("HDRColorScale")]
44-
public partial int HDRColorScale { get; set; }
138+
public partial float HDRColorScale { get; set; }
45139
}

‎Game.Shared/GarrysMod/LuaEntity.cs‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,7 @@ static int Entity__SetSaveValue(ILuaInterface lua) {
22312231
TypeDescription? td = null;
22322232
for (DataMap? map = ent.GetDataDescMap(); map != null && td == null; map = map.BaseMap) {
22332233
foreach (TypeDescription field in map.DataDesc) {
2234-
if (field.Flags != FieldTypeDescFlags.Input && (field.Flags & FieldTypeDescFlags.Output) == 0 && ((field.ExternalName.Length != 0 && stricmp(name, field.ExternalName) == 0) || (field.FieldName.Length != 0 && stricmp(name, field.FieldName) == 0))) {
2234+
if (field.Flags != FieldTypeDescFlags.Input && (field.Flags & FieldTypeDescFlags.Output) == 0 && ((field.ExternalName.Length != 0 && stricmp(name, field.ExternalName) == 0) || (field.FieldName.Length != 0 && stricmp(ReallyUglyHack(name), field.FieldName) == 0))) {
22352235
td = field;
22362236
break;
22372237
}
@@ -2240,7 +2240,7 @@ static int Entity__SetSaveValue(ILuaInterface lua) {
22402240
#if CLIENT_DLL
22412241
for (DataMap? map = ent.GetPredDescMap(); map != null && td == null; map = map.BaseMap) {
22422242
foreach (TypeDescription field in map.DataDesc) {
2243-
if (field.Flags != FieldTypeDescFlags.Input && (field.Flags & FieldTypeDescFlags.Output) == 0 && ((field.ExternalName.Length != 0 && stricmp(name, field.ExternalName) == 0) || (field.FieldName.Length != 0 && stricmp(name, field.FieldName) == 0))) {
2243+
if (field.Flags != FieldTypeDescFlags.Input && (field.Flags & FieldTypeDescFlags.Output) == 0 && ((field.ExternalName.Length != 0 && stricmp(name, field.ExternalName) == 0) || (field.FieldName.Length != 0 && stricmp(ReallyUglyHack(name), field.FieldName) == 0))) {
22442244
td = field;
22452245
break;
22462246
}
@@ -2461,6 +2461,18 @@ static void PushInternalValue(TypeDescription td, object? value) {
24612461
}
24622462
}
24632463

2464+
// FIXME FIXME FIXME
2465+
// datadesc stores c# names rn..
2466+
static ReadOnlySpan<char> ReallyUglyHack(ReadOnlySpan<char> name) {
2467+
if (name.Length < 3 || name[0] != 'm' || name[1] != '_')
2468+
return name;
2469+
name = name[2..];
2470+
int i = 0;
2471+
while (i < name.Length && char.IsLower(name[i]))
2472+
i++;
2473+
return i == name.Length ? name : name[i..];
2474+
}
2475+
24642476
[LuaMethod]
24652477
static int Entity__GetInternalVariable(ILuaInterface lua) {
24662478
BaseEntity ent = Get_Entity(1, false)!;
@@ -2469,7 +2481,7 @@ static int Entity__GetInternalVariable(ILuaInterface lua) {
24692481
TypeDescription? td = null;
24702482
for (DataMap? map = ent.GetDataDescMap(); map != null && td == null; map = map.BaseMap) {
24712483
foreach (TypeDescription field in map.DataDesc) {
2472-
if (field.Flags != FieldTypeDescFlags.Input && (field.Flags & FieldTypeDescFlags.Output) == 0 && ((field.ExternalName.Length != 0 && stricmp(name, field.ExternalName) == 0) || (field.FieldName.Length != 0 && stricmp(name, field.FieldName) == 0))) {
2484+
if (field.Flags != FieldTypeDescFlags.Input && (field.Flags & FieldTypeDescFlags.Output) == 0 && ((field.ExternalName.Length != 0 && stricmp(name, field.ExternalName) == 0) || (field.FieldName.Length != 0 && stricmp(ReallyUglyHack(name), field.FieldName) == 0))) {
24732485
td = field;
24742486
break;
24752487
}
@@ -2478,7 +2490,7 @@ static int Entity__GetInternalVariable(ILuaInterface lua) {
24782490
#if CLIENT_DLL
24792491
for (DataMap? map = ent.GetPredDescMap(); map != null && td == null; map = map.BaseMap) {
24802492
foreach (TypeDescription field in map.DataDesc) {
2481-
if (field.Flags != FieldTypeDescFlags.Input && (field.Flags & FieldTypeDescFlags.Output) == 0 && ((field.ExternalName.Length != 0 && stricmp(name, field.ExternalName) == 0) || (field.FieldName.Length != 0 && stricmp(name, field.FieldName) == 0))) {
2493+
if (field.Flags != FieldTypeDescFlags.Input && (field.Flags & FieldTypeDescFlags.Output) == 0 && ((field.ExternalName.Length != 0 && stricmp(name, field.ExternalName) == 0) || (field.FieldName.Length != 0 && stricmp(ReallyUglyHack(name), field.FieldName) == 0))) {
24822494
td = field;
24832495
break;
24842496
}

‎Game.Shared/GarrysMod/LuaPlayer.cs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,15 @@ static int Player__SendLua(ILuaInterface lua) {
894894
}
895895
return 0;
896896
}
897+
898+
[LuaMethod]
899+
static int Player__PrintMessage(ILuaInterface lua) {
900+
BasePlayer player = Get_Player(1, false)!;
901+
ReadOnlySpan<char> msg = lua.CheckString(3);
902+
HudPrint type = (HudPrint)(int)lua.CheckNumber(2);
903+
Util.ClientPrint(player, type, msg);
904+
return 0;
905+
}
897906
#endif
898907

899908
[LuaMethod]

‎Source.Common/MapUtils.cs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Source.Common.Mathematics;
2+
3+
using System.Numerics;
4+
5+
using static Source.Common.Formats.BSP.BSPFileCommon;
6+
using static Source.Common.Mathematics.MathLib;
7+
using static Source.Common.Mathematics.MathLibConsts;
8+
9+
namespace Source.Common;
10+
11+
public static class MapUtils
12+
{
13+
public static void SetupLightNormalFromProps(in QAngle angles, float angle, float pitch, out Vector3 output) {
14+
output = default;
15+
if (angle == ANGLE_UP) {
16+
output.X = output.Y = 0;
17+
output.Z = 1;
18+
}
19+
else if (angle == ANGLE_DOWN) {
20+
output.X = output.Y = 0;
21+
output.Z = -1;
22+
}
23+
else {
24+
if (angle == 0)
25+
angle = angles[YAW];
26+
27+
output.Z = 0;
28+
29+
SinCos(DEG2RAD(angle), out output.Y, out output.X);
30+
}
31+
32+
if (pitch == 0)
33+
pitch = angles[PITCH];
34+
35+
SinCos(DEG2RAD(pitch), out float sin, out float cos);
36+
37+
output.X *= cos;
38+
output.Y *= cos;
39+
output.Z = sin;
40+
}
41+
}

0 commit comments

Comments
 (0)