Technology Disks (#13077)

* clean up a bunch of R&D code

* don't store components

* brug

* speedrun some sloth review

* technology disks

* expand functionality, begin work on printer

* disk printer ui

* file

* fix the rebase

* disk console is finito

* Update DiskConsoleSystem.cs
This commit is contained in:
Nemanja
2022-12-20 17:39:57 -05:00
committed by GitHub
parent 88ed188b42
commit 050e157005
34 changed files with 659 additions and 151 deletions

View File

@@ -1,7 +0,0 @@
using Content.Shared.Research.Components;
namespace Content.Server.Research.Components
{
[RegisterComponent]
public sealed class TechnologyDatabaseComponent : SharedTechnologyDatabaseComponent {}
}

View File

@@ -1,5 +1,6 @@
using Content.Server.Power.EntitySystems;
using Content.Server.Research.Components;
using Content.Shared.Research.Components;
using Content.Shared.Research.Prototypes;
namespace Content.Server.Research.Systems;
@@ -44,7 +45,7 @@ public sealed partial class ResearchSystem
{
if (!Resolve(component.Owner, ref databaseComponent, false))
return false;
return databaseComponent.IsTechnologyUnlocked(prototype.ID);
return IsTechnologyUnlocked(databaseComponent.Owner, prototype.ID, databaseComponent);
}
public bool CanUnlockTechnology(ResearchServerComponent component, TechnologyPrototype technology, TechnologyDatabaseComponent? databaseComponent = null)
@@ -52,7 +53,7 @@ public sealed partial class ResearchSystem
if (!Resolve(component.Owner, ref databaseComponent, false))
return false;
if (!databaseComponent.CanUnlockTechnology(technology) ||
if (!CanUnlockTechnology(databaseComponent.Owner, technology, databaseComponent) ||
component.Points < technology.RequiredPoints ||
IsTechnologyUnlocked(component, technology, databaseComponent))
return false;

View File

@@ -1,3 +1,4 @@
using System.Linq;
using Content.Server.Research.Components;
using Content.Shared.Research.Components;
using Content.Shared.Research.Prototypes;
@@ -7,16 +8,6 @@ namespace Content.Server.Research.Systems;
public sealed partial class ResearchSystem
{
private void InitializeTechnology()
{
SubscribeLocalEvent<TechnologyDatabaseComponent, ComponentGetState>(OnTechnologyGetState);
}
private void OnTechnologyGetState(EntityUid uid, TechnologyDatabaseComponent component, ref ComponentGetState args)
{
args.State = new TechnologyDatabaseState(component.TechnologyIds);
}
/// <summary>
/// Synchronizes this database against other,
/// adding all technologies from the other that
@@ -27,11 +18,8 @@ public sealed partial class ResearchSystem
/// <param name="twoway">Whether the other database should be synced against this one too or not.</param>
public void Sync(TechnologyDatabaseComponent component, TechnologyDatabaseComponent otherDatabase, bool twoway = true)
{
foreach (var tech in otherDatabase.TechnologyIds)
{
if (!component.IsTechnologyUnlocked(tech))
AddTechnology(component, tech);
}
otherDatabase.TechnologyIds = otherDatabase.TechnologyIds.Union(component.TechnologyIds).ToList();
otherDatabase.RecipeIds = otherDatabase.RecipeIds.Union(component.RecipeIds).ToList();
if (twoway)
Sync(otherDatabase, component, false);
@@ -65,11 +53,9 @@ public sealed partial class ResearchSystem
/// <returns></returns>
public bool UnlockTechnology(TechnologyDatabaseComponent component, TechnologyPrototype technology)
{
if (!component.CanUnlockTechnology(technology))
return false;
if (!CanUnlockTechnology(component.Owner, technology, component)) return false;
AddTechnology(component, technology.ID);
Dirty(component);
return true;
}
@@ -80,6 +66,39 @@ public sealed partial class ResearchSystem
/// <param name="technology"></param>
public void AddTechnology(TechnologyDatabaseComponent component, string technology)
{
component.TechnologyIds.Add(technology);
if (!_prototypeManager.TryIndex<TechnologyPrototype>(technology, out var prototype))
return;
AddTechnology(component, prototype);
}
public void AddTechnology(TechnologyDatabaseComponent component, TechnologyPrototype technology)
{
component.TechnologyIds.Add(technology.ID);
foreach (var unlock in technology.UnlockedRecipes)
{
if (component.RecipeIds.Contains(unlock))
continue;
component.RecipeIds.Add(unlock);
}
Dirty(component);
if (!TryComp<ResearchServerComponent>(component.Owner, out var server))
return;
foreach (var client in server.Clients)
{
if (!TryComp<ResearchConsoleComponent>(client, out var console))
continue;
UpdateConsoleInterface(console);
}
}
public void AddLatheRecipe(TechnologyDatabaseComponent component, string recipe, bool dirty = true)
{
if (component.RecipeIds.Contains(recipe))
return;
component.RecipeIds.Add(recipe);
if (dirty)
Dirty(component);
}
}

View File

@@ -1,5 +1,6 @@
using System.Linq;
using Content.Server.Research.Components;
using Content.Shared.Research.Systems;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.Prototypes;
@@ -8,7 +9,7 @@ using Robust.Shared.Timing;
namespace Content.Server.Research.Systems
{
[UsedImplicitly]
public sealed partial class ResearchSystem : EntitySystem
public sealed partial class ResearchSystem : SharedResearchSystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
@@ -22,7 +23,6 @@ namespace Content.Server.Research.Systems
InitializeClient();
InitializeConsole();
InitializeSource();
InitializeTechnology();
}
private void OnStartup(EntityUid uid, ResearchServerComponent component, ComponentStartup args)

View File

@@ -0,0 +1,21 @@
using Robust.Shared.Audio;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.Research.TechnologyDisk.Components;
[RegisterComponent]
public sealed class DiskConsoleComponent : Component
{
[DataField("pricePerDisk"), ViewVariables(VVAccess.ReadWrite)]
public int PricePerDisk = 2500;
[DataField("diskPrototype", customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
public string DiskPrototype = "TechnologyDisk";
[DataField("printDuration")]
public TimeSpan PrintDuration = TimeSpan.FromSeconds(1);
[DataField("printSound")]
public SoundSpecifier PrintSound = new SoundPathSpecifier("/Audio/Machines/printer.ogg");
}

View File

@@ -0,0 +1,7 @@
namespace Content.Server.Research.TechnologyDisk.Components;
[RegisterComponent]
public sealed class DiskConsolePrintingComponent : Component
{
public TimeSpan FinishTime;
}

View File

@@ -0,0 +1,11 @@
namespace Content.Server.Research.TechnologyDisk.Components;
[RegisterComponent]
public sealed class TechnologyDiskComponent : Component
{
/// <summary>
/// The recipe that will be added. If null, one will be randomly generated
/// </summary>
[DataField("recipes")]
public List<string>? Recipes;
}

View File

@@ -0,0 +1,87 @@
using Content.Server.Research.Components;
using Content.Server.Research.Systems;
using Content.Server.Research.TechnologyDisk.Components;
using Content.Server.UserInterface;
using Content.Shared.Research;
using Robust.Server.GameObjects;
using Robust.Shared.Timing;
namespace Content.Server.Research.TechnologyDisk.Systems;
public sealed class DiskConsoleSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly AudioSystem _audio = default!;
[Dependency] private readonly ResearchSystem _research = default!;
[Dependency] private readonly UserInterfaceSystem _ui = default!;
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<DiskConsoleComponent, DiskConsolePrintDiskMessage>(OnPrintDisk);
SubscribeLocalEvent<DiskConsoleComponent, ResearchServerPointsChangedEvent>(OnPointsChanged);
SubscribeLocalEvent<DiskConsoleComponent, BeforeActivatableUIOpenEvent>(OnBeforeUiOpen);
SubscribeLocalEvent<DiskConsolePrintingComponent, ComponentShutdown>(OnShutdown);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
foreach (var (printing, console, xform) in EntityQuery<DiskConsolePrintingComponent, DiskConsoleComponent, TransformComponent>())
{
if (printing.FinishTime > _timing.CurTime)
continue;
RemComp(printing.Owner, printing);
EntityManager.SpawnEntity(console.DiskPrototype, xform.Coordinates);
}
}
private void OnPrintDisk(EntityUid uid, DiskConsoleComponent component, DiskConsolePrintDiskMessage args)
{
if (!TryComp<ResearchClientComponent>(uid, out var client) || client.Server == null)
return;
if (client.Server.Points < component.PricePerDisk)
return;
_research.ChangePointsOnServer(client.Server.Owner, -component.PricePerDisk, client.Server);
_audio.PlayPvs(component.PrintSound, uid);
var printing = EnsureComp<DiskConsolePrintingComponent>(uid);
printing.FinishTime = _timing.CurTime + component.PrintDuration;
}
private void OnPointsChanged(EntityUid uid, DiskConsoleComponent component, ref ResearchServerPointsChangedEvent args)
{
UpdateUserInterface(uid, component);
}
private void OnBeforeUiOpen(EntityUid uid, DiskConsoleComponent component, BeforeActivatableUIOpenEvent args)
{
UpdateUserInterface(uid, component);
}
public void UpdateUserInterface(EntityUid uid, DiskConsoleComponent? component = null)
{
if (!Resolve(uid, ref component, false))
return;
var totalPoints = 0;
if (TryComp<ResearchClientComponent>(uid, out var client) && client.Server != null)
{
totalPoints = client.Server.Points;
}
var canPrint = !HasComp<DiskConsolePrintingComponent>(uid) && totalPoints >= component.PricePerDisk;
var state = new DiskConsoleBoundUserInterfaceState(totalPoints, component.PricePerDisk, canPrint);
_ui.TrySetUiState(uid, DiskConsoleUiKey.Key, state);
}
private void OnShutdown(EntityUid uid, DiskConsolePrintingComponent component, ComponentShutdown args)
{
UpdateUserInterface(uid);
}
}

View File

@@ -0,0 +1,94 @@
using System.Linq;
using Content.Server.Popups;
using Content.Server.Research.Components;
using Content.Server.Research.Systems;
using Content.Server.Research.TechnologyDisk.Components;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Research.Components;
using Content.Shared.Research.Prototypes;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Server.Research.TechnologyDisk.Systems;
public sealed class TechnologyDiskSystem : EntitySystem
{
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly ResearchSystem _research = default!;
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] private readonly IRobustRandom _random = default!;
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<TechnologyDiskComponent, AfterInteractEvent>(OnAfterInteract);
SubscribeLocalEvent<TechnologyDiskComponent, ExaminedEvent>(OnExamine);
SubscribeLocalEvent<TechnologyDiskComponent, MapInitEvent>(OnMapInit);
}
private void OnAfterInteract(EntityUid uid, TechnologyDiskComponent component, AfterInteractEvent args)
{
if (args.Handled || !args.CanReach || args.Target is not { } target)
return;
if (!HasComp<ResearchServerComponent>(target) || !TryComp<TechnologyDatabaseComponent>(target, out var database))
return;
if (component.Recipes != null)
{
foreach (var recipe in component.Recipes)
{
_research.AddLatheRecipe(database, recipe, false);
}
Dirty(database);
}
_popup.PopupEntity(Loc.GetString("tech-disk-inserted"), target, args.User);
EntityManager.DeleteEntity(uid);
args.Handled = true;
}
private void OnExamine(EntityUid uid, TechnologyDiskComponent component, ExaminedEvent args)
{
var message = Loc.GetString("tech-disk-examine-none");
if (component.Recipes != null && component.Recipes.Any())
{
var prototype = _prototype.Index<LatheRecipePrototype>(component.Recipes[0]);
var resultPrototype = _prototype.Index<EntityPrototype>(prototype.Result);
message = Loc.GetString("tech-disk-examine", ("result", resultPrototype.Name));
if (component.Recipes.Count > 1) //idk how to do this well. sue me.
message += " " + Loc.GetString("tech-disk-examine-more");
}
args.PushMarkup(message);
}
private void OnMapInit(EntityUid uid, TechnologyDiskComponent component, MapInitEvent args)
{
if (component.Recipes != null)
return;
//get a list of every distinct recipe in all the technologies.
var allTechs = new List<string>();
foreach (var tech in _prototype.EnumeratePrototypes<TechnologyPrototype>())
{
allTechs.AddRange(tech.UnlockedRecipes);
}
allTechs = allTechs.Distinct().ToList();
//get a list of every distinct unlocked tech across all databases
var allUnlocked = new List<string>();
foreach (var database in EntityQuery<TechnologyDatabaseComponent>())
{
allUnlocked.AddRange(database.RecipeIds);
}
allUnlocked = allUnlocked.Distinct().ToList();
//make a list of every single non-unlocked tech
var validTechs = allTechs.Where(tech => !allUnlocked.Contains(tech)).ToList();
//pick one
component.Recipes = new();
component.Recipes.Add(_random.Pick(validTechs));
}
}