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:
Nemanja
2024-01-03 01:16:02 -05:00
committed by GitHub
parent 04fc06e9d4
commit 1c11332fa4
40 changed files with 1066 additions and 101 deletions

View 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
}

View File

@@ -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
{
}

View File

@@ -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;
}
}
}

View 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;
}
}

View File

@@ -1,8 +1,10 @@
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Emag.Systems;
using Content.Shared.Materials;
using Content.Shared.Research.Prototypes;
using JetBrains.Annotations;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Shared.Lathe;
@@ -14,11 +16,15 @@ public abstract class SharedLatheSystem : EntitySystem
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedMaterialStorageSystem _materialStorage = default!;
private readonly Dictionary<string, List<LatheRecipePrototype>> _inverseRecipeDictionary = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EmagLatheRecipesComponent, GotEmaggedEvent>(OnEmagged);
SubscribeLocalEvent<PrototypesReloadedEventArgs>(OnPrototypesReloaded);
BuildInverseRecipeDictionary();
}
[PublicAPI]
@@ -53,4 +59,28 @@ public abstract class SharedLatheSystem : EntitySystem
=> reduce ? (int) MathF.Ceiling(original * multiplier) : original;
protected abstract bool HasRecipe(EntityUid uid, LatheRecipePrototype recipe, LatheComponent component);
private void OnPrototypesReloaded(PrototypesReloadedEventArgs obj)
{
if (!obj.WasModified<LatheRecipePrototype>())
return;
BuildInverseRecipeDictionary();
}
private void BuildInverseRecipeDictionary()
{
_inverseRecipeDictionary.Clear();
foreach (var latheRecipe in _proto.EnumeratePrototypes<LatheRecipePrototype>())
{
_inverseRecipeDictionary.GetOrNew(latheRecipe.Result).Add(latheRecipe);
}
}
public bool TryGetRecipesFromEntity(string prototype, [NotNullWhen(true)] out List<LatheRecipePrototype>? recipes)
{
recipes = new();
if (_inverseRecipeDictionary.TryGetValue(prototype, out var r))
recipes.AddRange(r);
return recipes.Count != 0;
}
}

View File

@@ -81,7 +81,7 @@ public abstract class SharedMaterialStorageSystem : EntitySystem
{
if (!Resolve(uid, ref component))
return 0; //you have nothing
return !component.Storage.TryGetValue(material, out var amount) ? 0 : amount;
return component.Storage.GetValueOrDefault(material, 0);
}
/// <summary>
@@ -123,9 +123,35 @@ public abstract class SharedMaterialStorageSystem : EntitySystem
{
if (!Resolve(uid, ref component))
return false;
return CanTakeVolume(uid, volume, component) &&
(component.MaterialWhiteList == null || component.MaterialWhiteList.Contains(materialId)) &&
(!component.Storage.TryGetValue(materialId, out var amount) || amount + volume >= 0);
if (!CanTakeVolume(uid, volume, component))
return false;
if (component.MaterialWhiteList != null && !component.MaterialWhiteList.Contains(materialId))
return false;
var amount = component.Storage.GetValueOrDefault(materialId);
return amount + volume >= 0;
}
/// <summary>
/// Checks if the specified materials can be changed by the specified volumes.
/// </summary>
/// <param name="entity"></param>
/// <param name="materials"></param>
/// <returns>If the amount can be changed</returns>
public bool CanChangeMaterialAmount(Entity<MaterialStorageComponent?> entity, Dictionary<string,int> materials)
{
if (!Resolve(entity, ref entity.Comp))
return false;
foreach (var (material, amount) in materials)
{
if (!CanChangeMaterialAmount(entity, material, amount, entity.Comp))
return false;
}
return true;
}
/// <summary>
@@ -136,20 +162,47 @@ public abstract class SharedMaterialStorageSystem : EntitySystem
/// <param name="materialId"></param>
/// <param name="volume"></param>
/// <param name="component"></param>
/// <param name="dirty"></param>
/// <returns>If it was successful</returns>
public bool TryChangeMaterialAmount(EntityUid uid, string materialId, int volume, MaterialStorageComponent? component = null)
public bool TryChangeMaterialAmount(EntityUid uid, string materialId, int volume, MaterialStorageComponent? component = null, bool dirty = true)
{
if (!Resolve(uid, ref component))
return false;
if (!CanChangeMaterialAmount(uid, materialId, volume, component))
return false;
if (!component.Storage.ContainsKey(materialId))
component.Storage.Add(materialId, 0);
component.Storage.TryAdd(materialId, 0);
component.Storage[materialId] += volume;
var ev = new MaterialAmountChangedEvent();
RaiseLocalEvent(uid, ref ev);
Dirty(component);
if (dirty)
Dirty(uid, component);
return true;
}
/// <summary>
/// Changes the amount of a specific material in the storage.
/// Still respects the filters in place.
/// </summary>
/// <param name="entity"></param>
/// <param name="materials"></param>
/// <returns>If the amount can be changed</returns>
public bool TryChangeMaterialAmount(Entity<MaterialStorageComponent?> entity, Dictionary<string,int> materials)
{
if (!Resolve(entity, ref entity.Comp))
return false;
if (!CanChangeMaterialAmount(entity, materials))
return false;
foreach (var (material, amount) in materials)
{
if (!TryChangeMaterialAmount(entity, material, amount, entity.Comp, false))
return false;
}
Dirty(entity, entity.Comp);
return true;
}
@@ -225,7 +278,7 @@ public abstract class SharedMaterialStorageSystem : EntitySystem
insertingComp.MaterialColor = lastMat?.Color;
}
_appearance.SetData(receiver, MaterialStorageVisuals.Inserting, true);
Dirty(insertingComp);
Dirty(receiver, insertingComp);
var ev = new MaterialEntityInsertedEvent(material);
RaiseLocalEvent(receiver, ref ev);
@@ -245,7 +298,7 @@ public abstract class SharedMaterialStorageSystem : EntitySystem
var ev = new GetMaterialWhitelistEvent(uid);
RaiseLocalEvent(uid, ref ev);
component.MaterialWhiteList = ev.Whitelist;
Dirty(component);
Dirty(uid, component);
}
private void OnInteractUsing(EntityUid uid, MaterialStorageComponent component, InteractUsingEvent args)