Files
tbd-station-14/Content.Server/Gatherable/GatherableSystem.cs
metalgearsloth bfbb1a689f 1 do_after for ore veins (#14083)
* Ore veins

I dislike rocks just providing generic drops and this factors into mining more.

* fixes

* descriptions

* comment

* every flipping time

* Make mining destroy rocks with 1 hit

Having to click 3 times was pretty annoying.

* a
2023-02-13 15:32:09 +00:00

120 lines
4.1 KiB
C#

using System.Threading;
using Content.Server.Destructible;
using Content.Server.DoAfter;
using Content.Server.Gatherable.Components;
using Content.Shared.Damage;
using Content.Shared.Destructible;
using Content.Shared.EntityList;
using Content.Shared.Interaction;
using Content.Shared.Tag;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server.Gatherable;
public sealed class GatherableSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly DamageableSystem _damageableSystem = default!;
[Dependency] private readonly DestructibleSystem _destructible = default!;
[Dependency] private readonly DoAfterSystem _doAfterSystem = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly TagSystem _tagSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GatherableComponent, InteractUsingEvent>(OnInteractUsing);
SubscribeLocalEvent<GatheringDoafterCancel>(OnDoafterCancel);
SubscribeLocalEvent<GatherableComponent, GatheringDoafterSuccess>(OnDoafterSuccess);
}
private void OnInteractUsing(EntityUid uid, GatherableComponent component, InteractUsingEvent args)
{
if (!TryComp<GatheringToolComponent>(args.Used, out var tool) ||
component.ToolWhitelist?.IsValid(args.Used) == false ||
tool.GatheringEntities.TryGetValue(uid, out var cancelToken))
return;
// Can't gather too many entities at once.
if (tool.MaxGatheringEntities < tool.GatheringEntities.Count + 1)
return;
var damageRequired = _destructible.DestroyedAt(uid);
var damageTime = (damageRequired / tool.Damage.Total).Float();
damageTime = Math.Max(1f, damageTime);
cancelToken = new CancellationTokenSource();
tool.GatheringEntities[uid] = cancelToken;
var doAfter = new DoAfterEventArgs(args.User, damageTime, cancelToken.Token, uid)
{
BreakOnDamage = true,
BreakOnStun = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
MovementThreshold = 0.25f,
BroadcastCancelledEvent = new GatheringDoafterCancel { Tool = args.Used, Resource = uid },
TargetFinishedEvent = new GatheringDoafterSuccess { Tool = args.Used, Resource = uid, Player = args.User }
};
_doAfterSystem.DoAfter(doAfter);
}
private void OnDoafterSuccess(EntityUid uid, GatherableComponent component, GatheringDoafterSuccess ev)
{
if (!TryComp(ev.Tool, out GatheringToolComponent? tool))
return;
// Complete the gathering process
_destructible.DestroyEntity(uid);
_audio.PlayPvs(tool.GatheringSound, ev.Resource);
tool.GatheringEntities.Remove(ev.Resource);
// Spawn the loot!
if (component.MappedLoot == null)
return;
var playerPos = Transform(ev.Player).MapPosition;
foreach (var (tag, table) in component.MappedLoot)
{
if (tag != "All")
{
if (!_tagSystem.HasTag(tool.Owner, tag))
continue;
}
var getLoot = _prototypeManager.Index<EntityLootTablePrototype>(table);
var spawnLoot = getLoot.GetSpawns();
var spawnPos = playerPos.Offset(_random.NextVector2(0.3f));
Spawn(spawnLoot[0], spawnPos);
}
}
private void OnDoafterCancel(GatheringDoafterCancel ev)
{
if (!TryComp<GatheringToolComponent>(ev.Tool, out var tool))
return;
tool.GatheringEntities.Remove(ev.Resource);
}
private sealed class GatheringDoafterCancel : EntityEventArgs
{
public EntityUid Tool;
public EntityUid Resource;
}
private sealed class GatheringDoafterSuccess : EntityEventArgs
{
public EntityUid Tool;
public EntityUid Resource;
public EntityUid Player;
}
}