Cargo selling fixes (#17876)

- Prevent selling dead mobs (getting used as easy corpse disposal).
- Fix a broadcast event being raised O(n) times instead of O(1)
This commit is contained in:
metalgearsloth
2023-07-10 05:24:48 +10:00
committed by GitHub
parent ec58d2e716
commit 68b56878bc
3 changed files with 24 additions and 20 deletions

View File

@@ -188,7 +188,7 @@ namespace Content.Server.Cargo.Systems
if (!_protoMan.TryIndex<CargoProductPrototype>(args.CargoProductId, out var product)) if (!_protoMan.TryIndex<CargoProductPrototype>(args.CargoProductId, out var product))
{ {
_sawmill.Error($"Tried to add invalid cargo product {args.CargoProductId} as order!"); Log.Error($"Tried to add invalid cargo product {args.CargoProductId} as order!");
return; return;
} }

View File

@@ -231,16 +231,16 @@ public sealed partial class CargoSystem
station ??= _station.GetOwningStation(gridUid); station ??= _station.GetOwningStation(gridUid);
GetPalletGoods(gridUid, out var toSell, out amount); GetPalletGoods(gridUid, out var toSell, out amount);
_sawmill.Debug($"Cargo sold {toSell.Count} entities for {amount}"); Log.Debug($"Cargo sold {toSell.Count} entities for {amount}");
if (station != null)
{
var ev = new EntitySoldEvent(station.Value, toSell);
RaiseLocalEvent(ref ev);
}
foreach (var ent in toSell) foreach (var ent in toSell)
{ {
if (station != null)
{
var ev = new EntitySoldEvent(station.Value, toSell);
RaiseLocalEvent(ref ev);
}
Del(ent); Del(ent);
} }
} }
@@ -248,9 +248,6 @@ public sealed partial class CargoSystem
private void GetPalletGoods(EntityUid gridUid, out HashSet<EntityUid> toSell, out double amount) private void GetPalletGoods(EntityUid gridUid, out HashSet<EntityUid> toSell, out double amount)
{ {
amount = 0; amount = 0;
var xformQuery = GetEntityQuery<TransformComponent>();
var blacklistQuery = GetEntityQuery<CargoSellBlacklistComponent>();
var mobStateQuery = GetEntityQuery<MobStateComponent>();
toSell = new HashSet<EntityUid>(); toSell = new HashSet<EntityUid>();
foreach (var (palletUid, _) in GetCargoPallets(gridUid)) foreach (var (palletUid, _) in GetCargoPallets(gridUid))
@@ -263,13 +260,13 @@ public sealed partial class CargoSystem
// - anything anchored (e.g. light fixtures) // - anything anchored (e.g. light fixtures)
// - anything blacklisted (e.g. players). // - anything blacklisted (e.g. players).
if (toSell.Contains(ent) || if (toSell.Contains(ent) ||
xformQuery.TryGetComponent(ent, out var xform) && _xformQuery.TryGetComponent(ent, out var xform) &&
(xform.Anchored || !CanSell(ent, xform, mobStateQuery, xformQuery))) (xform.Anchored || !CanSell(ent, xform)))
{ {
continue; continue;
} }
if (blacklistQuery.HasComponent(ent)) if (_blacklistQuery.HasComponent(ent))
continue; continue;
var price = _pricing.GetPrice(ent); var price = _pricing.GetPrice(ent);
@@ -281,10 +278,9 @@ public sealed partial class CargoSystem
} }
} }
private bool CanSell(EntityUid uid, TransformComponent xform, EntityQuery<MobStateComponent> mobStateQuery, EntityQuery<TransformComponent> xformQuery) private bool CanSell(EntityUid uid, TransformComponent xform)
{ {
if (mobStateQuery.TryGetComponent(uid, out var mobState) && if (_mobQuery.HasComponent(uid))
mobState.CurrentState != MobState.Dead)
{ {
return false; return false;
} }
@@ -293,7 +289,7 @@ public sealed partial class CargoSystem
var children = xform.ChildEnumerator; var children = xform.ChildEnumerator;
while (children.MoveNext(out var child)) while (children.MoveNext(out var child))
{ {
if (!CanSell(child.Value, xformQuery.GetComponent(child.Value), mobStateQuery, xformQuery)) if (!CanSell(child.Value, _xformQuery.GetComponent(child.Value)))
return false; return false;
} }

View File

@@ -10,6 +10,7 @@ using Content.Shared.Access.Systems;
using Content.Shared.Administration.Logs; using Content.Shared.Administration.Logs;
using Content.Shared.Cargo; using Content.Shared.Cargo;
using Content.Shared.Containers.ItemSlots; using Content.Shared.Containers.ItemSlots;
using Content.Shared.Mobs.Components;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Shared.Configuration; using Robust.Shared.Configuration;
@@ -45,12 +46,18 @@ public sealed partial class CargoSystem : SharedCargoSystem
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!; [Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
[Dependency] private readonly MetaDataSystem _metaSystem = default!; [Dependency] private readonly MetaDataSystem _metaSystem = default!;
private ISawmill _sawmill = default!; private EntityQuery<TransformComponent> _xformQuery;
private EntityQuery<CargoSellBlacklistComponent> _blacklistQuery;
private EntityQuery<MobStateComponent> _mobQuery;
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
_sawmill = Logger.GetSawmill("cargo");
_xformQuery = GetEntityQuery<TransformComponent>();
_blacklistQuery = GetEntityQuery<CargoSellBlacklistComponent>();
_mobQuery = GetEntityQuery<MobStateComponent>();
InitializeConsole(); InitializeConsole();
InitializeShuttle(); InitializeShuttle();
InitializeTelepad(); InitializeTelepad();
@@ -60,6 +67,7 @@ public sealed partial class CargoSystem : SharedCargoSystem
public override void Shutdown() public override void Shutdown()
{ {
base.Shutdown(); base.Shutdown();
ShutdownShuttle();
CleanupCargoShuttle(); CleanupCargoShuttle();
} }