Skip to content
Merged
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
392 changes: 388 additions & 4 deletions Game.Server/AI_BaseNPC.cs

Large diffs are not rendered by default.

36 changes: 34 additions & 2 deletions Game.Server/AI_FollowBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

using Game.Shared;

using Source;

namespace Game.Server;

public static class AI_FollowBehaviorGlobals
Expand Down Expand Up @@ -43,11 +45,38 @@ public class AI_FollowManager

public class AI_FollowBehavior : AI_Behavior_AI_BaseNPC_100000
{
public AI_FollowBehavior() { }
public AI_FollowBehavior() {
FollowDelay.Set(1.0f, 3.0f);
}

public override string GetName() => "Follow";

public override bool CanSelectSchedule() => throw new NotImplementedException();
public BaseEntity? GetFollowTarget() => FollowTarget.Get();

public override bool CanSelectSchedule() {
if (!GetOuter()!.IsInterruptable())
return false;

if (!ShouldFollow())
return false;

return true;
}

protected virtual bool ShouldFollow() {
if (GetFollowTarget() == null)
return false;

if ((GetFollowTarget()!.GetFlags() & EntityFlags.NoTarget) != 0)
return false;

if (FollowNavFailed && FollowDelay.IsRunning() && !FollowDelay.Expired())
return false;

FollowNavFailed = false;

return true;
}

public override void GatherConditions() => throw new NotImplementedException();

Expand All @@ -67,4 +96,7 @@ protected override void Precache() {

protected AI_FollowManagerInfoHandle_t FollowManagerInfo;
protected AI_FollowParams Params = new();

bool FollowNavFailed;
readonly RandStopwatch FollowDelay = new();
}
1 change: 1 addition & 0 deletions Game.Server/AI_Squad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class AI_Squad
public bool FOkToMakeSound(SentencePriority soundPriority) => throw new NotImplementedException();
public void JustMadeSound(SentencePriority soundPriority, TimeUnit_t time) => throw new NotImplementedException();
public bool IsLeader(AI_BaseNPC npc) => throw new NotImplementedException();
public AI_BaseNPC? GetLeader() => throw new NotImplementedException();
public void SquadRemember(AI_MemoryFlags memory) => throw new NotImplementedException();
public void BroadcastInteraction(AI_Interaction interactionType, object? data, BaseCombatCharacter? sender) => throw new NotImplementedException();
public AI_BaseNPC? GetFirstMember(out int iter) => throw new NotImplementedException();
Expand Down
117 changes: 116 additions & 1 deletion Game.Server/AI_TacticalServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,122 @@ public void Init(AI_Network? network) {
Assert(Pathfinder != null);
}

public bool FindCoverPos(in Vector3 threatPos, in Vector3 threatEyePos, float minDist, float maxDist, out Vector3 result) => throw new NotImplementedException();
public bool FindCoverPos(in Vector3 threatPos, in Vector3 threatEyePos, float minDist, float maxDist, out Vector3 result)
=> FindCoverPos(GetLocalOrigin(), threatPos, threatEyePos, minDist, maxDist, out result);

public bool FindCoverPos(in Vector3 nearPos, in Vector3 threatPos, in Vector3 threatEyePos, float minDist, float maxDist, out Vector3 result) {
int node = FindCoverNode(nearPos, threatPos, threatEyePos, minDist, maxDist);

if (node == NO_NODE) {
result = default;
return false;
}

result = GetNodePos(node);
return true;
}

static int SearchRandomizer;
static readonly PriorityQueue<int, float> NodeList = new();

public int FindCoverNode(in Vector3 threatPos, in Vector3 threatEyePos, float minDist, float maxDist)
=> FindCoverNode(GetLocalOrigin(), threatPos, threatEyePos, minDist, maxDist);

public int FindCoverNode(in Vector3 nearPos, in Vector3 threatPos, in Vector3 threatEyePos, float minDist, float maxDist) {
if (!AI_NetworkManager.NetworksLoaded())
return NO_NODE;

AI_BaseNPC outer = GetOuter()!;
int myNode = Pathfinder!.NearestNodeToPoint(nearPos);

if (myNode == NO_NODE) {
Vector3 pos = outer.GetAbsOrigin();
DevWarning(2, $"FindCover() - {outer.GetClassname()} has no nearest node! (Check near {pos.X} {pos.Y} {pos.Z})\n");
return NO_NODE;
}

if (maxDist == 0)
maxDist = 784;

if (minDist > 0.5f * maxDist)
minDist = 0.5f * maxDist;

int numNodes = Network!.NumNodes();
PriorityQueue<int, float> list = NodeList;
list.Clear();
Span<bool> wasVisited = stackalloc bool[numNodes];

list.Enqueue(myNode, 0);
wasVisited[myNode] = true;
float minDistSqr = minDist * minDist;
float maxDistSqr = maxDist * maxDist;

while (list.Count > 0) {
int nodeIndex = list.Dequeue();

AI_Node node = Network.GetNode(nodeIndex)!;
Vector3 nodeOrigin = node.GetPosition(GetHullType());

float dist = (nearPos - nodeOrigin).LengthSquared();
if (dist >= minDistSqr && dist < maxDistSqr) {
Activity coverActivity = outer.GetCoverActivity(node.GetHint());
Vector3 eyePos = nodeOrigin + outer.EyeOffset(coverActivity);

if (outer.IsValidCover(nodeOrigin, node.GetHint())) {
if (outer.IsCoverPosition(threatEyePos, eyePos)) {
node.Lock(1.0);

AI_Hint? hint = node.GetHint();
if (hint != null && (hint.HintType() == Hint.TacticalCoverMed || hint.HintType() == Hint.TacticalCoverLow)) {
if (outer.GetHintNode() != null) {
outer.GetHintNode()!.Unlock(outer.GetHintDelay(outer.GetHintNode()!.HintType()));
outer.SetHintNode(null);
}

outer.SetHintNode(hint);
}

SearchRandomizer = nodeIndex;
return nodeIndex;
}
}
}

int numLinks = node.NumLinks();
for (int link = 0; link < numLinks; link++) {
int index = (link + SearchRandomizer) % numLinks;
ref AI_Link nodeLink = ref node.GetLinkByIndex(index);

if (!Pathfinder.IsLinkUsable(ref nodeLink, myNode))
continue;

int newID = nodeLink.DestNodeID(nodeIndex);

if (!wasVisited[newID]) {
AI_Node newNode = Network.GetNode(newID)!;
if (newNode.NodeType != NodeType.Climb && !newNode.IsLocked()) {
nodeOrigin = newNode.GetPosition(GetHullType());
dist = (nearPos - nodeOrigin).LengthSquared();

// Use the distance to the threat as a heuristic so the NPC does not run towards it
float threatDist = (threatPos - nodeOrigin).LengthSquared();

if (dist < threatDist * 1.5f)
list.Enqueue(newID, dist);
}
wasVisited[newID] = true;
}
}
}

outer.ClearHintNode();
return NO_NODE;
}

public Vector3 GetNodePos(int node) => Network!.GetNode(node)!.GetPosition(GetHullType());

Vector3 GetLocalOrigin() => GetOuter()!.GetLocalOrigin();
AI_HullType GetHullType() => GetOuter()!.GetHullType();

public AI_Network? Network;
public AI_Pathfinder? Pathfinder;
Expand Down
78 changes: 78 additions & 0 deletions Game.Server/BaseCombatWeapon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,59 @@ public override void HandleAnimEvent(ref AnimEvent animEvent) {

public virtual void Operator_ForceNPCFire(BaseCombatCharacter op, bool secondary) { }

public virtual bool WeaponLOSCondition(in Vector3 ownerPos, in Vector3 targetPos, bool setConditions) {
AI_BaseNPC npcOwner = GetOwner()!.MyNPCPointer()!;

Vector3 relativeShootPosition = npcOwner.Weapon_ShootPosition() - npcOwner.GetAbsOrigin();
Vector3 barrelPos = ownerPos + relativeShootPosition;

WeaponLOSFilter traceFilter = new(GetOwner(), npcOwner.GetEnemy(), Source.CollisionGroup.BreakableGlass);
Util.TraceLine(barrelPos, targetPos, Mask.Shot, ref traceFilter, out Trace tr);

if (tr.Fraction == 1.0f) {
if (ai_debug_shoot_positions.GetBool())
DebugOverlay.Line(barrelPos, targetPos, 0, 255, 0, false, 1.0f);

return true;
}

BaseEntity? hitEnt = tr.Ent;

BasePlayer? enemyPlayer = ToBasePlayer(npcOwner.GetEnemy());
if (enemyPlayer != null && enemyPlayer.IsInAVehicle()) {
BaseEntity? vehicleEnt = enemyPlayer.GetVehicle()!.GetVehicleEnt();
if (hitEnt == vehicleEnt || hitEnt?.GetOwnerEntity() == vehicleEnt)
return true;
}

if (hitEnt == npcOwner.GetEnemy()) {
if (ai_debug_shoot_positions.GetBool())
DebugOverlay.Line(barrelPos, targetPos, 0, 255, 0, false, 1.0f);

return true;
}

IServerVehicle? vehicle = hitEnt?.GetServerVehicle();
BaseCombatCharacter? bcc = vehicle != null ? vehicle.GetPassenger() : ToBaseCombatCharacter(hitEnt);

if (bcc != null) {
if (npcOwner.IRelationType(bcc) == Disposition.HT)
return true;

if (setConditions)
npcOwner.SetCondition((int)SCOND_t.COND_WEAPON_BLOCKED_BY_FRIEND);
}
else if (setConditions) {
npcOwner.SetCondition((int)SCOND_t.COND_WEAPON_SIGHT_OCCLUDED);
npcOwner.SetEnemyOccluder(hitEnt);

if (ai_debug_shoot_positions.GetBool())
DebugOverlay.Line(tr.StartPos, tr.EndPos, 255, 0, 0, false, 1.0f);
}

return false;
}

public virtual SCOND_t WeaponRangeAttack1Condition(float dot, float dist) {
if (UsesPrimaryAmmo() && !HasPrimaryAmmo())
return SCOND_t.COND_NO_PRIMARY_AMMO;
Expand Down Expand Up @@ -314,3 +367,28 @@ public void CheckRespawn() {

public override bool IsBaseCombatWeapon() => true;
}

public struct WeaponLOSFilter : ITraceFilter
{
TraceFilterSkipTwoEntities SkipTwo;
readonly BaseEntity? Vehicle;

public WeaponLOSFilter(IHandleEntity? handleEntity, IHandleEntity? handleEntity2, CollisionGroup collisionGroup) {
SkipTwo = new(handleEntity, handleEntity2, collisionGroup);
Vehicle = (handleEntity as BaseEntity)?.MyCombatCharacterPointer()?.GetVehicleEntity();
}

public bool ShouldHitEntity(IHandleEntity handleEntity, Contents contentsMask) {
BaseEntity? entity = EntityFromEntityHandle(handleEntity);

if (entity != null && entity.GetCollisionGroup() == CollisionGroup.Weapon)
return false;

if (Vehicle != null && entity == Vehicle)
return false;

// todo: let the trace pass through breakable glass once func_breakable materials are ported

return SkipTwo.ShouldHitEntity(handleEntity, contentsMask);
}
}
Loading
Loading