Adds Research, unlockable technologies, Protolathes... (#264)

* Work on Research so far
More work on UI...
Fix ResearchClient and Protolathe UI stuff.
Fix infinite select -> request state -> select -> ... loop
Add UI to ResearchClient, etc.
Technology Database states, and a bit of work on the research console ui
A bit of work on Research Console UI
Protolathe sync
Stuff that actually does things
Protolathe databases yay
Alright got my motivation back
Yeah, no. It's almost 3 AM already
Fix serialization bug again
More work on stuff
Stuff
Adds files for most new components/systems.

* Protolathes actually work now

* Research. Just Research.

* Adds icons from Eris.

* Address reviews

* Change LatheMenu resize behaviour

* Update Content.Client/GameObjects/Components/Research/ResearchConsoleBoundUserInterface.cs

Co-Authored-By: Pieter-Jan Briers <pieterjan.briers@gmail.com>

* Update Content.Client/Research/ResearchConsoleMenu.cs

Co-Authored-By: Pieter-Jan Briers <pieterjan.briers@gmail.com>

* Move IoC Resolve out of for loop

* Address review

* Localize stuff
This commit is contained in:
Víctor Aguilera Puerto
2019-09-03 22:51:19 +02:00
committed by Pieter-Jan Briers
parent b62fb4a318
commit ba8b495ec0
61 changed files with 1884 additions and 23 deletions

View File

@@ -6,6 +6,7 @@ using Content.Shared.GameObjects.Components.Research;
using Content.Shared.Research;
using Robust.Server.GameObjects.Components.UserInterface;
using Robust.Server.Interfaces.GameObjects;
using Robust.Server.Interfaces.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.UserInterface;
using Robust.Shared.Timers;
@@ -37,10 +38,9 @@ namespace Content.Server.GameObjects.Components.Research
_userInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
}
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage serverMsg)
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage message)
{
var message = serverMsg.Message;
switch (message)
switch (message.Message)
{
case LatheQueueRecipeMessage msg:
_prototypeManager.TryIndex(msg.ID, out LatheRecipePrototype recipe);
@@ -57,6 +57,20 @@ namespace Content.Server.GameObjects.Components.Research
if (_producingRecipe != null)
_userInterface.SendMessage(new LatheProducingRecipeMessage(_producingRecipe.ID));
break;
case LatheServerSelectionMessage msg:
if (!Owner.TryGetComponent(out ResearchClientComponent researchClient)) return;
researchClient.OpenUserInterface(message.Session);
break;
case LatheServerSyncMessage msg:
if (!Owner.TryGetComponent(out TechnologyDatabaseComponent database)
|| !Owner.TryGetComponent(out ProtolatheDatabaseComponent protoDatabase)) return;
if(database.SyncWithServer())
protoDatabase.Sync();
break;
}
}
@@ -88,12 +102,17 @@ namespace Content.Server.GameObjects.Components.Research
return true;
}
public void OpenUserInterface(IPlayerSession session)
{
_userInterface.Open(session);
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent(out IActorComponent actor))
return;
_userInterface.Open(actor.playerSession);
OpenUserInterface(actor.playerSession);
return;
}
bool IAttackBy.AttackBy(AttackByEventArgs eventArgs)

View File

@@ -30,12 +30,14 @@ namespace Content.Server.GameObjects.Components.Research
public override void Clear()
{
if (Static) return;
base.Clear();
Dirty();
}
public override void AddRecipe(LatheRecipePrototype recipe)
{
if (Static) return;
base.AddRecipe(recipe);
Dirty();
}

View File

@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using Content.Shared.GameObjects;
using Content.Shared.GameObjects.Components.Research;
using Content.Shared.Research;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
namespace Content.Server.GameObjects.Components.Research
{
[RegisterComponent]
[ComponentReference(typeof(SharedLatheDatabaseComponent))]
public class ProtolatheDatabaseComponent : SharedProtolatheDatabaseComponent
{
public override string Name => "ProtolatheDatabase";
public override ComponentState GetComponentState()
{
return new ProtolatheDatabaseState(GetRecipeIdList());
}
/// <summary>
/// Adds unlocked recipes from technologies to the database.
/// </summary>
public void Sync()
{
if (!Owner.TryGetComponent(out TechnologyDatabaseComponent database)) return;
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
foreach (var technology in database.Technologies)
{
foreach (var id in technology.UnlockedRecipes)
{
var recipe = (LatheRecipePrototype)prototypeManager.Index(typeof(LatheRecipePrototype), id);
UnlockRecipe(recipe);
}
}
Dirty();
}
/// <summary>
/// Unlocks a recipe but only if it's one of the allowed recipes on this protolathe.
/// </summary>
/// <param name="recipe">The recipe</param>
/// <returns>Whether it could add it or not.</returns>
public bool UnlockRecipe(LatheRecipePrototype recipe)
{
if (!ProtolatheRecipes.Contains(recipe)) return false;
AddRecipe(recipe);
return true;
}
}
}

View File

@@ -0,0 +1,109 @@
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Research;
using Robust.Server.GameObjects.Components.UserInterface;
using Robust.Server.Interfaces.GameObjects;
using Robust.Server.Interfaces.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.UserInterface;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Systems;
using Robust.Shared.IoC;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Research
{
[RegisterComponent]
public class ResearchClientComponent : SharedResearchClientComponent, IActivate
{
// TODO: Create GUI for changing RD server.
private BoundUserInterface _userInterface;
#pragma warning disable 649
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
#pragma warning restore 649
public bool ConnectedToServer => Server != null;
[ViewVariables(VVAccess.ReadOnly)]
public ResearchServerComponent Server { get; set; }
public bool RegisterServer(ResearchServerComponent server)
{
var result = server != null && server.RegisterClient(this);
return result;
}
public void UnregisterFromServer()
{
Server?.UnregisterClient(this);
}
public override void Initialize()
{
base.Initialize();
// For now it just registers on the first server it can find.
var servers = _entitySystemManager.GetEntitySystem<ResearchSystem>().Servers;
if(servers.Count > 0)
RegisterServer(servers[0]);
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>().GetBoundUserInterface(ResearchClientUiKey.Key);
_userInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
}
public void OpenUserInterface(IPlayerSession session)
{
UpdateUserInterface();
_userInterface.Open(session);
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent(out IActorComponent actor))
return;
OpenUserInterface(actor.playerSession);
return;
}
public void UpdateUserInterface()
{
_userInterface?.SetState(GetNewUiState());
}
private ResearchClientBoundInterfaceState GetNewUiState()
{
var rd = _entitySystemManager.GetEntitySystem<ResearchSystem>();
return new ResearchClientBoundInterfaceState(rd.Servers.Count, rd.GetServerNames(),
rd.GetServerIds(), ConnectedToServer ? Server.Id : -1);
}
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage msg)
{
switch (msg.Message)
{
case ResearchClientSyncMessage _:
UpdateUserInterface();
break;
case ResearchClientServerSelectedMessage selectedMessage:
UnregisterFromServer();
RegisterServer(_entitySystemManager.GetEntitySystem<ResearchSystem>().GetServerById(selectedMessage.ServerId));
UpdateUserInterface();
break;
case ResearchClientServerDeselectedMessage _:
UnregisterFromServer();
UpdateUserInterface();
break;
}
}
public override void Shutdown()
{
base.Shutdown();
UnregisterFromServer();
}
}
}

View File

@@ -0,0 +1,94 @@
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Research;
using Content.Shared.Research;
using Robust.Server.GameObjects.Components.UserInterface;
using Robust.Server.Interfaces.GameObjects;
using Robust.Server.Interfaces.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.UserInterface;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
namespace Content.Server.GameObjects.Components.Research
{
[RegisterComponent]
[ComponentReference(typeof(IActivate))]
public class ResearchConsoleComponent : SharedResearchConsoleComponent, IActivate
{
private BoundUserInterface _userInterface;
private ResearchClientComponent _client;
public override void Initialize()
{
base.Initialize();
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>().GetBoundUserInterface(ResearchConsoleUiKey.Key);
_userInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
_client = Owner.GetComponent<ResearchClientComponent>();
}
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage message)
{
if (!Owner.TryGetComponent(out TechnologyDatabaseComponent database)) return;
switch (message.Message)
{
case ConsoleUnlockTechnologyMessage msg:
var protoMan = IoCManager.Resolve<IPrototypeManager>();
if (!protoMan.TryIndex(msg.Id, out TechnologyPrototype tech)) break;
if(!_client.Server.CanUnlockTechnology(tech)) break;
if (_client.Server.UnlockTechnology(tech))
{
database.SyncWithServer();
database.Dirty();
UpdateUserInterface();
}
break;
case ConsoleServerSyncMessage msg:
database.SyncWithServer();
UpdateUserInterface();
break;
case ConsoleServerSelectionMessage msg:
if (!Owner.TryGetComponent(out ResearchClientComponent client)) break;
client.OpenUserInterface(message.Session);
break;
}
}
/// <summary>
/// Method to update the user interface on the clients.
/// </summary>
public void UpdateUserInterface()
{
_userInterface.SetState(GetNewUiState());
}
private ResearchConsoleBoundInterfaceState GetNewUiState()
{
var points = _client.ConnectedToServer ? _client.Server.Point : 0;
var pointsPerSecond = _client.ConnectedToServer ? _client.Server.PointsPerSecond : 0;
return new ResearchConsoleBoundInterfaceState(points, pointsPerSecond);
}
/// <summary>
/// Open the user interface on a certain player session.
/// </summary>
/// <param name="session">Session where the UI will be shown</param>
public void OpenUserInterface(IPlayerSession session)
{
_userInterface.Open(session);
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent(out IActorComponent actor))
return;
OpenUserInterface(actor.playerSession);
return;
}
}
}

View File

@@ -0,0 +1,40 @@
using Content.Server.GameObjects.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Research
{
[RegisterComponent]
[ComponentReference(typeof(IActivate))]
public class ResearchPointSourceComponent : ResearchClientComponent
{
public override string Name => "ResearchPointSource";
private int _pointsPerSecond;
private bool _active;
[ViewVariables]
public int PointsPerSecond
{
get => _pointsPerSecond;
set => value = _pointsPerSecond;
}
[ViewVariables]
public bool Active
{
get => _active;
set => value = _active;
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _pointsPerSecond, "pointspersecond", 0);
serializer.DataField(ref _active, "active", false);
}
}
}

View File

@@ -0,0 +1,162 @@
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices.WindowsRuntime;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.Research;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Research
{
[RegisterComponent]
public class ResearchServerComponent : Component
{
public static int ServerCount = 0;
public override string Name => "ResearchServer";
[ViewVariables(VVAccess.ReadWrite)]
public string ServerName => _serverName;
private string _serverName = "RDSERVER";
private float _timer = 0f;
public TechnologyDatabaseComponent Database { get; private set; }
[ViewVariables(VVAccess.ReadWrite)]
private int _points = 0;
[ViewVariables(VVAccess.ReadOnly)]
public int Id { get; private set; }
// You could optimize research by keeping a list of unlocked recipes too.
[ViewVariables(VVAccess.ReadOnly)]
public IReadOnlyList<TechnologyPrototype> UnlockedTechnologies => Database.Technologies;
[ViewVariables(VVAccess.ReadOnly)]
public List<ResearchPointSourceComponent> PointSources { get; } = new List<ResearchPointSourceComponent>();
[ViewVariables(VVAccess.ReadOnly)]
public List<ResearchClientComponent> Clients { get; } = new List<ResearchClientComponent>();
public int Point => _points;
/// <summary>
/// How many points per second this R&D server gets.
/// The value is calculated from all point sources connected to it.
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
public int PointsPerSecond
{
// This could be changed to PointsPerMinute quite easily for optimization.
get
{
var points = 0;
foreach (var source in PointSources)
{
if (source.Active) points += source.PointsPerSecond;
}
return points;
}
}
public override void Initialize()
{
base.Initialize();
Id = ServerCount++;
IoCManager.Resolve<IEntitySystemManager>()?.GetEntitySystem<ResearchSystem>()?.RegisterServer(this);
Database = Owner.GetComponent<TechnologyDatabaseComponent>();
}
public override void Shutdown()
{
base.Shutdown();
IoCManager.Resolve<IEntitySystemManager>()?.GetEntitySystem<ResearchSystem>()?.UnregisterServer(this);
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _serverName, "servername", "RDSERVER");
serializer.DataField(ref _points, "points", 0);
}
public bool CanUnlockTechnology(TechnologyPrototype technology)
{
if (!Database.CanUnlockTechnology(technology) || _points < technology.RequiredPoints || Database.IsTechnologyUnlocked(technology)) return false;
return true;
}
/// <summary>
/// Unlocks a technology, but only if there are enough research points for it.
/// If there are, it subtracts the amount of points from the total.
/// </summary>
/// <param name="technology"></param>
/// <returns></returns>
public bool UnlockTechnology(TechnologyPrototype technology)
{
if (!CanUnlockTechnology(technology)) return false;
var result = Database.UnlockTechnology(technology);
if(result)
_points -= technology.RequiredPoints;
return result;
}
/// <summary>
/// Check whether a technology is unlocked or not.
/// </summary>
/// <param name="technology"></param>
/// <returns></returns>
public bool IsTechnologyUnlocked(TechnologyPrototype technology)
{
return Database.IsTechnologyUnlocked(technology);
}
/// <summary>
/// Registers a remote client on this research server.
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
public bool RegisterClient(ResearchClientComponent client)
{
if (client is ResearchPointSourceComponent source)
{
if (PointSources.Contains(source)) return false;
PointSources.Add(source);
source.Server = this;
return true;
}
if (Clients.Contains(client)) return false;
Clients.Add(client);
client.Server = this;
return true;
}
/// <summary>
/// Unregisters a remote client from this server.
/// </summary>
/// <param name="client"></param>
public void UnregisterClient(ResearchClientComponent client)
{
if (client is ResearchPointSourceComponent source)
{
PointSources.Remove(source);
return;
}
Clients.Remove(client);
}
public void Update(float frameTime)
{
_timer += frameTime;
if (_timer < 1f) return;
_timer = 0f;
_points += PointsPerSecond;
}
}
}

View File

@@ -0,0 +1,77 @@
using Content.Shared.GameObjects.Components.Research;
using Content.Shared.Research;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Prototypes;
namespace Content.Server.GameObjects.Components.Research
{
[RegisterComponent]
public class TechnologyDatabaseComponent : SharedTechnologyDatabaseComponent
{
public override ComponentState GetComponentState()
{
return new TechnologyDatabaseState(_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 otherDatabase, bool twoway = true)
{
foreach (var tech in otherDatabase.Technologies)
{
if (!IsTechnologyUnlocked(tech)) AddTechnology(tech);
}
if(twoway)
otherDatabase.Sync(this, false);
Dirty();
}
/// <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()
{
if (!Owner.TryGetComponent(out ResearchClientComponent client)) return false;
if (!client.ConnectedToServer) return false;
Sync(client.Server.Database);
return true;
}
/// <summary>
/// If possible, unlocks a technology on this database.
/// </summary>
/// <param name="technology"></param>
/// <returns></returns>
public bool UnlockTechnology(TechnologyPrototype technology)
{
if (!CanUnlockTechnology(technology)) return false;
AddTechnology(technology);
Dirty();
return true;
}
/// <summary>
/// Adds a technology to the database without checking if it could be unlocked.
/// </summary>
/// <param name="technology"></param>
public void AddTechnology(TechnologyPrototype technology)
{
_technologies.Add(technology);
}
}
}