action refactor proper ecs edition (#27422)

This commit is contained in:
deltanedas
2025-05-28 19:52:11 +00:00
committed by GitHub
parent a9f7cfbcb6
commit b3825dce97
111 changed files with 1995 additions and 2901 deletions

View File

@@ -1,5 +1,6 @@
using System.Linq;
using Content.Shared.Actions;
using Content.Shared.Actions.Components;
using Content.Shared.Charges.Components;
using Content.Shared.Charges.Systems;
using Content.Shared.Interaction;
@@ -60,8 +61,10 @@ public sealed class ActionOnInteractSystem : EntitySystem
if (!TryUseCharge((uid, component)))
return;
var (actId, act) = _random.Pick(options);
_actions.PerformAction(args.User, null, actId, act, act.Event, _timing.CurTime, false);
// not predicted as this is in server due to random
// TODO: use predicted random and move to shared?
var (actId, action, comp) = _random.Pick(options);
_actions.PerformAction(args.User, (actId, action), predicted: false);
args.Handled = true;
}
@@ -79,13 +82,13 @@ public sealed class ActionOnInteractSystem : EntitySystem
}
// First, try entity target actions
if (args.Target != null)
if (args.Target is {} target)
{
var entOptions = GetValidActions<EntityTargetActionComponent>(actionEnts, args.CanReach);
for (var i = entOptions.Count - 1; i >= 0; i--)
{
var action = entOptions[i];
if (!_actions.ValidateEntityTarget(args.User, args.Target.Value, action))
if (!_actions.ValidateEntityTarget(args.User, target, (action, action.Comp2)))
entOptions.RemoveAt(i);
}
@@ -94,50 +97,19 @@ public sealed class ActionOnInteractSystem : EntitySystem
if (!TryUseCharge((uid, component)))
return;
var (entActId, entAct) = _random.Pick(entOptions);
if (entAct.Event != null)
{
entAct.Event.Target = args.Target.Value;
}
_actions.PerformAction(args.User, null, entActId, entAct, entAct.Event, _timing.CurTime, false);
var (actionId, action, _) = _random.Pick(entOptions);
_actions.SetEventTarget(actionId, target);
_actions.PerformAction(args.User, (actionId, action), predicted: false);
args.Handled = true;
return;
}
}
// Then EntityWorld target actions
var entWorldOptions = GetValidActions<EntityWorldTargetActionComponent>(actionEnts, args.CanReach);
for (var i = entWorldOptions.Count - 1; i >= 0; i--)
{
var action = entWorldOptions[i];
if (!_actions.ValidateEntityWorldTarget(args.User, args.Target, args.ClickLocation, action))
entWorldOptions.RemoveAt(i);
}
if (entWorldOptions.Count > 0)
{
if (!TryUseCharge((uid, component)))
return;
var (entActId, entAct) = _random.Pick(entWorldOptions);
if (entAct.Event != null)
{
entAct.Event.Entity = args.Target;
entAct.Event.Coords = args.ClickLocation;
}
_actions.PerformAction(args.User, null, entActId, entAct, entAct.Event, _timing.CurTime, false);
args.Handled = true;
return;
}
// else: try world target actions
var options = GetValidActions<WorldTargetActionComponent>(component.ActionEntities, args.CanReach);
for (var i = options.Count - 1; i >= 0; i--)
{
var action = options[i];
if (!_actions.ValidateWorldTarget(args.User, args.ClickLocation, action))
if (!_actions.ValidateWorldTarget(args.User, args.ClickLocation, (action, action.Comp2)))
options.RemoveAt(i);
}
@@ -147,33 +119,34 @@ public sealed class ActionOnInteractSystem : EntitySystem
if (!TryUseCharge((uid, component)))
return;
var (actId, act) = _random.Pick(options);
if (act.Event != null)
var (actId, comp, world) = _random.Pick(options);
if (world.Event is {} worldEv)
{
act.Event.Target = args.ClickLocation;
worldEv.Target = args.ClickLocation;
worldEv.Entity = HasComp<EntityTargetActionComponent>(actId) ? args.Target : null;
}
_actions.PerformAction(args.User, null, actId, act, act.Event, _timing.CurTime, false);
_actions.PerformAction(args.User, (actId, comp), world.Event, predicted: false);
args.Handled = true;
}
private List<(EntityUid Id, T Comp)> GetValidActions<T>(List<EntityUid>? actions, bool canReach = true) where T : BaseActionComponent
private List<Entity<ActionComponent, T>> GetValidActions<T>(List<EntityUid>? actions, bool canReach = true) where T: Component
{
var valid = new List<(EntityUid Id, T Comp)>();
var valid = new List<Entity<ActionComponent, T>>();
if (actions == null)
return valid;
foreach (var id in actions)
{
if (!_actions.TryGetActionData(id, out var baseAction) ||
baseAction as T is not { } action ||
if (_actions.GetAction(id) is not {} action ||
!TryComp<T>(id, out var comp) ||
!_actions.ValidAction(action, canReach))
{
continue;
}
valid.Add((id, action));
valid.Add((id, action, comp));
}
return valid;