Re-organize all projects (#4166)
This commit is contained in:
236
Content.Server/Lathe/Components/LatheComponent.cs
Normal file
236
Content.Server/Lathe/Components/LatheComponent.cs
Normal file
@@ -0,0 +1,236 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Materials;
|
||||
using Content.Server.Power.Components;
|
||||
using Content.Server.Research.Components;
|
||||
using Content.Server.Stack;
|
||||
using Content.Server.UserInterface;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Lathe;
|
||||
using Content.Shared.Power;
|
||||
using Content.Shared.Research.Prototypes;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Lathe.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(IActivate))]
|
||||
public class LatheComponent : SharedLatheComponent, IInteractUsing, IActivate
|
||||
{
|
||||
public const int VolumePerSheet = 100;
|
||||
|
||||
[ViewVariables]
|
||||
public Queue<LatheRecipePrototype> Queue { get; } = new();
|
||||
|
||||
[ViewVariables]
|
||||
public bool Producing { get; private set; }
|
||||
|
||||
private LatheState _state = LatheState.Base;
|
||||
|
||||
protected virtual LatheState State
|
||||
{
|
||||
get => _state;
|
||||
set => _state = value;
|
||||
}
|
||||
|
||||
[ViewVariables]
|
||||
private LatheRecipePrototype? _producingRecipe;
|
||||
[ViewVariables]
|
||||
private bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered;
|
||||
|
||||
private static readonly TimeSpan InsertionTime = TimeSpan.FromSeconds(0.9f);
|
||||
|
||||
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(LatheUiKey.Key);
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
if (UserInterface != null)
|
||||
{
|
||||
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
|
||||
}
|
||||
}
|
||||
|
||||
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage message)
|
||||
{
|
||||
if (!Powered)
|
||||
return;
|
||||
|
||||
switch (message.Message)
|
||||
{
|
||||
case LatheQueueRecipeMessage msg:
|
||||
PrototypeManager.TryIndex(msg.ID, out LatheRecipePrototype? recipe);
|
||||
if (recipe != null!)
|
||||
for (var i = 0; i < msg.Quantity; i++)
|
||||
{
|
||||
Queue.Enqueue(recipe);
|
||||
UserInterface?.SendMessage(new LatheFullQueueMessage(GetIdQueue()));
|
||||
}
|
||||
break;
|
||||
case LatheSyncRequestMessage _:
|
||||
if (!Owner.HasComponent<MaterialStorageComponent>()) return;
|
||||
UserInterface?.SendMessage(new LatheFullQueueMessage(GetIdQueue()));
|
||||
if (_producingRecipe != null)
|
||||
UserInterface?.SendMessage(new LatheProducingRecipeMessage(_producingRecipe.ID));
|
||||
break;
|
||||
|
||||
case LatheServerSelectionMessage _:
|
||||
if (!Owner.TryGetComponent(out ResearchClientComponent? researchClient)) return;
|
||||
researchClient.OpenUserInterface(message.Session);
|
||||
break;
|
||||
|
||||
case LatheServerSyncMessage _:
|
||||
if (!Owner.TryGetComponent(out TechnologyDatabaseComponent? database)
|
||||
|| !Owner.TryGetComponent(out ProtolatheDatabaseComponent? protoDatabase)) return;
|
||||
|
||||
if (database.SyncWithServer())
|
||||
protoDatabase.Sync();
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
internal bool Produce(LatheRecipePrototype recipe)
|
||||
{
|
||||
if (Producing || !Powered || !CanProduce(recipe) || !Owner.TryGetComponent(out MaterialStorageComponent? storage)) return false;
|
||||
|
||||
UserInterface?.SendMessage(new LatheFullQueueMessage(GetIdQueue()));
|
||||
|
||||
Producing = true;
|
||||
_producingRecipe = recipe;
|
||||
|
||||
foreach (var (material, amount) in recipe.RequiredMaterials)
|
||||
{
|
||||
// This should always return true, otherwise CanProduce fucked up.
|
||||
storage.RemoveMaterial(material, amount);
|
||||
}
|
||||
|
||||
UserInterface?.SendMessage(new LatheProducingRecipeMessage(recipe.ID));
|
||||
|
||||
State = LatheState.Producing;
|
||||
SetAppearance(LatheVisualState.Producing);
|
||||
|
||||
Owner.SpawnTimer(recipe.CompleteTime, () =>
|
||||
{
|
||||
Producing = false;
|
||||
_producingRecipe = null;
|
||||
Owner.EntityManager.SpawnEntity(recipe.Result, Owner.Transform.Coordinates);
|
||||
UserInterface?.SendMessage(new LatheStoppedProducingRecipeMessage());
|
||||
State = LatheState.Base;
|
||||
SetAppearance(LatheVisualState.Idle);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void OpenUserInterface(IPlayerSession session)
|
||||
{
|
||||
UserInterface?.Open(session);
|
||||
}
|
||||
|
||||
void IActivate.Activate(ActivateEventArgs eventArgs)
|
||||
{
|
||||
if (!eventArgs.User.TryGetComponent(out ActorComponent? actor))
|
||||
return;
|
||||
if (!Powered)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
OpenUserInterface(actor.PlayerSession);
|
||||
}
|
||||
|
||||
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
|
||||
{
|
||||
if (!Owner.TryGetComponent(out MaterialStorageComponent? storage)
|
||||
|| !eventArgs.Using.TryGetComponent(out MaterialComponent? material)) return false;
|
||||
|
||||
var multiplier = 1;
|
||||
|
||||
if (eventArgs.Using.TryGetComponent(out StackComponent? stack)) multiplier = stack.Count;
|
||||
|
||||
var totalAmount = 0;
|
||||
|
||||
// Check if it can insert all materials.
|
||||
foreach (var mat in material.MaterialIds)
|
||||
{
|
||||
// TODO: Change how MaterialComponent works so this is not hard-coded.
|
||||
if (!storage.CanInsertMaterial(mat, VolumePerSheet * multiplier)) return false;
|
||||
totalAmount += VolumePerSheet * multiplier;
|
||||
}
|
||||
|
||||
// Check if it can take ALL of the material's volume.
|
||||
if (storage.CanTakeAmount(totalAmount)) return false;
|
||||
|
||||
foreach (var mat in material.MaterialIds)
|
||||
{
|
||||
storage.InsertMaterial(mat, VolumePerSheet * multiplier);
|
||||
}
|
||||
|
||||
State = LatheState.Inserting;
|
||||
switch (material.Materials.FirstOrDefault()?.ID)
|
||||
{
|
||||
case "Steel":
|
||||
SetAppearance(LatheVisualState.InsertingMetal);
|
||||
break;
|
||||
case "Glass":
|
||||
SetAppearance(LatheVisualState.InsertingGlass);
|
||||
break;
|
||||
case "Gold":
|
||||
SetAppearance(LatheVisualState.InsertingGold);
|
||||
break;
|
||||
case "Plastic":
|
||||
SetAppearance(LatheVisualState.InsertingPlastic);
|
||||
break;
|
||||
case "Plasma":
|
||||
SetAppearance(LatheVisualState.InsertingPlasma);
|
||||
break;
|
||||
}
|
||||
|
||||
Owner.SpawnTimer(InsertionTime, () =>
|
||||
{
|
||||
State = LatheState.Base;
|
||||
SetAppearance(LatheVisualState.Idle);
|
||||
});
|
||||
|
||||
eventArgs.Using.Delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void SetAppearance(LatheVisualState state)
|
||||
{
|
||||
if (Owner.TryGetComponent(out AppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(PowerDeviceVisuals.VisualState, state);
|
||||
}
|
||||
}
|
||||
|
||||
private Queue<string> GetIdQueue()
|
||||
{
|
||||
var queue = new Queue<string>();
|
||||
foreach (var recipePrototype in Queue)
|
||||
{
|
||||
queue.Enqueue(recipePrototype.ID);
|
||||
}
|
||||
|
||||
return queue;
|
||||
}
|
||||
|
||||
protected enum LatheState
|
||||
{
|
||||
Base,
|
||||
Inserting,
|
||||
Producing
|
||||
}
|
||||
}
|
||||
}
|
||||
47
Content.Server/Lathe/Components/LatheDatabaseComponent.cs
Normal file
47
Content.Server/Lathe/Components/LatheDatabaseComponent.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using Content.Shared.Lathe;
|
||||
using Content.Shared.Research.Prototypes;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Lathe.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedLatheDatabaseComponent))]
|
||||
public class LatheDatabaseComponent : SharedLatheDatabaseComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether new recipes can be added to this database or not.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
[DataField("static")]
|
||||
public bool Static { get; private set; } = false;
|
||||
|
||||
public override ComponentState GetComponentState(ICommonSession player)
|
||||
{
|
||||
return new LatheDatabaseState(GetRecipeIdList());
|
||||
}
|
||||
|
||||
public override void Clear()
|
||||
{
|
||||
if (Static) return;
|
||||
base.Clear();
|
||||
Dirty();
|
||||
}
|
||||
|
||||
public override void AddRecipe(LatheRecipePrototype recipe)
|
||||
{
|
||||
if (Static) return;
|
||||
base.AddRecipe(recipe);
|
||||
Dirty();
|
||||
}
|
||||
|
||||
public override bool RemoveRecipe(LatheRecipePrototype recipe)
|
||||
{
|
||||
if (Static || !base.RemoveRecipe(recipe)) return false;
|
||||
Dirty();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
82
Content.Server/Lathe/Components/MaterialStorageComponent.cs
Normal file
82
Content.Server/Lathe/Components/MaterialStorageComponent.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.Lathe;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Server.Lathe.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedMaterialStorageComponent))]
|
||||
public class MaterialStorageComponent : SharedMaterialStorageComponent
|
||||
{
|
||||
[ViewVariables]
|
||||
protected override Dictionary<string, int> Storage { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// How much material the storage can store in total.
|
||||
/// </summary>
|
||||
[ViewVariables]
|
||||
public int StorageLimit => _storageLimit;
|
||||
[DataField("StorageLimit")]
|
||||
private int _storageLimit = -1;
|
||||
|
||||
public override ComponentState GetComponentState(ICommonSession player)
|
||||
{
|
||||
return new MaterialStorageState(Storage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the storage can take a volume of material without surpassing its own limits.
|
||||
/// </summary>
|
||||
/// <param name="amount">The volume of material</param>
|
||||
/// <returns></returns>
|
||||
public bool CanTakeAmount(int amount)
|
||||
{
|
||||
return CurrentAmount + amount <= StorageLimit;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if it can insert a material.
|
||||
/// </summary>
|
||||
/// <param name="ID">Material ID</param>
|
||||
/// <param name="amount">How much to insert</param>
|
||||
/// <returns>Whether it can insert the material or not.</returns>
|
||||
public bool CanInsertMaterial(string ID, int amount)
|
||||
{
|
||||
return (CanTakeAmount(amount) || StorageLimit < 0) && (!Storage.ContainsKey(ID) || Storage[ID] + amount >= 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts material into the storage.
|
||||
/// </summary>
|
||||
/// <param name="ID">Material ID</param>
|
||||
/// <param name="amount">How much to insert</param>
|
||||
/// <returns>Whether it inserted it or not.</returns>
|
||||
public bool InsertMaterial(string ID, int amount)
|
||||
{
|
||||
if (!CanInsertMaterial(ID, amount)) return false;
|
||||
|
||||
if (!Storage.ContainsKey(ID))
|
||||
Storage.Add(ID, 0);
|
||||
|
||||
Storage[ID] += amount;
|
||||
|
||||
Dirty();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes material from the storage.
|
||||
/// </summary>
|
||||
/// <param name="ID">Material ID</param>
|
||||
/// <param name="amount">How much to remove</param>
|
||||
/// <returns>Whether it removed it or not.</returns>
|
||||
public bool RemoveMaterial(string ID, int amount)
|
||||
{
|
||||
return InsertMaterial(ID, -amount);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Research.Components;
|
||||
using Content.Shared.Lathe;
|
||||
using Content.Shared.Research.Prototypes;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.Lathe.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedLatheDatabaseComponent))]
|
||||
public class ProtolatheDatabaseComponent : SharedProtolatheDatabaseComponent
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
|
||||
public override string Name => "ProtolatheDatabase";
|
||||
|
||||
public override ComponentState GetComponentState(ICommonSession player)
|
||||
{
|
||||
return new ProtolatheDatabaseState(GetRecipeIdList());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds unlocked recipes from technologies to the database.
|
||||
/// </summary>
|
||||
public void Sync()
|
||||
{
|
||||
if (!Owner.TryGetComponent(out TechnologyDatabaseComponent? database)) return;
|
||||
|
||||
foreach (var technology in database.Technologies)
|
||||
{
|
||||
foreach (var id in technology.UnlockedRecipes)
|
||||
{
|
||||
var recipe = (LatheRecipePrototype) _prototypeManager.Index(typeof(LatheRecipePrototype), id);
|
||||
UnlockRecipe(recipe);
|
||||
}
|
||||
}
|
||||
|
||||
Dirty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unlocks a recipe but only if it's one of the allowed recipes on this protolathe.
|
||||
/// </summary>
|
||||
/// <param name="recipe">The recipe</param>
|
||||
/// <returns>Whether it could add it or not.</returns>
|
||||
public bool UnlockRecipe(LatheRecipePrototype recipe)
|
||||
{
|
||||
if (!ProtolatheRecipes.Contains(recipe)) return false;
|
||||
|
||||
AddRecipe(recipe);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user