Predict PickRandom verb (#39326)

Co-authored-by: ArtisticRoomba <145879011+ArtisticRoomba@users.noreply.github.com>
This commit is contained in:
slarticodefast
2025-08-06 04:29:13 +02:00
committed by GitHub
parent 983cebb69d
commit 99336a33fb
2 changed files with 21 additions and 13 deletions

View File

@@ -1,31 +1,32 @@
using Content.Server.Storage.EntitySystems; using Content.Shared.Storage.EntitySystems;
using Content.Shared.Whitelist; using Content.Shared.Whitelist;
using Robust.Shared.GameStates;
namespace Content.Server.Storage.Components; namespace Content.Shared.Storage.Components;
/// <summary> /// <summary>
/// Adds a verb to pick a random item from a container. /// Adds a verb to pick a random item from a container.
/// Only picks items that match the whitelist. /// Only picks items that match the whitelist.
/// </summary> /// </summary>
[RegisterComponent] [RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[Access(typeof(PickRandomSystem))] [Access(typeof(PickRandomSystem))]
public sealed partial class PickRandomComponent : Component public sealed partial class PickRandomComponent : Component
{ {
/// <summary> /// <summary>
/// Whitelist for potential picked items. /// Whitelist for potential picked items.
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)] [DataField, AutoNetworkedField]
public EntityWhitelist? Whitelist; public EntityWhitelist? Whitelist;
/// <summary> /// <summary>
/// Locale id for the pick verb text. /// Locale id for the pick verb text.
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)] [DataField, AutoNetworkedField]
public LocId VerbText = "comp-pick-random-verb-text"; public LocId VerbText = "comp-pick-random-verb-text";
/// <summary> /// <summary>
/// Locale id for the empty storage message. /// Locale id for the empty storage message.
/// </summary> /// </summary>
[DataField, ViewVariables(VVAccess.ReadWrite)] [DataField, AutoNetworkedField]
public LocId EmptyText = "comp-pick-random-empty"; public LocId EmptyText = "comp-pick-random-empty";
} }

View File

@@ -1,22 +1,22 @@
using System.Linq; using System.Linq;
using Content.Server.Storage.Components;
using Content.Shared.Database; using Content.Shared.Database;
using Content.Shared.Hands.EntitySystems; using Content.Shared.Hands.EntitySystems;
using Content.Shared.Storage; using Content.Shared.Storage.Components;
using Content.Shared.Verbs; using Content.Shared.Verbs;
using Content.Shared.Whitelist; using Content.Shared.Whitelist;
using Robust.Shared.Containers; using Robust.Shared.Containers;
using Robust.Shared.Network;
using Robust.Shared.Random; using Robust.Shared.Random;
namespace Content.Server.Storage.EntitySystems; namespace Content.Shared.Storage.EntitySystems;
// TODO: move this to shared for verb prediction if/when storage is in shared
public sealed class PickRandomSystem : EntitySystem public sealed class PickRandomSystem : EntitySystem
{ {
[Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
[Dependency] private readonly INetManager _net = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedContainerSystem _container = default!; [Dependency] private readonly SharedContainerSystem _container = default!;
[Dependency] private readonly SharedHandsSystem _hands = default!; [Dependency] private readonly SharedHandsSystem _hands = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly EntityWhitelistSystem _whitelistSystem = default!;
public override void Initialize() public override void Initialize()
{ {
@@ -50,12 +50,19 @@ public sealed class PickRandomSystem : EntitySystem
private void TryPick(EntityUid uid, PickRandomComponent comp, StorageComponent storage, EntityUid user) private void TryPick(EntityUid uid, PickRandomComponent comp, StorageComponent storage, EntityUid user)
{ {
// It's hard to predict picking a random entity from a container since the contained entity list will have a different order on the server and client.
// One idea might be to sort them by NetEntity ID, but that is expensive if there are a lot of entities.
// Another option would be to make this client authorative.
if (_net.IsClient)
return;
var entities = storage.Container.ContainedEntities.Where(item => _whitelistSystem.IsWhitelistPassOrNull(comp.Whitelist, item)).ToArray(); var entities = storage.Container.ContainedEntities.Where(item => _whitelistSystem.IsWhitelistPassOrNull(comp.Whitelist, item)).ToArray();
if (!entities.Any()) if (entities.Length == 0)
return; return;
var picked = _random.Pick(entities); var picked = _random.Pick(entities);
// if it fails to go into a hand of the user, will be on the storage // if it fails to go into a hand of the user, will be on the storage
_container.AttachParentToContainerOrGrid((picked, Transform(picked))); _container.AttachParentToContainerOrGrid((picked, Transform(picked)));