Syndicate surplus crates (#7992)

This commit is contained in:
Alex Evgrashin
2022-05-08 10:52:00 +03:00
committed by GitHub
parent e60d1ec909
commit dc2ef6708d
6 changed files with 140 additions and 0 deletions

View File

@@ -142,6 +142,7 @@ namespace Content.Client.Entry
"PowerMonitoringConsole", "PowerMonitoringConsole",
"RCD", "RCD",
"RCDAmmo", "RCDAmmo",
"SurplusBundle",
"CursedEntityStorage", "CursedEntityStorage",
"DiseaseArtifact", "DiseaseArtifact",
"Radio", "Radio",

View File

@@ -0,0 +1,15 @@
namespace Content.Server.Traitor.Uplink.SurplusBundle;
/// <summary>
/// Fill crate with a random uplink items.
/// </summary>
[RegisterComponent]
public sealed class SurplusBundleComponent : Component
{
/// <summary>
/// Total price of all content inside bundle.
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
[DataField("totalPrice")]
public int TotalPrice = 20;
}

View File

@@ -0,0 +1,89 @@
using System.Linq;
using Content.Server.Storage.Components;
using Content.Shared.PDA;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server.Traitor.Uplink.SurplusBundle;
public sealed class SurplusBundleSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
private UplinkStoreListingPrototype[] _uplinks = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SurplusBundleComponent, MapInitEvent>(OnMapInit);
InitList();
}
private void InitList()
{
// sort data in price descending order
_uplinks = _prototypeManager.EnumeratePrototypes<UplinkStoreListingPrototype>()
.Where(item => item.CanSurplus).ToArray();
Array.Sort(_uplinks, (a, b) => b.Price - a.Price);
}
private void OnMapInit(EntityUid uid, SurplusBundleComponent component, MapInitEvent args)
{
FillStorage(uid, component: component);
}
private void FillStorage(EntityUid uid, IStorageComponent? storage = null,
SurplusBundleComponent? component = null)
{
if (!Resolve(uid, ref storage, ref component))
return;
var cords = Transform(uid).Coordinates;
var content = GetRandomContent(component.TotalPrice);
foreach (var item in content)
{
var ent = EntityManager.SpawnEntity(item.ItemId, cords);
storage.Insert(ent);
}
}
// wow, is this leetcode reference?
private List<UplinkStoreListingPrototype> GetRandomContent(int targetCost)
{
var ret = new List<UplinkStoreListingPrototype>();
if (_uplinks.Length == 0)
return ret;
var totalCost = 0;
var index = 0;
while (totalCost < targetCost)
{
// All data is sorted in price descending order
// Find new item with the lowest acceptable price
// All expansive items will be before index, all acceptable after
var remainingBudget = targetCost - totalCost;
while (_uplinks[index].Price > remainingBudget)
{
index++;
if (index >= _uplinks.Length)
{
// Looks like no cheap items left
// It shouldn't be case for ss14 content
// Because there are 1 TC items
return ret;
}
}
// Select random listing and add into crate
var randomIndex = _random.Next(index, _uplinks.Length);
var randomItem = _uplinks[randomIndex];
ret.Add(randomItem);
totalCost += randomItem.Price;
}
return ret;
}
}

View File

@@ -30,5 +30,8 @@ namespace Content.Shared.PDA
[DataField("icon")] [DataField("icon")]
public SpriteSpecifier? Icon { get; } = null; public SpriteSpecifier? Icon { get; } = null;
[DataField("surplus")]
public bool CanSurplus = true;
} }
} }

View File

@@ -0,0 +1,15 @@
- type: entity
id: CrateSyndicateSurplusBundle
name: syndicate surplus crate
parent: CrateGenericSteel
components:
- type: SurplusBundle
totalPrice: 50
- type: entity
id: CrateSyndicateSuperSurplusBundle
name: syndicate super surplus crate
parent: CrateGenericSteel
components:
- type: SurplusBundle
totalPrice: 125

View File

@@ -89,6 +89,7 @@
category: Explosives category: Explosives
itemId: MobGrenadePenguin itemId: MobGrenadePenguin
price: 6 price: 6
surplus: false # got wrecked by penguins from surplus crate
- type: uplinkListing - type: uplinkListing
id: UplinkC4 id: UplinkC4
@@ -211,6 +212,21 @@
price: 50 price: 50
icon: /Textures/Structures/Wallmounts/signs.rsi/bio.png icon: /Textures/Structures/Wallmounts/signs.rsi/bio.png
- type: uplinkListing
id: UplinkSurplusBundle
category: Bundles
itemId: CrateSyndicateSurplusBundle
description: Contains 50 telecrystals worth of completely random Syndicate items. It can be useless junk or really good.
price: 20
surplus: false
- type: uplinkListing
id: UplinkSuperSurplusBundle
category: Bundles
itemId: CrateSyndicateSuperSurplusBundle
description: Contains 125 telecrystals worth of completely random Syndicate items.
price: 40
surplus: false
#- type: uplinkListing #- type: uplinkListing
# id: UplinkCarbineBundle # id: UplinkCarbineBundle
@@ -384,3 +400,4 @@
listingName: syndicate segway listingName: syndicate segway
description: Be an enemy of the corporation, in style! description: Be an enemy of the corporation, in style!
price: 5 price: 5
surplus: false