Port medibot + bot spawners from nyano (#9854)
* Port medibot + bot spawners from nyano * Make the injection thresholds constants * Remove warning * Check against const in system too * resolving systems just isn't worth it * only resolve entity manager once * Reduceother resolves too * fix post-merge * woops
This commit is contained in:
38
Content.Server/AI/Considerations/Bots/CanInjectCon.cs
Normal file
38
Content.Server/AI/Considerations/Bots/CanInjectCon.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Content.Server.AI.WorldState;
|
||||
using Content.Server.AI.WorldState.States;
|
||||
using Content.Server.AI.Tracking;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.MobState.Components;
|
||||
using Content.Server.Silicons.Bots;
|
||||
|
||||
namespace Content.Server.AI.Utility.Considerations.Bot
|
||||
{
|
||||
public sealed class CanInjectCon : Consideration
|
||||
{
|
||||
protected override float GetScore(Blackboard context)
|
||||
{
|
||||
var entMan = IoCManager.Resolve<IEntityManager>();
|
||||
var target = context.GetState<TargetEntityState>().GetValue();
|
||||
|
||||
if (target == null || !entMan.TryGetComponent(target, out DamageableComponent? damageableComponent))
|
||||
return 0;
|
||||
|
||||
if (entMan.TryGetComponent(target, out RecentlyInjectedComponent? recently))
|
||||
return 0f;
|
||||
|
||||
if (!entMan.TryGetComponent(target, out MobStateComponent? mobState) || mobState.IsDead())
|
||||
return 0f;
|
||||
|
||||
if (damageableComponent.TotalDamage == 0)
|
||||
return 0f;
|
||||
|
||||
if (damageableComponent.TotalDamage <= MedibotComponent.StandardMedDamageThreshold)
|
||||
return 1f;
|
||||
|
||||
if (damageableComponent.TotalDamage >= MedibotComponent.EmergencyMedDamageThreshold)
|
||||
return 1f;
|
||||
|
||||
return 0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
74
Content.Server/AI/EntitySystems/InjectNearbySystem.cs
Normal file
74
Content.Server/AI/EntitySystems/InjectNearbySystem.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using Content.Server.Chemistry.Components.SolutionManager;
|
||||
using Content.Server.Chemistry.EntitySystems;
|
||||
using Content.Server.AI.Tracking;
|
||||
using Content.Server.Popups;
|
||||
using Content.Server.Chat.Systems;
|
||||
using Content.Server.Silicons.Bots;
|
||||
using Content.Shared.MobState.Components;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.Interaction;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Audio;
|
||||
|
||||
namespace Content.Server.AI.EntitySystems
|
||||
{
|
||||
public sealed class InjectNearbySystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly EntityLookupSystem _lookup = default!;
|
||||
[Dependency] private readonly SolutionContainerSystem _solutionSystem = default!;
|
||||
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
||||
[Dependency] private readonly ChatSystem _chat = default!;
|
||||
[Dependency] private readonly SharedInteractionSystem _interactionSystem = default!;
|
||||
|
||||
public EntityUid GetNearbyInjectable(EntityUid medibot, float range = 4)
|
||||
{
|
||||
foreach (var entity in _lookup.GetEntitiesInRange(medibot, range))
|
||||
{
|
||||
if (HasComp<InjectableSolutionComponent>(entity) && HasComp<MobStateComponent>(entity))
|
||||
return entity;
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public bool Inject(EntityUid medibot, EntityUid target)
|
||||
{
|
||||
if (!TryComp<MedibotComponent>(medibot, out var botComp))
|
||||
return false;
|
||||
|
||||
if (!TryComp<DamageableComponent>(target, out var damage))
|
||||
return false;
|
||||
|
||||
if (!_solutionSystem.TryGetInjectableSolution(target, out var injectable))
|
||||
return false;
|
||||
|
||||
if (!_interactionSystem.InRangeUnobstructed(medibot, target))
|
||||
return true; // return true lets the bot reattempt the action on the same target
|
||||
|
||||
if (damage.TotalDamage == 0)
|
||||
return false;
|
||||
|
||||
if (damage.TotalDamage <= MedibotComponent.StandardMedDamageThreshold)
|
||||
{
|
||||
_solutionSystem.TryAddReagent(target, injectable, botComp.StandardMed, botComp.StandardMedInjectAmount, out var accepted);
|
||||
EnsureComp<RecentlyInjectedComponent>(target);
|
||||
_popupSystem.PopupEntity(Loc.GetString("hypospray-component-feel-prick-message"), target, Filter.Entities(target));
|
||||
SoundSystem.Play("/Audio/Items/hypospray.ogg", Filter.Pvs(target), target);
|
||||
_chat.TrySendInGameICMessage(medibot, Loc.GetString("medibot-finish-inject"), InGameICChatType.Speak, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (damage.TotalDamage >= MedibotComponent.EmergencyMedDamageThreshold)
|
||||
{
|
||||
_solutionSystem.TryAddReagent(target, injectable, botComp.EmergencyMed, botComp.EmergencyMedInjectAmount, out var accepted);
|
||||
EnsureComp<RecentlyInjectedComponent>(target);
|
||||
_popupSystem.PopupEntity(Loc.GetString("hypospray-component-feel-prick-message"), target, Filter.Entities(target));
|
||||
SoundSystem.Play("/Audio/Items/hypospray.ogg", Filter.Pvs(target), target);
|
||||
_chat.TrySendInGameICMessage(medibot, Loc.GetString("medibot-finish-inject"), InGameICChatType.Speak, false);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
24
Content.Server/AI/Operators/Bots/InjectOperator.cs
Normal file
24
Content.Server/AI/Operators/Bots/InjectOperator.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using Content.Server.AI.EntitySystems;
|
||||
|
||||
namespace Content.Server.AI.Operators.Bots
|
||||
{
|
||||
public sealed class InjectOperator : AiOperator
|
||||
{
|
||||
private EntityUid _medibot;
|
||||
private EntityUid _target;
|
||||
public InjectOperator(EntityUid medibot, EntityUid target)
|
||||
{
|
||||
_medibot = medibot;
|
||||
_target = target;
|
||||
}
|
||||
|
||||
public override Outcome Execute(float frameTime)
|
||||
{
|
||||
var injectSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<InjectNearbySystem>();
|
||||
if (injectSystem.Inject(_medibot, _target))
|
||||
return Outcome.Success;
|
||||
|
||||
return Outcome.Failed;
|
||||
}
|
||||
}
|
||||
}
|
||||
22
Content.Server/AI/Operators/Speech/SpeechOperator.cs
Normal file
22
Content.Server/AI/Operators/Speech/SpeechOperator.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Content.Server.Chat.Systems;
|
||||
|
||||
namespace Content.Server.AI.Operators.Speech
|
||||
{
|
||||
public sealed class SpeakOperator : AiOperator
|
||||
{
|
||||
private EntityUid _speaker;
|
||||
private string _speechString;
|
||||
public SpeakOperator(EntityUid speaker, string speechString)
|
||||
{
|
||||
_speaker = speaker;
|
||||
_speechString = speechString;
|
||||
}
|
||||
|
||||
public override Outcome Execute(float frameTime)
|
||||
{
|
||||
var chatSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<ChatSystem>();
|
||||
chatSystem.TrySendInGameICMessage(_speaker, _speechString, InGameICChatType.Speak, false);
|
||||
return Outcome.Success;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Content.Server/AI/Tracking/RecentlyInjectedComponent.cs
Normal file
12
Content.Server/AI/Tracking/RecentlyInjectedComponent.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace Content.Server.AI.Tracking
|
||||
{
|
||||
/// Added when a medibot injects someone
|
||||
/// So they don't get injected again for at least a minute.
|
||||
[RegisterComponent]
|
||||
public sealed class RecentlyInjectedComponent : Component
|
||||
{
|
||||
public float Accumulator = 0f;
|
||||
|
||||
public TimeSpan RemoveTime = TimeSpan.FromMinutes(1);
|
||||
}
|
||||
}
|
||||
25
Content.Server/AI/Tracking/RecentlyInjectedSystem.cs
Normal file
25
Content.Server/AI/Tracking/RecentlyInjectedSystem.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace Content.Server.AI.Tracking
|
||||
{
|
||||
public sealed class RecentlyInjectedSystem : EntitySystem
|
||||
{
|
||||
|
||||
Queue<EntityUid> RemQueue = new();
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
foreach (var toRemove in RemQueue)
|
||||
{
|
||||
RemComp<RecentlyInjectedComponent>(toRemove);
|
||||
}
|
||||
RemQueue.Clear();
|
||||
foreach (var entity in EntityQuery<RecentlyInjectedComponent>())
|
||||
{
|
||||
entity.Accumulator += frameTime;
|
||||
if (entity.Accumulator < entity.RemoveTime.TotalSeconds)
|
||||
continue;
|
||||
entity.Accumulator = 0;
|
||||
RemQueue.Enqueue(entity.Owner);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
57
Content.Server/AI/Utility/Actions/Bots/InjectNearby.cs
Normal file
57
Content.Server/AI/Utility/Actions/Bots/InjectNearby.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using Content.Server.AI.Operators;
|
||||
using Content.Server.AI.Operators.Generic;
|
||||
using Content.Server.AI.Operators.Movement;
|
||||
using Content.Server.AI.Operators.Bots;
|
||||
using Content.Server.AI.Operators.Speech;
|
||||
using Content.Server.AI.WorldState;
|
||||
using Content.Server.AI.Utility.Considerations.Containers;
|
||||
using Content.Server.AI.Utility.Considerations;
|
||||
using Content.Server.AI.Utility.Considerations.ActionBlocker;
|
||||
using Content.Server.AI.WorldState.States.Movement;
|
||||
using Content.Server.AI.WorldState.States;
|
||||
using Content.Server.AI.Utility.Considerations.Bot;
|
||||
|
||||
|
||||
namespace Content.Server.AI.Utility.Actions.Bots
|
||||
{
|
||||
public sealed class InjectNearby : UtilityAction
|
||||
{
|
||||
public EntityUid Target { get; set; } = default!;
|
||||
|
||||
public override void SetupOperators(Blackboard context)
|
||||
{
|
||||
MoveToEntityOperator moveOperator = new MoveToEntityOperator(Owner, Target);
|
||||
float waitTime = 3f;
|
||||
|
||||
ActionOperators = new Queue<AiOperator>(new AiOperator[]
|
||||
{
|
||||
moveOperator,
|
||||
new SpeakOperator(Owner, Loc.GetString("medibot-start-inject")),
|
||||
new WaitOperator(waitTime),
|
||||
new InjectOperator(Owner, Target),
|
||||
});
|
||||
}
|
||||
|
||||
protected override void UpdateBlackboard(Blackboard context)
|
||||
{
|
||||
base.UpdateBlackboard(context);
|
||||
context.GetState<TargetEntityState>().SetValue(Target);
|
||||
context.GetState<MoveTargetState>().SetValue(Target);
|
||||
}
|
||||
|
||||
protected override IReadOnlyCollection<Func<float>> GetConsiderations(Blackboard context)
|
||||
{
|
||||
var considerationsManager = IoCManager.Resolve<ConsiderationsManager>();
|
||||
|
||||
return new[]
|
||||
{
|
||||
considerationsManager.Get<CanMoveCon>()
|
||||
.BoolCurve(context),
|
||||
considerationsManager.Get<TargetAccessibleCon>()
|
||||
.BoolCurve(context),
|
||||
considerationsManager.Get<CanInjectCon>()
|
||||
.BoolCurve(context),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Content.Server.AI.Components;
|
||||
using Content.Server.AI.EntitySystems;
|
||||
using Content.Server.AI.Utility.Actions;
|
||||
using Content.Server.AI.Utility.Actions.Bots;
|
||||
using Content.Server.AI.Utility.Considerations;
|
||||
using Content.Server.AI.WorldState;
|
||||
using Content.Server.AI.WorldState.States;
|
||||
using Content.Server.AI.Utility.Considerations.ActionBlocker;
|
||||
using Content.Server.Silicons.Bots;
|
||||
|
||||
namespace Content.Server.AI.Utility.ExpandableActions.Bots
|
||||
{
|
||||
public sealed class InjectNearbyExp : ExpandableUtilityAction
|
||||
{
|
||||
public override float Bonus => 30;
|
||||
IEntityManager _entMan = IoCManager.Resolve<IEntityManager>();
|
||||
|
||||
protected override IReadOnlyCollection<Func<float>> GetCommonConsiderations(Blackboard context)
|
||||
{
|
||||
var considerationsManager = IoCManager.Resolve<ConsiderationsManager>();
|
||||
|
||||
return new[]
|
||||
{
|
||||
considerationsManager.Get<CanMoveCon>()
|
||||
.BoolCurve(context),
|
||||
};
|
||||
}
|
||||
public override IEnumerable<UtilityAction> GetActions(Blackboard context)
|
||||
{
|
||||
var owner = context.GetState<SelfState>().GetValue();
|
||||
if (!_entMan.TryGetComponent(owner, out NPCComponent? controller)
|
||||
|| !_entMan.TryGetComponent(owner, out MedibotComponent? bot))
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
yield return new InjectNearby() {Owner = owner, Target = EntitySystem.Get<InjectNearbySystem>().GetNearbyInjectable(Owner), Bonus = Bonus};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user