Skip to content

Commit e9b4596

Browse files
committed
Scope spawn flags in enums, bulk refactor it all
1 parent ae45102 commit e9b4596

36 files changed

Lines changed: 534 additions & 395 deletions

‎Game.Server/AI_BaseNPC.cs‎

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,6 @@ public static class AI_BaseNPCGlobals
7777
{
7878
// TODO: Enum these
7979
// need a good name for its enum type
80-
public const int SF_NPC_WAIT_TILL_SEEN = 1 << 0;
81-
public const int SF_NPC_GAG = 1 << 1;
82-
public const int SF_NPC_FALL_TO_GROUND = 1 << 2;
83-
public const int SF_NPC_DROP_HEALTHKIT = 1 << 3;
84-
public const int SF_NPC_START_EFFICIENT = 1 << 4;
85-
public const int SF_NPC_WAIT_FOR_SCRIPT = 1 << 7;
86-
public const int SF_NPC_LONG_RANGE = 1 << 8;
87-
public const int SF_NPC_FADE_CORPSE = 1 << 9;
88-
public const int SF_NPC_ALWAYSTHINK = 1 << 10;
89-
public const int SF_NPC_TEMPLATE = 1 << 11;
90-
public const int SF_NPC_ALTCOLLISION = 1 << 12;
91-
public const int SF_NPC_NO_WEAPON_DROP = 1 << 13;
92-
public const int SF_NPC_NO_PLAYER_PUSHAWAY = 1 << 14;
9380

9481
public const string PLAYER_SQUADNAME = "player_squad";
9582

@@ -334,6 +321,24 @@ public bool EnumEntity(IHandleEntity? handleEntity) {
334321
[NetworkName("CAI_BaseNPC")]
335322
public class AI_BaseNPC : BaseCombatCharacter, IAI_MovementSink
336323
{
324+
[Flags]
325+
public enum SF
326+
{
327+
WaitTillSeen = 1 << 0,
328+
Gag = 1 << 1,
329+
FallToGround = 1 << 2,
330+
DropHealthkit = 1 << 3,
331+
StartEfficient = 1 << 4,
332+
WaitForScript = 1 << 7,
333+
LongRange = 1 << 8,
334+
FadeCorpse = 1 << 9,
335+
AlwaysThink = 1 << 10,
336+
Template = 1 << 11,
337+
AltCollision = 1 << 12,
338+
NoWeaponDrop = 1 << 13,
339+
NoPlayerPushaway = 1 << 14,
340+
}
341+
337342
public static ReadOnlySpan<char> GetActivityName(Activity actID) {
338343
if (actID == Activity.ACT_INVALID)
339344
return "ACT_INVALID";
@@ -2638,7 +2643,7 @@ public static void InitSchedulingTables() {
26382643

26392644
public NPCState GetIdealState() => IdealNPCState;
26402645

2641-
public override bool IsTemplate() => HasSpawnFlags(SF_NPC_TEMPLATE);
2646+
public override bool IsTemplate() => HasSpawnFlags(SF.Template);
26422647

26432648
public static bool FindSpotForNPCInRadius(out Vector3 result, in Vector3 startPos, AI_BaseNPC npc, float radius, bool outOfPlayerViewcone = false) {
26442649
BasePlayer? player = npc.AI_GetClosestPlayer();
@@ -2699,7 +2704,7 @@ public bool CineCleanup() {
26992704
throw new NotImplementedException();
27002705

27012706
if (oldCine != null && !string.IsNullOrEmpty(oldCine.Play) && oldCine.PlayedSequence()) {
2702-
if (!oldCine.HasSpawnFlags(AI_ScriptedSequence.SF_SCRIPT_DONT_TELEPORT_AT_END)) {
2707+
if (!oldCine.HasSpawnFlags(AI_ScriptedSequence.SF.DontTeleportAtEnd)) {
27032708
GetBonePosition(0, out Vector3 newOrigin, out _);
27042709

27052710
Vector3 oldOrigin = GetLocalOrigin();
@@ -2757,7 +2762,7 @@ public bool CineCleanup() {
27572762
SetCondition((int)SCOND_t.COND_LIGHT_DAMAGE);
27582763
}
27592764

2760-
RemoveSpawnFlags(SF_NPC_WAIT_FOR_SCRIPT);
2765+
RemoveSpawnFlags(SF.WaitForScript);
27612766

27622767
if (destroyCine)
27632768
Util.Remove(oldCine);
@@ -3584,7 +3589,7 @@ public void UnlockBestSound() {
35843589
public AI_MoveEfficiency GetMoveEfficiency() => MoveEfficiency;
35853590
public void SetMoveEfficiency(AI_MoveEfficiency efficiency) => MoveEfficiency = efficiency;
35863591

3587-
public bool IsFlaggedEfficient() => HasSpawnFlags(SF_NPC_START_EFFICIENT);
3592+
public bool IsFlaggedEfficient() => HasSpawnFlags(SF.StartEfficient);
35883593

35893594
public void RemoveSleepFlags(AI_SleepFlags flags) => SleepFlags &= ~flags;
35903595
public bool HasSleepFlags(AI_SleepFlags flags) => (SleepFlags & flags) == flags;
@@ -5956,7 +5961,7 @@ public virtual bool ShouldPlayerAvoid() {
59565961
if (IsInLockedScene() == true)
59575962
return true;
59585963

5959-
if (HasSpawnFlags(SF_NPC_ALTCOLLISION))
5964+
if (HasSpawnFlags(SF.AltCollision))
59605965
return true;
59615966

59625967
return false;
@@ -7168,7 +7173,7 @@ public void CheckOnGround() {
71687173

71697174
public virtual bool ShouldPlayIdleSound() {
71707175
if ((NPCState == NPCState.Idle || NPCState == NPCState.Alert) &&
7171-
RandomInt(0, 99) == 0 && !HasSpawnFlags(SF_NPC_GAG)) {
7176+
RandomInt(0, 99) == 0 && !HasSpawnFlags(SF.Gag)) {
71727177
return true;
71737178
}
71747179

@@ -7187,7 +7192,7 @@ public bool FOkToMakeSound(SentencePriority soundPriority = SentencePriority.Nor
71877192
}
71887193
}
71897194

7190-
if (HasSpawnFlags(SF_NPC_GAG) && (NPCState != NPCState.Combat))
7195+
if (HasSpawnFlags(SF.Gag) && (NPCState != NPCState.Combat))
71917196
return false;
71927197

71937198
return true;
@@ -7205,7 +7210,7 @@ public virtual void DeathSound(in TakeDamageInfo info) { }
72057210
public virtual void LostEnemySound() { }
72067211
public virtual void FoundEnemySound() { }
72077212

7208-
public virtual bool ShouldAlwaysThink() => HasSpawnFlags(SF_NPC_ALWAYSTHINK);
7213+
public virtual bool ShouldAlwaysThink() => HasSpawnFlags(SF.AlwaysThink);
72097214

72107215
static readonly int[] SenseConditionsToClear = [
72117216
(int)SCOND_t.COND_SEE_HATE,
@@ -7522,7 +7527,7 @@ public virtual void NPCInit() {
75227527
DistTooFar = 1024.0f;
75237528
SetDistLook(2048.0f);
75247529

7525-
if (HasSpawnFlags(SF_NPC_LONG_RANGE)) {
7530+
if (HasSpawnFlags(SF.LongRange)) {
75267531
DistTooFar = 1e9f;
75277532
SetDistLook(6000.0f);
75287533
}
@@ -7553,15 +7558,15 @@ public virtual void NPCInit() {
75537558

75547559
ForceGatherConditions();
75557560

7556-
if (HasSpawnFlags(SF_NPC_WAIT_FOR_SCRIPT)) {
7561+
if (HasSpawnFlags(SF.WaitForScript)) {
75577562
string? startSequence = AI_ScriptedSequence.GetSpawnPreIdleSequenceForScript(this);
75587563
if (startSequence != null)
75597564
SetSequence(LookupSequence(startSequence));
75607565
}
75617566

75627567
CreateVPhysics();
75637568

7564-
if (HasSpawnFlags(SF_NPC_START_EFFICIENT))
7569+
if (HasSpawnFlags(SF.StartEfficient))
75657570
SetEfficiency(AI_Efficiency.Efficient);
75667571

75677572
FadeCorpse = ShouldFadeOnDeath();
@@ -7606,7 +7611,7 @@ public virtual void PostNPCInit() { }
76067611
public virtual void StartNPC() {
76077612
if ((GetMoveType() != Source.MoveType.Fly) && (GetMoveType() != Source.MoveType.FlyGravity) &&
76087613
(CapabilitiesGet() & Server.Capability.MoveFly) == 0 &&
7609-
!HasSpawnFlags(SF_NPC_FALL_TO_GROUND) && !IsWaitingToRappel() && GetMoveParent() == null) {
7614+
!HasSpawnFlags(SF.FallToGround) && !IsWaitingToRappel() && GetMoveParent() == null) {
76107615
Vector3 origin = GetLocalOrigin();
76117616

76127617
if (!GetMoveProbe()!.FloorPoint(origin + new Vector3(0, 0, 0.1f), Mask.NPCSolid, 0, -2048, out origin)) {
@@ -7649,7 +7654,7 @@ public virtual void StartNPC() {
76497654
ScriptArrivalActivity = AIN_DEF_ACTIVITY;
76507655
ScriptArrivalSequence = null;
76517656

7652-
if (HasSpawnFlags(SF_NPC_WAIT_FOR_SCRIPT)) {
7657+
if (HasSpawnFlags(SF.WaitForScript)) {
76537658
SetState(NPCState.Idle);
76547659
Activity = IdealActivity;
76557660
IdealSequence = GetSequence();
@@ -8172,7 +8177,7 @@ public bool ConditionInterruptsCurSchedule(int condition) {
81728177
}
81738178

81748179
public void TestPlayerPushing(BaseEntity? entity) {
8175-
if (HasSpawnFlags(SF_NPC_NO_PLAYER_PUSHAWAY))
8180+
if (HasSpawnFlags(SF.NoPlayerPushaway))
81768181
return;
81778182

81788183
BasePlayer? player = ToBasePlayer(entity);
@@ -8846,7 +8851,7 @@ public void UpdateSleepState(bool inPVS) {
88468851
}
88478852
}
88488853

8849-
if ((GetSoundInterests() & (int)SoundInstanceType.Danger) != 0 && !HasSpawnFlags(SF_NPC_WAIT_TILL_SEEN)) {
8854+
if ((GetSoundInterests() & (int)SoundInstanceType.Danger) != 0 && !HasSpawnFlags(SF.WaitTillSeen)) {
88508855
int sound = SoundEnt.ActiveList();
88518856

88528857
while (sound != SOUNDLIST_EMPTY) {

‎Game.Server/AI_GoalPolice.cs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ namespace Game.Server;
1313
[LinkEntityToClass("ai_goal_police")]
1414
public class AI_PoliceGoal : BaseEntity
1515
{
16-
public const int SF_POLICE_GOAL_KNOCKOUT_BEHIND = 1 << 1;
17-
public const int SF_POLICE_GOAL_DO_NOT_LEAVE_POST = 1 << 2;
16+
[Flags]
17+
public enum SF
18+
{
19+
KnockoutBehind = 1 << 1,
20+
DoNotLeavePost = 1 << 2,
21+
}
1822

1923
public static readonly new DataMap DataDesc = new(typeof(AI_PoliceGoal), BaseEntity.DataDesc, [
2024
DEFINE.KEYFIELD(nameof(Radius), FieldType.Float, "PoliceRadius"),
@@ -59,7 +63,7 @@ public bool ShouldKnockOutTarget(in Vector3 targetPos, bool targetVisible) {
5963
if (OverrideKnockOut)
6064
return true;
6165

62-
if (HasSpawnFlags(SF_POLICE_GOAL_KNOCKOUT_BEHIND) == false)
66+
if (HasSpawnFlags(SF.KnockoutBehind) == false)
6367
return false;
6468

6569
if (!targetVisible)
@@ -78,7 +82,7 @@ public bool ShouldKnockOutTarget(in Vector3 targetPos, bool targetVisible) {
7882

7983
public void KnockOutTarget(BaseEntity? target) => OnKnockOut.FireOutput(target, this);
8084

81-
public bool ShouldRemainAtPost() => HasSpawnFlags(SF_POLICE_GOAL_DO_NOT_LEAVE_POST);
85+
public bool ShouldRemainAtPost() => HasSpawnFlags(SF.DoNotLeavePost);
8286

8387
public void FireWarningLevelOutput(int level) {
8488
switch (level) {

‎Game.Server/AI_Hint.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,11 @@ static bool InZone(List<HintZone> zone, in Vector3 testPosition) {
233233
[LinkEntityToClass("ai_hint")]
234234
public class AI_Hint : ServerOnlyEntity
235235
{
236-
public const int SF_ALLOW_JUMP_UP = 65536;
236+
[Flags]
237+
public enum SF
238+
{
239+
AllowJumpUp = 65536,
240+
}
237241

238242
public static readonly new DataMap DataDesc = new(typeof(AI_Hint), BaseEntity.DataDesc, [
239243
DEFINE.EMBEDDED(nameof(NodeData)),

‎Game.Server/AI_Pathfinder.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ public bool IsLinkUsable(ref AI_Link link, int startID) {
424424
if (startHint != null && endHint != null) {
425425
if (startHint.HintType() == Hint.JumpOverride &&
426426
endHint.HintType() == Hint.JumpOverride &&
427-
(((startHint.SpawnFlags | endHint.SpawnFlags) & AI_Hint.SF_ALLOW_JUMP_UP) != 0 || startHint.GetAbsOrigin().Z > endHint.GetAbsOrigin().Z)) {
427+
(startHint.HasSpawnFlags(AI_Hint.SF.AllowJumpUp) || endHint.HasSpawnFlags(AI_Hint.SF.AllowJumpUp) ||startHint.GetAbsOrigin().Z > endHint.GetAbsOrigin().Z)) {
428428
if (!startNode.IsLocked()) {
429429
if (startHint.GetTargetNode() == -1 || startHint.GetTargetNode() == endID)
430430
moveType = Capability.MoveJump;

‎Game.Server/AI_ScriptConditions.cs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ public class AI_ScriptConditionsElement
6767
[LinkEntityToClass("ai_script_conditions")]
6868
public class AI_ScriptConditions : BaseEntity, IEntityListener
6969
{
70-
const int SF_ACTOR_AS_ACTIVATOR = 1 << 0;
70+
[Flags]
71+
public enum SF
72+
{
73+
ActorAsActivator = 1 << 0,
74+
}
7175

7276
static readonly ConVar debugscriptconditions = new("ai_debugscriptconditions", "0");
7377

@@ -419,7 +423,7 @@ void EvaluationThink() {
419423
BaseEntity activator = this;
420424

421425
#if HL2_EPISODIC
422-
if (actor != null && HasSpawnFlags(SF_ACTOR_AS_ACTIVATOR))
426+
if (actor != null && HasSpawnFlags(SF.ActorAsActivator))
423427
activator = actor;
424428
#endif
425429

‎Game.Server/AI_Senses.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void Listen() {
8787

8888
int soundMask = GetOuter()!.GetSoundInterests();
8989

90-
if (soundMask != (int)SoundInstanceType.None && !GetOuter()!.HasSpawnFlags(SF_NPC_WAIT_TILL_SEEN)) {
90+
if (soundMask != (int)SoundInstanceType.None && !GetOuter()!.HasSpawnFlags(AI_BaseNPC.SF.WaitTillSeen)) {
9191
int iSound = SoundEnt.ActiveList();
9292

9393
while (iSound != SOUNDLIST_EMPTY) {
@@ -115,7 +115,7 @@ public bool ShouldSeeEntity(BaseEntity sightEnt) {
115115
if (sightEnt.IsPlayer() && g_IgnorePlayers.GetBool())
116116
return false;
117117

118-
if (sightEnt.HasSpawnFlags(SF_NPC_WAIT_TILL_SEEN))
118+
if (sightEnt.HasSpawnFlags(AI_BaseNPC.SF.WaitTillSeen))
119119
return false;
120120

121121
if (!sightEnt.CanBeSeenBy(GetOuter()!))
@@ -151,13 +151,13 @@ void NoteSeenEntity(BaseEntity sightEnt) {
151151
}
152152

153153
bool WaitingUntilSeen(BaseEntity sightEnt) {
154-
if (GetOuter()!.HasSpawnFlags(SF_NPC_WAIT_TILL_SEEN)) {
154+
if (GetOuter()!.HasSpawnFlags(AI_BaseNPC.SF.WaitTillSeen)) {
155155
if (sightEnt.IsPlayer()) {
156156
BasePlayer? player = ToBasePlayer(sightEnt);
157157
if (player != null
158158
&& player.FInViewCone(GetOuter()!)
159159
&& FBoxVisible(sightEnt, GetOuter()!, out _)) {
160-
GetOuter()!.RemoveSpawnFlags(SF_NPC_WAIT_TILL_SEEN);
160+
GetOuter()!.RemoveSpawnFlags(AI_BaseNPC.SF.WaitTillSeen);
161161
return false;
162162
}
163163
}

‎Game.Server/AI_Sentence.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ bool MatchesCriteria(SentenceCriteria criteria) {
7171
return true;
7272

7373
case SentenceCriteria.Normal:
74-
return (GetOuter()!.GetState() == NPCState.Combat) || (GetOuter()!.HasSpawnFlags(SF_NPC_GAG) == false);
74+
return (GetOuter()!.GetState() == NPCState.Combat) || (GetOuter()!.HasSpawnFlags(AI_BaseNPC.SF.Gag) == false);
7575

7676
case SentenceCriteria.InSquad:
77-
if ((GetOuter()!.GetState() != NPCState.Combat) && GetOuter()!.HasSpawnFlags(SF_NPC_GAG))
77+
if ((GetOuter()!.GetState() != NPCState.Combat) && GetOuter()!.HasSpawnFlags(AI_BaseNPC.SF.Gag))
7878
return false;
7979
return GetOuter()!.GetSquad() != null && (GetOuter()!.GetSquad()!.NumMembers() > 1);
8080

8181
case SentenceCriteria.SquadLeader: {
82-
if ((GetOuter()!.GetState() != NPCState.Combat) && GetOuter()!.HasSpawnFlags(SF_NPC_GAG))
82+
if ((GetOuter()!.GetState() != NPCState.Combat) && GetOuter()!.HasSpawnFlags(AI_BaseNPC.SF.Gag))
8383
return false;
8484

8585
AI_Squad? squad = GetOuter()!.GetSquad();

‎Game.Server/BaseCombatCharacter.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ public bool Weapon_IsOnGround(BaseCombatWeapon weapon) {
492492

493493
float curDist = (weapon.GetLocalOrigin() - GetLocalOrigin()).Length();
494494

495-
if (weapon.HasSpawnFlags(BaseCombatWeapon.SF_WEAPON_NO_PLAYER_PICKUP))
495+
if (weapon.HasSpawnFlags(BaseCombatWeapon.SF.NoPlayerPickup))
496496
curDist *= 0.5f;
497497

498498
if (bestWeapon != null) {
@@ -584,7 +584,7 @@ public virtual void Weapon_Equip(BaseCombatWeapon weapon) {
584584
}
585585

586586
if (IsPlayer() == false) {
587-
if (HasSpawnFlags(AI_BaseNPCGlobals.SF_NPC_LONG_RANGE)) {
587+
if (HasSpawnFlags(AI_BaseNPC.SF.LongRange)) {
588588
ActiveWeapon.Get()!.MaxRange1 = 999999999;
589589
ActiveWeapon.Get()!.MaxRange2 = 999999999;
590590
}

‎Game.Server/BaseCombatWeapon.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static int GetAvailableWeaponsInBox(Span<BaseCombatWeapon?> list, in Vect
7171

7272
public override EntityCapabilities ObjectCaps() {
7373
EntityCapabilities caps = base.ObjectCaps();
74-
if (!IsFollowingEntity() && !HasSpawnFlags(SF_WEAPON_NO_PLAYER_PICKUP))
74+
if (!IsFollowingEntity() && !HasSpawnFlags(BaseCombatWeapon.SF.NoPlayerPickup))
7575
caps |= EntityCapabilities.ImpulseUse;
7676

7777
return caps;
@@ -234,7 +234,7 @@ public void Materialize() {
234234
DoMuzzleFlash();
235235
}
236236

237-
if (HasSpawnFlags(BasePlayer.SF_NORESPAWN) == false) {
237+
if (HasSpawnFlags(BasePlayer.SF.NoRespawn) == false) {
238238
VPhysicsInitNormal(SolidType.BBox, GetSolidFlags() | SolidFlags.Trigger, false);
239239
SetMoveType(Source.MoveType.VPhysics);
240240
}

‎Game.Server/EnvFade.cs‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,20 @@ public class EnvFade : LogicalEntity
3232
public void SetDuration(float duration) => Duration = duration;
3333
public void SetHoldTime(float hold) => HoldTime = hold;
3434

35-
/// <summary>
36-
/// Fade in, not out
37-
/// </summary>
38-
const int SF_FADE_IN = 0x0001;
39-
/// <summary>
40-
/// Modulate, don't blend
41-
/// </summary>
42-
const int SF_FADE_MODULATE = 0x0002;
43-
const int SF_FADE_ONLYONE = 0x0004;
44-
const int SF_FADE_STAYOUT = 0x0008;
35+
[Flags]
36+
public enum SF
37+
{
38+
/// <summary>
39+
/// Fade in, not out
40+
/// </summary>
41+
In = 0x0001,
42+
/// <summary>
43+
/// Modulate, don't blend
44+
/// </summary>
45+
Modulate = 0x0002,
46+
OnlyOne = 0x0004,
47+
StayOut = 0x0008,
48+
}
4549

4650
public override void Spawn() { }
4751

@@ -51,18 +55,18 @@ public override void Spawn() { }
5155
public void InputFade(InputData inputdata) {
5256
FadeFlags fadeFlags = 0;
5357

54-
if (HasSpawnFlags(SF_FADE_IN))
58+
if (HasSpawnFlags(SF.In))
5559
fadeFlags |= FadeFlags.In;
5660
else
5761
fadeFlags |= FadeFlags.Out;
5862

59-
if (HasSpawnFlags(SF_FADE_MODULATE))
63+
if (HasSpawnFlags(SF.Modulate))
6064
fadeFlags |= FadeFlags.Modulate;
6165

62-
if (HasSpawnFlags(SF_FADE_STAYOUT))
66+
if (HasSpawnFlags(SF.StayOut))
6367
fadeFlags |= FadeFlags.StayOut;
6468

65-
if (HasSpawnFlags(SF_FADE_ONLYONE)) {
69+
if (HasSpawnFlags(SF.OnlyOne)) {
6670
if (inputdata.Activator != null && inputdata.Activator.IsNetClient())
6771
Util.ScreenFade(inputdata.Activator, ColorRender, GetDuration(), GetHoldTime(), fadeFlags);
6872
}

0 commit comments

Comments
 (0)