Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make actions use a list of events #34881

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Content.Client/Actions/ActionsSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public void TriggerAction(EntityUid actionId, BaseActionComponent action)

if (action.ClientExclusive)
{
PerformAction(user, actions, actionId, instantAction, instantAction.Event, GameTiming.CurTime);
PerformAction(user, actions, actionId, instantAction, instantAction.BaseEvents, GameTiming.CurTime);
}
else
{
Expand Down
27 changes: 15 additions & 12 deletions Content.Client/Decals/DecalPlacementSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ private void OnFillSlot(FillActionSlotEvent ev)
if (_decalId == null || !_protoMan.TryIndex<DecalPrototype>(_decalId, out var decalProto))
return;

var worldTargetActionEventList = new List<WorldTargetActionEvent>();
var actionEvent = new PlaceDecalActionEvent()
{
DecalId = _decalId,
Expand All @@ -151,20 +152,22 @@ private void OnFillSlot(FillActionSlotEvent ev)
ZIndex = _zIndex,
Cleanable = _cleanable,
};
worldTargetActionEventList.Add(actionEvent);

var actionId = Spawn(null);
AddComp(actionId, new WorldTargetActionComponent
{
// non-unique actions may be considered duplicates when saving/loading.
Icon = decalProto.Sprite,
Repeat = true,
ClientExclusive = true,
CheckCanAccess = false,
CheckCanInteract = false,
Range = -1,
Event = actionEvent,
IconColor = _decalColor,
});
AddComp(actionId,
new WorldTargetActionComponent
{
// non-unique actions may be considered duplicates when saving/loading.
Icon = decalProto.Sprite,
Repeat = true,
ClientExclusive = true,
CheckCanAccess = false,
CheckCanInteract = false,
Range = -1,
Events = worldTargetActionEventList,
IconColor = _decalColor,
});

_metaData.SetEntityName(actionId, $"{_decalId} ({_decalColor.ToHex()}, {(int) _decalAngle.Degrees})");

Expand Down
9 changes: 6 additions & 3 deletions Content.Client/Mapping/MappingSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ private void OnFillActionSlot(FillActionSlotEvent ev)
if (ev.Action != null)
return;

var instantActionEventList = new List<InstantActionEvent>();
var actionEvent = new StartPlacementActionEvent();
ITileDefinition? tileDef = null;

Expand All @@ -67,6 +68,7 @@ private void OnFillActionSlot(FillActionSlotEvent ev)
else
return;

instantActionEventList.Add(actionEvent);
InstantActionComponent action;
string name;

Expand All @@ -79,11 +81,12 @@ private void OnFillActionSlot(FillActionSlotEvent ev)
? _spaceIcon
: new Texture(contentTileDef.Sprite!.Value);


action = new InstantActionComponent
{
ClientExclusive = true,
CheckCanInteract = false,
Event = actionEvent,
Events = instantActionEventList,
Icon = tileIcon
};

Expand All @@ -95,7 +98,7 @@ private void OnFillActionSlot(FillActionSlotEvent ev)
{
ClientExclusive = true,
CheckCanInteract = false,
Event = actionEvent,
Events = instantActionEventList,
Icon = _deleteIcon,
};

Expand All @@ -110,7 +113,7 @@ private void OnFillActionSlot(FillActionSlotEvent ev)
{
ClientExclusive = true,
CheckCanInteract = false,
Event = actionEvent,
Events = instantActionEventList,
Icon = new EntityPrototype(actionEvent.EntityType),
};

Expand Down
20 changes: 10 additions & 10 deletions Content.Client/UserInterface/Systems/Actions/ActionUIController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ private bool TryTargetWorld(in PointerInputCmdArgs args, EntityUid actionId, Wor

if (action.ClientExclusive)
{
if (action.Event != null)
foreach (var worldTargetActionEvent in action.Events)
{
action.Event.Target = coords;
worldTargetActionEvent.Target = coords;
}

_actionsSystem.PerformAction(user, actionComp, actionId, action, action.Event, _timing.CurTime);
_actionsSystem.PerformAction(user, actionComp, actionId, action, action.BaseEvents, _timing.CurTime);
}
else
EntityManager.RaisePredictiveEvent(new RequestPerformActionEvent(EntityManager.GetNetEntity(actionId), EntityManager.GetNetCoordinates(coords)));
Expand Down Expand Up @@ -249,12 +249,12 @@ private bool TryTargetEntity(in PointerInputCmdArgs args, EntityUid actionId, En

if (action.ClientExclusive)
{
if (action.Event != null)
foreach (var entityTargetActionEvent in action.Events)
{
action.Event.Target = entity;
entityTargetActionEvent.Target = entity;
}

_actionsSystem.PerformAction(user, actionComp, actionId, action, action.Event, _timing.CurTime);
_actionsSystem.PerformAction(user, actionComp, actionId, action, action.BaseEvents, _timing.CurTime);
}
else
EntityManager.RaisePredictiveEvent(new RequestPerformActionEvent(EntityManager.GetNetEntity(actionId), EntityManager.GetNetEntity(args.EntityUid)));
Expand Down Expand Up @@ -287,13 +287,13 @@ private bool TryTargetEntityWorld(in PointerInputCmdArgs args,

if (action.ClientExclusive)
{
if (action.Event != null)
foreach (var entityWorldTargetActionEvent in action.Events)
{
action.Event.Entity = entity;
action.Event.Coords = coords;
entityWorldTargetActionEvent.Entity = entity;
entityWorldTargetActionEvent.Coords = coords;
}

_actionsSystem.PerformAction(user, actionComp, actionId, action, action.Event, _timing.CurTime);
_actionsSystem.PerformAction(user, actionComp, actionId, action, action.BaseEvents, _timing.CurTime);
}
else
EntityManager.RaisePredictiveEvent(new RequestPerformActionEvent(EntityManager.GetNetEntity(actionId), EntityManager.GetNetEntity(args.EntityUid), EntityManager.GetNetCoordinates(coords)));
Expand Down
6 changes: 3 additions & 3 deletions Content.IntegrationTests/Tests/Actions/ActionsAddedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public async Task TestCombatActionsAdded()
var evType = typeof(ToggleCombatActionEvent);

var sActions = sActionSystem.GetActions(serverEnt).Where(
x => x.Comp is InstantActionComponent act && act.Event?.GetType() == evType).ToArray();
x => x.Comp is InstantActionComponent act && act.Events?[0].GetType() == evType).ToArray();
var cActions = cActionSystem.GetActions(clientEnt).Where(
x => x.Comp is InstantActionComponent act && act.Event?.GetType() == evType).ToArray();
x => x.Comp is InstantActionComponent act && act.Events?[0].GetType() == evType).ToArray();

Assert.That(sActions.Length, Is.EqualTo(1));
Assert.That(cActions.Length, Is.EqualTo(1));
Expand All @@ -63,7 +63,7 @@ public async Task TestCombatActionsAdded()
// Finally, these two actions are not the same object
// required, because integration tests do not respect the [NonSerialized] attribute and will simply events by reference.
Assert.That(ReferenceEquals(sAct, cAct), Is.False);
Assert.That(ReferenceEquals(sAct.BaseEvent, cAct.BaseEvent), Is.False);
Assert.That(ReferenceEquals(sAct.BaseEvents, cAct.BaseEvents), Is.False);

await pair.CleanReturnAsync();
}
Expand Down
22 changes: 11 additions & 11 deletions Content.Server/Actions/ActionOnInteractSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void OnActivate(EntityUid uid, ActionOnInteractComponent component, Acti
return;

var (actId, act) = _random.Pick(options);
_actions.PerformAction(args.User, null, actId, act, act.Event, _timing.CurTime, false);
_actions.PerformAction(args.User, null, actId, act, act.BaseEvents, _timing.CurTime, false);
args.Handled = true;
}

Expand Down Expand Up @@ -86,12 +86,12 @@ private void OnAfterInteract(EntityUid uid, ActionOnInteractComponent component,
if (entOptions.Count > 0)
{
var (entActId, entAct) = _random.Pick(entOptions);
if (entAct.Event != null)
foreach (var instantActionEvent in entAct.Events)
{
entAct.Event.Target = args.Target.Value;
instantActionEvent.Target = args.Target.Value;
}

_actions.PerformAction(args.User, null, entActId, entAct, entAct.Event, _timing.CurTime, false);
_actions.PerformAction(args.User, null, entActId, entAct, entAct.BaseEvents, _timing.CurTime, false);
args.Handled = true;
return;
}
Expand All @@ -109,13 +109,13 @@ private void OnAfterInteract(EntityUid uid, ActionOnInteractComponent component,
if (entWorldOptions.Count > 0)
{
var (entActId, entAct) = _random.Pick(entWorldOptions);
if (entAct.Event != null)
foreach (var entityWorldTargetActionEvent in entAct.Events)
{
entAct.Event.Entity = args.Target;
entAct.Event.Coords = args.ClickLocation;
entityWorldTargetActionEvent.Entity = args.Target;
entityWorldTargetActionEvent.Coords = args.ClickLocation;
}

_actions.PerformAction(args.User, null, entActId, entAct, entAct.Event, _timing.CurTime, false);
_actions.PerformAction(args.User, null, entActId, entAct, entAct.BaseEvents, _timing.CurTime, false);
args.Handled = true;
return;
}
Expand All @@ -133,12 +133,12 @@ private void OnAfterInteract(EntityUid uid, ActionOnInteractComponent component,
return;

var (actId, act) = _random.Pick(options);
if (act.Event != null)
foreach (var worldTargetAction in act.Events)
{
act.Event.Target = args.ClickLocation;
worldTargetAction.Target = args.ClickLocation;
}

_actions.PerformAction(args.User, null, actId, act, act.Event, _timing.CurTime, false);
_actions.PerformAction(args.User, null, actId, act, act.BaseEvents, _timing.CurTime, false);
args.Handled = true;
}

Expand Down
12 changes: 3 additions & 9 deletions Content.Server/NPC/Systems/NPCUseActionOnTargetSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,12 @@ public bool TryUseTentacleAttack(Entity<NPCUseActionOnTargetComponent?> user, En
if (!_actions.ValidAction(action))
return false;

if (action.Event != null)
foreach (var entityWorldTargetActionEvent in action.Events)
{
action.Event.Coords = Transform(target).Coordinates;
entityWorldTargetActionEvent.Coords = Transform(target).Coordinates;
}

_actions.PerformAction(user,
null,
user.Comp.ActionEnt.Value,
action,
action.BaseEvent,
_timing.CurTime,
false);
_actions.PerformAction(user, null, user.Comp.ActionEnt.Value, action, action.BaseEvents, _timing.CurTime, false);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion Content.Server/Polymorph/Systems/PolymorphSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public void CreatePolymorphAction(ProtoId<PolymorphPrototype> id, Entity<Polymor

baseAction.Icon = new SpriteSpecifier.EntityPrototype(polyProto.Configuration.Entity);
if (baseAction is InstantActionComponent action)
action.Event = new PolymorphActionEvent(id);
action.Events.Add(new PolymorphActionEvent(id));
}

public void RemovePolymorphAction(ProtoId<PolymorphPrototype> id, Entity<PolymorphableComponent> target)
Expand Down
2 changes: 1 addition & 1 deletion Content.Shared/Actions/BaseActionComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Content.Shared.Actions;
[EntityCategory("Actions")]
public abstract partial class BaseActionComponent : Component
{
public abstract BaseActionEvent? BaseEvent { get; }
public abstract List<BaseActionEvent> BaseEvents { get; }

/// <summary>
/// Icon representing this action in the UI.
Expand Down
22 changes: 17 additions & 5 deletions Content.Shared/Actions/EntityTargetActionComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,37 @@ namespace Content.Shared.Actions;
[RegisterComponent, NetworkedComponent]
public sealed partial class EntityTargetActionComponent : BaseTargetActionComponent
{
public override BaseActionEvent? BaseEvent => Event;
public override List<BaseActionEvent> BaseEvents
{
get
{
var list = new List<BaseActionEvent>();
foreach (var ev in Events)
{
list.Add(ev);
}

return list;
}
}

/// <summary>
/// The local-event to raise when this action is performed.
/// </summary>
[DataField("event")]
[DataField]
[NonSerialized]
public EntityTargetActionEvent? Event;
public List<EntityTargetActionEvent> Events;

/// <summary>
/// Determines which entities are valid targets for this action.
/// </summary>
/// <remarks>No whitelist check when null.</remarks>
[DataField("whitelist")] public EntityWhitelist? Whitelist;
[DataField] public EntityWhitelist? Whitelist;

/// <summary>
/// Whether this action considers the user as a valid target entity when using this action.
/// </summary>
[DataField("canTargetSelf")] public bool CanTargetSelf = true;
[DataField] public bool CanTargetSelf = true;
}

[Serializable, NetSerializable]
Expand Down
16 changes: 14 additions & 2 deletions Content.Shared/Actions/EntityWorldTargetActionComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,26 @@ namespace Content.Shared.Actions;
[RegisterComponent, NetworkedComponent]
public sealed partial class EntityWorldTargetActionComponent : BaseTargetActionComponent
{
public override BaseActionEvent? BaseEvent => Event;
public override List<BaseActionEvent> BaseEvents
{
get
{
var list = new List<BaseActionEvent>();
foreach (var ev in Events)
{
list.Add(ev);
}

return list;
}
}

/// <summary>
/// The local-event to raise when this action is performed.
/// </summary>
[DataField]
[NonSerialized]
public EntityWorldTargetActionEvent? Event;
public List<EntityWorldTargetActionEvent> Events;

/// <summary>
/// Determines which entities are valid targets for this action.
Expand Down
18 changes: 15 additions & 3 deletions Content.Shared/Actions/InstantActionComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@ namespace Content.Shared.Actions;
[RegisterComponent, NetworkedComponent]
public sealed partial class InstantActionComponent : BaseActionComponent
{
public override BaseActionEvent? BaseEvent => Event;
public override List<BaseActionEvent> BaseEvents
{
get
{
var list = new List<BaseActionEvent>();
foreach (var ev in Events)
{
list.Add(ev);
}

return list;
}
}

/// <summary>
/// The local-event to raise when this action is performed.
/// </summary>
[DataField("event")]
[DataField]
[NonSerialized]
public InstantActionEvent? Event;
public List<InstantActionEvent> Events;
}

[Serializable, NetSerializable]
Expand Down
Loading
Loading