fish-station/Content.Client/Actions/ActionsSystem.cs
Vigers Ray 95b86d7a4b Merge remote-tracking branch 'space-wizards/master'
# Conflicts:
#	Content.Client/Construction/UI/ConstructionMenu.xaml
#	Content.Client/Construction/UI/ConstructionMenu.xaml.cs
#	Content.Client/Construction/UI/ConstructionMenuPresenter.cs
#	Content.Server/Warps/WarpPointSystem.cs
#	Content.Server/Weapons/Ranged/Systems/GunSystem.Battery.cs
#	Content.Server/Weapons/Ranged/Systems/GunSystem.cs
#	Content.Shared/Construction/Steps/ArbitraryInsertConstructionGraphStep.cs
#	Content.Shared/Construction/Steps/MaterialConstructionGraphStep.cs
#	Content.Shared/Humanoid/SharedHumanoidAppearanceSystem.cs
#	Content.Shared/Inventory/InventorySystem.Relay.cs
#	Content.Shared/Weapons/Reflect/ReflectSystem.cs
#	Resources/Locale/en-US/_strings/plants/mutations.ftl
#	Resources/Locale/en-US/_strings/stack/stacks.ftl
#	Resources/Locale/en-US/_strings/teleportation/teleportation-menu-gui.ftl
#	Resources/Prototypes/Catalog/Fills/Backpacks/duffelbag.yml
#	Resources/Prototypes/Catalog/Fills/Lockers/heads.yml
#	Resources/Prototypes/Catalog/VendingMachines/Inventories/winterdrobe.yml
#	Resources/Prototypes/Entities/Clothing/Eyes/glasses.yml
#	Resources/Prototypes/Entities/Clothing/OuterClothing/armor.yml
#	Resources/Prototypes/Entities/Objects/Power/powercells.yml
#	Resources/Prototypes/Entities/Objects/Specific/Cargo/cargo_pallet.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Ammunition/Projectiles/shotgun.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Battery/battery_guns.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Melee/sword.yml
#	Resources/Prototypes/Entities/Stations/base.yml
#	Resources/Prototypes/Entities/Structures/Machines/nuke.yml
#	Resources/Prototypes/Reagents/toxins.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/clothing/clown_banana.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/machines/computer.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/mechs/hamtr_construction.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/mechs/honker_construction.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/mechs/ripley_construction.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/mechs/vim_construction.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/structures/airlock.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/structures/airlock_clockwork.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/structures/blast_door.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/structures/conveyor.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/structures/firelock.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/structures/glassbox.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/structures/secretdoor.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/structures/shutter.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/structures/shuttle.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/structures/windoor.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/tools/logic_gate.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/utilities/APC.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/utilities/air_alarms.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/utilities/disposal_machines.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/utilities/intercom.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/utilities/lighting.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/utilities/solarpanel.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/utilities/station_maps.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/utilities/wallmount_generator.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/utilities/wallmount_substation.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/weapons/bladed_hats.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/weapons/bola.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/weapons/improvised_arrow.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/weapons/modular_grenade.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/weapons/modular_mine.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/weapons/shiv.yml
#	Resources/Prototypes/Recipes/Construction/Graphs/weapons/spear.yml
#	Resources/Prototypes/Recipes/Construction/structures.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/bots/cleanbot.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/bots/firebot.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/bots/honkbot.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/bots/medibot.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/bots/mimebot.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/bots/supplybot.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/improvised/firebomb.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/improvised/improvised_shotgun.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/improvised/improvised_shotgun_shell.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/improvised/makeshiftstunprod.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/improvised/pipebomb.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/improvised/pneumatic_cannon.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/improvised/potato.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/storage/cratefreezer.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/storage/tallbox.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/strawhat.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/toys.yml
#	Resources/Prototypes/Recipes/Crafting/improvised.yml
#	Resources/Prototypes/Recipes/Reactions/medicine.yml
#	Resources/Prototypes/Roles/Jobs/CentComm/official.yml
#	Resources/Textures/Clothing/Neck/mantles/cemantle.rsi/equipped-NECK.png
#	Resources/Textures/Clothing/Neck/mantles/cemantle.rsi/icon.png
#	Resources/Textures/Clothing/Neck/mantles/cemantle.rsi/meta.json
#	Resources/Textures/Clothing/Uniforms/Jumpskirt/mime.rsi/equipped-INNERCLOTHING.png
#	Resources/Textures/Clothing/Uniforms/Jumpskirt/mime.rsi/icon.png
#	Resources/Textures/Clothing/Uniforms/Jumpskirt/mime.rsi/inhand-left.png
#	Resources/Textures/Clothing/Uniforms/Jumpskirt/mime.rsi/inhand-right.png
#	Resources/Textures/Clothing/Uniforms/Jumpskirt/mime.rsi/meta.json
#	Resources/Textures/Clothing/Uniforms/Jumpsuit/mime.rsi/equipped-INNERCLOTHING.png
#	Resources/Textures/Clothing/Uniforms/Jumpsuit/mime.rsi/icon.png
#	Resources/Textures/Clothing/Uniforms/Jumpsuit/mime.rsi/inhand-left.png
#	Resources/Textures/Clothing/Uniforms/Jumpsuit/mime.rsi/inhand-right.png
#	Resources/Textures/Clothing/Uniforms/Jumpsuit/mime.rsi/meta.json
2025-05-04 23:13:12 +03:00

336 lines
13 KiB
C#

using System.IO;
using System.Linq;
using Content.Shared.Actions;
using Content.Shared.Charges.Systems;
using JetBrains.Annotations;
using Robust.Client.Player;
using Robust.Shared.ContentPack;
using Robust.Shared.GameStates;
using Robust.Shared.Input.Binding;
using Robust.Shared.Player;
using Robust.Shared.Serialization.Manager;
using Robust.Shared.Serialization.Markdown;
using Robust.Shared.Serialization.Markdown.Mapping;
using Robust.Shared.Serialization.Markdown.Sequence;
using Robust.Shared.Serialization.Markdown.Value;
using Robust.Shared.Utility;
using YamlDotNet.RepresentationModel;
namespace Content.Client.Actions
{
[UsedImplicitly]
public sealed class ActionsSystem : SharedActionsSystem
{
public delegate void OnActionReplaced(EntityUid actionId);
[Dependency] private readonly SharedChargesSystem _sharedCharges = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IResourceManager _resources = default!;
[Dependency] private readonly ISerializationManager _serialization = default!;
[Dependency] private readonly MetaDataSystem _metaData = default!;
public event Action<EntityUid>? OnActionAdded;
public event Action<EntityUid>? OnActionRemoved;
public event Action? ActionsUpdated;
public event Action<ActionsComponent>? LinkActions;
public event Action? UnlinkActions;
public event Action? ClearAssignments;
public event Action<List<SlotAssignment>>? AssignSlot;
private readonly List<EntityUid> _removed = new();
private readonly List<(EntityUid, BaseActionComponent?)> _added = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ActionsComponent, LocalPlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<ActionsComponent, LocalPlayerDetachedEvent>(OnPlayerDetached);
SubscribeLocalEvent<ActionsComponent, ComponentHandleState>(HandleComponentState);
SubscribeLocalEvent<InstantActionComponent, ComponentHandleState>(OnInstantHandleState);
SubscribeLocalEvent<EntityTargetActionComponent, ComponentHandleState>(OnEntityTargetHandleState);
SubscribeLocalEvent<WorldTargetActionComponent, ComponentHandleState>(OnWorldTargetHandleState);
SubscribeLocalEvent<EntityWorldTargetActionComponent, ComponentHandleState>(OnEntityWorldTargetHandleState);
}
private void OnInstantHandleState(EntityUid uid, InstantActionComponent component, ref ComponentHandleState args)
{
if (args.Current is not InstantActionComponentState state)
return;
BaseHandleState<InstantActionComponent>(uid, component, state);
}
private void OnEntityTargetHandleState(EntityUid uid, EntityTargetActionComponent component, ref ComponentHandleState args)
{
if (args.Current is not EntityTargetActionComponentState state)
return;
component.Whitelist = state.Whitelist;
component.Blacklist = state.Blacklist;
component.CanTargetSelf = state.CanTargetSelf;
component.IgnoreContainer = state.IgnoreContainer; // Sunrise-Edit
BaseHandleState<EntityTargetActionComponent>(uid, component, state);
}
private void OnWorldTargetHandleState(EntityUid uid, WorldTargetActionComponent component, ref ComponentHandleState args)
{
if (args.Current is not WorldTargetActionComponentState state)
return;
BaseHandleState<WorldTargetActionComponent>(uid, component, state);
}
private void OnEntityWorldTargetHandleState(EntityUid uid,
EntityWorldTargetActionComponent component,
ref ComponentHandleState args)
{
if (args.Current is not EntityWorldTargetActionComponentState state)
return;
component.Whitelist = state.Whitelist;
component.CanTargetSelf = state.CanTargetSelf;
BaseHandleState<EntityWorldTargetActionComponent>(uid, component, state);
}
private void BaseHandleState<T>(EntityUid uid, BaseActionComponent component, BaseActionComponentState state) where T : BaseActionComponent
{
// TODO ACTIONS use auto comp states
component.Icon = state.Icon;
component.IconOn = state.IconOn;
component.IconColor = state.IconColor;
component.OriginalIconColor = state.OriginalIconColor;
component.DisabledIconColor = state.DisabledIconColor;
component.Keywords.Clear();
component.Keywords.UnionWith(state.Keywords);
component.Enabled = state.Enabled;
component.Toggled = state.Toggled;
component.Cooldown = state.Cooldown;
component.UseDelay = state.UseDelay;
component.Container = EnsureEntity<T>(state.Container, uid);
component.EntityIcon = EnsureEntity<T>(state.EntityIcon, uid);
component.CheckCanInteract = state.CheckCanInteract;
component.CheckConsciousness = state.CheckConsciousness;
component.ClientExclusive = state.ClientExclusive;
component.Priority = state.Priority;
component.AttachedEntity = EnsureEntity<T>(state.AttachedEntity, uid);
component.RaiseOnUser = state.RaiseOnUser;
component.RaiseOnAction = state.RaiseOnAction;
component.AutoPopulate = state.AutoPopulate;
component.Temporary = state.Temporary;
component.ItemIconStyle = state.ItemIconStyle;
component.Sound = state.Sound;
UpdateAction(uid, component);
}
public override void UpdateAction(EntityUid? actionId, BaseActionComponent? action = null)
{
if (!ResolveActionData(actionId, ref action))
return;
// TODO: Decouple this.
action.IconColor = _sharedCharges.GetCurrentCharges(actionId.Value) == 0 ? action.DisabledIconColor : action.OriginalIconColor;
base.UpdateAction(actionId, action);
if (_playerManager.LocalEntity != action.AttachedEntity)
return;
ActionsUpdated?.Invoke();
}
private void HandleComponentState(EntityUid uid, ActionsComponent component, ref ComponentHandleState args)
{
if (args.Current is not ActionsComponentState state)
return;
_added.Clear();
_removed.Clear();
var stateEnts = EnsureEntitySet<ActionsComponent>(state.Actions, uid);
foreach (var act in component.Actions)
{
if (!stateEnts.Contains(act) && !IsClientSide(act))
_removed.Add(act);
}
component.Actions.ExceptWith(_removed);
foreach (var actionId in stateEnts)
{
if (!actionId.IsValid())
continue;
if (!component.Actions.Add(actionId))
continue;
TryGetActionData(actionId, out var action);
_added.Add((actionId, action));
}
if (_playerManager.LocalEntity != uid)
return;
foreach (var action in _removed)
{
OnActionRemoved?.Invoke(action);
}
_added.Sort(ActionComparer);
foreach (var action in _added)
{
OnActionAdded?.Invoke(action.Item1);
}
ActionsUpdated?.Invoke();
}
public static int ActionComparer((EntityUid, BaseActionComponent?) a, (EntityUid, BaseActionComponent?) b)
{
var priorityA = a.Item2?.Priority ?? 0;
var priorityB = b.Item2?.Priority ?? 0;
if (priorityA != priorityB)
return priorityA - priorityB;
priorityA = a.Item2?.Container?.Id ?? 0;
priorityB = b.Item2?.Container?.Id ?? 0;
return priorityA - priorityB;
}
protected override void ActionAdded(EntityUid performer, EntityUid actionId, ActionsComponent comp,
BaseActionComponent action)
{
if (_playerManager.LocalEntity != performer)
return;
OnActionAdded?.Invoke(actionId);
ActionsUpdated?.Invoke();
}
protected override void ActionRemoved(EntityUid performer, EntityUid actionId, ActionsComponent comp, BaseActionComponent action)
{
if (_playerManager.LocalEntity != performer)
return;
OnActionRemoved?.Invoke(actionId);
ActionsUpdated?.Invoke();
}
public IEnumerable<(EntityUid Id, BaseActionComponent Comp)> GetClientActions()
{
if (_playerManager.LocalEntity is not { } user)
return Enumerable.Empty<(EntityUid, BaseActionComponent)>();
return GetActions(user);
}
private void OnPlayerAttached(EntityUid uid, ActionsComponent component, LocalPlayerAttachedEvent args)
{
LinkAllActions(component);
}
private void OnPlayerDetached(EntityUid uid, ActionsComponent component, LocalPlayerDetachedEvent? args = null)
{
UnlinkAllActions();
}
public void UnlinkAllActions()
{
UnlinkActions?.Invoke();
}
public void LinkAllActions(ActionsComponent? actions = null)
{
if (_playerManager.LocalEntity is not { } user ||
!Resolve(user, ref actions, false))
{
return;
}
LinkActions?.Invoke(actions);
}
public override void Shutdown()
{
base.Shutdown();
CommandBinds.Unregister<ActionsSystem>();
}
public void TriggerAction(EntityUid actionId, BaseActionComponent action)
{
if (_playerManager.LocalEntity is not { } user ||
!TryComp(user, out ActionsComponent? actions))
{
return;
}
if (action is not InstantActionComponent instantAction)
return;
if (action.ClientExclusive)
{
PerformAction(user, actions, actionId, instantAction, instantAction.Event, GameTiming.CurTime);
}
else
{
var request = new RequestPerformActionEvent(GetNetEntity(actionId));
EntityManager.RaisePredictiveEvent(request);
}
}
/// <summary>
/// Load actions and their toolbar assignments from a file.
/// </summary>
public void LoadActionAssignments(string path, bool userData)
{
if (_playerManager.LocalEntity is not { } user)
return;
var file = new ResPath(path).ToRootedPath();
TextReader reader = userData
? _resources.UserData.OpenText(file)
: _resources.ContentFileReadText(file);
var yamlStream = new YamlStream();
yamlStream.Load(reader);
if (yamlStream.Documents[0].RootNode.ToDataNode() is not SequenceDataNode sequence)
return;
ClearAssignments?.Invoke();
var assignments = new List<SlotAssignment>();
foreach (var entry in sequence.Sequence)
{
if (entry is not MappingDataNode map)
continue;
if (!map.TryGet("action", out var actionNode))
continue;
var action = _serialization.Read<BaseActionComponent>(actionNode, notNullableOverride: true);
var actionId = Spawn();
AddComp(actionId, action);
AddActionDirect(user, actionId);
if (map.TryGet<ValueDataNode>("name", out var nameNode))
_metaData.SetEntityName(actionId, nameNode.Value);
if (!map.TryGet("assignments", out var assignmentNode))
continue;
var nodeAssignments = _serialization.Read<List<(byte Hotbar, byte Slot)>>(assignmentNode, notNullableOverride: true);
foreach (var index in nodeAssignments)
{
var assignment = new SlotAssignment(index.Hotbar, index.Slot, actionId);
assignments.Add(assignment);
}
}
AssignSlot?.Invoke(assignments);
}
public record struct SlotAssignment(byte Hotbar, byte Slot, EntityUid ActionId);
}
}