RND Rework [Death to Techweb] (#16370)

* Techweb rework

* more ui work

* finishing ui

* Finish all the C# logic

* the techs + lathes

* remove old-tech

* mirror-review
This commit is contained in:
Nemanja
2023-05-15 16:17:30 -04:00
committed by GitHub
parent a71d9c8eff
commit 9efc727fe1
51 changed files with 1732 additions and 1401 deletions

View File

@@ -2,73 +2,62 @@ using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
namespace Content.Shared.Research.Components
namespace Content.Shared.Research.Components;
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
public sealed partial class ResearchServerComponent : Component
{
[RegisterComponent, NetworkedComponent]
public sealed class ResearchServerComponent : Component
{
/// <summary>
/// The name of the server
/// </summary>
[DataField("servername"), ViewVariables(VVAccess.ReadWrite)]
public string ServerName = "RDSERVER";
/// <summary>
/// The amount of points on the server.
/// </summary>
[DataField("points"), ViewVariables(VVAccess.ReadWrite)]
public int Points;
/// <summary>
/// A unique numeric id representing the server
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
public int Id;
/// <summary>
/// Entities connected to the server
/// </summary>
/// <remarks>
/// This is not safe to read clientside
/// </remarks>
[ViewVariables(VVAccess.ReadOnly)]
public List<EntityUid> Clients = new();
[DataField("nextUpdateTime", customTypeSerializer: typeof(TimeOffsetSerializer))]
public TimeSpan NextUpdateTime = TimeSpan.Zero;
[DataField("researchConsoleUpdateTime"), ViewVariables(VVAccess.ReadWrite)]
public readonly TimeSpan ResearchConsoleUpdateTime = TimeSpan.FromSeconds(1);
}
[Serializable, NetSerializable]
public sealed class ResearchServerState : ComponentState
{
public string ServerName;
public int Points;
public int Id;
public ResearchServerState(string serverName, int points, int id)
{
ServerName = serverName;
Points = points;
Id = id;
}
}
/// <summary>
/// The name of the server
/// </summary>
[AutoNetworkedField]
[DataField("serverName"), ViewVariables(VVAccess.ReadWrite)]
public string ServerName = "RDSERVER";
/// <summary>
/// Event raised on a server's clients when the point value of the server is changed.
/// The amount of points on the server.
/// </summary>
/// <param name="Server"></param>
/// <param name="Total"></param>
/// <param name="Delta"></param>
[ByRefEvent]
public readonly record struct ResearchServerPointsChangedEvent(EntityUid Server, int Total, int Delta);
[AutoNetworkedField]
[DataField("points"), ViewVariables(VVAccess.ReadWrite)]
public int Points;
/// <summary>
/// Event raised every second to calculate the amount of points added to the server.
/// A unique numeric id representing the server
/// </summary>
/// <param name="Server"></param>
/// <param name="Points"></param>
[ByRefEvent]
public record struct ResearchServerGetPointsPerSecondEvent(EntityUid Server, int Points);
[AutoNetworkedField]
[ViewVariables(VVAccess.ReadOnly)]
public int Id;
/// <summary>
/// Entities connected to the server
/// </summary>
/// <remarks>
/// This is not safe to read clientside
/// </remarks>
[ViewVariables(VVAccess.ReadOnly)]
public List<EntityUid> Clients = new();
[DataField("nextUpdateTime", customTypeSerializer: typeof(TimeOffsetSerializer))]
public TimeSpan NextUpdateTime = TimeSpan.Zero;
[DataField("researchConsoleUpdateTime"), ViewVariables(VVAccess.ReadWrite)]
public readonly TimeSpan ResearchConsoleUpdateTime = TimeSpan.FromSeconds(1);
}
/// <summary>
/// Event raised on a server's clients when the point value of the server is changed.
/// </summary>
/// <param name="Server"></param>
/// <param name="Total"></param>
/// <param name="Delta"></param>
[ByRefEvent]
public readonly record struct ResearchServerPointsChangedEvent(EntityUid Server, int Total, int Delta);
/// <summary>
/// Event raised every second to calculate the amount of points added to the server.
/// </summary>
/// <param name="Server"></param>
/// <param name="Points"></param>
[ByRefEvent]
public record struct ResearchServerGetPointsPerSecondEvent(EntityUid Server, int Points);

View File

@@ -35,11 +35,9 @@ namespace Content.Shared.Research.Components
public sealed class ResearchConsoleBoundInterfaceState : BoundUserInterfaceState
{
public int Points;
public int PointsPerSecond;
public ResearchConsoleBoundInterfaceState(int points, int pointsPerSecond)
public ResearchConsoleBoundInterfaceState(int points)
{
Points = points;
PointsPerSecond = pointsPerSecond;
}
}
}

View File

@@ -1,48 +1,56 @@
using Content.Shared.Research.Prototypes;
using Content.Shared.Research.Systems;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
namespace Content.Shared.Research.Components
{
[RegisterComponent, NetworkedComponent]
public sealed class TechnologyDatabaseComponent : Component
{
/// <summary>
/// The ids of all the technologies which have been unlocked.
/// </summary>
[DataField("technologyIds", customTypeSerializer: typeof(PrototypeIdListSerializer<TechnologyPrototype>))]
public List<string> TechnologyIds = new();
namespace Content.Shared.Research.Components;
/// <summary>
/// The ids of all the lathe recipes which have been unlocked.
/// This is maintained alongside the TechnologyIds
/// </summary>
[DataField("recipeIds", customTypeSerializer: typeof(PrototypeIdListSerializer<LatheRecipePrototype>))]
public List<string> RecipeIds = new();
}
[RegisterComponent, NetworkedComponent, Access(typeof(SharedResearchSystem)), AutoGenerateComponentState]
public sealed partial class TechnologyDatabaseComponent : Component
{
/// <summary>
/// A main discipline that locks out other discipline technology past a certain tier.
/// </summary>
[AutoNetworkedField]
[DataField("mainDiscipline", customTypeSerializer: typeof(PrototypeIdSerializer<TechDisciplinePrototype>))]
public string? MainDiscipline;
[AutoNetworkedField(true)]
[DataField("currentTechnologyCards")]
public List<string> CurrentTechnologyCards = new();
/// <summary>
/// Event raised on the database whenever its
/// technologies or recipes are modified.
/// Which research disciplines are able to be unlocked
/// </summary>
/// <remarks>
/// This event is forwarded from the
/// server to all of it's clients.
/// </remarks>
[ByRefEvent]
public readonly record struct TechnologyDatabaseModifiedEvent;
[AutoNetworkedField(true)]
[DataField("supportedDisciplines", customTypeSerializer: typeof(PrototypeIdListSerializer<TechDisciplinePrototype>))]
public List<string> SupportedDisciplines = new();
[Serializable, NetSerializable]
public sealed class TechnologyDatabaseState : ComponentState
{
public List<string> Technologies;
public List<string> Recipes;
/// <summary>
/// The ids of all the technologies which have been unlocked.
/// </summary>
[AutoNetworkedField(true)]
[DataField("unlockedTechnologies", customTypeSerializer: typeof(PrototypeIdListSerializer<TechnologyPrototype>))]
public List<string> UnlockedTechnologies = new();
public TechnologyDatabaseState(List<string> technologies, List<string> recipes)
{
Technologies = technologies;
Recipes = recipes;
}
}
/// <summary>
/// The ids of all the lathe recipes which have been unlocked.
/// This is maintained alongside the TechnologyIds
/// </summary>
/// todo: if you unlock all the recipes in a tech, it doesn't count as unlocking the tech. sadge
[AutoNetworkedField(true)]
[DataField("unlockedRecipes", customTypeSerializer: typeof(PrototypeIdListSerializer<LatheRecipePrototype>))]
public List<string> UnlockedRecipes = new();
}
/// <summary>
/// Event raised on the database whenever its
/// technologies or recipes are modified.
/// </summary>
/// <remarks>
/// This event is forwarded from the
/// server to all of it's clients.
/// </remarks>
[ByRefEvent]
public readonly record struct TechnologyDatabaseModifiedEvent;

View File

@@ -0,0 +1,48 @@
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Shared.Research.Prototypes;
/// <summary>
/// This is a prototype for a research discipline, a category
/// that governs how <see cref="TechnologyPrototype"/>s are unlocked.
/// </summary>
[Prototype("techDiscipline")]
public sealed class TechDisciplinePrototype : IPrototype
{
/// <inheritdoc/>
[IdDataField]
public string ID { get; } = default!;
/// <summary>
/// Player-facing name.
/// Supports locale strings.
/// </summary>
[DataField("name", required: true)]
public readonly string Name = string.Empty;
/// <summary>
/// A color used for UI
/// </summary>
[DataField("color", required: true)]
public readonly Color Color;
/// <summary>
/// An icon used to visually represent the discipline in UI.
/// </summary>
[DataField("icon")]
public readonly SpriteSpecifier Icon = default!;
/// <summary>
/// For each tier a discipline supports, what percentage
/// of the previous tier must be unlocked for it to become available
/// </summary>
[DataField("tierPrerequisites", required: true)]
public readonly Dictionary<int, float> TierPrerequisites = new();
/// <summary>
/// Purchasing this tier of technology causes a server to become "locked" to this discipline.
/// </summary>
[DataField("lockoutTier")]
public readonly int LockoutTier = 3;
}

View File

@@ -1,54 +1,92 @@
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype.List;
using Robust.Shared.Utility;
namespace Content.Shared.Research.Prototypes
namespace Content.Shared.Research.Prototypes;
/// <summary>
/// This is a prototype for a technology that can be unlocked.
/// </summary>
[Prototype("technology")]
public sealed class TechnologyPrototype : IPrototype
{
[NetSerializable, Serializable, Prototype("technology")]
public sealed class TechnologyPrototype : IPrototype
{
/// <summary>
/// The ID of this technology prototype.
/// </summary>
[ViewVariables]
[IdDataField]
public string ID { get; } = default!;
/// <inheritdoc/>
[IdDataField]
public string ID { get; } = default!;
/// <summary>
/// The name this technology will have on user interfaces.
/// </summary>
[DataField("name")]
public string? Name { get; private set; }
/// <summary>
/// The name of the technology.
/// Supports locale strings
/// </summary>
[DataField("name", required: true)]
public readonly string Name = string.Empty;
/// <summary>
/// An icon that represent this technology.
/// </summary>
[DataField("icon")]
public SpriteSpecifier Icon { get; } = SpriteSpecifier.Invalid;
/// <summary>
/// An icon used to visually represent the technology in UI.
/// </summary>
[DataField("icon", required: true)]
public readonly SpriteSpecifier Icon = default!;
/// <summary>
/// A short description of the technology.
/// </summary>
[DataField("description")]
public string Description { get; private set; } = "";
/// <summary>
/// What research discipline this technology belongs to.
/// </summary>
[DataField("discipline", required: true, customTypeSerializer: typeof(PrototypeIdSerializer<TechDisciplinePrototype>))]
public readonly string Discipline = default!;
/// <summary>
/// The required research points to unlock this technology.
/// </summary>
[DataField("requiredPoints")]
public int RequiredPoints { get; }
/// <summary>
/// What tier research is this?
/// The tier governs how much lower-tier technology
/// needs to be unlocked before this one.
/// </summary>
[DataField("tier", required: true)]
public readonly int Tier;
/// <summary>
/// A list of technology IDs required to unlock this technology.
/// </summary>
[DataField("requiredTechnologies", customTypeSerializer: typeof(PrototypeIdListSerializer<TechnologyPrototype>))]
public List<string> RequiredTechnologies { get; } = new();
/// <summary>
/// Hidden tech is not ever available at the research console.
/// </summary>
[DataField("hidden")]
public readonly bool Hidden;
/// <summary>
/// A list of recipe IDs this technology unlocks.
/// </summary>
[DataField("unlockedRecipes", customTypeSerializer:typeof(PrototypeIdListSerializer<LatheRecipePrototype>))]
public List<string> UnlockedRecipes { get; } = new();
}
/// <summary>
/// How much research is needed to unlock.
/// </summary>
[DataField("cost")]
public readonly int Cost = 10000;
/// <summary>
/// A list of <see cref="TechnologyPrototype"/>s that need to be unlocked in order to unlock this technology.
/// </summary>
[DataField("technologyPrerequisites", customTypeSerializer: typeof(PrototypeIdListSerializer<TechnologyPrototype>))]
public readonly IReadOnlyList<string> TechnologyPrerequisites = new List<string>();
/// <summary>
/// A list of <see cref="LatheRecipePrototype"/>s that are unlocked by this technology
/// </summary>
[DataField("recipeUnlocks", customTypeSerializer: typeof(PrototypeIdListSerializer<LatheRecipePrototype>))]
public readonly IReadOnlyList<string> RecipeUnlocks = new List<string>();
/// <summary>
/// A list of non-standard effects that are done when this technology is unlocked.
/// </summary>
[DataField("genericUnlocks")]
public readonly IReadOnlyList<GenericUnlock> GenericUnlocks = new List<GenericUnlock>();
}
[DataDefinition]
public record struct GenericUnlock()
{
/// <summary>
/// What event is raised when this is unlocked?
/// Used for doing non-standard logic.
/// </summary>
[DataField("purchaseEvent")]
public readonly object? PurchaseEvent = null;
/// <summary>
/// A player facing tooltip for what the unlock does.
/// Supports locale strings.
/// </summary>
[DataField("unlockDescription")]
public readonly string UnlockDescription = string.Empty;
}

View File

@@ -1,46 +1,145 @@
using Content.Shared.Research.Components;
using System.Linq;
using Content.Shared.Research.Components;
using Content.Shared.Research.Prototypes;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Shared.Research.Systems;
public abstract class SharedResearchSystem : EntitySystem
{
[Dependency] protected readonly IPrototypeManager PrototypeManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ResearchServerComponent, ComponentGetState>(OnServerGetState);
SubscribeLocalEvent<ResearchServerComponent, ComponentHandleState>(OnServerHandleState);
SubscribeLocalEvent<TechnologyDatabaseComponent, ComponentGetState>(OnTechnologyGetState);
SubscribeLocalEvent<TechnologyDatabaseComponent, ComponentHandleState>(OnTechnologyHandleState);
SubscribeLocalEvent<TechnologyDatabaseComponent, MapInitEvent>(OnMapInit);
}
private void OnServerGetState(EntityUid uid, ResearchServerComponent component, ref ComponentGetState args)
private void OnMapInit(EntityUid uid, TechnologyDatabaseComponent component, MapInitEvent args)
{
args.State = new ResearchServerState(component.ServerName, component.Points, component.Id);
UpdateTechnologyCards(uid, component);
}
private void OnServerHandleState(EntityUid uid, ResearchServerComponent component, ref ComponentHandleState args)
public void UpdateTechnologyCards(EntityUid uid, TechnologyDatabaseComponent? component = null)
{
if (args.Current is not ResearchServerState state)
if (!Resolve(uid, ref component))
return;
component.ServerName = state.ServerName;
component.Points = state.Points;
component.Id = state.Id;
var availableTechnology = GetAvailableTechnologies(uid, component);
_random.Shuffle(availableTechnology);
component.CurrentTechnologyCards.Clear();
foreach (var discipline in component.SupportedDisciplines)
{
var selected = availableTechnology.FirstOrDefault(p => p.Discipline == discipline);
if (selected == null)
continue;
component.CurrentTechnologyCards.Add(selected.ID);
}
Dirty(component);
}
private void OnTechnologyHandleState(EntityUid uid, TechnologyDatabaseComponent component, ref ComponentHandleState args)
public List<TechnologyPrototype> GetAvailableTechnologies(EntityUid uid, TechnologyDatabaseComponent? component = null)
{
if (args.Current is not TechnologyDatabaseState state)
return;
component.TechnologyIds = new (state.Technologies);
component.RecipeIds = new(state.Recipes);
if (!Resolve(uid, ref component, false))
return new List<TechnologyPrototype>();
var availableTechnologies = new List<TechnologyPrototype>();
var disciplineTiers = GetDisciplineTiers(component);
foreach (var tech in PrototypeManager.EnumeratePrototypes<TechnologyPrototype>())
{
if (IsTechnologyAvailable(component, tech, disciplineTiers))
availableTechnologies.Add(tech);
}
return availableTechnologies;
}
private void OnTechnologyGetState(EntityUid uid, TechnologyDatabaseComponent component, ref ComponentGetState args)
public bool IsTechnologyAvailable(TechnologyDatabaseComponent component, TechnologyPrototype tech, Dictionary<string, int>? disciplineTiers = null)
{
args.State = new TechnologyDatabaseState(component.TechnologyIds, component.RecipeIds);
disciplineTiers ??= GetDisciplineTiers(component);
if (tech.Hidden)
return false;
if (!component.SupportedDisciplines.Contains(tech.Discipline))
return false;
if (tech.Tier > disciplineTiers[tech.Discipline])
return false;
if (component.UnlockedTechnologies.Contains(tech.ID))
return false;
foreach (var prereq in tech.TechnologyPrerequisites)
{
if (!component.UnlockedTechnologies.Contains(prereq))
return false;
}
return true;
}
public Dictionary<string, int> GetDisciplineTiers(TechnologyDatabaseComponent component)
{
var tiers = new Dictionary<string, int>();
foreach (var discipline in component.SupportedDisciplines)
{
tiers.Add(discipline, GetHighestDisciplineTier(component, discipline));
}
return tiers;
}
public int GetHighestDisciplineTier(TechnologyDatabaseComponent component, string disciplineId)
{
return GetHighestDisciplineTier(component, PrototypeManager.Index<TechDisciplinePrototype>(disciplineId));
}
public int GetHighestDisciplineTier(TechnologyDatabaseComponent component, TechDisciplinePrototype techDiscipline)
{
var allTech = PrototypeManager.EnumeratePrototypes<TechnologyPrototype>()
.Where(p => p.Discipline == techDiscipline.ID && !p.Hidden).ToList();
var allUnlocked = new List<TechnologyPrototype>();
foreach (var recipe in component.UnlockedTechnologies)
{
var proto = PrototypeManager.Index<TechnologyPrototype>(recipe);
if (proto.Discipline != techDiscipline.ID)
continue;
allUnlocked.Add(proto);
}
var highestTier = techDiscipline.TierPrerequisites.Keys.Max();
var tier = 2; //tier 1 is always given
// todo this might break if you have hidden technologies. i'm not sure
while (tier <= highestTier)
{
// we need to get the tech for the tier 1 below because that's
// what the percentage in TierPrerequisites is referring to.
var unlockedTierTech = allUnlocked.Where(p => p.Tier == tier - 1).ToList();
var allTierTech = allTech.Where(p => p.Discipline == techDiscipline.ID && p.Tier == tier - 1).ToList();
if (allTierTech.Count == 0)
break;
var percent = (float) unlockedTierTech.Count / allTierTech.Count;
if (percent < techDiscipline.TierPrerequisites[tier])
break;
if (tier >= techDiscipline.LockoutTier &&
component.MainDiscipline != null &&
techDiscipline.ID != component.MainDiscipline)
break;
tier++;
}
return tier - 1;
}
/// <summary>
@@ -58,26 +157,18 @@ public abstract class SharedResearchSystem : EntitySystem
/// <returns>Whether it is unlocked or not</returns>
public bool IsTechnologyUnlocked(EntityUid uid, string technologyId, TechnologyDatabaseComponent? component = null)
{
return Resolve(uid, ref component, false) && component.TechnologyIds.Contains(technologyId);
return Resolve(uid, ref component, false) && component.UnlockedTechnologies.Contains(technologyId);
}
/// <summary>
/// Returns whether or not all the prerequisite
/// technologies for a technology are unlocked.
/// </summary>
/// <param name="uid"></param>
/// <param name="prototype"></param>
/// <param name="component"></param>
/// <returns>Whether or not the prerequesites are present</returns>
public bool ArePrerequesitesUnlocked(EntityUid uid, TechnologyPrototype prototype, TechnologyDatabaseComponent? component = null)
public void TrySetMainDiscipline(TechnologyPrototype prototype, EntityUid uid, TechnologyDatabaseComponent? component = null)
{
if (!Resolve(uid, ref component))
return false;
foreach (var technologyId in prototype.RequiredTechnologies)
{
if (!IsTechnologyUnlocked(uid, technologyId, component))
return false;
}
return true;
return;
var discipline = PrototypeManager.Index<TechDisciplinePrototype>(prototype.Discipline);
if (prototype.Tier < discipline.LockoutTier)
return;
component.MainDiscipline = prototype.Discipline;
Dirty(component);
}
}