Skip to content

Commit

Permalink
add mining points (#2419)
Browse files Browse the repository at this point in the history
* add mining points

* add claim points button to oreproc

* funny

* its over

* :trollface:

* xml fail

Signed-off-by: deltanedas <[email protected]>

---------

Signed-off-by: deltanedas <[email protected]>
Co-authored-by: deltanedas <@deltanedas:kde.org>
  • Loading branch information
deltanedas authored and sleepyyapril committed Dec 28, 2024
1 parent 711367e commit 54583f5
Show file tree
Hide file tree
Showing 12 changed files with 308 additions and 0 deletions.
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
53 changes: 53 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,52 @@ 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();

if (_entityManager.TryGetComponent<LatheComponent>(Entity, out var latheComp))
{
AmountLineEdit.SetText(latheComp.DefaultProductionAmount.ToString());
}
}

/// <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
26 changes: 26 additions & 0 deletions Content.Shared/DeltaV/Salvage/Components/MiningPointsComponent.cs
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 @@ -60,5 +60,12 @@ public sealed partial class LatheRecipePrototype : IPrototype
/// </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 @@ -1320,6 +1320,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 Down Expand Up @@ -1350,6 +1354,7 @@
- IngotGold30
- IngotSilver30
- MaterialBananium10
- MaterialDiamond

- type: entity
parent: BaseLathe
Expand Down
Loading

0 comments on commit 54583f5

Please sign in to comment.