Files
crystall-punk-14/Content.Server/_CP14/UniqueLoot/CP14UniqueLootSystem.cs

98 lines
2.5 KiB
C#
Raw Normal View History

2025-03-09 18:48:48 +03:00
using System.Linq;
using Content.Shared._CP14.UniqueLoot;
using Content.Shared.GameTicking;
2025-03-09 18:48:48 +03:00
using Content.Shared.Tag;
using Robust.Client.GameObjects;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server._CP14.UniqueLoot;
public sealed partial class CP14UniqueLootSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;
2025-03-09 18:48:48 +03:00
private readonly Dictionary<CP14UniqueLootPrototype, int> _uniqueLootCount = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnCleanup);
SubscribeLocalEvent<CP14UniqueLootSpawnerComponent, MapInitEvent>(OnMapInit);
RefreshUniqueLoot();
}
private void OnMapInit(Entity<CP14UniqueLootSpawnerComponent> ent, ref MapInitEvent args)
{
2025-03-09 18:48:48 +03:00
var loot = GetNextUniqueLoot(ent.Comp.Tag);
2025-02-10 20:45:53 +03:00
if (loot == null)
return;
if (TerminatingOrDeleted(ent) || !Exists(ent))
return;
var coords = Transform(ent).Coordinates;
var spawned = Spawn(loot, coords);
_transform.SetWorldRotation(spawned, _transform.GetWorldRotation(ent));
}
private void OnCleanup(RoundRestartCleanupEvent ev)
{
RefreshUniqueLoot();
}
private void RefreshUniqueLoot()
{
_uniqueLootCount.Clear();
foreach (var loot in _proto.EnumeratePrototypes<CP14UniqueLootPrototype>())
{
2025-03-09 18:48:48 +03:00
_uniqueLootCount[loot] = loot.Count;
}
}
2025-03-09 18:48:48 +03:00
public EntProtoId? GetNextUniqueLoot(ProtoId<TagPrototype>? withTag = null)
{
if (_uniqueLootCount.Count == 0)
return null;
2025-03-09 18:48:48 +03:00
var possibleLoot = _uniqueLootCount.Keys.ToList();
2025-03-09 18:48:48 +03:00
CP14UniqueLootPrototype? selectedLoot = null;
2025-03-09 18:48:48 +03:00
while (selectedLoot is null)
{
if (possibleLoot.Count == 0)
return null;
2025-03-09 18:48:48 +03:00
var tryLoot = _random.Pick(possibleLoot);
if (withTag != null && !tryLoot.Tags.Contains(withTag.Value))
{
possibleLoot.Remove(tryLoot);
continue;
}
selectedLoot = tryLoot;
break;
}
if (_uniqueLootCount[selectedLoot] > 1)
_uniqueLootCount[selectedLoot] -= 1;
else
_uniqueLootCount.Remove(selectedLoot);
2025-03-09 18:48:48 +03:00
return selectedLoot.Entity;
}
}