Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Система ключей #7

Merged
merged 22 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions Content.Server/CrystallPunk/LockKey/KeyholeGenerationSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@

using Content.Server.GameTicking.Events;
using Content.Shared.Containers.ItemSlots;
using Content.Shared.CrystallPunk.LockKey;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Content.Shared.Lock;
using Content.Shared.Verbs;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace Content.Server.CrystallPunk.LockKey;


public sealed partial class KeyholeGenerationSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedPopupSystem _popup = default!;
[Dependency] private readonly ItemSlotsSystem _itemSlots = default!;
[Dependency] private readonly LockSystem _lock = default!;

private Dictionary<ProtoId<CPLockCategoryPrototype>, List<int>> _roundKeyData = new();

private const int DepthCompexity = 2;

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<RoundStartingEvent>(OnRoundStart);

SubscribeLocalEvent<CPLockComponent, MapInitEvent>(OnLockInit);
SubscribeLocalEvent<CPKeyComponent, MapInitEvent>(OnKeyInit);

SubscribeLocalEvent<CPKeyComponent, ExaminedEvent>(OnKeyExamine);
}

#region Init
private void OnRoundStart(RoundStartingEvent ev)
{
_roundKeyData = new();
}

private void OnKeyInit(Entity<CPKeyComponent> keyEnt, ref MapInitEvent args)
{
if (keyEnt.Comp.AutoGenerateShape != null)
{
keyEnt.Comp.LockShape = GetKeyLockData(keyEnt.Comp.AutoGenerateShape.Value);
}
}

private void OnLockInit(Entity<CPLockComponent> lockEnt, ref MapInitEvent args)
{
if (lockEnt.Comp.AutoGenerateShape != null)
{
lockEnt.Comp.LockShape = GetKeyLockData(lockEnt.Comp.AutoGenerateShape.Value);
}
}
#endregion

private void OnKeyExamine(Entity<CPKeyComponent> key, ref ExaminedEvent args)
{
var parent = Transform(key).ParentUid;
if (parent != args.Examiner)
return;

if (key.Comp.LockShape == null)
return;

var markup = Loc.GetString("cp-lock-examine-key", ("item", MetaData(key).EntityName));
markup += " (";
foreach (var item in key.Comp.LockShape)
{
markup += $"{item} ";
}
markup += ")";
args.PushMarkup(markup);
}

private List<int> GetKeyLockData(ProtoId<CPLockCategoryPrototype> category)
{
if (_roundKeyData.ContainsKey(category))
return _roundKeyData[category];
else
{
var newData = GenerateNewUniqueLockData(category);
_roundKeyData[category] = newData;
return newData;
}
}

private List<int> GenerateNewUniqueLockData(ProtoId<CPLockCategoryPrototype> category)
{
List<int> newKeyData = new List<int>();
var categoryData = _proto.Index(category);
var ready = false;
var iteration = 0;

while (!ready)
{
//Generate try
newKeyData = new List<int>();
for (int i = 0; i < categoryData.Complexity; i++)
{
newKeyData.Add(_random.Next(-DepthCompexity, DepthCompexity));
}

//Identity Check shitcode
// На текущий момент он пытается сгенерировать уникальный код. Если он 100 раз не смог сгенерировать уникальный код, он выдаст последний сгенерированный неуникальный.
var unique = true;
foreach (var pair in _roundKeyData)
{
if (newKeyData.SequenceEqual(pair.Value))
{
unique = false;
break;
}
}
if (unique)
return newKeyData;
else
iteration++;

if (iteration > 100)
{
break;
}
}
Log.Error("The unique key for CPLockSystem could not be generated!");
return newKeyData; //FUCK
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Content.Shared.Parallax.Biomes;
using Robust.Shared.Prototypes;

namespace Content.Server.Corvax.CrystallPunk.SpawnMapBiome;
namespace Content.Server.CrystallPunk.SpawnMapBiome;

/// <summary>
/// allows you to initialize a planet on a specific map at initialization time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Robust.Shared.Map;
using Robust.Shared.Prototypes;

namespace Content.Server.Corvax.CrystallPunk.SpawnMapBiome;
namespace Content.Server.CrystallPunk.SpawnMapBiome;
public sealed partial class StationBiomeSystem : EntitySystem
{
[Dependency] private readonly BiomeSystem _biome = default!;
Expand Down
3 changes: 2 additions & 1 deletion Content.Server/Storage/EntitySystems/EntityStorageSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Content.Server.Body.Systems;
using Content.Server.Construction;
using Content.Server.Construction.Components;
using Content.Server.CrystallPunk.LockKey;
using Content.Server.Storage.Components;
using Content.Shared.Destructible;
using Content.Shared.Explosion;
Expand Down Expand Up @@ -36,7 +37,7 @@ public override void Initialize()
SubscribeLocalEvent<EntityStorageComponent, EntityUnpausedEvent>(OnEntityUnpausedEvent);
SubscribeLocalEvent<EntityStorageComponent, ComponentInit>(OnComponentInit);
SubscribeLocalEvent<EntityStorageComponent, ComponentStartup>(OnComponentStartup);
SubscribeLocalEvent<EntityStorageComponent, ActivateInWorldEvent>(OnInteract, after: new[] { typeof(LockSystem) });
SubscribeLocalEvent<EntityStorageComponent, ActivateInWorldEvent>(OnInteract, after: new[] { typeof(LockSystem), typeof(KeyholeGenerationSystem) });
SubscribeLocalEvent<EntityStorageComponent, LockToggleAttemptEvent>(OnLockToggleAttempt);
SubscribeLocalEvent<EntityStorageComponent, DestructionEventArgs>(OnDestruction);
SubscribeLocalEvent<EntityStorageComponent, GetVerbsEvent<InteractionVerb>>(AddToggleOpenVerb);
Expand Down
19 changes: 19 additions & 0 deletions Content.Shared/CrystallPunk/LockKey/CPLockCategoryPrototype.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Robust.Shared.Prototypes;

namespace Content.Shared.CrystallPunk.LockKey;

/// <summary>
/// A prototype of the lock category. Need a roundstart mapping to ensure that keys and locks will fit together despite randomization.
/// </summary>
[Prototype("CPLockCategory")]
public sealed partial class CPLockCategoryPrototype : IPrototype
{
[ViewVariables]
[IdDataField]
public string ID { get; private set; } = default!;

/// <summary>
/// The number of elements that will be generated for the category.
/// </summary>
[DataField] public int Complexity = 3;
}
19 changes: 19 additions & 0 deletions Content.Shared/CrystallPunk/LockKey/Components/CPKeyComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Robust.Shared.Prototypes;

namespace Content.Shared.CrystallPunk.LockKey;

/// <summary>
/// a key component that can be used to unlock and lock locks from CPLockComponent
/// </summary>
[RegisterComponent]
public sealed partial class CPKeyComponent : Component
{
[DataField]
public List<int>? LockShape = null;

/// <summary>
/// If not null, automatically generates a key for the specified category on initialization. This ensures that the lock will be opened with a key of the same category.
/// </summary>
[DataField]
public ProtoId<CPLockCategoryPrototype>? AutoGenerateShape = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Content.Shared.CrystallPunk.LockKey;

/// <summary>
/// A component that allows you to track a ring of keys to quickly open and lock doors with the entire bunch.
/// </summary>
[RegisterComponent]
public sealed partial class CPKeyRingComponent : Component
{
}
37 changes: 37 additions & 0 deletions Content.Shared/CrystallPunk/LockKey/Components/CPLockComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Content.Shared.CrystallPunk.LockKey;
using Content.Shared.Damage;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;

namespace Content.Shared.CrystallPunk.LockKey;

/// <summary>
/// A component of a lock that stores its keyhole shape, complexity, and current state.
/// </summary>
[RegisterComponent]
public sealed partial class CPLockComponent : Component
{
[DataField]
public List<int>? LockShape = null;

[DataField]
public float LockPickDamageChance = 0.2f;

/// <summary>
/// On which element of the shape sequence the lock is now located. It's necessary for the mechanics of breaking and entering.
/// </summary>
[DataField]
public int LockpickStatus = 0;

/// <summary>
/// after a lock is broken into, it leaves a description on it that it's been tampered with.
/// </summary>
[DataField]
public bool LockpickeddFailMarkup = false;

/// <summary>
/// If not null, automatically generates a lock for the specified category on initialization. This ensures that the lock will be opened with a key of the same category.
/// </summary>
[DataField]
public ProtoId<CPLockCategoryPrototype>? AutoGenerateShape = null;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Content.Shared.CrystallPunk.LockKey;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;

namespace Content.Shared.CrystallPunk.LockKey;

/// <summary>
/// A component of a lock that stores its keyhole shape, complexity, and current state.
/// </summary>
[RegisterComponent]
public sealed partial class CPLockpickComponent : Component
{
[DataField]
public int Health = 3;

[DataField]
public SoundSpecifier SuccessSound = new SoundPathSpecifier("/Audio/_CP14/Items/lockpick_use.ogg")
{
Params = AudioParams.Default
.WithVariation(0.05f)
.WithVolume(0.5f)
};

[DataField]
public SoundSpecifier FailSound = new SoundPathSpecifier("/Audio/_CP14/Items/lockpick_fail.ogg")
{
Params = AudioParams.Default
.WithVariation(0.05f)
.WithVolume(0.5f)
};
}
Loading
Loading