ECS and bandaid research (#9251)
This commit is contained in:
87
Content.Server/Research/Systems/ResearchSystem.Client.cs
Normal file
87
Content.Server/Research/Systems/ResearchSystem.Client.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using Content.Server.Research.Components;
|
||||
using Content.Shared.Research.Components;
|
||||
|
||||
namespace Content.Server.Research;
|
||||
|
||||
public sealed partial class ResearchSystem
|
||||
{
|
||||
private void InitializeClient()
|
||||
{
|
||||
SubscribeLocalEvent<ResearchClientComponent, ComponentStartup>(OnClientStartup);
|
||||
SubscribeLocalEvent<ResearchClientComponent, ComponentShutdown>(OnClientShutdown);
|
||||
SubscribeLocalEvent<ResearchClientComponent, BoundUIOpenedEvent>(OnClientUIOpen);
|
||||
|
||||
SubscribeLocalEvent<ResearchClientComponent, ResearchClientSyncMessage>(OnClientSyncMessage);
|
||||
SubscribeLocalEvent<ResearchClientComponent, ResearchClientServerSelectedMessage>(OnClientSelected);
|
||||
SubscribeLocalEvent<ResearchClientComponent, ResearchClientServerDeselectedMessage>(OnClientDeselected);
|
||||
}
|
||||
|
||||
#region UI
|
||||
|
||||
private void OnClientDeselected(EntityUid uid, ResearchClientComponent component, ResearchClientServerDeselectedMessage args)
|
||||
{
|
||||
UnregisterClientServer(component);
|
||||
UpdateClientInterface(component);
|
||||
|
||||
if (TryComp<ResearchConsoleComponent>(uid, out var console))
|
||||
{
|
||||
UpdateConsoleInterface(console, component);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnClientSelected(EntityUid uid, ResearchClientComponent component, ResearchClientServerSelectedMessage args)
|
||||
{
|
||||
UnregisterClientServer(component);
|
||||
RegisterClientServer(component, GetServerById(args.ServerId));
|
||||
UpdateClientInterface(component);
|
||||
|
||||
if (TryComp<ResearchConsoleComponent>(uid, out var console))
|
||||
{
|
||||
UpdateConsoleInterface(console, component);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnClientSyncMessage(EntityUid uid, ResearchClientComponent component, ResearchClientSyncMessage args)
|
||||
{
|
||||
UpdateClientInterface(component);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private void OnClientStartup(EntityUid uid, ResearchClientComponent component, ComponentStartup args)
|
||||
{
|
||||
if (Servers.Count > 0)
|
||||
RegisterClientServer(component, Servers[0]);
|
||||
}
|
||||
|
||||
private void OnClientShutdown(EntityUid uid, ResearchClientComponent component, ComponentShutdown args)
|
||||
{
|
||||
UnregisterClientServer(component);
|
||||
}
|
||||
|
||||
private void OnClientUIOpen(EntityUid uid, ResearchClientComponent component, BoundUIOpenedEvent args)
|
||||
{
|
||||
UpdateClientInterface(component);
|
||||
}
|
||||
|
||||
private void UpdateClientInterface(ResearchClientComponent component)
|
||||
{
|
||||
var state = new ResearchClientBoundInterfaceState(Servers.Count, GetServerNames(),
|
||||
GetServerIds(), component.ConnectedToServer ? component.Server!.Id : -1);
|
||||
|
||||
_uiSystem.GetUiOrNull(component.Owner, ResearchClientUiKey.Key)?.SetState(state);
|
||||
}
|
||||
|
||||
private bool RegisterClientServer(ResearchClientComponent component, ResearchServerComponent? server = null)
|
||||
{
|
||||
if (server == null) return false;
|
||||
return RegisterServerClient(server, component);
|
||||
}
|
||||
|
||||
private void UnregisterClientServer(ResearchClientComponent component)
|
||||
{
|
||||
if (component.Server == null) return;
|
||||
|
||||
UnregisterServerClient(component.Server, component);
|
||||
}
|
||||
}
|
||||
74
Content.Server/Research/Systems/ResearchSystem.Console.cs
Normal file
74
Content.Server/Research/Systems/ResearchSystem.Console.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using Content.Server.Power.EntitySystems;
|
||||
using Content.Server.Research.Components;
|
||||
using Content.Shared.Research.Components;
|
||||
using Content.Shared.Research.Prototypes;
|
||||
using Robust.Server.Player;
|
||||
|
||||
namespace Content.Server.Research;
|
||||
|
||||
public sealed partial class ResearchSystem
|
||||
{
|
||||
private void InitializeConsole()
|
||||
{
|
||||
SubscribeLocalEvent<ResearchConsoleComponent, ConsoleUnlockTechnologyMessage>(OnConsoleUnlock);
|
||||
SubscribeLocalEvent<ResearchConsoleComponent, ConsoleServerSyncMessage>(OnConsoleSync);
|
||||
SubscribeLocalEvent<ResearchConsoleComponent, ConsoleServerSelectionMessage>(OnConsoleSelect);
|
||||
}
|
||||
|
||||
private void OnConsoleSelect(EntityUid uid, ResearchConsoleComponent component, ConsoleServerSelectionMessage args)
|
||||
{
|
||||
if (!HasComp<TechnologyDatabaseComponent>(uid) ||
|
||||
!HasComp<ResearchClientComponent>(uid) ||
|
||||
!this.IsPowered(uid, EntityManager))
|
||||
return;
|
||||
|
||||
_uiSystem.TryOpen(uid, ResearchClientUiKey.Key, (IPlayerSession) args.Session);
|
||||
}
|
||||
|
||||
private void OnConsoleSync(EntityUid uid, ResearchConsoleComponent component, ConsoleServerSyncMessage args)
|
||||
{
|
||||
if (!TryComp<TechnologyDatabaseComponent>(uid, out var database) ||
|
||||
!HasComp<ResearchClientComponent>(uid) ||
|
||||
!this.IsPowered(uid, EntityManager))
|
||||
return;
|
||||
|
||||
SyncWithServer(database);
|
||||
UpdateConsoleInterface(component);
|
||||
}
|
||||
|
||||
private void OnConsoleUnlock(EntityUid uid, ResearchConsoleComponent component, ConsoleUnlockTechnologyMessage args)
|
||||
{
|
||||
if (!TryComp<TechnologyDatabaseComponent>(uid, out var database) ||
|
||||
!TryComp<ResearchClientComponent>(uid, out var client) ||
|
||||
!this.IsPowered(uid, EntityManager))
|
||||
return;
|
||||
|
||||
if (!_prototypeManager.TryIndex(args.Id, out TechnologyPrototype? tech) ||
|
||||
client.Server == null ||
|
||||
!CanUnlockTechnology(client.Server, tech)) return;
|
||||
|
||||
if (!UnlockTechnology(client.Server, tech)) return;
|
||||
|
||||
SyncWithServer(database);
|
||||
Dirty(database);
|
||||
UpdateConsoleInterface(component);
|
||||
}
|
||||
|
||||
private void UpdateConsoleInterface(ResearchConsoleComponent component, ResearchClientComponent? clientComponent = null)
|
||||
{
|
||||
ResearchConsoleBoundInterfaceState state;
|
||||
|
||||
if (!Resolve(component.Owner, ref clientComponent, false) ||
|
||||
clientComponent.Server == null)
|
||||
{
|
||||
state = new ResearchConsoleBoundInterfaceState(default, default);
|
||||
}
|
||||
else
|
||||
{
|
||||
var points = clientComponent.ConnectedToServer ? clientComponent.Server.Points : 0;
|
||||
var pointsPerSecond = clientComponent.ConnectedToServer ? PointsPerSecond(clientComponent.Server) : 0;
|
||||
state = new ResearchConsoleBoundInterfaceState(points, pointsPerSecond);
|
||||
}
|
||||
_uiSystem.GetUiOrNull(component.Owner, ResearchConsoleUiKey.Key)?.SetState(state);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Content.Server.Power.EntitySystems;
|
||||
using Content.Server.Research.Components;
|
||||
|
||||
namespace Content.Server.Research;
|
||||
|
||||
public sealed partial class ResearchSystem
|
||||
{
|
||||
public bool CanProduce(ResearchPointSourceComponent component)
|
||||
{
|
||||
return component.Active && this.IsPowered(component.Owner, EntityManager);
|
||||
}
|
||||
}
|
||||
109
Content.Server/Research/Systems/ResearchSystem.Server.cs
Normal file
109
Content.Server/Research/Systems/ResearchSystem.Server.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using Content.Server.Power.EntitySystems;
|
||||
using Content.Server.Research.Components;
|
||||
using Content.Shared.Research.Prototypes;
|
||||
|
||||
namespace Content.Server.Research;
|
||||
|
||||
public sealed partial class ResearchSystem
|
||||
{
|
||||
private void InitializeServer()
|
||||
{
|
||||
SubscribeLocalEvent<ResearchServerComponent, ComponentStartup>(OnServerStartup);
|
||||
SubscribeLocalEvent<ResearchServerComponent, ComponentShutdown>(OnServerShutdown);
|
||||
}
|
||||
|
||||
private void OnServerShutdown(EntityUid uid, ResearchServerComponent component, ComponentShutdown args)
|
||||
{
|
||||
UnregisterServer(component);
|
||||
}
|
||||
|
||||
private void OnServerStartup(EntityUid uid, ResearchServerComponent component, ComponentStartup args)
|
||||
{
|
||||
RegisterServer(component);
|
||||
}
|
||||
|
||||
private bool CanRun(ResearchServerComponent component)
|
||||
{
|
||||
return this.IsPowered(component.Owner, EntityManager);
|
||||
}
|
||||
|
||||
private void UpdateServer(ResearchServerComponent component, int time)
|
||||
{
|
||||
if (!CanRun(component)) return;
|
||||
component.Points += PointsPerSecond(component) * time;
|
||||
}
|
||||
|
||||
public bool RegisterServerClient(ResearchServerComponent component, ResearchClientComponent clientComponent)
|
||||
{
|
||||
// TODO: This is shit but I'm just trying to fix RND for now until it gets bulldozed
|
||||
if (TryComp<ResearchPointSourceComponent>(clientComponent.Owner, out var source))
|
||||
{
|
||||
if (component.PointSources.Contains(source)) return false;
|
||||
component.PointSources.Add(source);
|
||||
source.Server = component;
|
||||
}
|
||||
|
||||
if (component.Clients.Contains(clientComponent)) return false;
|
||||
component.Clients.Add(clientComponent);
|
||||
clientComponent.Server = component;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void UnregisterServerClient(ResearchServerComponent component, ResearchClientComponent clientComponent)
|
||||
{
|
||||
if (TryComp<ResearchPointSourceComponent>(clientComponent.Owner, out var source))
|
||||
{
|
||||
component.PointSources.Remove(source);
|
||||
}
|
||||
|
||||
component.Clients.Remove(clientComponent);
|
||||
clientComponent.Server = null;
|
||||
}
|
||||
|
||||
public bool IsTechnologyUnlocked(ResearchServerComponent component, TechnologyPrototype prototype,
|
||||
TechnologyDatabaseComponent? databaseComponent = null)
|
||||
{
|
||||
if (!Resolve(component.Owner, ref databaseComponent, false)) return false;
|
||||
return databaseComponent.IsTechnologyUnlocked(prototype);
|
||||
}
|
||||
|
||||
public bool CanUnlockTechnology(ResearchServerComponent component, TechnologyPrototype technology, TechnologyDatabaseComponent? databaseComponent = null)
|
||||
{
|
||||
if (!Resolve(component.Owner, ref databaseComponent, false))
|
||||
return false;
|
||||
|
||||
if (!databaseComponent.CanUnlockTechnology(technology) ||
|
||||
component.Points < technology.RequiredPoints ||
|
||||
IsTechnologyUnlocked(component, technology, databaseComponent))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool UnlockTechnology(ResearchServerComponent component, TechnologyPrototype prototype,
|
||||
TechnologyDatabaseComponent? databaseComponent = null)
|
||||
{
|
||||
if (!Resolve(component.Owner, ref databaseComponent, false)) return false;
|
||||
|
||||
if (!CanUnlockTechnology(component, prototype, databaseComponent)) return false;
|
||||
var result = UnlockTechnology(databaseComponent, prototype);
|
||||
if (result)
|
||||
component.Points -= prototype.RequiredPoints;
|
||||
return result;
|
||||
}
|
||||
|
||||
public int PointsPerSecond(ResearchServerComponent component)
|
||||
{
|
||||
var points = 0;
|
||||
|
||||
if (CanRun(component))
|
||||
{
|
||||
foreach (var source in component.PointSources)
|
||||
{
|
||||
if (CanProduce(source)) points += source.PointsPerSecond;
|
||||
}
|
||||
}
|
||||
|
||||
return points;
|
||||
}
|
||||
}
|
||||
78
Content.Server/Research/Systems/ResearchSystem.Technology.cs
Normal file
78
Content.Server/Research/Systems/ResearchSystem.Technology.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using Content.Server.Research.Components;
|
||||
using Content.Shared.Research.Components;
|
||||
using Content.Shared.Research.Prototypes;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Server.Research;
|
||||
|
||||
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.Technologies);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Synchronizes this database against other,
|
||||
/// adding all technologies from the other that
|
||||
/// this one doesn't have.
|
||||
/// </summary>
|
||||
/// <param name="otherDatabase">The other database</param>
|
||||
/// <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.Technologies)
|
||||
{
|
||||
if (!component.IsTechnologyUnlocked(tech)) AddTechnology(component, tech);
|
||||
}
|
||||
|
||||
if (twoway)
|
||||
Sync(otherDatabase, component, false);
|
||||
|
||||
Dirty(component);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If there's a research client component attached to the owner entity,
|
||||
/// and the research client is connected to a research server, this method
|
||||
/// syncs against the research server, and the server against the local database.
|
||||
/// </summary>
|
||||
/// <returns>Whether it could sync or not</returns>
|
||||
public bool SyncWithServer(TechnologyDatabaseComponent component, ResearchClientComponent? clientComponent = null)
|
||||
{
|
||||
if (!Resolve(component.Owner, ref clientComponent, false)) return false;
|
||||
if (!TryComp<TechnologyDatabaseComponent>(clientComponent.Server?.Owner, out var clientDatabase)) return false;
|
||||
|
||||
Sync(component, clientDatabase);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If possible, unlocks a technology on this database.
|
||||
/// </summary>
|
||||
/// <param name="technology"></param>
|
||||
/// <returns></returns>
|
||||
public bool UnlockTechnology(TechnologyDatabaseComponent component, TechnologyPrototype technology)
|
||||
{
|
||||
if (!component.CanUnlockTechnology(technology)) return false;
|
||||
|
||||
AddTechnology(component, technology);
|
||||
Dirty(component);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a technology to the database without checking if it could be unlocked.
|
||||
/// </summary>
|
||||
/// <param name="technology"></param>
|
||||
public void AddTechnology(TechnologyDatabaseComponent component, TechnologyPrototype technology)
|
||||
{
|
||||
component.Technologies.Add(technology);
|
||||
}
|
||||
}
|
||||
97
Content.Server/Research/Systems/ResearchSystem.cs
Normal file
97
Content.Server/Research/Systems/ResearchSystem.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using Content.Server.Research.Components;
|
||||
using Content.Shared.Research.Components;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.Research
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public sealed partial class ResearchSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
|
||||
|
||||
private const int ResearchConsoleUIUpdateTime = 5;
|
||||
|
||||
private float _timer = ResearchConsoleUIUpdateTime;
|
||||
private readonly List<ResearchServerComponent> _servers = new();
|
||||
public IReadOnlyList<ResearchServerComponent> Servers => _servers;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
InitializeClient();
|
||||
InitializeConsole();
|
||||
InitializeServer();
|
||||
InitializeTechnology();
|
||||
}
|
||||
|
||||
public bool RegisterServer(ResearchServerComponent server)
|
||||
{
|
||||
if (_servers.Contains(server)) return false;
|
||||
_servers.Add(server);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void UnregisterServer(ResearchServerComponent server)
|
||||
{
|
||||
_servers.Remove(server);
|
||||
}
|
||||
|
||||
public ResearchServerComponent? GetServerById(int id)
|
||||
{
|
||||
foreach (var server in Servers)
|
||||
{
|
||||
if (server.Id == id) return server;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public string[] GetServerNames()
|
||||
{
|
||||
var list = new string[Servers.Count];
|
||||
|
||||
for (var i = 0; i < Servers.Count; i++)
|
||||
{
|
||||
list[i] = Servers[i].ServerName;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public int[] GetServerIds()
|
||||
{
|
||||
var list = new int[Servers.Count];
|
||||
|
||||
for (var i = 0; i < Servers.Count; i++)
|
||||
{
|
||||
list[i] = Servers[i].Id;
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
_timer += frameTime;
|
||||
|
||||
while (_timer > ResearchConsoleUIUpdateTime)
|
||||
{
|
||||
foreach (var server in _servers)
|
||||
{
|
||||
UpdateServer(server, ResearchConsoleUIUpdateTime);
|
||||
}
|
||||
|
||||
foreach (var console in EntityManager.EntityQuery<ResearchConsoleComponent>())
|
||||
{
|
||||
if (!_uiSystem.IsUiOpen(console.Owner, ResearchConsoleUiKey.Key)) continue;
|
||||
UpdateConsoleInterface(console);
|
||||
}
|
||||
|
||||
_timer -= ResearchConsoleUIUpdateTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user