Skip to content

Fix NPC crashes in combat AI - #284

Merged
marchc1 merged 8 commits into
SourceDotNET:mainfrom
Yahzy:fix/npc-crashes
Nov 21, 2026
Merged

marchc1 merged 8 commits into
SourceDotNET:mainfrom
Yahzy:fix/npc-crashes

Conversation

@Yahzy

@Yahzy Yahzy commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Spawning npc_gman, an armed npc_metropolice, or a npc_citizen that acquires an enemy crashed the game with a NotImplementedException from the AI think. This ports the missing pieces from the 2013 SDK:

  • AI_FollowBehavior: CanSelectSchedule, ShouldFollow, GetFollowTarget
  • NPC_MetroPolice: SelectCombatSchedule, SelectScheduleNewEnemy, SelectScheduleArrestEnemy, SelectScheduleNoDirectEnemy, SelectRangeAttackSchedule, TryToEnterPistolSlot, AnnounceEnemyType
  • AI_BaseNPC: SelectCombatSchedule, GatherAttackConditions, Range/MeleeAttack1/2Conditions, WeaponLOSCondition, CurrentWeaponLOSCondition, InnateWeaponLOSCondition, PlayerInSpread, IsWeaponStateChanging, IsCoverPosition, IsValidCover, GetCoverActivity, FearSound, UseAttackSquadSlots
  • BaseCombatWeapon.WeaponLOSCondition and WeaponLOSFilter
  • AI_TacticalServices: FindCoverPos, FindCoverNode, GetNodePos
  • RandSimTimer (CRandSimTimer) and the ai_debug_shoot_positions convar

Notes:

  • WeaponLOSFilter does not let traces pass through breakable glass yet, func_breakable has no material type so far (left a todo).
  • AI_Squad.GetLeader is a stub like the rest of AI_Squad. Squads are never created at the moment, so those paths are not reached.
  • SelectCombatSchedule and GatherAttackConditions are no longer virtual, matching the SDK (NPC_MetroPolice hides SelectCombatSchedule there too).
  • NPCs no longer crash but still mostly stand still instead of fighting, that is a separate issue.

Tested: all four configurations build and Source.Common.Tests pass. On gm_construct I spawned every entry of the NPC, Vehicles, SpawnableEntities and Weapon lists and used every tool mode: 2 crashes before (npc_gman, npc_metropolice), none after. Rebels against metro police for 25 seconds also run without any exception.

Port AI_FollowBehavior.CanSelectSchedule, NPC_MetroPolice combat schedule
selection and AI_BaseNPC.GatherAttackConditions with the weapon line of
sight checks. Spawning npc_gman or an armed npc_metropolice no longer
throws NotImplementedException from the AI think.
Implement AI_TacticalServices.FindCoverPos/FindCoverNode and the
AI_BaseNPC cover helpers (IsCoverPosition, IsValidCover,
GetCoverActivity) so NPCs that take cover no longer crash.
Citizens and other NPCs relying on the base combat schedule selection
crashed as soon as they acquired an enemy.
SelectCombatSchedule and GatherAttackConditions are not virtual in the SDK
(NPC_MetroPolice hides SelectCombatSchedule), PlayerInSpread is. Track
visited nodes in a stack buffer during the cover search like the
pathfinder does.
so we get FLT_EPSILON
@Yahzy

Yahzy commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

I think you forgot the ; 😭

@marchc1 marchc1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine, but @ZehMatt should take a pass over this too.

@marchc1
marchc1 merged commit fc01a9d into SourceDotNET:main Nov 21, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants