Flatpacks and the Flatpacker 1001 (#23338)
* Flatpacker and flatpacks * ok that's good enough * convert solars/AME to flatpacks * mats, mats, we are the mats * basic mechanics are DONE * thing * final UI * sloth * rped jumpscare * rename
This commit is contained in:
51
Content.Shared/Construction/Components/FlatpackComponent.cs
Normal file
51
Content.Shared/Construction/Components/FlatpackComponent.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using Content.Shared.Tools;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.Construction.Components;
|
||||
|
||||
/// <summary>
|
||||
/// This is used for an object that can instantly create a machine upon having a tool applied to it.
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
|
||||
[Access(typeof(SharedFlatpackSystem))]
|
||||
public sealed partial class FlatpackComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// The tool quality that, upon used to interact with this object, will create the <see cref="Entity"/>
|
||||
/// </summary>
|
||||
[DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
|
||||
public ProtoId<ToolQualityPrototype> QualityNeeded = "Pulsing";
|
||||
|
||||
/// <summary>
|
||||
/// The entity that is spawned when this object is unpacked.
|
||||
/// </summary>
|
||||
[DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
|
||||
public EntProtoId? Entity;
|
||||
|
||||
/// <summary>
|
||||
/// Sound effect played upon the object being unpacked.
|
||||
/// </summary>
|
||||
[DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField]
|
||||
public SoundSpecifier UnpackSound = new SoundPathSpecifier("/Audio/Effects/unwrap.ogg");
|
||||
|
||||
/// <summary>
|
||||
/// A dictionary relating a machine board sprite state to a color used for the overlay.
|
||||
/// Kinda shitty but it gets the job done.
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public Dictionary<string, Color> BoardColors = new();
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public enum FlatpackVisuals : byte
|
||||
{
|
||||
Machine
|
||||
}
|
||||
|
||||
public enum FlatpackVisualLayers : byte
|
||||
{
|
||||
Overlay
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using Content.Shared.Materials;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
|
||||
|
||||
namespace Content.Shared.Construction.Components;
|
||||
|
||||
/// <summary>
|
||||
/// This is used for a machine that creates flatpacks at the cost of materials
|
||||
/// </summary>
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
[Access(typeof(SharedFlatpackSystem))]
|
||||
[AutoGenerateComponentState]
|
||||
public sealed partial class FlatpackCreatorComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether or not packing is occuring
|
||||
/// </summary>
|
||||
[DataField, ViewVariables(VVAccess.ReadWrite)]
|
||||
[AutoNetworkedField]
|
||||
public bool Packing;
|
||||
|
||||
/// <summary>
|
||||
/// The time at which packing ends
|
||||
/// </summary>
|
||||
[DataField(customTypeSerializer: typeof(TimeOffsetSerializer)), ViewVariables(VVAccess.ReadWrite)]
|
||||
[AutoNetworkedField]
|
||||
public TimeSpan PackEndTime;
|
||||
|
||||
/// <summary>
|
||||
/// How long packing lasts.
|
||||
/// </summary>
|
||||
[DataField, ViewVariables(VVAccess.ReadWrite)]
|
||||
public TimeSpan PackDuration = TimeSpan.FromSeconds(3);
|
||||
|
||||
/// <summary>
|
||||
/// The prototype used when spawning a flatpack.
|
||||
/// </summary>
|
||||
[DataField, ViewVariables(VVAccess.ReadWrite)]
|
||||
public EntProtoId BaseFlatpackPrototype = "BaseFlatpack";
|
||||
|
||||
/// <summary>
|
||||
/// A default cost applied to all flatpacks outside of the cost of constructing the machine.
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public Dictionary<ProtoId<MaterialPrototype>, int> BaseMaterialCost = new();
|
||||
|
||||
[DataField, ViewVariables(VVAccess.ReadWrite)]
|
||||
public string SlotId = "board_slot";
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public enum FlatpackCreatorUIKey : byte
|
||||
{
|
||||
Key
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public enum FlatpackCreatorVisuals : byte
|
||||
{
|
||||
Packing
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class FlatpackCreatorStartPackBuiMessage : BoundUserInterfaceMessage
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
using System.Linq;
|
||||
using Content.Shared.Construction.Components;
|
||||
using Content.Shared.Construction.Prototypes;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Lathe;
|
||||
using Content.Shared.Materials;
|
||||
using Content.Shared.Stacks;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.Construction
|
||||
@@ -11,6 +15,7 @@ namespace Content.Shared.Construction
|
||||
public sealed class MachinePartSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototype = default!;
|
||||
[Dependency] private readonly SharedLatheSystem _lathe = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -61,5 +66,87 @@ namespace Content.Shared.Construction
|
||||
args.PushMarkup(Loc.GetString("machine-part-component-on-examine-type-text", ("type",
|
||||
Loc.GetString(_prototype.Index<MachinePartPrototype>(component.PartType).Name))));
|
||||
}
|
||||
|
||||
public Dictionary<string, int> GetMachineBoardMaterialCost(Entity<MachineBoardComponent> entity, int coefficient = 1)
|
||||
{
|
||||
var (_, comp) = entity;
|
||||
|
||||
var materials = new Dictionary<string, int>();
|
||||
foreach (var (partId, amount) in comp.Requirements)
|
||||
{
|
||||
var partProto = _prototype.Index<MachinePartPrototype>(partId);
|
||||
|
||||
if (!_lathe.TryGetRecipesFromEntity(partProto.StockPartPrototype, out var recipes))
|
||||
continue;
|
||||
|
||||
var partRecipe = recipes[0];
|
||||
if (recipes.Count > 1)
|
||||
partRecipe = recipes.MinBy(p => p.RequiredMaterials.Values.Sum());
|
||||
|
||||
foreach (var (mat, matAmount) in partRecipe!.RequiredMaterials)
|
||||
{
|
||||
materials.TryAdd(mat, 0);
|
||||
materials[mat] += matAmount * amount * coefficient;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var (stackId, amount) in comp.MaterialIdRequirements)
|
||||
{
|
||||
var stackProto = _prototype.Index<StackPrototype>(stackId);
|
||||
|
||||
if (_prototype.TryIndex(stackProto.Spawn, out var defaultProto) &&
|
||||
defaultProto.TryGetComponent<PhysicalCompositionComponent>(out var physComp))
|
||||
{
|
||||
foreach (var (mat, matAmount) in physComp.MaterialComposition)
|
||||
{
|
||||
materials.TryAdd(mat, 0);
|
||||
materials[mat] += matAmount * amount * coefficient;
|
||||
}
|
||||
}
|
||||
else if (_lathe.TryGetRecipesFromEntity(stackProto.Spawn, out var recipes))
|
||||
{
|
||||
var partRecipe = recipes[0];
|
||||
if (recipes.Count > 1)
|
||||
partRecipe = recipes.MinBy(p => p.RequiredMaterials.Values.Sum());
|
||||
|
||||
foreach (var (mat, matAmount) in partRecipe!.RequiredMaterials)
|
||||
{
|
||||
materials.TryAdd(mat, 0);
|
||||
materials[mat] += matAmount * amount * coefficient;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var genericPartInfo = comp.ComponentRequirements.Values.Concat(comp.ComponentRequirements.Values);
|
||||
foreach (var info in genericPartInfo)
|
||||
{
|
||||
var amount = info.Amount;
|
||||
var defaultProtoId = info.DefaultPrototype;
|
||||
|
||||
if (_lathe.TryGetRecipesFromEntity(defaultProtoId, out var recipes))
|
||||
{
|
||||
var partRecipe = recipes[0];
|
||||
if (recipes.Count > 1)
|
||||
partRecipe = recipes.MinBy(p => p.RequiredMaterials.Values.Sum());
|
||||
|
||||
foreach (var (mat, matAmount) in partRecipe!.RequiredMaterials)
|
||||
{
|
||||
materials.TryAdd(mat, 0);
|
||||
materials[mat] += matAmount * amount * coefficient;
|
||||
}
|
||||
}
|
||||
else if (_prototype.TryIndex(defaultProtoId, out var defaultProto) &&
|
||||
defaultProto.TryGetComponent<PhysicalCompositionComponent>(out var physComp))
|
||||
{
|
||||
foreach (var (mat, matAmount) in physComp.MaterialComposition)
|
||||
{
|
||||
materials.TryAdd(mat, 0);
|
||||
materials[mat] += matAmount * amount * coefficient;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return materials;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
150
Content.Shared/Construction/SharedFlatpackSystem.cs
Normal file
150
Content.Shared/Construction/SharedFlatpackSystem.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
using System.Numerics;
|
||||
using Content.Shared.Construction.Components;
|
||||
using Content.Shared.Administration.Logs;
|
||||
using Content.Shared.Database;
|
||||
using Content.Shared.Examine;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Materials;
|
||||
using Content.Shared.Popups;
|
||||
using Content.Shared.Tools.Systems;
|
||||
using Robust.Shared.Audio.Systems;
|
||||
using Robust.Shared.Containers;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Map.Components;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Physics.Components;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.Construction;
|
||||
|
||||
public abstract class SharedFlatpackSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly INetManager _net = default!;
|
||||
[Dependency] protected readonly IPrototypeManager PrototypeManager = default!;
|
||||
[Dependency] protected readonly SharedAppearanceSystem Appearance = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audio = default!;
|
||||
[Dependency] private readonly SharedContainerSystem _container = default!;
|
||||
[Dependency] private readonly EntityLookupSystem _entityLookup = default!;
|
||||
[Dependency] private readonly SharedMapSystem _map = default!;
|
||||
[Dependency] protected readonly MachinePartSystem MachinePart = default!;
|
||||
[Dependency] protected readonly SharedMaterialStorageSystem MaterialStorage = default!;
|
||||
[Dependency] private readonly MetaDataSystem _metaData = default!;
|
||||
[Dependency] private readonly SharedPopupSystem _popup = default!;
|
||||
[Dependency] private readonly SharedToolSystem _tool = default!;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeLocalEvent<FlatpackComponent, InteractUsingEvent>(OnFlatpackInteractUsing);
|
||||
SubscribeLocalEvent<FlatpackComponent, ExaminedEvent>(OnFlatpackExamined);
|
||||
|
||||
SubscribeLocalEvent<FlatpackCreatorComponent, ContainerIsRemovingAttemptEvent>(OnCreatorRemovingAttempt);
|
||||
SubscribeLocalEvent<FlatpackCreatorComponent, EntityUnpausedEvent>(OnCreatorUnpaused);
|
||||
}
|
||||
|
||||
private void OnFlatpackInteractUsing(Entity<FlatpackComponent> ent, ref InteractUsingEvent args)
|
||||
{
|
||||
var (uid, comp) = ent;
|
||||
if (!_tool.HasQuality(args.Used, comp.QualityNeeded) || _container.IsEntityInContainer(ent))
|
||||
return;
|
||||
|
||||
var xform = Transform(ent);
|
||||
|
||||
if (xform.GridUid is not { } grid || !TryComp<MapGridComponent>(grid, out var gridComp))
|
||||
return;
|
||||
|
||||
args.Handled = true;
|
||||
|
||||
if (comp.Entity == null)
|
||||
{
|
||||
Log.Error($"No entity prototype present for flatpack {ToPrettyString(ent)}.");
|
||||
|
||||
if (_net.IsServer)
|
||||
QueueDel(ent);
|
||||
return;
|
||||
}
|
||||
|
||||
var buildPos = _map.TileIndicesFor(grid, gridComp, xform.Coordinates);
|
||||
var intersecting = _entityLookup.GetEntitiesIntersecting(buildPos.ToEntityCoordinates(grid, _mapManager).Offset(new Vector2(0.5f, 0.5f))
|
||||
, 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)
|
||||
{
|
||||
if (!TryComp<PhysicsComponent>(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
|
||||
if (_net.IsServer)
|
||||
_popup.PopupEntity(Loc.GetString("flatpack-unpack-no-room"), uid, args.User);
|
||||
return;
|
||||
}
|
||||
|
||||
if (_net.IsServer)
|
||||
{
|
||||
var spawn = Spawn(comp.Entity, _map.GridTileToLocal(grid, gridComp, buildPos));
|
||||
_adminLogger.Add(LogType.Construction, LogImpact.Low,
|
||||
$"{ToPrettyString(args.User):player} unpacked {ToPrettyString(spawn):entity} at {xform.Coordinates} from {ToPrettyString(uid):entity}");
|
||||
QueueDel(uid);
|
||||
}
|
||||
|
||||
_audio.PlayPredicted(comp.UnpackSound, args.Used, args.User);
|
||||
}
|
||||
|
||||
private void OnFlatpackExamined(Entity<FlatpackComponent> ent, ref ExaminedEvent args)
|
||||
{
|
||||
if (!args.IsInDetailsRange)
|
||||
return;
|
||||
args.PushMarkup(Loc.GetString("flatpack-examine"));
|
||||
}
|
||||
|
||||
private void OnCreatorRemovingAttempt(Entity<FlatpackCreatorComponent> ent, ref ContainerIsRemovingAttemptEvent args)
|
||||
{
|
||||
if (args.Container.ID == ent.Comp.SlotId && ent.Comp.Packing)
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
private void OnCreatorUnpaused(Entity<FlatpackCreatorComponent> ent, ref EntityUnpausedEvent args)
|
||||
{
|
||||
ent.Comp.PackEndTime += args.PausedTime;
|
||||
}
|
||||
|
||||
public void SetupFlatpack(Entity<FlatpackComponent?> ent, Entity<MachineBoardComponent?> machineBoard)
|
||||
{
|
||||
if (!Resolve(ent, ref ent.Comp) || !Resolve(machineBoard, ref machineBoard.Comp))
|
||||
return;
|
||||
|
||||
if (machineBoard.Comp.Prototype is not { } machinePrototypeId)
|
||||
return;
|
||||
|
||||
var comp = ent.Comp!;
|
||||
var machinePrototype = PrototypeManager.Index(machinePrototypeId);
|
||||
|
||||
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);
|
||||
|
||||
Appearance.SetData(ent, FlatpackVisuals.Machine, MetaData(machineBoard).EntityPrototype?.ID ?? string.Empty);
|
||||
}
|
||||
|
||||
public Dictionary<string, int> GetFlatpackCreationCost(Entity<FlatpackCreatorComponent> entity, Entity<MachineBoardComponent> machineBoard)
|
||||
{
|
||||
var cost = MachinePart.GetMachineBoardMaterialCost(machineBoard, -1);
|
||||
foreach (var (mat, amount) in entity.Comp.BaseMaterialCost)
|
||||
{
|
||||
cost.TryAdd(mat, 0);
|
||||
cost[mat] -= amount;
|
||||
}
|
||||
|
||||
return cost;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user