From 68b56878bc79318fba40f10ae952a522f0a9d22b Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Mon, 10 Jul 2023 05:24:48 +1000 Subject: [PATCH] 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) --- .../Cargo/Systems/CargoSystem.Orders.cs | 2 +- .../Cargo/Systems/CargoSystem.Shuttle.cs | 30 ++++++++----------- Content.Server/Cargo/Systems/CargoSystem.cs | 12 ++++++-- 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Content.Server/Cargo/Systems/CargoSystem.Orders.cs b/Content.Server/Cargo/Systems/CargoSystem.Orders.cs index 1e78dc3575..0f49b8d944 100644 --- a/Content.Server/Cargo/Systems/CargoSystem.Orders.cs +++ b/Content.Server/Cargo/Systems/CargoSystem.Orders.cs @@ -188,7 +188,7 @@ namespace Content.Server.Cargo.Systems if (!_protoMan.TryIndex(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; } diff --git a/Content.Server/Cargo/Systems/CargoSystem.Shuttle.cs b/Content.Server/Cargo/Systems/CargoSystem.Shuttle.cs index e596360c87..c8864c2648 100644 --- a/Content.Server/Cargo/Systems/CargoSystem.Shuttle.cs +++ b/Content.Server/Cargo/Systems/CargoSystem.Shuttle.cs @@ -231,16 +231,16 @@ public sealed partial class CargoSystem station ??= _station.GetOwningStation(gridUid); 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) { - if (station != null) - { - var ev = new EntitySoldEvent(station.Value, toSell); - RaiseLocalEvent(ref ev); - } - Del(ent); } } @@ -248,9 +248,6 @@ public sealed partial class CargoSystem private void GetPalletGoods(EntityUid gridUid, out HashSet toSell, out double amount) { amount = 0; - var xformQuery = GetEntityQuery(); - var blacklistQuery = GetEntityQuery(); - var mobStateQuery = GetEntityQuery(); toSell = new HashSet(); foreach (var (palletUid, _) in GetCargoPallets(gridUid)) @@ -263,13 +260,13 @@ public sealed partial class CargoSystem // - anything anchored (e.g. light fixtures) // - anything blacklisted (e.g. players). if (toSell.Contains(ent) || - xformQuery.TryGetComponent(ent, out var xform) && - (xform.Anchored || !CanSell(ent, xform, mobStateQuery, xformQuery))) + _xformQuery.TryGetComponent(ent, out var xform) && + (xform.Anchored || !CanSell(ent, xform))) { continue; } - if (blacklistQuery.HasComponent(ent)) + if (_blacklistQuery.HasComponent(ent)) continue; var price = _pricing.GetPrice(ent); @@ -281,10 +278,9 @@ public sealed partial class CargoSystem } } - private bool CanSell(EntityUid uid, TransformComponent xform, EntityQuery mobStateQuery, EntityQuery xformQuery) + private bool CanSell(EntityUid uid, TransformComponent xform) { - if (mobStateQuery.TryGetComponent(uid, out var mobState) && - mobState.CurrentState != MobState.Dead) + if (_mobQuery.HasComponent(uid)) { return false; } @@ -293,7 +289,7 @@ public sealed partial class CargoSystem var children = xform.ChildEnumerator; 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; } diff --git a/Content.Server/Cargo/Systems/CargoSystem.cs b/Content.Server/Cargo/Systems/CargoSystem.cs index b5926dcfd6..32b63f7105 100644 --- a/Content.Server/Cargo/Systems/CargoSystem.cs +++ b/Content.Server/Cargo/Systems/CargoSystem.cs @@ -10,6 +10,7 @@ using Content.Shared.Access.Systems; using Content.Shared.Administration.Logs; using Content.Shared.Cargo; using Content.Shared.Containers.ItemSlots; +using Content.Shared.Mobs.Components; using JetBrains.Annotations; using Robust.Server.GameObjects; using Robust.Shared.Configuration; @@ -45,12 +46,18 @@ public sealed partial class CargoSystem : SharedCargoSystem [Dependency] private readonly UserInterfaceSystem _uiSystem = default!; [Dependency] private readonly MetaDataSystem _metaSystem = default!; - private ISawmill _sawmill = default!; + private EntityQuery _xformQuery; + private EntityQuery _blacklistQuery; + private EntityQuery _mobQuery; public override void Initialize() { base.Initialize(); - _sawmill = Logger.GetSawmill("cargo"); + + _xformQuery = GetEntityQuery(); + _blacklistQuery = GetEntityQuery(); + _mobQuery = GetEntityQuery(); + InitializeConsole(); InitializeShuttle(); InitializeTelepad(); @@ -60,6 +67,7 @@ public sealed partial class CargoSystem : SharedCargoSystem public override void Shutdown() { base.Shutdown(); + ShutdownShuttle(); CleanupCargoShuttle(); }