diff --git a/Content.Client/Construction/UI/FlatpackCreatorMenu.xaml.cs b/Content.Client/Construction/UI/FlatpackCreatorMenu.xaml.cs index eae70d5367..9f3d5695bb 100644 --- a/Content.Client/Construction/UI/FlatpackCreatorMenu.xaml.cs +++ b/Content.Client/Construction/UI/FlatpackCreatorMenu.xaml.cs @@ -68,7 +68,7 @@ public sealed partial class FlatpackCreatorMenu : FancyWindow if (_entityManager.TryGetComponent(_currentBoard, out var machineBoardComp)) cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker), (_currentBoard.Value, machineBoardComp)); else - cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker)); + cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker), null); PackButton.Disabled = !_materialStorage.CanChangeMaterialAmount(_owner, cost); } @@ -93,7 +93,7 @@ public sealed partial class FlatpackCreatorMenu : FancyWindow else if (_entityManager.TryGetComponent(_currentBoard, out var computerBoard)) { prototype = computerBoard.Prototype; - cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker)); + cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker), null); } if (prototype is not null && cost is not null) diff --git a/Content.Server/Construction/FlatpackSystem.cs b/Content.Server/Construction/FlatpackSystem.cs index be082eba30..af2132723c 100644 --- a/Content.Server/Construction/FlatpackSystem.cs +++ b/Content.Server/Construction/FlatpackSystem.cs @@ -4,6 +4,7 @@ using Content.Server.Power.EntitySystems; using Content.Shared.Construction; using Content.Shared.Construction.Components; using Content.Shared.Containers.ItemSlots; +using Robust.Shared.Prototypes; using Robust.Shared.Timing; namespace Content.Server.Construction; @@ -30,21 +31,24 @@ public sealed class FlatpackSystem : SharedFlatpackSystem if (!this.IsPowered(ent, EntityManager) || comp.Packing) return; - if (!_itemSlots.TryGetSlot(uid, comp.SlotId, out var itemSlot) || itemSlot.Item is not { } machineBoard) + if (!_itemSlots.TryGetSlot(uid, comp.SlotId, out var itemSlot) || itemSlot.Item is not { } board) return; - Dictionary? cost = null; - if (TryComp(machineBoard, out var machineBoardComponent)) - cost = GetFlatpackCreationCost(ent, (machineBoard, machineBoardComponent)); - if (HasComp(machineBoard)) - cost = GetFlatpackCreationCost(ent); - - if (cost is null) + Dictionary cost; + if (TryComp(board, out var machine)) + cost = GetFlatpackCreationCost(ent, (board, machine)); + else if (TryComp(board, out var computer) && computer.Prototype != null) + cost = GetFlatpackCreationCost(ent, null); + else + { + Log.Error($"Encountered invalid flatpack board while packing: {ToPrettyString(board)}"); return; + } if (!MaterialStorage.CanChangeMaterialAmount(uid, cost)) return; + _itemSlots.SetLock(uid, comp.SlotId, true); comp.Packing = true; comp.PackEndTime = _timing.CurTime + comp.PackDuration; Appearance.SetData(uid, FlatpackCreatorVisuals.Packing, true); @@ -63,6 +67,7 @@ public sealed class FlatpackSystem : SharedFlatpackSystem { var (uid, comp) = ent; + _itemSlots.SetLock(uid, comp.SlotId, false); comp.Packing = false; Appearance.SetData(uid, FlatpackCreatorVisuals.Packing, false); _ambientSound.SetAmbience(uid, false); @@ -71,24 +76,33 @@ public sealed class FlatpackSystem : SharedFlatpackSystem if (interrupted) return; - if (!_itemSlots.TryGetSlot(uid, comp.SlotId, out var itemSlot) || itemSlot.Item is not { } machineBoard) + if (!_itemSlots.TryGetSlot(uid, comp.SlotId, out var itemSlot) || itemSlot.Item is not { } board) return; - Dictionary? cost = null; - if (TryComp(machineBoard, out var machineBoardComponent)) - cost = GetFlatpackCreationCost(ent, (machineBoard, machineBoardComponent)); - if (HasComp(machineBoard)) - cost = GetFlatpackCreationCost(ent); - - if (cost is null) + Dictionary cost; + EntProtoId proto; + if (TryComp(board, out var machine)) + { + cost = GetFlatpackCreationCost(ent, (board, machine)); + proto = machine.Prototype; + } + else if (TryComp(board, out var computer) && computer.Prototype != null) + { + cost = GetFlatpackCreationCost(ent, null); + proto = computer.Prototype; + } + else + { + Log.Error($"Encountered invalid flatpack board while packing: {ToPrettyString(board)}"); return; + } if (!MaterialStorage.TryChangeMaterialAmount((ent, null), cost)) return; var flatpack = Spawn(comp.BaseFlatpackPrototype, Transform(ent).Coordinates); - SetupFlatpack(flatpack, machineBoard); - Del(machineBoard); + SetupFlatpack(flatpack, proto, board); + Del(board); } public override void Update(float frameTime) diff --git a/Content.Shared/Construction/SharedFlatpackSystem.cs b/Content.Shared/Construction/SharedFlatpackSystem.cs index 62d2089fb7..a83948b167 100644 --- a/Content.Shared/Construction/SharedFlatpackSystem.cs +++ b/Content.Shared/Construction/SharedFlatpackSystem.cs @@ -1,5 +1,6 @@ using Content.Shared.Construction.Components; using Content.Shared.Administration.Logs; +using Content.Shared.Containers.ItemSlots; using Content.Shared.Database; using Content.Shared.Examine; using Content.Shared.Interaction; @@ -10,7 +11,6 @@ using Robust.Shared.Audio.Systems; using Robust.Shared.Containers; using Robust.Shared.Map.Components; using Robust.Shared.Network; -using Robust.Shared.Physics.Components; using Robust.Shared.Prototypes; namespace Content.Shared.Construction; @@ -37,7 +37,21 @@ public abstract class SharedFlatpackSystem : EntitySystem SubscribeLocalEvent(OnFlatpackInteractUsing); SubscribeLocalEvent(OnFlatpackExamined); - SubscribeLocalEvent(OnCreatorRemovingAttempt); + SubscribeLocalEvent(OnInsertAttempt); + } + + private void OnInsertAttempt(Entity ent, ref ItemSlotInsertAttemptEvent args) + { + if (args.Slot.ID != ent.Comp.SlotId || args.Cancelled) + return; + + if (HasComp(args.Item)) + return; + + if (TryComp(args.Item, out var computer) && computer.Prototype != null) + return; + + args.Cancelled = true; } private void OnFlatpackInteractUsing(Entity ent, ref InteractUsingEvent args) @@ -65,19 +79,12 @@ public abstract class SharedFlatpackSystem : EntitySystem var buildPos = _map.TileIndicesFor(grid, gridComp, xform.Coordinates); var coords = _map.ToCenterCoordinates(grid, buildPos); - var intersecting = _entityLookup.GetEntitiesIntersecting(coords, LookupFlags.Dynamic | LookupFlags.Static); - - // todo make this logic smarter. - // This should eventually allow for shit like building microwaves on tables and such. - foreach (var intersect in intersecting) + // TODO FLATPAK + // Make this logic smarter. This should eventually allow for shit like building microwaves on tables and such. + // Also: make it ignore ghosts + if (_entityLookup.AnyEntitiesIntersecting(coords, LookupFlags.Dynamic | LookupFlags.Static)) { - if (!TryComp(intersect, out var intersectBody)) - continue; - - if (!intersectBody.Hard || !intersectBody.CanCollide) - continue; - - // this popup is on the server because the mispredicts on the intersection is crazy + // this popup is on the server because the predicts on the intersection is crazy if (_net.IsServer) _popup.PopupEntity(Loc.GetString("flatpack-unpack-no-room"), uid, args.User); return; @@ -102,38 +109,24 @@ public abstract class SharedFlatpackSystem : EntitySystem args.PushMarkup(Loc.GetString("flatpack-examine")); } - private void OnCreatorRemovingAttempt(Entity ent, ref ContainerIsRemovingAttemptEvent args) - { - if (args.Container.ID == ent.Comp.SlotId && ent.Comp.Packing) - args.Cancel(); - } - - public void SetupFlatpack(Entity ent, EntityUid? board) + protected void SetupFlatpack(Entity ent, EntProtoId proto, EntityUid board) { if (!Resolve(ent, ref ent.Comp)) return; - var machinePrototypeId = new EntProtoId(); - if (TryComp(board, out var computerBoard) && computerBoard.Prototype is not null) - machinePrototypeId = computerBoard.Prototype; - - var comp = ent.Comp!; - var machinePrototype = PrototypeManager.Index(machinePrototypeId); + ent.Comp.Entity = proto; + var machinePrototype = PrototypeManager.Index(proto); var meta = MetaData(ent); _metaData.SetEntityName(ent, Loc.GetString("flatpack-entity-name", ("name", machinePrototype.Name)), meta); _metaData.SetEntityDescription(ent, Loc.GetString("flatpack-entity-description", ("name", machinePrototype.Name)), meta); - comp.Entity = machinePrototypeId; - Dirty(ent, comp); - - if (board is null) - return; - - Appearance.SetData(ent, FlatpackVisuals.Machine, MetaData(board.Value).EntityPrototype?.ID ?? string.Empty); + Dirty(ent, meta); + Appearance.SetData(ent, FlatpackVisuals.Machine, MetaData(board).EntityPrototype?.ID ?? string.Empty); } - public Dictionary GetFlatpackCreationCost(Entity entity, Entity? machineBoard = null) + /// The machine board to pack. If null, this implies we are packing a computer board + public Dictionary GetFlatpackCreationCost(Entity entity, Entity? machineBoard) { Dictionary cost = new(); Dictionary, int> baseCost;