Skip to content

Commit

Permalink
upd AI Core
Browse files Browse the repository at this point in the history
  • Loading branch information
Rxup committed Nov 12, 2023
1 parent dd0ffe1 commit 876bb8a
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ public override void Initialize()

SubscribeLocalEvent<ShowHealthBarsComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<ShowHealthBarsComponent, ComponentRemove>(OnRemove);
SubscribeLocalEvent<ShowHealthBarsComponent, LocalPlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<ShowHealthBarsComponent, LocalPlayerDetachedEvent>(OnPlayerDetached);
SubscribeLocalEvent<ShowHealthBarsComponent, AfterAutoHandleStateEvent>(OnUpdate);
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundRestart);

_overlay = new(EntityManager, _protoMan);
}

private void OnUpdate(Entity<ShowHealthBarsComponent> ent, ref AfterAutoHandleStateEvent args)
{
_overlay.DamageContainers.Clear();
_overlay.DamageContainers.AddRange(ent.Comp.DamageContainers);
}

private void OnInit(EntityUid uid, ShowHealthBarsComponent component, ComponentInit args)
{
if (_player.LocalSession?.AttachedEntity == uid)
Expand Down
30 changes: 0 additions & 30 deletions Content.Client/Backmen/StationAI/Systems/StationAISystem.cs

This file was deleted.

10 changes: 10 additions & 0 deletions Content.Server/Backmen/StationAI/InnateItemSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Content.Shared.Interaction;
using Content.Shared.Mind.Components;
using Content.Shared.Tag;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;

Expand All @@ -18,10 +19,19 @@ public override void Initialize()
base.Initialize();

SubscribeLocalEvent<InnateItemComponent, MindAddedMessage>(OnMindAdded);
SubscribeLocalEvent<InnateItemComponent, PlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<InnateItemComponent, InnateAfterInteractActionEvent>(StartAfterInteract);
SubscribeLocalEvent<InnateItemComponent, InnateBeforeInteractActionEvent>(StartBeforeInteract);
}

private void OnPlayerAttached(Entity<InnateItemComponent> ent, ref PlayerAttachedEvent args)
{
if (!ent.Comp.AlreadyInitialized)
RefreshItems(ent, ent);

ent.Comp.AlreadyInitialized = true;
}

private void OnMindAdded(EntityUid uid, InnateItemComponent component, MindAddedMessage args)
{
if (!component.AlreadyInitialized)
Expand Down
119 changes: 103 additions & 16 deletions Content.Server/Backmen/StationAI/Systems/AIEyeSystem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using Content.Server.Backmen.Abilities.Psionics;
using Content.Server.Mind;
using Content.Server.Power.Components;
using Content.Server.Speech.Components;
using Content.Shared.Actions;
using Content.Shared.Backmen.StationAI;
using Content.Shared.Eye;
using Content.Shared.Mind;
using Content.Shared.Mind.Components;
using Robust.Shared.Prototypes;
using Content.Shared.Mobs;
Expand All @@ -21,12 +25,13 @@ public sealed class AIEyePowerSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly SharedActionsSystem _actions = default!;
[Dependency] private readonly MindSwapPowerSystem _mindSwap = default!;
[Dependency] private readonly MindSystem _mindSystem = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly TransformSystem _transformSystem = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;

[Dependency] private readonly MobStateSystem _mobState = default!;

//[Dependency] private readonly LawsSystem _laws = default!;
[Dependency] private readonly VisibilitySystem _visibilitySystem = default!;
[Dependency] private readonly MetaDataSystem _metaDataSystem = default!;
[Dependency] private readonly SharedEyeSystem _sharedEyeSystem = default!;
Expand All @@ -39,11 +44,44 @@ public override void Initialize()
SubscribeLocalEvent<AIEyePowerComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<AIEyePowerComponent, AIEyePowerActionEvent>(OnPowerUsed);

SubscribeLocalEvent<AIEyeComponent, AIEyePowerReturnActionEvent>(OnPowerReturnUsed);

SubscribeLocalEvent<AIEyeComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<AIEyeComponent, MindRemovedMessage>(OnMindRemoved);
SubscribeLocalEvent<AIEyeComponent, MindUnvisitedMessage>(OnMindRemoved2);

SubscribeLocalEvent<StationAIComponent, MobStateChangedEvent>(OnMobStateChanged);
SubscribeLocalEvent<StationAIComponent, GetSiliconLawsEvent>(OnGetLaws);

SubscribeLocalEvent<StationAIComponent, PowerChangedEvent>(OnPowerChange);
}

private void OnPowerChange(EntityUid uid, StationAIComponent component, ref PowerChangedEvent args)
{
if (HasComp<AIEyeComponent>(uid))
{
return;
}

foreach (var (actionId,action) in _actions.GetActions(uid))
{
_actions.SetEnabled(actionId, args.Powered);
}

if (!args.Powered && component.ActiveEye.IsValid())
{
QueueDel(component.ActiveEye);
component.ActiveEye = EntityUid.Invalid;
}

if (!args.Powered)
{
EnsureComp<ReplacementAccentComponent>(uid).Accent = "dwarf";
}
else
{
RemCompDeferred<ReplacementAccentComponent>(uid);
}
}

[ValidatePrototypeId<SiliconLawsetPrototype>]
Expand Down Expand Up @@ -81,31 +119,48 @@ private void OnInit(EntityUid uid, AIEyePowerComponent component, ComponentInit

private void OnShutdown(EntityUid uid, AIEyePowerComponent component, ComponentShutdown args)
{
if (!_entityManager.HasComponent<StationAIComponent>(uid))
if (!HasComp<StationAIComponent>(uid))
return;

if (component.EyePowerAction != null)
_actions.RemoveAction(uid, component.EyePowerAction);
}

private void OnPowerUsed(EntityUid uid, AIEyePowerComponent component, AIEyePowerActionEvent args)
private void OnPowerReturnUsed(EntityUid uid, AIEyeComponent component, AIEyePowerReturnActionEvent args)
{
// var ai = _entityManager.EnsureComponent<StationAIComponent>(uid);
if (!_entityManager.TryGetComponent<StationAIComponent>(uid, out var ai))
if (
!TryComp<VisitingMindComponent>(args.Performer, out var mindId) ||
mindId!.MindId == null ||
!TryComp<MindComponent>(mindId.MindId.Value, out var mind)
)
return;

// Mind swap
var projection = Spawn(component.Prototype, Transform(uid).Coordinates);
ai.ActiveEye = projection;
var core = _entityManager.GetComponent<MetaDataComponent>(uid);
_mindSystem.UnVisit(mindId.MindId.Value, mind);
QueueDel(args.Performer);
args.Handled = true;
}

private void OnPowerUsed(EntityUid uid, AIEyePowerComponent component, AIEyePowerActionEvent args)
{
if (!_mindSystem.TryGetMind(args.Performer, out var mindId, out var mind))
return;

Transform(projection).AttachToGridOrMap();
_mindSwap.Swap(uid, projection);
if (!TryComp<StationAIComponent>(uid, out var ai))
return;

var coords = Transform(uid).Coordinates;
var projection = EntityManager.CreateEntityUninitialized(component.Prototype, coords);
ai.ActiveEye = projection;
EnsureComp<AIEyeComponent>(projection).AiCore = (uid, ai);
EnsureComp<StationAIComponent>(projection).SelectedLaw = ai.SelectedLaw;
EnsureComp<SiliconLawBoundComponent>(projection);
var core = MetaData(uid);
// Consistent name
_metaDataSystem.SetEntityName(projection, core.EntityName != "" ? core.EntityName : "Invalid AI");
EntityManager.InitializeAndStartEntity(projection, coords.GetMapId(EntityManager));

EnsureComp<SiliconLawBoundComponent>(projection);
_transformSystem.AttachToGridOrMap(projection);
_mindSystem.Visit(mindId, projection, mind); // Mind swap

args.Handled = true;
}
Expand All @@ -120,22 +175,54 @@ private void OnStartup(EntityUid uid, AIEyeComponent component, ComponentStartup

_sharedEyeSystem.SetVisibilityMask(uid, eye.VisibilityMask | (int) VisibilityFlags.AIEye, eye);
_visibilitySystem.AddLayer(uid, visibility, (int) VisibilityFlags.AIEye);
_actions.AddAction(uid, ref component.ReturnActionUid, component.ReturnAction);
}

private void OnMindRemoved(EntityUid uid, AIEyeComponent component, MindRemovedMessage args)
{
QueueDel(uid);
if (component.AiCore?.Comp != null)
component.AiCore.Value.Comp.ActiveEye = EntityUid.Invalid;
}
private void OnMindRemoved2(EntityUid uid, AIEyeComponent component, MindUnvisitedMessage args)
{
QueueDel(uid);
if (component.AiCore?.Comp != null)
component.AiCore.Value.Comp.ActiveEye = EntityUid.Invalid;
}

private void ClearState(EntityUid uid, AIEyeComponent? component = null)
{
if (!Resolve(uid, ref component))
{
return;
}

QueueDel(uid);
if (!component.AiCore.HasValue)
return;

if (_mindSystem.TryGetMind(component.AiCore.Value, out var mindId, out var mind))
{
_mindSystem.UnVisit(mindId, mind);
}

component.AiCore.Value.Comp.ActiveEye = EntityUid.Invalid;
}

private static readonly SoundSpecifier AIDeath =
new SoundPathSpecifier("/Audio/SimpleStation14/Machines/AI/borg_death.ogg");

private void OnMobStateChanged(EntityUid uid, StationAIComponent component, MobStateChangedEvent args)
{
if (!_mobState.IsDead(uid))
return;

if (component.ActiveEye != EntityUid.Invalid)
_mindSwap.Swap(component.ActiveEye, uid);
if (component.ActiveEye.IsValid() && _mindSystem.TryGetMind(uid, out var mindId, out var mind))
{
ClearState(component.ActiveEye);
}

SoundSystem.Play("/Audio/SimpleStation14/Machines/AI/borg_death.ogg", Filter.Pvs(uid), uid);
_audioSystem.PlayPvs(AIDeath, uid);
}
}
21 changes: 16 additions & 5 deletions Content.Server/Backmen/StationAI/Systems/StationAISystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Content.Server.Backmen.Abilities.Psionics;
using Content.Shared.Actions;
using Content.Shared.Backmen.EntityHealthBar;
using Robust.Shared.Prototypes;
using Content.Shared.Backmen.StationAI.Events;

Expand All @@ -24,12 +25,11 @@ public override void Initialize()

private void OnTerminated(Entity<StationAIComponent> ent, ref EntityTerminatingEvent args)
{
if (!TryComp<MindSwappedComponent>(ent, out var mindSwappedComponent))
return;
if (mindSwappedComponent.OriginalEntity.Valid)
if (!ent.Comp.ActiveEye.IsValid())
{
QueueDel(mindSwappedComponent.OriginalEntity);
return;
}
QueueDel(ent.Comp.ActiveEye);
}

private void OnStartup(EntityUid uid, StationAIComponent component, ComponentStartup args)
Expand All @@ -44,7 +44,18 @@ private void OnShutdown(EntityUid uid, StationAIComponent component, ComponentSh

private void OnHealthOverlayEvent(AIHealthOverlayEvent args)
{
RaiseNetworkEvent(new NetworkedAIHealthOverlayEvent(GetNetEntity(args.Performer)));
if (HasComp<ShowHealthBarsComponent>(args.Performer))
{
RemCompDeferred<ShowHealthBarsComponent>(args.Performer);
}
else
{
var comp = EnsureComp<ShowHealthBarsComponent>(args.Performer);
comp.DamageContainers.Clear();
comp.DamageContainers.Add("Biological");
comp.DamageContainers.Add("HalfSpirit");
Dirty(args.Performer, comp);
}
args.Handled = true;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using Content.Shared.Damage.Prototypes;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;

namespace Content.Shared.Backmen.EntityHealthBar;

/// <summary>
/// This component allows you to see health bars above damageable mobs.
/// </summary>
[RegisterComponent]
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)]
public sealed partial class ShowHealthBarsComponent : Component
{
/// <summary>
/// If null, displays all health bars.
/// If not null, displays health bars of only that damage container.
/// </summary>

[AutoNetworkedField]
[DataField("damageContainers", customTypeSerializer: typeof(PrototypeIdListSerializer<DamageContainerPrototype>))]
public List<string> DamageContainers = new();

public override bool SendOnlyToOwner => true;
}
11 changes: 11 additions & 0 deletions Content.Shared/Backmen/StationAI/Components/AIEyeComponent.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
using Content.Shared.Actions;
using Robust.Shared.Prototypes;

namespace Content.Shared.Backmen.StationAI;

[RegisterComponent]
public sealed partial class AIEyeComponent : Component
{
public Entity<StationAIComponent>? AiCore;

public EntProtoId ReturnAction = "AIEyeReturnAction";

public EntityUid? ReturnActionUid;
}

public sealed partial class AIEyePowerActionEvent : InstantActionEvent
{

}

public sealed partial class AIEyePowerReturnActionEvent : InstantActionEvent
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public sealed partial class StationAIComponent : Component
[DataField("lawsId")]
public ProtoId<WeightedRandomPrototype> LawsId = "LawsStationAIDefault";

[ViewVariables]
public SiliconLawsetPrototype? SelectedLaw;
}
12 changes: 0 additions & 12 deletions Content.Shared/Backmen/StationAI/Events/StationAIEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ public AIHealthOverlayEvent()
}
}

[Serializable, NetSerializable]
public sealed class NetworkedAIHealthOverlayEvent : EntityEventArgs
{
public NetEntity Performer = NetEntity.Invalid;

public NetworkedAIHealthOverlayEvent(NetEntity performer)
{
Performer = performer;
}
}


[Serializable, NetSerializable]
public sealed class AICameraListMessage : BoundUserInterfaceMessage
{
Expand Down
Loading

0 comments on commit 876bb8a

Please sign in to comment.