lathe machine upgrading (#12032)

This commit is contained in:
Nemanja
2022-10-30 03:12:11 -04:00
committed by GitHub
parent 56a93139b1
commit 3ab98e320a
7 changed files with 107 additions and 14 deletions

View File

@@ -25,7 +25,6 @@ public sealed partial class LatheMenu : DefaultWindow
public event Action<string, int>? RecipeQueueAction; public event Action<string, int>? RecipeQueueAction;
public List<string> Recipes = new(); public List<string> Recipes = new();
private List<LatheRecipePrototype> _oldRecipesToShow = new();
public LatheMenu(LatheBoundUserInterface owner) public LatheMenu(LatheBoundUserInterface owner)
{ {
@@ -85,6 +84,9 @@ public sealed partial class LatheMenu : DefaultWindow
/// <param name="lathe"></param> /// <param name="lathe"></param>
public void PopulateRecipes(EntityUid lathe) public void PopulateRecipes(EntityUid lathe)
{ {
if (!_entityManager.TryGetComponent<LatheComponent>(lathe, out var component))
return;
var recipesToShow = new List<LatheRecipePrototype>(); var recipesToShow = new List<LatheRecipePrototype>();
foreach (var recipe in Recipes) foreach (var recipe in Recipes)
{ {
@@ -106,7 +108,6 @@ public sealed partial class LatheMenu : DefaultWindow
quantity = 1; quantity = 1;
RecipeList.Children.Clear(); RecipeList.Children.Clear();
_oldRecipesToShow = recipesToShow;
foreach (var prototype in recipesToShow) foreach (var prototype in recipesToShow)
{ {
StringBuilder sb = new(); StringBuilder sb = new();
@@ -121,7 +122,7 @@ public sealed partial class LatheMenu : DefaultWindow
else else
sb.Append('\n'); sb.Append('\n');
sb.Append(amount); sb.Append((int) (amount * component.MaterialUseMultiplier));
sb.Append(' '); sb.Append(' ');
sb.Append(proto.Name); sb.Append(proto.Name);
} }

View File

@@ -7,9 +7,15 @@ namespace Content.Server.Lathe.Components;
public sealed class LatheProducingComponent : Component public sealed class LatheProducingComponent : Component
{ {
/// <summary> /// <summary>
/// How much production time has passed, in seconds. /// The time at which production began
/// </summary> /// </summary>
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
public float AccumulatedTime; public TimeSpan StartTime;
/// <summary>
/// How long it takes to produce the recipe.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public TimeSpan ProductionLength;
} }

View File

@@ -10,17 +10,20 @@ using Robust.Server.GameObjects;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using JetBrains.Annotations; using JetBrains.Annotations;
using System.Linq; using System.Linq;
using Content.Server.Construction;
using Content.Server.Materials; using Content.Server.Materials;
using Content.Server.Power.Components; using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems; using Content.Server.Power.EntitySystems;
using Content.Server.UserInterface; using Content.Server.UserInterface;
using Robust.Server.Player; using Robust.Server.Player;
using Robust.Shared.Timing;
namespace Content.Server.Lathe namespace Content.Server.Lathe
{ {
[UsedImplicitly] [UsedImplicitly]
public sealed class LatheSystem : SharedLatheSystem public sealed class LatheSystem : SharedLatheSystem
{ {
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPrototypeManager _proto = default!; [Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!; [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!; [Dependency] private readonly SharedAudioSystem _audio = default!;
@@ -35,6 +38,7 @@ namespace Content.Server.Lathe
SubscribeLocalEvent<LatheComponent, GetMaterialWhitelistEvent>(OnGetWhitelist); SubscribeLocalEvent<LatheComponent, GetMaterialWhitelistEvent>(OnGetWhitelist);
SubscribeLocalEvent<LatheComponent, MapInitEvent>(OnMapInit); SubscribeLocalEvent<LatheComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<LatheComponent, PowerChangedEvent>(OnPowerChanged); SubscribeLocalEvent<LatheComponent, PowerChangedEvent>(OnPowerChanged);
SubscribeLocalEvent<LatheComponent, RefreshPartsEvent>(OnPartsRefresh);
SubscribeLocalEvent<LatheComponent, LatheQueueRecipeMessage>(OnLatheQueueRecipeMessage); SubscribeLocalEvent<LatheComponent, LatheQueueRecipeMessage>(OnLatheQueueRecipeMessage);
SubscribeLocalEvent<LatheComponent, LatheSyncRequestMessage>(OnLatheSyncRequestMessage); SubscribeLocalEvent<LatheComponent, LatheSyncRequestMessage>(OnLatheSyncRequestMessage);
@@ -65,9 +69,7 @@ namespace Content.Server.Lathe
if (lathe.CurrentRecipe == null) if (lathe.CurrentRecipe == null)
continue; continue;
comp.AccumulatedTime += frameTime; if ( _timing.CurTime - comp.StartTime >= comp.ProductionLength)
if (comp.AccumulatedTime >= (float) lathe.CurrentRecipe.CompleteTime.TotalSeconds)
FinishProducing(comp.Owner, lathe); FinishProducing(comp.Owner, lathe);
} }
} }
@@ -132,7 +134,7 @@ namespace Content.Server.Lathe
foreach (var (mat, amount) in recipe.RequiredMaterials) foreach (var (mat, amount) in recipe.RequiredMaterials)
{ {
_materialStorage.TryChangeMaterialAmount(uid, mat, -amount); _materialStorage.TryChangeMaterialAmount(uid, mat, (int) (-amount * component.MaterialUseMultiplier));
} }
component.Queue.Add(recipe); component.Queue.Add(recipe);
@@ -149,7 +151,9 @@ namespace Content.Server.Lathe
var recipe = component.Queue.First(); var recipe = component.Queue.First();
component.Queue.RemoveAt(0); component.Queue.RemoveAt(0);
EnsureComp<LatheProducingComponent>(uid); var lathe = EnsureComp<LatheProducingComponent>(uid);
lathe.StartTime = _timing.CurTime;
lathe.ProductionLength = recipe.CompleteTime * component.TimeMultiplier;
component.CurrentRecipe = recipe; component.CurrentRecipe = recipe;
_audio.PlayPvs(component.ProducingSound, component.Owner); _audio.PlayPvs(component.ProducingSound, component.Owner);
@@ -166,7 +170,7 @@ namespace Content.Server.Lathe
if (comp.CurrentRecipe != null) if (comp.CurrentRecipe != null)
Spawn(comp.CurrentRecipe.Result, Transform(uid).Coordinates); Spawn(comp.CurrentRecipe.Result, Transform(uid).Coordinates);
comp.CurrentRecipe = null; comp.CurrentRecipe = null;
prodComp.AccumulatedTime = 0; prodComp.StartTime = _timing.CurTime;
if (!TryStartProducing(uid, comp)) if (!TryStartProducing(uid, comp))
{ {
@@ -257,6 +261,17 @@ namespace Content.Server.Lathe
} }
} }
private void OnPartsRefresh(EntityUid uid, LatheComponent component, RefreshPartsEvent args)
{
var printTimeRating = args.PartRatings[component.MachinePartPrintTime];
var materialUseRating = args.PartRatings[component.MachinePartMaterialUse];
component.TimeMultiplier = MathF.Pow(component.PartRatingPrintTimeMultiplier, printTimeRating - 1);
component.MaterialUseMultiplier = MathF.Pow(component.PartRatingMaterialUseMultiplier, materialUseRating - 1);
Dirty(component);
}
protected override bool HasRecipe(EntityUid uid, LatheRecipePrototype recipe, LatheComponent component) protected override bool HasRecipe(EntityUid uid, LatheRecipePrototype recipe, LatheComponent component)
{ {
return GetAvailableRecipes(component).Contains(recipe.ID); return GetAvailableRecipes(component).Contains(recipe.ID);

View File

@@ -1,6 +1,8 @@
using Content.Shared.Construction.Prototypes;
using Content.Shared.Research.Prototypes; using Content.Shared.Research.Prototypes;
using Robust.Shared.Audio; using Robust.Shared.Audio;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
namespace Content.Shared.Lathe namespace Content.Shared.Lathe
@@ -56,7 +58,43 @@ namespace Content.Shared.Lathe
[ViewVariables] [ViewVariables]
public LatheRecipePrototype? CurrentRecipe; public LatheRecipePrototype? CurrentRecipe;
#region MachineUpgrading
/// <summary>
/// A modifier that changes how long it takes to print a recipe
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float TimeMultiplier = 1;
/// <summary>
/// The machine part that reduces how long it takes to print a recipe.
/// </summary>
[DataField("machinePartPrintSpeed", customTypeSerializer: typeof(PrototypeIdSerializer<MachinePartPrototype>))]
public string MachinePartPrintTime = "Manipulator";
/// <summary>
/// The value that is used to calculate the modified <see cref="TimeMultiplier"/>
/// </summary>
[DataField("partRatingPrintTimeMultiplier")]
public float PartRatingPrintTimeMultiplier = 0.5f;
/// <summary>
/// A modifier that changes how much of a material is needed to print a recipe
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float MaterialUseMultiplier = 1;
/// <summary>
/// The machine part that reduces how much material it takes to print a recipe.
/// </summary>
[DataField("machinePartMaterialUse", customTypeSerializer: typeof(PrototypeIdSerializer<MachinePartPrototype>))]
public string MachinePartMaterialUse = "MatterBin";
/// <summary>
/// The value that is used to calculate the modifier <see cref="MaterialUseMultiplier"/>
/// </summary>
[DataField("partRatingMaterialUseMultiplier")]
public float PartRatingMaterialUseMultiplier = 0.75f;
#endregion
} }
public sealed class LatheGetRecipesEvent : EntityEventArgs public sealed class LatheGetRecipesEvent : EntityEventArgs

View File

@@ -1,7 +1,9 @@
using Content.Shared.Materials; using Content.Shared.Materials;
using Content.Shared.Research.Prototypes; using Content.Shared.Research.Prototypes;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
namespace Content.Shared.Lathe; namespace Content.Shared.Lathe;
@@ -13,6 +15,26 @@ public abstract class SharedLatheSystem : EntitySystem
[Dependency] private readonly IPrototypeManager _proto = default!; [Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedMaterialStorageSystem _materialStorage = default!; [Dependency] private readonly SharedMaterialStorageSystem _materialStorage = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<LatheComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<LatheComponent, ComponentHandleState>(OnHandleState);
}
private void OnGetState(EntityUid uid, LatheComponent component, ref ComponentGetState args)
{
args.State = new LatheComponentState(component.MaterialUseMultiplier);
}
private void OnHandleState(EntityUid uid, LatheComponent component, ref ComponentHandleState args)
{
if (args.Current is not LatheComponentState state)
return;
component.MaterialUseMultiplier = state.MaterialUseMultiplier;
}
[PublicAPI] [PublicAPI]
public bool CanProduce(EntityUid uid, string recipe, int amount = 1, LatheComponent? component = null) public bool CanProduce(EntityUid uid, string recipe, int amount = 1, LatheComponent? component = null)
{ {
@@ -28,7 +50,7 @@ public abstract class SharedLatheSystem : EntitySystem
foreach (var (material, needed) in recipe.RequiredMaterials) foreach (var (material, needed) in recipe.RequiredMaterials)
{ {
if (_materialStorage.GetMaterialAmount(component.Owner, material) < (amount * needed)) if (_materialStorage.GetMaterialAmount(component.Owner, material) < amount * needed * component.MaterialUseMultiplier)
return false; return false;
} }
return true; return true;
@@ -36,3 +58,14 @@ public abstract class SharedLatheSystem : EntitySystem
protected abstract bool HasRecipe(EntityUid uid, LatheRecipePrototype recipe, LatheComponent component); protected abstract bool HasRecipe(EntityUid uid, LatheRecipePrototype recipe, LatheComponent component);
} }
[Serializable, NetSerializable]
public sealed class LatheComponentState : ComponentState
{
public float MaterialUseMultiplier;
public LatheComponentState(float materialUseMultiplier)
{
MaterialUseMultiplier = materialUseMultiplier;
}
}

View File

@@ -94,8 +94,7 @@
prototype: UniformPrinter prototype: UniformPrinter
requirements: requirements:
MatterBin: 1 MatterBin: 1
Manipulator: 1 Laser: 2
Laser: 1
- type: entity - type: entity
id: VaccinatorMachineCircuitboard id: VaccinatorMachineCircuitboard

View File

@@ -377,6 +377,7 @@
- type: Lathe - type: Lathe
idleState: icon idleState: icon
runningState: icon runningState: icon
machinePartPrintSpeed: Laser
dynamicRecipes: dynamicRecipes:
- HandheldHealthAnalyzer - HandheldHealthAnalyzer
- ClothingHandsGlovesLatex - ClothingHandsGlovesLatex