Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mining points #2419

Merged
merged 6 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Content.Client/Lathe/UI/LatheBoundUserInterface.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Content.Shared.DeltaV.Salvage; // DeltaV
using Content.Shared.Lathe;
using Content.Shared.Research.Components;
using JetBrains.Annotations;
Expand Down Expand Up @@ -31,6 +32,8 @@ protected override void Open()
{
SendMessage(new LatheQueueRecipeMessage(recipe, amount));
};

_menu.OnClaimMiningPoints += () => SendMessage(new LatheClaimMiningPointsMessage()); // DeltaV
}

protected override void UpdateState(BoundUserInterfaceState state)
Expand Down
6 changes: 6 additions & 0 deletions Content.Client/Lathe/UI/LatheMenu.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@
HorizontalExpand="True">
<ui:MaterialStorageControl Name="MaterialsList" SizeFlagsStretchRatio="8"/>
</BoxContainer>
<!-- Begin DeltaV Additions: Mining points -->
<BoxContainer Orientation="Horizontal" Name="MiningPointsContainer" Visible="False">
<Label Name="MiningPointsLabel" HorizontalExpand="True"/>
<Button Name="MiningPointsClaimButton" Text="{Loc 'lathe-menu-mining-points-claim-button'}"/>
</BoxContainer>
<!-- End DeltaV Additions: Mining points -->
</BoxContainer>
</BoxContainer>

Expand Down
43 changes: 43 additions & 0 deletions Content.Client/Lathe/UI/LatheMenu.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
using System.Linq;
using System.Text;
using Content.Client.Materials;
using Content.Shared.DeltaV.Salvage.Components; // DeltaV
using Content.Shared.DeltaV.Salvage.Systems; // DeltaV
using Content.Shared.Lathe;
using Content.Shared.Lathe.Prototypes;
using Content.Shared.Research.Prototypes;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.Player; // DeltaV
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing; // DeltaV

namespace Content.Client.Lathe.UI;

[GenerateTypedNameReferences]
public sealed partial class LatheMenu : DefaultWindow
{
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IPlayerManager _player = default!; // DeltaV
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;

private readonly SpriteSystem _spriteSystem;
private readonly LatheSystem _lathe;
private readonly MaterialStorageSystem _materialStorage;
private readonly MiningPointsSystem _miningPoints; // DeltaV

public event Action<BaseButton.ButtonEventArgs>? OnServerListButtonPressed;
public event Action<string, int>? RecipeQueueAction;
public event Action? OnClaimMiningPoints; // DeltaV

public List<ProtoId<LatheRecipePrototype>> Recipes = new();

Expand All @@ -35,6 +42,8 @@ public sealed partial class LatheMenu : DefaultWindow

public EntityUid Entity;

private uint? _lastMiningPoints; // DeltaV: used to avoid Loc.GetString every frame

public LatheMenu()
{
RobustXamlLoader.Load(this);
Expand All @@ -43,6 +52,7 @@ public LatheMenu()
_spriteSystem = _entityManager.System<SpriteSystem>();
_lathe = _entityManager.System<LatheSystem>();
_materialStorage = _entityManager.System<MaterialStorageSystem>();
_miningPoints = _entityManager.System<MiningPointsSystem>(); // DeltaV

SearchBar.OnTextChanged += _ =>
{
Expand Down Expand Up @@ -70,9 +80,31 @@ public void SetEntity(EntityUid uid)
}
}

// Begin DeltaV Additions: Mining points UI
MiningPointsContainer.Visible = _entityManager.TryGetComponent<MiningPointsComponent>(Entity, out var points);
MiningPointsClaimButton.OnPressed += _ => OnClaimMiningPoints?.Invoke();
if (points != null)
UpdateMiningPoints(points.Points);
// End DeltaV Additions

MaterialsList.SetOwner(Entity);
}

/// <summary>
/// DeltaV: Updates the UI elements for mining points.
/// </summary>
private void UpdateMiningPoints(uint points)
{
MiningPointsClaimButton.Disabled = points == 0 ||
_player.LocalSession?.AttachedEntity is not {} player ||
_miningPoints.TryFindIdCard(player) == null;
if (points == _lastMiningPoints)
return;

_lastMiningPoints = points;
MiningPointsLabel.Text = Loc.GetString("lathe-menu-mining-points", ("points", points));
}

protected override void Opened()
{
base.Opened();
Expand All @@ -83,6 +115,17 @@ protected override void Opened()
}
}

/// <summary>
/// DeltaV: Update mining points UI whenever it changes.
/// </summary>
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);

if (_entityManager.TryGetComponent<MiningPointsComponent>(Entity, out var points))
UpdateMiningPoints(points.Points);
}

/// <summary>
/// Populates the list of all the recipes
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Content.Shared.DeltaV.Salvage.Systems;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;

namespace Content.Shared.DeltaV.Salvage.Components;

/// <summary>
/// Stores mining points for a holder, such as an ID card or ore processor.
/// Mining points are gained by smelting ore and redeeming them to your ID card.
/// </summary>
[RegisterComponent, NetworkedComponent, Access(typeof(MiningPointsSystem))]
[AutoGenerateComponentState]
public sealed partial class MiningPointsComponent : Component
{
/// <summary>
/// The number of points stored.
/// </summary>
[DataField, AutoNetworkedField]
public uint Points;

/// <summary>
/// Sound played when successfully transferring points to another holder.
/// </summary>
[DataField]
public SoundSpecifier? TransferSound;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Robust.Shared.GameStates;

namespace Content.Shared.DeltaV.Salvage.Components;

/// <summary>
/// Adds points to <see cref="MiningPointsComponent"/> when making a recipe that has miningPoints set.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed partial class MiningPointsLatheComponent : Component;
9 changes: 9 additions & 0 deletions Content.Shared/DeltaV/Salvage/MiningPointsUI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Robust.Shared.Serialization;

namespace Content.Shared.DeltaV.Salvage;

/// <summary>
/// Message for a lathe to transfer its mining points to the user's id card.
/// </summary>
[Serializable, NetSerializable]
public sealed class LatheClaimMiningPointsMessage : BoundUserInterfaceMessage;
121 changes: 121 additions & 0 deletions Content.Shared/DeltaV/Salvage/Systems/MiningPointsSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using Content.Shared.Access.Systems;
using Content.Shared.DeltaV.Salvage.Components;
using Content.Shared.Lathe;
using Robust.Shared.Audio.Systems;

namespace Content.Shared.DeltaV.Salvage.Systems;

public sealed class MiningPointsSystem : EntitySystem
{
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedIdCardSystem _idCard = default!;

private EntityQuery<MiningPointsComponent> _query;

public override void Initialize()
{
base.Initialize();

_query = GetEntityQuery<MiningPointsComponent>();

SubscribeLocalEvent<MiningPointsLatheComponent, LatheStartPrintingEvent>(OnStartPrinting);
Subs.BuiEvents<MiningPointsLatheComponent>(LatheUiKey.Key, subs =>
{
subs.Event<LatheClaimMiningPointsMessage>(OnClaimMiningPoints);
});
}

#region Event Handlers

private void OnStartPrinting(Entity<MiningPointsLatheComponent> ent, ref LatheStartPrintingEvent args)
{
var points = args.Recipe.MiningPoints;
if (points > 0)
AddPoints(ent.Owner, points);
}

private void OnClaimMiningPoints(Entity<MiningPointsLatheComponent> ent, ref LatheClaimMiningPointsMessage args)
{
var user = args.Actor;
if (TryFindIdCard(user) is {} dest)
TransferAll(ent.Owner, dest);
}

#endregion
#region Public API

/// <summary>
/// Tries to find the user's id card and gets its <see cref="MiningPointsComponent"/>.
/// </summary>
/// <remarks>
/// Component is nullable for easy usage with the API due to Entity&lt;T&gt; not being usable for Entity&lt;T?&gt; arguments.
/// </remarks>
public Entity<MiningPointsComponent?>? TryFindIdCard(EntityUid user)
{
if (!_idCard.TryFindIdCard(user, out var idCard))
return null;

if (!_query.TryComp(idCard, out var comp))
return null;

return (idCard, comp);
}

/// <summary>
/// Removes points from a holder, returning true if it succeeded.
/// </summary>
public bool RemovePoints(Entity<MiningPointsComponent?> ent, uint amount)
{
if (!_query.Resolve(ent, ref ent.Comp) || amount > ent.Comp.Points)
return false;

ent.Comp.Points -= amount;
Dirty(ent);
return true;
}

/// <summary>
/// Add points to a holder.
/// </summary>
public bool AddPoints(Entity<MiningPointsComponent?> ent, uint amount)
{
if (!_query.Resolve(ent, ref ent.Comp))
return false;

ent.Comp.Points += amount;
Dirty(ent);
return true;
}

/// <summary>
/// Transfer a number of points from source to destination.
/// Returns true if the transfer succeeded.
/// </summary>
public bool Transfer(Entity<MiningPointsComponent?> src, Entity<MiningPointsComponent?> dest, uint amount)
{
// don't make a sound or anything
if (amount == 0)
return true;

if (!_query.Resolve(src, ref src.Comp) || !_query.Resolve(dest, ref dest.Comp))
return false;

if (!RemovePoints(src, amount))
return false;

AddPoints(dest, amount);
_audio.PlayPvs(src.Comp.TransferSound, src);
return true;
}

/// <summary>
/// Transfers all points from source to destination.
/// Returns true if the transfer succeeded.
/// </summary>
public bool TransferAll(Entity<MiningPointsComponent?> src, Entity<MiningPointsComponent?> dest)
{
return _query.Resolve(src, ref src.Comp) && Transfer(src, dest, src.Comp.Points);
}

#endregion
}
7 changes: 7 additions & 0 deletions Content.Shared/Research/Prototypes/LatheRecipePrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,12 @@ public sealed partial class LatheRecipePrototype : IPrototype, IInheritingProtot
/// </summary>
[DataField]
public ProtoId<LatheCategoryPrototype>? Category;

/// <summary>
/// DeltaV: Number of mining points this recipe adds to an oreproc when printed.
/// Scales with stack count.
/// </summary>
[DataField]
public uint MiningPoints;
}
}
2 changes: 2 additions & 0 deletions Resources/Locale/en-US/deltav/lathe/ui/lathe-menu.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lathe-menu-mining-points = Mining Points: {$points}
lathe-menu-mining-points-claim-button = Claim Points
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- type: StealTarget
stealGroup: IDCard
- type: NanoChatCard # DeltaV
- type: MiningPoints # DeltaV

#IDs with layers

Expand Down
5 changes: 5 additions & 0 deletions Resources/Prototypes/Entities/Structures/Machines/lathe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,10 @@
- type: MaterialStorageMagnetPickup # Delta V - Summary: Adds magnet pull from Frontier
magnetEnabled: True
range: 0.30 # Delta V - End Magnet Pull
- type: MiningPoints # DeltaV - Source of mining points for miners
transferSound:
path: /Audio/Effects/Cargo/ping.ogg
- type: MiningPointsLathe # DeltaV

- type: entity
parent: OreProcessor
Expand All @@ -1383,6 +1387,7 @@
materialUseMultiplier: 0.75
timeMultiplier: 0.5
staticRecipes:
- BluespaceCrystal # DeltaV - Bluespace Crystals can be created here.
- SheetSteel
- SheetGlass1
- SheetRGlass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
state: bluespace
result: MaterialBluespace1
completetime: 0
miningPoints: 50 # DeltaV
materials:
RawBluespace: 100
Loading
Loading