AI sleeping (#1708)

* AI sleeping

AI no longer update when dead.

* It was easier to merge master and re-apply it.

* Update AiControllerComponent.cs

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
This commit is contained in:
metalgearsloth
2020-08-18 23:14:55 +10:00
committed by GitHub
parent 5de57d6cd2
commit 04bc20c365
4 changed files with 101 additions and 28 deletions

View File

@@ -6,10 +6,13 @@ using Content.Server.AI.Utility.Actions;
using Content.Server.AI.Utility.BehaviorSets;
using Content.Server.AI.WorldState;
using Content.Server.AI.WorldState.States.Utility;
using Content.Server.GameObjects.EntitySystems.AI;
using Content.Server.GameObjects.EntitySystems.AI.LoadBalancer;
using Content.Server.GameObjects.EntitySystems.JobQueues;
using Content.Shared.GameObjects.Components.Damage;
using Robust.Server.AI;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
@@ -62,6 +65,13 @@ namespace Content.Server.AI.Utility.AiLogic
{
SortActions();
}
if (BehaviorSets.Count == 1 && !EntitySystem.Get<AiSystem>().IsAwake(this))
{
IoCManager.Resolve<IEntityManager>()
.EventBus
.RaiseEvent(EventSource.Local, new SleepAiMessage(this, false));
}
}
public void RemoveBehaviorSet(Type behaviorSet)
@@ -73,6 +83,13 @@ namespace Content.Server.AI.Utility.AiLogic
BehaviorSets.Remove(behaviorSet);
SortActions();
}
if (BehaviorSets.Count == 0)
{
IoCManager.Resolve<IEntityManager>()
.EventBus
.RaiseEvent(EventSource.Local, new SleepAiMessage(this, true));
}
}
/// <summary>
@@ -133,7 +150,23 @@ namespace Content.Server.AI.Utility.AiLogic
private void DeathHandle(HealthChangedEventArgs eventArgs)
{
var oldDeadState = _isDead;
_isDead = eventArgs.Damageable.CurrentDamageState == DamageState.Dead || eventArgs.Damageable.CurrentDamageState == DamageState.Critical;
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()
@@ -167,16 +200,6 @@ namespace Content.Server.AI.Utility.AiLogic
public override void Update(float frameTime)
{
// If we can't do anything then there's no point thinking
if (_isDead || BehaviorSets.Count == 0)
{
_actionCancellation?.Cancel();
_blackboard.GetState<LastUtilityScoreState>().SetValue(0.0f);
CurrentAction?.Shutdown();
CurrentAction = null;
return;
}
// If we asked for a new action we don't want to dump the existing one.
if (_actionRequest != null)
{