|
| 1 | +using Content.Server.Storage.Events; |
| 2 | +using Content.Shared.Dataset; |
| 3 | +using Content.Shared.NameModifier.EntitySystems; |
| 4 | +using Content.Shared.Storage; |
| 5 | +using Content.Shared.Weapons.Ranged.Components; |
| 6 | +using Robust.Shared.Prototypes; |
| 7 | +using Robust.Shared.Random; |
| 8 | + |
| 9 | +namespace Content.Server._NF.Weapons.Rarity; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// A stand-alone system intended to modularly sit atop the existing gun and weapon systems to create dynamic weapon rarities. |
| 13 | +/// </summary> |
| 14 | +public sealed partial class WeaponRaritySystem : EntitySystem |
| 15 | +{ |
| 16 | + [Dependency] private readonly IRobustRandom _random = default!; |
| 17 | + [Dependency] private readonly MetaDataSystem _metaSystem = default!; |
| 18 | + [Dependency] private readonly NameModifierSystem _namingSystem = default!; |
| 19 | + [Dependency] private readonly IPrototypeManager _protoMan = default!; |
| 20 | + |
| 21 | + public override void Initialize() |
| 22 | + { |
| 23 | + base.Initialize(); |
| 24 | + SubscribeLocalEvent<RareWeaponSpawnerCaseComponent, StorageFilledEvent>(OnCaseSpawn); |
| 25 | + SubscribeLocalEvent<RareWeaponComponent, RefreshNameModifiersEvent>(OnRefreshNameModifiers); |
| 26 | + } |
| 27 | + |
| 28 | + private void OnCaseSpawn(Entity<RareWeaponSpawnerCaseComponent> ent, ref StorageFilledEvent args) |
| 29 | + { |
| 30 | + if (!TryComp(ent, out StorageComponent? storage)) |
| 31 | + return; |
| 32 | + |
| 33 | + foreach (var item in storage.StoredItems) |
| 34 | + { |
| 35 | + if (TryComp(item.Key, out GunComponent? gunComp)) |
| 36 | + { |
| 37 | + ModifyGun(ent.Comp, item.Key, gunComp); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private void ModifyGun(RareWeaponSpawnerCaseComponent comp, EntityUid gun, GunComponent gunComp) |
| 43 | + { |
| 44 | + //Basic functionality of 3 rarity levels |
| 45 | + var rarity = comp.Rarity; |
| 46 | + if (comp.RandomRarity) |
| 47 | + { |
| 48 | + rarity = _random.Next(0, 4); |
| 49 | + } |
| 50 | + |
| 51 | + RenameGun(gun, rarity); |
| 52 | + |
| 53 | + while (rarity > 0) |
| 54 | + { |
| 55 | + var chance = _random.Next(1, 4); |
| 56 | + switch (chance) |
| 57 | + { |
| 58 | + case 1: |
| 59 | + ImproveAccuracy(comp.AccuracyModifier, gun, gunComp); |
| 60 | + break; |
| 61 | + case 2: |
| 62 | + ImproveFireRate(comp.FireRateModifier, gun, gunComp); |
| 63 | + break; |
| 64 | + case 3: |
| 65 | + ImproveProjectileSpeed(comp.ProjectileSpeedModifier, gun, gunComp); |
| 66 | + break; |
| 67 | + } |
| 68 | + rarity--; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private void ImproveAccuracy(StatModifier modifier, EntityUid gun, GunComponent? gunComp) |
| 73 | + { |
| 74 | + if (!Resolve(gun, ref gunComp)) |
| 75 | + return; |
| 76 | + |
| 77 | + var buff = modifier.Next(_random); |
| 78 | + |
| 79 | + gunComp.MinAngle *= buff; |
| 80 | + gunComp.MinAngleModified *= buff; |
| 81 | + gunComp.MaxAngle *= buff; |
| 82 | + gunComp.MaxAngleModified *= buff; |
| 83 | + gunComp.AngleIncrease *= buff; |
| 84 | + gunComp.AngleIncreaseModified *= buff; |
| 85 | + gunComp.AngleDecay *= 1 / buff; |
| 86 | + gunComp.AngleDecayModified *= 1 / buff; |
| 87 | + } |
| 88 | + |
| 89 | + private void ImproveFireRate(StatModifier modifier, EntityUid gun, GunComponent? gunComp) |
| 90 | + { |
| 91 | + if (!Resolve(gun, ref gunComp)) |
| 92 | + return; |
| 93 | + |
| 94 | + var buff = modifier.Next(_random); |
| 95 | + |
| 96 | + gunComp.FireRate *= buff; |
| 97 | + gunComp.FireRateModified *= buff; |
| 98 | + } |
| 99 | + |
| 100 | + private void ImproveProjectileSpeed(StatModifier modifier, EntityUid gun, GunComponent? gunComp) |
| 101 | + { |
| 102 | + if (!Resolve(gun, ref gunComp)) |
| 103 | + return; |
| 104 | + |
| 105 | + var buff = modifier.Next(_random); |
| 106 | + |
| 107 | + gunComp.ProjectileSpeed *= buff; |
| 108 | + gunComp.ProjectileSpeedModified *= buff; |
| 109 | + } |
| 110 | + |
| 111 | + private void RenameGun(EntityUid gun, int rarity) |
| 112 | + { |
| 113 | + var datasetUncommon = _protoMan.Index<LocalizedDatasetPrototype>("NFNamesGunsUncommon"); |
| 114 | + var datasetRare = _protoMan.Index<LocalizedDatasetPrototype>("NFNamesGunsRare"); |
| 115 | + var datasetEpic = _protoMan.Index<LocalizedDatasetPrototype>("NFNamesGunsEpic"); |
| 116 | + |
| 117 | + var meta = MetaData(gun); |
| 118 | + var rareComp = EnsureComp<RareWeaponComponent>(gun); |
| 119 | + |
| 120 | + if (rarity == 1) |
| 121 | + { |
| 122 | + rareComp.NameModifiers.Add(_random.Pick(datasetUncommon.Values)); |
| 123 | + } |
| 124 | + else if (rarity == 2) |
| 125 | + { |
| 126 | + rareComp.NameModifiers.Add(_random.Pick(datasetUncommon.Values)); |
| 127 | + rareComp.NameModifiers.Add(_random.Pick(datasetRare.Values)); |
| 128 | + } |
| 129 | + else if (rarity >= 3) |
| 130 | + { |
| 131 | + // At this rarity, we rename the entire gun instead of adding a modifier |
| 132 | + var pick = _random.Pick(datasetEpic.Values); |
| 133 | + _metaSystem.SetEntityName(gun, Loc.GetString(pick), meta, false); |
| 134 | + } |
| 135 | + |
| 136 | + _namingSystem.RefreshNameModifiers(gun); |
| 137 | + } |
| 138 | + |
| 139 | + private void OnRefreshNameModifiers(Entity<RareWeaponComponent> ent, ref RefreshNameModifiersEvent args) |
| 140 | + { |
| 141 | + foreach (var modifier in ent.Comp.NameModifiers) |
| 142 | + { |
| 143 | + args.AddModifier(modifier); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments