Fix flatpacker (#28736)

* Fix flatpacker

* a
This commit is contained in:
Leon Friedrich
2024-06-08 20:48:23 +12:00
committed by GitHub
parent c2bbc72cf4
commit 2c7598fd5b
3 changed files with 62 additions and 55 deletions

View File

@@ -68,7 +68,7 @@ public sealed partial class FlatpackCreatorMenu : FancyWindow
if (_entityManager.TryGetComponent<MachineBoardComponent>(_currentBoard, out var machineBoardComp)) if (_entityManager.TryGetComponent<MachineBoardComponent>(_currentBoard, out var machineBoardComp))
cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker), (_currentBoard.Value, machineBoardComp)); cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker), (_currentBoard.Value, machineBoardComp));
else else
cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker)); cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker), null);
PackButton.Disabled = !_materialStorage.CanChangeMaterialAmount(_owner, cost); PackButton.Disabled = !_materialStorage.CanChangeMaterialAmount(_owner, cost);
} }
@@ -93,7 +93,7 @@ public sealed partial class FlatpackCreatorMenu : FancyWindow
else if (_entityManager.TryGetComponent<ComputerBoardComponent>(_currentBoard, out var computerBoard)) else if (_entityManager.TryGetComponent<ComputerBoardComponent>(_currentBoard, out var computerBoard))
{ {
prototype = computerBoard.Prototype; prototype = computerBoard.Prototype;
cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker)); cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker), null);
} }
if (prototype is not null && cost is not null) if (prototype is not null && cost is not null)

View File

@@ -4,6 +4,7 @@ using Content.Server.Power.EntitySystems;
using Content.Shared.Construction; using Content.Shared.Construction;
using Content.Shared.Construction.Components; using Content.Shared.Construction.Components;
using Content.Shared.Containers.ItemSlots; using Content.Shared.Containers.ItemSlots;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing; using Robust.Shared.Timing;
namespace Content.Server.Construction; namespace Content.Server.Construction;
@@ -30,21 +31,24 @@ public sealed class FlatpackSystem : SharedFlatpackSystem
if (!this.IsPowered(ent, EntityManager) || comp.Packing) if (!this.IsPowered(ent, EntityManager) || comp.Packing)
return; 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; return;
Dictionary<string, int>? cost = null; Dictionary<string, int> cost;
if (TryComp<MachineBoardComponent>(machineBoard, out var machineBoardComponent)) if (TryComp<MachineBoardComponent>(board, out var machine))
cost = GetFlatpackCreationCost(ent, (machineBoard, machineBoardComponent)); cost = GetFlatpackCreationCost(ent, (board, machine));
if (HasComp<ComputerBoardComponent>(machineBoard)) else if (TryComp<ComputerBoardComponent>(board, out var computer) && computer.Prototype != null)
cost = GetFlatpackCreationCost(ent); cost = GetFlatpackCreationCost(ent, null);
else
if (cost is null) {
Log.Error($"Encountered invalid flatpack board while packing: {ToPrettyString(board)}");
return; return;
}
if (!MaterialStorage.CanChangeMaterialAmount(uid, cost)) if (!MaterialStorage.CanChangeMaterialAmount(uid, cost))
return; return;
_itemSlots.SetLock(uid, comp.SlotId, true);
comp.Packing = true; comp.Packing = true;
comp.PackEndTime = _timing.CurTime + comp.PackDuration; comp.PackEndTime = _timing.CurTime + comp.PackDuration;
Appearance.SetData(uid, FlatpackCreatorVisuals.Packing, true); Appearance.SetData(uid, FlatpackCreatorVisuals.Packing, true);
@@ -63,6 +67,7 @@ public sealed class FlatpackSystem : SharedFlatpackSystem
{ {
var (uid, comp) = ent; var (uid, comp) = ent;
_itemSlots.SetLock(uid, comp.SlotId, false);
comp.Packing = false; comp.Packing = false;
Appearance.SetData(uid, FlatpackCreatorVisuals.Packing, false); Appearance.SetData(uid, FlatpackCreatorVisuals.Packing, false);
_ambientSound.SetAmbience(uid, false); _ambientSound.SetAmbience(uid, false);
@@ -71,24 +76,33 @@ public sealed class FlatpackSystem : SharedFlatpackSystem
if (interrupted) if (interrupted)
return; 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; return;
Dictionary<string, int>? cost = null; Dictionary<string, int> cost;
if (TryComp<MachineBoardComponent>(machineBoard, out var machineBoardComponent)) EntProtoId proto;
cost = GetFlatpackCreationCost(ent, (machineBoard, machineBoardComponent)); if (TryComp<MachineBoardComponent>(board, out var machine))
if (HasComp<ComputerBoardComponent>(machineBoard)) {
cost = GetFlatpackCreationCost(ent); cost = GetFlatpackCreationCost(ent, (board, machine));
proto = machine.Prototype;
if (cost is null) }
else if (TryComp<ComputerBoardComponent>(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; return;
}
if (!MaterialStorage.TryChangeMaterialAmount((ent, null), cost)) if (!MaterialStorage.TryChangeMaterialAmount((ent, null), cost))
return; return;
var flatpack = Spawn(comp.BaseFlatpackPrototype, Transform(ent).Coordinates); var flatpack = Spawn(comp.BaseFlatpackPrototype, Transform(ent).Coordinates);
SetupFlatpack(flatpack, machineBoard); SetupFlatpack(flatpack, proto, board);
Del(machineBoard); Del(board);
} }
public override void Update(float frameTime) public override void Update(float frameTime)

View File

@@ -1,5 +1,6 @@
using Content.Shared.Construction.Components; using Content.Shared.Construction.Components;
using Content.Shared.Administration.Logs; using Content.Shared.Administration.Logs;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.Database; using Content.Shared.Database;
using Content.Shared.Examine; using Content.Shared.Examine;
using Content.Shared.Interaction; using Content.Shared.Interaction;
@@ -10,7 +11,6 @@ using Robust.Shared.Audio.Systems;
using Robust.Shared.Containers; using Robust.Shared.Containers;
using Robust.Shared.Map.Components; using Robust.Shared.Map.Components;
using Robust.Shared.Network; using Robust.Shared.Network;
using Robust.Shared.Physics.Components;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
namespace Content.Shared.Construction; namespace Content.Shared.Construction;
@@ -37,7 +37,21 @@ public abstract class SharedFlatpackSystem : EntitySystem
SubscribeLocalEvent<FlatpackComponent, InteractUsingEvent>(OnFlatpackInteractUsing); SubscribeLocalEvent<FlatpackComponent, InteractUsingEvent>(OnFlatpackInteractUsing);
SubscribeLocalEvent<FlatpackComponent, ExaminedEvent>(OnFlatpackExamined); SubscribeLocalEvent<FlatpackComponent, ExaminedEvent>(OnFlatpackExamined);
SubscribeLocalEvent<FlatpackCreatorComponent, ContainerIsRemovingAttemptEvent>(OnCreatorRemovingAttempt); SubscribeLocalEvent<FlatpackCreatorComponent, ItemSlotInsertAttemptEvent>(OnInsertAttempt);
}
private void OnInsertAttempt(Entity<FlatpackCreatorComponent> ent, ref ItemSlotInsertAttemptEvent args)
{
if (args.Slot.ID != ent.Comp.SlotId || args.Cancelled)
return;
if (HasComp<MachineBoardComponent>(args.Item))
return;
if (TryComp<ComputerBoardComponent>(args.Item, out var computer) && computer.Prototype != null)
return;
args.Cancelled = true;
} }
private void OnFlatpackInteractUsing(Entity<FlatpackComponent> ent, ref InteractUsingEvent args) private void OnFlatpackInteractUsing(Entity<FlatpackComponent> ent, ref InteractUsingEvent args)
@@ -65,19 +79,12 @@ public abstract class SharedFlatpackSystem : EntitySystem
var buildPos = _map.TileIndicesFor(grid, gridComp, xform.Coordinates); var buildPos = _map.TileIndicesFor(grid, gridComp, xform.Coordinates);
var coords = _map.ToCenterCoordinates(grid, buildPos); var coords = _map.ToCenterCoordinates(grid, buildPos);
var intersecting = _entityLookup.GetEntitiesIntersecting(coords, LookupFlags.Dynamic | LookupFlags.Static); // TODO FLATPAK
// Make this logic smarter. This should eventually allow for shit like building microwaves on tables and such.
// todo make this logic smarter. // Also: make it ignore ghosts
// This should eventually allow for shit like building microwaves on tables and such. if (_entityLookup.AnyEntitiesIntersecting(coords, LookupFlags.Dynamic | LookupFlags.Static))
foreach (var intersect in intersecting)
{ {
if (!TryComp<PhysicsComponent>(intersect, out var intersectBody)) // this popup is on the server because the predicts on the intersection is crazy
continue;
if (!intersectBody.Hard || !intersectBody.CanCollide)
continue;
// this popup is on the server because the mispredicts on the intersection is crazy
if (_net.IsServer) if (_net.IsServer)
_popup.PopupEntity(Loc.GetString("flatpack-unpack-no-room"), uid, args.User); _popup.PopupEntity(Loc.GetString("flatpack-unpack-no-room"), uid, args.User);
return; return;
@@ -102,38 +109,24 @@ public abstract class SharedFlatpackSystem : EntitySystem
args.PushMarkup(Loc.GetString("flatpack-examine")); args.PushMarkup(Loc.GetString("flatpack-examine"));
} }
private void OnCreatorRemovingAttempt(Entity<FlatpackCreatorComponent> ent, ref ContainerIsRemovingAttemptEvent args) protected void SetupFlatpack(Entity<FlatpackComponent?> ent, EntProtoId proto, EntityUid board)
{
if (args.Container.ID == ent.Comp.SlotId && ent.Comp.Packing)
args.Cancel();
}
public void SetupFlatpack(Entity<FlatpackComponent?> ent, EntityUid? board)
{ {
if (!Resolve(ent, ref ent.Comp)) if (!Resolve(ent, ref ent.Comp))
return; return;
var machinePrototypeId = new EntProtoId(); ent.Comp.Entity = proto;
if (TryComp<ComputerBoardComponent>(board, out var computerBoard) && computerBoard.Prototype is not null) var machinePrototype = PrototypeManager.Index<EntityPrototype>(proto);
machinePrototypeId = computerBoard.Prototype;
var comp = ent.Comp!;
var machinePrototype = PrototypeManager.Index(machinePrototypeId);
var meta = MetaData(ent); var meta = MetaData(ent);
_metaData.SetEntityName(ent, Loc.GetString("flatpack-entity-name", ("name", machinePrototype.Name)), meta); _metaData.SetEntityName(ent, Loc.GetString("flatpack-entity-name", ("name", machinePrototype.Name)), meta);
_metaData.SetEntityDescription(ent, Loc.GetString("flatpack-entity-description", ("name", machinePrototype.Name)), meta); _metaData.SetEntityDescription(ent, Loc.GetString("flatpack-entity-description", ("name", machinePrototype.Name)), meta);
comp.Entity = machinePrototypeId; Dirty(ent, meta);
Dirty(ent, comp); Appearance.SetData(ent, FlatpackVisuals.Machine, MetaData(board).EntityPrototype?.ID ?? string.Empty);
if (board is null)
return;
Appearance.SetData(ent, FlatpackVisuals.Machine, MetaData(board.Value).EntityPrototype?.ID ?? string.Empty);
} }
public Dictionary<string, int> GetFlatpackCreationCost(Entity<FlatpackCreatorComponent> entity, Entity<MachineBoardComponent>? machineBoard = null) /// <param name="machineBoard">The machine board to pack. If null, this implies we are packing a computer board</param>
public Dictionary<string, int> GetFlatpackCreationCost(Entity<FlatpackCreatorComponent> entity, Entity<MachineBoardComponent>? machineBoard)
{ {
Dictionary<string, int> cost = new(); Dictionary<string, int> cost = new();
Dictionary<ProtoId<MaterialPrototype>, int> baseCost; Dictionary<ProtoId<MaterialPrototype>, int> baseCost;