Skip to content

Commit

Permalink
Mind Action Container (space-wizards#21336)
Browse files Browse the repository at this point in the history
  • Loading branch information
keronshb committed Nov 3, 2023
1 parent d218fa3 commit 3a788dd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions Content.Client/Actions/ActionsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ private void BaseHandleState<T>(EntityUid uid, BaseActionComponent component, Ba
component.ClientExclusive = state.ClientExclusive;
component.Priority = state.Priority;
component.AttachedEntity = EnsureEntity<T>(state.AttachedEntity, uid);
component.RaiseOnUser = state.RaiseOnUser;
component.AutoPopulate = state.AutoPopulate;
component.Temporary = state.Temporary;
component.ItemIconStyle = state.ItemIconStyle;
Expand Down
23 changes: 23 additions & 0 deletions Content.Shared/Actions/ActionContainerSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Shared.Ghost;
using Content.Shared.Mind;
using Content.Shared.Mind.Components;
using Robust.Shared.Containers;
using Robust.Shared.Network;
using Robust.Shared.Timing;
Expand All @@ -17,6 +20,7 @@ public sealed class ActionContainerSystem : EntitySystem
[Dependency] private readonly SharedActionsSystem _actions = default!;
[Dependency] private readonly INetManager _netMan = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
[Dependency] private readonly SharedMindSystem _mind = default!;

public override void Initialize()
{
Expand All @@ -26,6 +30,25 @@ public override void Initialize()
SubscribeLocalEvent<ActionsContainerComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<ActionsContainerComponent, EntRemovedFromContainerMessage>(OnEntityRemoved);
SubscribeLocalEvent<ActionsContainerComponent, EntInsertedIntoContainerMessage>(OnEntityInserted);
SubscribeLocalEvent<ActionsContainerComponent, MindAddedMessage>(OnMindAdded);
SubscribeLocalEvent<ActionsContainerComponent, MindRemovedMessage>(OnMindRemoved);
}

private void OnMindAdded(EntityUid uid, ActionsContainerComponent component, MindAddedMessage args)
{
if(!_mind.TryGetMind(uid, out var mindId, out _))
return;

if (!TryComp<ActionsContainerComponent>(mindId, out var mindActionContainerComp))
return;

if (!HasComp<GhostComponent>(uid) && mindActionContainerComp.Container.ContainedEntities.Count > 0 )
_actions.GrantContainedActions(uid, mindId);
}

private void OnMindRemoved(EntityUid uid, ActionsContainerComponent component, MindRemovedMessage args)
{
_actions.RemoveProvidedActions(uid, args.Mind);
}

/// <summary>
Expand Down
8 changes: 8 additions & 0 deletions Content.Shared/Actions/BaseActionComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ public EntityUid? EntityIcon
/// </summary>
[ViewVariables] public EntityUid? AttachedEntity;

/// <summary>
/// If true, this will cause the the action event to always be raised directed at the action performer/user instead of the action's container/provider.
/// </summary>
[DataField]
public bool RaiseOnUser;

/// <summary>
/// Whether or not to automatically add this action to the action bar when it becomes available.
/// </summary>
Expand Down Expand Up @@ -159,6 +165,7 @@ public abstract class BaseActionComponentState : ComponentState
public bool ClientExclusive;
public int Priority;
public NetEntity? AttachedEntity;
public bool RaiseOnUser;
public bool AutoPopulate;
public bool Temporary;
public ItemActionIconStyle ItemIconStyle;
Expand All @@ -169,6 +176,7 @@ protected BaseActionComponentState(BaseActionComponent component, IEntityManager
Container = entManager.GetNetEntity(component.Container);
EntityIcon = entManager.GetNetEntity(component.EntIcon);
AttachedEntity = entManager.GetNetEntity(component.AttachedEntity);
RaiseOnUser = component.RaiseOnUser;
Icon = component.Icon;
IconOn = component.IconOn;
IconColor = component.IconColor;
Expand Down
11 changes: 8 additions & 3 deletions Content.Shared/Actions/SharedActionsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Content.Shared.Hands;
using Content.Shared.Interaction;
using Content.Shared.Inventory.Events;
using Content.Shared.Mind;
using Robust.Shared.Containers;
using Robust.Shared.GameStates;
using Robust.Shared.Map;
Expand Down Expand Up @@ -221,7 +222,6 @@ private void OnActionRequest(RequestPerformActionEvent ev, EntitySessionEventArg
return;

DebugTools.Assert(action.AttachedEntity == user);

if (!action.Enabled)
return;

Expand Down Expand Up @@ -369,7 +369,7 @@ public void PerformAction(EntityUid performer, ActionsComponent? component, Enti

var toggledBefore = action.Toggled;

// Note that attached entity is allowed to be null here.
// Note that attached entity and attached container are allowed to be null here.
if (action.AttachedEntity != null && action.AttachedEntity != performer)
{
Log.Error($"{ToPrettyString(performer)} is attempting to perform an action {ToPrettyString(actionId)} that is attached to another entity {ToPrettyString(action.AttachedEntity.Value)}");
Expand All @@ -380,7 +380,12 @@ public void PerformAction(EntityUid performer, ActionsComponent? component, Enti
{
// This here is required because of client-side prediction (RaisePredictiveEvent results in event re-use).
actionEvent.Handled = false;
RaiseLocalEvent(action.Container ?? performer, (object) actionEvent, broadcast: true);
var target = performer;

if (!action.RaiseOnUser && action.Container != null && !HasComp<MindComponent>(action.Container))
target = action.Container.Value;

RaiseLocalEvent(target, (object) actionEvent, broadcast: true);
handled = actionEvent.Handled;
}

Expand Down
3 changes: 2 additions & 1 deletion Content.Shared/Mind/MindComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Content.Shared.GameTicking;
using Content.Shared.Actions;
using Content.Shared.GameTicking;
using Content.Shared.Mind.Components;
using Robust.Shared.GameStates;
using Robust.Shared.Network;
Expand Down

0 comments on commit 3a788dd

Please sign in to comment.