Merge branch 'master' into 2021-12-03-remove-IEntity-komm-süsser-todd
# Conflicts: # Content.Client/Crayon/CrayonDecalVisualizer.cs # Content.Client/Tabletop/TabletopSystem.cs # Content.IntegrationTests/Tests/InventoryHelpersTest.cs # Content.Server/AI/EntitySystems/AiSystem.cs # Content.Server/AI/Utility/AiLogic/UtilityAI.cs # Content.Server/AME/AMENodeGroup.cs # Content.Server/Administration/AdminVerbSystem.cs # Content.Server/Body/Systems/RespiratorSystem.cs # Content.Server/Chemistry/Components/InjectorComponent.cs # Content.Server/Chemistry/TileReactions/CleanTileReaction.cs # Content.Server/Chemistry/TileReactions/SpillTileReaction.cs # Content.Server/Crayon/CrayonComponent.cs # Content.Server/Doors/Components/ServerDoorComponent.cs # Content.Server/Explosion/EntitySystems/TriggerSystem.cs # Content.Server/Fluids/Components/MopComponent.cs # Content.Server/Fluids/Components/SpillExtensions.cs # Content.Server/Fluids/EntitySystems/PuddleSystem.cs # Content.Server/Instruments/InstrumentSystem.cs # Content.Server/Nutrition/EntitySystems/DrinkSystem.cs # Content.Server/Nutrition/EntitySystems/FoodSystem.cs # Content.Server/PneumaticCannon/PneumaticCannonSystem.cs # Content.Server/Storage/Components/EntityStorageComponent.cs # Content.Server/Storage/Components/StorageFillComponent.cs # Content.Server/Stunnable/StunbatonSystem.cs # Content.Server/Throwing/ThrowHelper.cs # Content.Server/Weapon/Ranged/Barrels/BarrelSystem.cs # Content.Server/Weapon/Ranged/Barrels/Components/ServerBatteryBarrelComponent.cs # Content.Server/Weapon/Ranged/ServerRangedWeaponComponent.cs # Content.Shared/Containers/ItemSlot/ItemSlotsSystem.cs # Content.Shared/Damage/Components/DamageableComponent.cs # Content.Shared/Damage/Systems/DamageableSystem.cs # Content.Shared/MobState/Components/MobStateComponent.cs # Content.Shared/Slippery/SharedSlipperySystem.cs
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Content.Server.GameTicking;
|
||||
using Content.Server.AI.EntitySystems;
|
||||
using Content.Server.GameTicking;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Content.Shared.Roles;
|
||||
using Robust.Shared.GameObjects;
|
||||
@@ -19,6 +20,29 @@ namespace Content.Server.AI.Components
|
||||
|
||||
public override string Name => "AiController";
|
||||
|
||||
// TODO: Need to ECS a lot more of the AI first before we can ECS this
|
||||
/// <summary>
|
||||
/// Whether the AI is actively iterated.
|
||||
/// </summary>
|
||||
public bool Awake
|
||||
{
|
||||
get => _awake;
|
||||
set
|
||||
{
|
||||
if (_awake == value) return;
|
||||
|
||||
_awake = value;
|
||||
|
||||
if (_awake)
|
||||
EntitySystem.Get<NPCSystem>().WakeNPC(this);
|
||||
else
|
||||
EntitySystem.Get<NPCSystem>().SleepNPC(this);
|
||||
}
|
||||
}
|
||||
|
||||
[DataField("awake")]
|
||||
private bool _awake = true;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("startingGear")]
|
||||
public string? StartingGearPrototype { get; set; }
|
||||
|
||||
@@ -1,133 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Server.AI.Components;
|
||||
using Content.Server.AI.Utility.AiLogic;
|
||||
using Content.Shared;
|
||||
using Content.Shared.CCVar;
|
||||
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);
|
||||
}
|
||||
|
||||
/// <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 ((!IoCManager.Resolve<IEntityManager>().EntityExists(message.Entity) ? EntityLifeStage.Deleted : IoCManager.Resolve<IEntityManager>().GetComponent<MetaDataComponent>(message.Entity).EntityLifeStage) >= EntityLifeStage.Deleted ||
|
||||
!IoCManager.Resolve<IEntityManager>().TryGetComponent(message.Entity, 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.Deleted)
|
||||
{
|
||||
toRemove.Add(npc);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (npc.Paused)
|
||||
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 (!IoCManager.Resolve<IEntityManager>().HasComponent<AiControllerComponent>(message.Entity))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_queuedMobStateMessages.Add(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
135
Content.Server/AI/EntitySystems/NPCSystem.cs
Normal file
135
Content.Server/AI/EntitySystems/NPCSystem.cs
Normal file
@@ -0,0 +1,135 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Server.AI.Components;
|
||||
using Content.Server.MobState.States;
|
||||
using Content.Shared.CCVar;
|
||||
using Content.Shared.MobState;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Configuration;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.AI.EntitySystems
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles NPCs running every tick.
|
||||
/// </summary>
|
||||
[UsedImplicitly]
|
||||
internal class NPCSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _robustRandom = default!;
|
||||
|
||||
/// <summary>
|
||||
/// To avoid iterating over dead AI continuously they can wake and sleep themselves when necessary.
|
||||
/// </summary>
|
||||
private readonly HashSet<AiControllerComponent> _awakeNPCs = new();
|
||||
|
||||
/// <summary>
|
||||
/// Whether any NPCs are allowed to run at all.
|
||||
/// </summary>
|
||||
public bool Enabled { get; set; } = true;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<AiControllerComponent, MobStateChangedEvent>(OnMobStateChange);
|
||||
SubscribeLocalEvent<AiControllerComponent, ComponentInit>(OnNPCInit);
|
||||
SubscribeLocalEvent<AiControllerComponent, ComponentShutdown>(OnNPCShutdown);
|
||||
_configurationManager.OnValueChanged(CCVars.NPCEnabled, SetEnabled, true);
|
||||
|
||||
var maxUpdates = _configurationManager.GetCVar(CCVars.NPCMaxUpdates);
|
||||
|
||||
if (maxUpdates < 1024)
|
||||
_awakeNPCs.EnsureCapacity(maxUpdates);
|
||||
}
|
||||
|
||||
private void SetEnabled(bool value) => Enabled = value;
|
||||
|
||||
public override void Shutdown()
|
||||
{
|
||||
base.Shutdown();
|
||||
_configurationManager.UnsubValueChanged(CCVars.NPCEnabled, SetEnabled);
|
||||
}
|
||||
|
||||
private void OnNPCInit(EntityUid uid, AiControllerComponent component, ComponentInit args)
|
||||
{
|
||||
if (!component.Awake) return;
|
||||
|
||||
_awakeNPCs.Add(component);
|
||||
}
|
||||
|
||||
private void OnNPCShutdown(EntityUid uid, AiControllerComponent component, ComponentShutdown args)
|
||||
{
|
||||
_awakeNPCs.Remove(component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allows the NPC to actively be updated.
|
||||
/// </summary>
|
||||
/// <param name="component"></param>
|
||||
public void WakeNPC(AiControllerComponent component)
|
||||
{
|
||||
_awakeNPCs.Add(component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stops the NPC from actively being updated.
|
||||
/// </summary>
|
||||
/// <param name="component"></param>
|
||||
public void SleepNPC(AiControllerComponent component)
|
||||
{
|
||||
_awakeNPCs.Remove(component);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
if (!Enabled) return;
|
||||
|
||||
var cvarMaxUpdates = _configurationManager.GetCVar(CCVars.NPCMaxUpdates);
|
||||
|
||||
if (cvarMaxUpdates <= 0) return;
|
||||
|
||||
var npcs = _awakeNPCs.ToArray();
|
||||
var startIndex = 0;
|
||||
|
||||
// If we're overcap we'll just update randomly so they all still at least do something
|
||||
// Didn't randomise the array (even though it'd probably be better) because god damn that'd be expensive.
|
||||
if (npcs.Length > cvarMaxUpdates)
|
||||
{
|
||||
startIndex = _robustRandom.Next(npcs.Length);
|
||||
}
|
||||
|
||||
for (var i = 0; i < npcs.Length; i++)
|
||||
{
|
||||
var index = (i + startIndex) % npcs.Length;
|
||||
var npc = npcs[index];
|
||||
|
||||
if (npc.Deleted)
|
||||
continue;
|
||||
|
||||
if (npc.Paused)
|
||||
continue;
|
||||
|
||||
npc.Update(frameTime);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnMobStateChange(EntityUid uid, AiControllerComponent component, MobStateChangedEvent args)
|
||||
{
|
||||
switch (args.CurrentMobState)
|
||||
{
|
||||
case NormalMobState:
|
||||
component.Awake = true;
|
||||
break;
|
||||
case CriticalMobState:
|
||||
case DeadMobState:
|
||||
component.Awake = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
using Content.Server.AI.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Server.AI
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates whether an AI should be updated by the AiSystem or not.
|
||||
/// Useful to sleep AI when they die or otherwise should be inactive.
|
||||
/// </summary>
|
||||
internal sealed class SleepAiMessage : EntityEventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Sleep or awake.
|
||||
/// </summary>
|
||||
public bool Sleep { get; }
|
||||
public AiControllerComponent Component { get; }
|
||||
|
||||
public SleepAiMessage(AiControllerComponent component, bool sleep)
|
||||
{
|
||||
Component = component;
|
||||
Sleep = sleep;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Runtime.ExceptionServices;
|
||||
using System.Threading;
|
||||
using Content.Server.AI.Components;
|
||||
using Content.Server.AI.EntitySystems;
|
||||
using Content.Server.AI.LoadBalancer;
|
||||
using Content.Server.AI.Operators;
|
||||
using Content.Server.AI.Utility.Actions;
|
||||
@@ -59,33 +60,13 @@ namespace Content.Server.AI.Utility.AiLogic
|
||||
|
||||
private CancellationTokenSource? _actionCancellation;
|
||||
|
||||
/// <summary>
|
||||
/// If we can't do anything then stop thinking; should probably use ActionBlocker instead
|
||||
/// </summary>
|
||||
private bool _isDead;
|
||||
|
||||
/*public void AfterDeserialization()
|
||||
{
|
||||
if (BehaviorSets.Count > 0)
|
||||
{
|
||||
var behaviorManager = IoCManager.Resolve<INpcBehaviorManager>();
|
||||
|
||||
foreach (var bSet in BehaviorSets)
|
||||
{
|
||||
behaviorManager.AddBehaviorSet(this, bSet, false);
|
||||
}
|
||||
|
||||
behaviorManager.RebuildActions(this);
|
||||
}
|
||||
}*/
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
if (BehaviorSets.Count > 0)
|
||||
{
|
||||
var behaviorManager = IoCManager.Resolve<INpcBehaviorManager>();
|
||||
behaviorManager.RebuildActions(this);
|
||||
IoCManager.Resolve<IEntityManager>().EventBus.RaiseEvent(EventSource.Local, new SleepAiMessage(this, false));
|
||||
EntitySystem.Get<NPCSystem>().WakeNPC(this);
|
||||
}
|
||||
|
||||
base.Initialize();
|
||||
@@ -103,27 +84,6 @@ namespace Content.Server.AI.Utility.AiLogic
|
||||
CurrentAction = null;
|
||||
}
|
||||
|
||||
public void MobStateChanged(MobStateChangedMessage message)
|
||||
{
|
||||
var oldDeadState = _isDead;
|
||||
_isDead = message.Component.IsIncapacitated();
|
||||
|
||||
if (oldDeadState != _isDead)
|
||||
{
|
||||
var entityManager = IoCManager.Resolve<IEntityManager>();
|
||||
|
||||
switch (_isDead)
|
||||
{
|
||||
case true:
|
||||
entityManager.EventBus.RaiseEvent(EventSource.Local, new SleepAiMessage(this, true));
|
||||
break;
|
||||
case false:
|
||||
entityManager.EventBus.RaiseEvent(EventSource.Local, new SleepAiMessage(this, false));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ReceivedAction()
|
||||
{
|
||||
if (_actionRequest == null)
|
||||
|
||||
@@ -84,9 +84,9 @@ namespace Content.Server.AI.Utility
|
||||
if (rebuild)
|
||||
RebuildActions(npc);
|
||||
|
||||
if (npc.BehaviorSets.Count == 1 && !EntitySystem.Get<AiSystem>().IsAwake(npc))
|
||||
if (npc.BehaviorSets.Count == 1 && !npc.Awake)
|
||||
{
|
||||
_entityManager.EventBus.RaiseEvent(EventSource.Local, new SleepAiMessage(npc, false));
|
||||
EntitySystem.Get<NPCSystem>().WakeNPC(npc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,9 +113,9 @@ namespace Content.Server.AI.Utility
|
||||
if (rebuild)
|
||||
RebuildActions(npc);
|
||||
|
||||
if (npc.BehaviorSets.Count == 0 && EntitySystem.Get<AiSystem>().IsAwake(npc))
|
||||
if (npc.BehaviorSets.Count == 0 && npc.Awake)
|
||||
{
|
||||
_entityManager.EventBus.RaiseEvent(EventSource.Local, new SleepAiMessage(npc, true));
|
||||
EntitySystem.Get<NPCSystem>().SleepNPC(npc);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user