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,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user