Re-organize all projects (#4166)
This commit is contained in:
119
Content.Server/AI/EntitySystems/AiFactionTagSystem.cs
Normal file
119
Content.Server/AI/EntitySystems/AiFactionTagSystem.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.AI.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.AI.EntitySystems
|
||||
{
|
||||
/// <summary>
|
||||
/// Outlines faction relationships with each other for AI.
|
||||
/// </summary>
|
||||
public sealed class AiFactionTagSystem : EntitySystem
|
||||
{
|
||||
/*
|
||||
* Currently factions are implicitly friendly if they are not hostile.
|
||||
* This may change where specified friendly factions are listed. (e.g. to get number of friendlies in area).
|
||||
*/
|
||||
|
||||
private readonly Dictionary<Faction, Faction> _hostileFactions = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
var protoManager = IoCManager.Resolve<IPrototypeManager>();
|
||||
|
||||
foreach (var faction in protoManager.EnumeratePrototypes<AiFactionPrototype>())
|
||||
{
|
||||
if (Enum.TryParse(faction.ID, out Faction @enum))
|
||||
{
|
||||
var parsedFaction = Faction.None;
|
||||
|
||||
foreach (var hostile in faction.Hostile)
|
||||
{
|
||||
if (Enum.TryParse(hostile, out Faction parsedHostile))
|
||||
{
|
||||
parsedFaction |= parsedHostile;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Error($"Unable to parse hostile faction {hostile} for {faction.ID}");
|
||||
}
|
||||
}
|
||||
|
||||
_hostileFactions[@enum] = parsedFaction;
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.Error($"Unable to parse AI faction {faction.ID}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Faction GetHostileFactions(Faction faction) => _hostileFactions.TryGetValue(faction, out var hostiles) ? hostiles : Faction.None;
|
||||
|
||||
public Faction GetFactions(IEntity entity) =>
|
||||
entity.TryGetComponent(out AiFactionTagComponent? factionTags)
|
||||
? factionTags.Factions
|
||||
: Faction.None;
|
||||
|
||||
public IEnumerable<IEntity> GetNearbyHostiles(IEntity entity, float range)
|
||||
{
|
||||
var ourFaction = GetFactions(entity);
|
||||
var hostile = GetHostileFactions(ourFaction);
|
||||
if (ourFaction == Faction.None || hostile == Faction.None)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
foreach (var component in ComponentManager.EntityQuery<AiFactionTagComponent>(true))
|
||||
{
|
||||
if ((component.Factions & hostile) == 0)
|
||||
continue;
|
||||
if (component.Owner.Transform.MapID != entity.Transform.MapID)
|
||||
continue;
|
||||
if (!component.Owner.Transform.MapPosition.InRange(entity.Transform.MapPosition, range))
|
||||
continue;
|
||||
|
||||
yield return component.Owner;
|
||||
}
|
||||
}
|
||||
|
||||
public void MakeFriendly(Faction source, Faction target)
|
||||
{
|
||||
if (!_hostileFactions.TryGetValue(source, out var hostileFactions))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
hostileFactions &= ~target;
|
||||
_hostileFactions[source] = hostileFactions;
|
||||
}
|
||||
|
||||
public void MakeHostile(Faction source, Faction target)
|
||||
{
|
||||
if (!_hostileFactions.TryGetValue(source, out var hostileFactions))
|
||||
{
|
||||
_hostileFactions[source] = target;
|
||||
return;
|
||||
}
|
||||
|
||||
hostileFactions |= target;
|
||||
_hostileFactions[source] = hostileFactions;
|
||||
}
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum Faction
|
||||
{
|
||||
None = 0,
|
||||
NanoTrasen = 1 << 0,
|
||||
SimpleHostile = 1 << 1,
|
||||
SimpleNeutral = 1 << 2,
|
||||
Syndicate = 1 << 3,
|
||||
Xeno = 1 << 4,
|
||||
}
|
||||
}
|
||||
139
Content.Server/AI/EntitySystems/AiSystem.cs
Normal file
139
Content.Server/AI/EntitySystems/AiSystem.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.AI.Components;
|
||||
using Content.Server.AI.Utility.AiLogic;
|
||||
using Content.Shared;
|
||||
using Content.Shared.MobState;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Log;
|
||||
|
||||
namespace Content.Server.AI.EntitySystems
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles NPCs running every tick.
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
internal class AiSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
|
||||
|
||||
/// <summary>
|
||||
/// To avoid iterating over dead AI continuously they can wake and sleep themselves when necessary.
|
||||
/// </summary>
|
||||
private readonly HashSet<AiControllerComponent> _awakeAi = new();
|
||||
|
||||
// To avoid modifying awakeAi while iterating over it.
|
||||
private readonly List<SleepAiMessage> _queuedSleepMessages = new();
|
||||
|
||||
private readonly List<MobStateChangedMessage> _queuedMobStateMessages = new();
|
||||
|
||||
public bool IsAwake(AiControllerComponent npc) => _awakeAi.Contains(npc);
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<SleepAiMessage>(HandleAiSleep);
|
||||
SubscribeLocalEvent<MobStateChangedMessage>(MobStateChanged);
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
UnsubscribeLocalEvent<SleepAiMessage>();
|
||||
UnsubscribeLocalEvent<MobStateChangedMessage>();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
var cvarMaxUpdates = _configurationManager.GetCVar(CCVars.AIMaxUpdates);
|
||||
if (cvarMaxUpdates <= 0)
|
||||
return;
|
||||
|
||||
foreach (var message in _queuedMobStateMessages)
|
||||
{
|
||||
// TODO: Need to generecise this but that will be part of a larger cleanup later anyway.
|
||||
if (message.Entity.Deleted ||
|
||||
!message.Entity.TryGetComponent(out UtilityAi? controller))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
controller.MobStateChanged(message);
|
||||
}
|
||||
|
||||
_queuedMobStateMessages.Clear();
|
||||
|
||||
foreach (var message in _queuedSleepMessages)
|
||||
{
|
||||
switch (message.Sleep)
|
||||
{
|
||||
case true:
|
||||
if (_awakeAi.Count == cvarMaxUpdates && _awakeAi.Contains(message.Component))
|
||||
{
|
||||
Logger.Warning($"Under AI limit again: {_awakeAi.Count - 1} / {cvarMaxUpdates}");
|
||||
}
|
||||
_awakeAi.Remove(message.Component);
|
||||
break;
|
||||
case false:
|
||||
_awakeAi.Add(message.Component);
|
||||
|
||||
if (_awakeAi.Count > cvarMaxUpdates)
|
||||
{
|
||||
Logger.Warning($"AI limit exceeded: {_awakeAi.Count} / {cvarMaxUpdates}");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_queuedSleepMessages.Clear();
|
||||
var toRemove = new List<AiControllerComponent>();
|
||||
var maxUpdates = Math.Min(_awakeAi.Count, cvarMaxUpdates);
|
||||
var count = 0;
|
||||
|
||||
foreach (var npc in _awakeAi)
|
||||
{
|
||||
if (npc.Paused) continue;
|
||||
|
||||
if (npc.Deleted)
|
||||
{
|
||||
toRemove.Add(npc);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (count >= maxUpdates)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
npc.Update(frameTime);
|
||||
count++;
|
||||
}
|
||||
|
||||
foreach (var processor in toRemove)
|
||||
{
|
||||
_awakeAi.Remove(processor);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleAiSleep(SleepAiMessage message)
|
||||
{
|
||||
_queuedSleepMessages.Add(message);
|
||||
}
|
||||
|
||||
private void MobStateChanged(MobStateChangedMessage message)
|
||||
{
|
||||
if (!message.Entity.HasComponent<AiControllerComponent>())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_queuedMobStateMessages.Add(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
30
Content.Server/AI/EntitySystems/ServerAiDebugSystem.cs
Normal file
30
Content.Server/AI/EntitySystems/ServerAiDebugSystem.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Content.Server.AI.LoadBalancer;
|
||||
using Content.Shared.AI;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.AI.EntitySystems
|
||||
{
|
||||
#if DEBUG
|
||||
[UsedImplicitly]
|
||||
public class ServerAiDebugSystem : EntitySystem
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
AiActionRequestJob.FoundAction += NotifyActionJob;
|
||||
}
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
AiActionRequestJob.FoundAction -= NotifyActionJob;
|
||||
}
|
||||
|
||||
private void NotifyActionJob(SharedAiDebug.UtilityAiDebugMessage message)
|
||||
{
|
||||
RaiseNetworkEvent(message);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
Reference in New Issue
Block a user