Mining Tweaks (#11468)
This commit is contained in:
32
Content.Server/Mining/Components/OreVeinComponent.cs
Normal file
32
Content.Server/Mining/Components/OreVeinComponent.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Content.Shared.Mining;
|
||||
using Content.Shared.Random;
|
||||
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
|
||||
|
||||
namespace Content.Server.Mining.Components;
|
||||
|
||||
/// <summary>
|
||||
/// Defines an entity that will drop a random ore after being destroyed.
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class OreVeinComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// How often an entity will be seeded with ore. Note: the amount of ore
|
||||
/// that is dropped is dependent on the ore prototype. <see crefalso="OrePrototype"/>
|
||||
/// </summary>
|
||||
[DataField("oreChance")]
|
||||
public float OreChance = 0.1f;
|
||||
|
||||
/// <summary>
|
||||
/// The weighted random prototype used for determining what ore will be dropped.
|
||||
/// </summary>
|
||||
[DataField("oreRarityPrototypeId", customTypeSerializer: typeof(PrototypeIdSerializer<WeightedRandomPrototype>))]
|
||||
public string? OreRarityPrototypeId;
|
||||
|
||||
/// <summary>
|
||||
/// The ore that this entity holds.
|
||||
/// If set in the prototype, it will not be overriden.
|
||||
/// </summary>
|
||||
[DataField("currentOre", customTypeSerializer: typeof(PrototypeIdSerializer<OrePrototype>)), ViewVariables(VVAccess.ReadWrite)]
|
||||
public string? CurrentOre;
|
||||
}
|
||||
52
Content.Server/Mining/MiningSystem.cs
Normal file
52
Content.Server/Mining/MiningSystem.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Content.Server.Mining.Components;
|
||||
using Content.Shared.Destructible;
|
||||
using Content.Shared.Mining;
|
||||
using Content.Shared.Random;
|
||||
using Content.Shared.Random.Helpers;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Mining;
|
||||
|
||||
/// <summary>
|
||||
/// This handles creating ores when the entity is destroyed.
|
||||
/// </summary>
|
||||
public sealed class MiningSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _proto = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<OreVeinComponent, MapInitEvent>(OnMapInit);
|
||||
SubscribeLocalEvent<OreVeinComponent, DestructionEventArgs>(OnDestruction);
|
||||
}
|
||||
|
||||
private void OnDestruction(EntityUid uid, OreVeinComponent component, DestructionEventArgs args)
|
||||
{
|
||||
if (component.CurrentOre == null)
|
||||
return;
|
||||
|
||||
var proto = _proto.Index<OrePrototype>(component.CurrentOre);
|
||||
|
||||
if (proto.OreEntity == null)
|
||||
return;
|
||||
|
||||
var coords = Transform(uid).Coordinates;
|
||||
var toSpawn = _random.Next(proto.MinOreYield, proto.MaxOreYield);
|
||||
for (var i = 0; i < toSpawn; i++)
|
||||
{
|
||||
Spawn(proto.OreEntity, coords.Offset(_random.NextVector2(0.3f)));
|
||||
}
|
||||
}
|
||||
|
||||
private void OnMapInit(EntityUid uid, OreVeinComponent component, MapInitEvent args)
|
||||
{
|
||||
if (component.CurrentOre != null || component.OreRarityPrototypeId == null || !_random.Prob(component.OreChance))
|
||||
return;
|
||||
|
||||
component.CurrentOre = _proto.Index<WeightedRandomPrototype>(component.OreRarityPrototypeId).Pick(_random);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user