Key distribution system (#625)
* data restruct * yay * Update arenas.yml * fixes * auto labeling * shuffle
This commit is contained in:
15
Content.Server/_CP14/LockKey/CP14AbstractKeyComponent.cs
Normal file
15
Content.Server/_CP14/LockKey/CP14AbstractKeyComponent.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
using Content.Shared._CP14.LockKey;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server._CP14.LockKey;
|
||||
|
||||
[RegisterComponent]
|
||||
public sealed partial class CP14AbstractKeyComponent : Component
|
||||
{
|
||||
[DataField(required: true)]
|
||||
public ProtoId<CP14LockGroupPrototype> Group = default;
|
||||
|
||||
[DataField]
|
||||
public bool DeleteOnFailure = true;
|
||||
}
|
||||
67
Content.Server/_CP14/LockKey/CP14KeyDistributionSystem.cs
Normal file
67
Content.Server/_CP14/LockKey/CP14KeyDistributionSystem.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using Content.Server.Station.Events;
|
||||
using Content.Shared._CP14.LockKey;
|
||||
using Content.Shared._CP14.LockKey.Components;
|
||||
using Content.Shared.Station.Components;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server._CP14.LockKey;
|
||||
|
||||
public sealed partial class CP14KeyDistributionSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _proto = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly CP14KeyholeGenerationSystem _keyGeneration = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<CP14AbstractKeyComponent, MapInitEvent>(OnMapInit);
|
||||
}
|
||||
|
||||
private void OnMapInit(Entity<CP14AbstractKeyComponent> ent, ref MapInitEvent args)
|
||||
{
|
||||
if (!TrySetShape(ent) && ent.Comp.DeleteOnFailure)
|
||||
QueueDel(ent);
|
||||
}
|
||||
|
||||
private bool TrySetShape(Entity<CP14AbstractKeyComponent> ent)
|
||||
{
|
||||
var grid = Transform(ent).GridUid;
|
||||
|
||||
if (grid is null)
|
||||
return false;
|
||||
|
||||
if (!TryComp<CP14KeyComponent>(ent, out var key))
|
||||
return false;
|
||||
|
||||
if (!TryComp<StationMemberComponent>(grid.Value, out var member))
|
||||
return false;
|
||||
|
||||
if (!TryComp<CP14StationKeyDistributionComponent>(member.Station, out var distribution))
|
||||
return false;
|
||||
|
||||
var keysList = new List<ProtoId<CP14LockTypePrototype>>(distribution.Keys);
|
||||
while (keysList.Count > 0)
|
||||
{
|
||||
var randomIndex = _random.Next(keysList.Count);
|
||||
var keyA = keysList[randomIndex];
|
||||
|
||||
var indexedKey = _proto.Index(keyA);
|
||||
|
||||
if (indexedKey.Group != ent.Comp.Group)
|
||||
{
|
||||
keysList.RemoveAt(randomIndex);
|
||||
continue;
|
||||
}
|
||||
|
||||
_keyGeneration.SetShape((ent, key), indexedKey);
|
||||
distribution.Keys.Remove(indexedKey);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Labels;
|
||||
using Content.Shared._CP14.LockKey;
|
||||
using Content.Shared._CP14.LockKey.Components;
|
||||
using Content.Shared.Examine;
|
||||
@@ -12,8 +13,9 @@ public sealed partial class CP14KeyholeGenerationSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _proto = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly LabelSystem _label = default!;
|
||||
|
||||
private Dictionary<ProtoId<CP14LockCategoryPrototype>, List<int>> _roundKeyData = new();
|
||||
private Dictionary<ProtoId<CP14LockTypePrototype>, List<int>> _roundKeyData = new();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
@@ -37,7 +39,7 @@ public sealed partial class CP14KeyholeGenerationSystem : EntitySystem
|
||||
{
|
||||
if (keyEnt.Comp.AutoGenerateShape != null)
|
||||
{
|
||||
keyEnt.Comp.LockShape = GetKeyLockData(keyEnt.Comp.AutoGenerateShape.Value);
|
||||
SetShape(keyEnt, keyEnt.Comp.AutoGenerateShape.Value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +47,7 @@ public sealed partial class CP14KeyholeGenerationSystem : EntitySystem
|
||||
{
|
||||
if (lockEnt.Comp.AutoGenerateShape != null)
|
||||
{
|
||||
lockEnt.Comp.LockShape = GetKeyLockData(lockEnt.Comp.AutoGenerateShape.Value);
|
||||
SetShape(lockEnt, lockEnt.Comp.AutoGenerateShape.Value);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
@@ -69,7 +71,7 @@ public sealed partial class CP14KeyholeGenerationSystem : EntitySystem
|
||||
args.PushMarkup(markup);
|
||||
}
|
||||
|
||||
private List<int> GetKeyLockData(ProtoId<CP14LockCategoryPrototype> category)
|
||||
private List<int> GetKeyLockData(ProtoId<CP14LockTypePrototype> category)
|
||||
{
|
||||
if (_roundKeyData.ContainsKey(category))
|
||||
return _roundKeyData[category];
|
||||
@@ -79,7 +81,25 @@ public sealed partial class CP14KeyholeGenerationSystem : EntitySystem
|
||||
return newData;
|
||||
}
|
||||
|
||||
private List<int> GenerateNewUniqueLockData(ProtoId<CP14LockCategoryPrototype> category)
|
||||
public void SetShape(Entity<CP14KeyComponent> keyEnt, ProtoId<CP14LockTypePrototype> type)
|
||||
{
|
||||
keyEnt.Comp.LockShape = GetKeyLockData(type);
|
||||
|
||||
var indexedType = _proto.Index(type);
|
||||
if (indexedType.Name is not null)
|
||||
_label.Label(keyEnt, Loc.GetString(indexedType.Name.Value));
|
||||
}
|
||||
|
||||
public void SetShape(Entity<CP14LockComponent> lockEnt, ProtoId<CP14LockTypePrototype> type)
|
||||
{
|
||||
lockEnt.Comp.LockShape = GetKeyLockData(type);
|
||||
|
||||
var indexedType = _proto.Index(type);
|
||||
if (indexedType.Name is not null)
|
||||
_label.Label(lockEnt, Loc.GetString(indexedType.Name.Value));
|
||||
}
|
||||
|
||||
private List<int> GenerateNewUniqueLockData(ProtoId<CP14LockTypePrototype> category)
|
||||
{
|
||||
List<int> newKeyData = new();
|
||||
var categoryData = _proto.Index(category);
|
||||
|
||||
Reference in New Issue
Block a user