2021-05-23 17:38:26 -07:00
|
|
|
using System.Collections.Generic;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Server.Power.Components;
|
|
|
|
|
using Content.Shared.Research.Prototypes;
|
2019-09-03 22:51:19 +02:00
|
|
|
using Robust.Shared.GameObjects;
|
2021-12-03 14:17:01 +01:00
|
|
|
using Robust.Shared.IoC;
|
2021-03-05 01:08:38 +01:00
|
|
|
using Robust.Shared.Serialization.Manager.Attributes;
|
2019-09-03 22:51:19 +02:00
|
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Server.Research.Components
|
2019-09-03 22:51:19 +02:00
|
|
|
{
|
|
|
|
|
[RegisterComponent]
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class ResearchServerComponent : Component
|
2019-09-03 22:51:19 +02:00
|
|
|
{
|
|
|
|
|
public static int ServerCount = 0;
|
|
|
|
|
|
2020-01-17 15:34:40 +01:00
|
|
|
[ViewVariables(VVAccess.ReadWrite)] public string ServerName => _serverName;
|
2019-09-03 22:51:19 +02:00
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[DataField("servername")]
|
2019-09-03 22:51:19 +02:00
|
|
|
private string _serverName = "RDSERVER";
|
|
|
|
|
private float _timer = 0f;
|
2021-03-16 15:50:20 +01:00
|
|
|
public TechnologyDatabaseComponent? Database { get; private set; }
|
2019-09-03 22:51:19 +02:00
|
|
|
|
2021-03-05 01:08:38 +01:00
|
|
|
[ViewVariables(VVAccess.ReadWrite)] [DataField("points")] private int _points = 0;
|
2019-09-03 22:51:19 +02:00
|
|
|
|
2020-01-17 15:34:40 +01:00
|
|
|
[ViewVariables(VVAccess.ReadOnly)] public int Id { get; private set; }
|
2019-09-03 22:51:19 +02:00
|
|
|
|
|
|
|
|
// You could optimize research by keeping a list of unlocked recipes too.
|
|
|
|
|
[ViewVariables(VVAccess.ReadOnly)]
|
2021-03-16 15:50:20 +01:00
|
|
|
public IReadOnlyList<TechnologyPrototype>? UnlockedTechnologies => Database?.Technologies;
|
2020-01-17 15:34:40 +01:00
|
|
|
|
2019-09-03 22:51:19 +02:00
|
|
|
[ViewVariables(VVAccess.ReadOnly)]
|
2020-11-27 11:00:49 +01:00
|
|
|
public List<ResearchPointSourceComponent> PointSources { get; } = new();
|
2020-01-17 15:34:40 +01:00
|
|
|
|
2019-09-03 22:51:19 +02:00
|
|
|
[ViewVariables(VVAccess.ReadOnly)]
|
2020-11-27 11:00:49 +01:00
|
|
|
public List<ResearchClientComponent> Clients { get; } = new();
|
2019-09-03 22:51:19 +02:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2020-01-17 15:34:40 +01:00
|
|
|
if (CanRun)
|
2019-09-03 22:51:19 +02:00
|
|
|
{
|
2020-01-17 15:34:40 +01:00
|
|
|
foreach (var source in PointSources)
|
|
|
|
|
{
|
|
|
|
|
if (source.CanProduce) points += source.PointsPerSecond;
|
|
|
|
|
}
|
2019-09-03 22:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return points;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-04 18:11:52 +02:00
|
|
|
/// <remarks>If no <see cref="ApcPowerReceiverComponent"/> is found, it's assumed power is not required.</remarks>
|
2020-01-17 15:34:40 +01:00
|
|
|
[ViewVariables]
|
2020-06-28 09:23:26 -06:00
|
|
|
public bool CanRun => _powerReceiver is null || _powerReceiver.Powered;
|
2020-01-17 15:34:40 +01:00
|
|
|
|
2021-07-04 18:11:52 +02:00
|
|
|
private ApcPowerReceiverComponent? _powerReceiver;
|
2020-01-17 15:34:40 +01:00
|
|
|
|
2021-06-19 19:41:26 -07:00
|
|
|
protected override void Initialize()
|
2019-09-03 22:51:19 +02:00
|
|
|
{
|
|
|
|
|
base.Initialize();
|
|
|
|
|
Id = ServerCount++;
|
2020-05-23 02:27:31 -07:00
|
|
|
EntitySystem.Get<ResearchSystem>()?.RegisterServer(this);
|
2020-08-22 22:29:20 +02:00
|
|
|
Database = Owner.EnsureComponent<TechnologyDatabaseComponent>();
|
2021-12-03 15:53:09 +01:00
|
|
|
IoCManager.Resolve<IEntityManager>().TryGetComponent(Owner, out _powerReceiver);
|
2019-09-03 22:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-18 11:29:12 -07:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
protected override void Shutdown()
|
2019-09-03 22:51:19 +02:00
|
|
|
{
|
|
|
|
|
base.Shutdown();
|
2020-05-23 02:27:31 -07:00
|
|
|
EntitySystem.Get<ResearchSystem>()?.UnregisterServer(this);
|
2019-09-03 22:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanUnlockTechnology(TechnologyPrototype technology)
|
|
|
|
|
{
|
2021-03-16 15:50:20 +01:00
|
|
|
if (Database == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!Database.CanUnlockTechnology(technology) ||
|
|
|
|
|
_points < technology.RequiredPoints ||
|
|
|
|
|
Database.IsTechnologyUnlocked(technology))
|
|
|
|
|
return false;
|
|
|
|
|
|
2019-09-03 22:51:19 +02:00
|
|
|
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;
|
2021-03-16 15:50:20 +01:00
|
|
|
var result = Database?.UnlockTechnology(technology) ?? false;
|
2020-01-17 15:34:40 +01:00
|
|
|
if (result)
|
2019-09-03 22:51:19 +02:00
|
|
|
_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)
|
|
|
|
|
{
|
2021-03-16 15:50:20 +01:00
|
|
|
return Database?.IsTechnologyUnlocked(technology) ?? false;
|
2019-09-03 22:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2020-01-17 15:34:40 +01:00
|
|
|
if (!CanRun) return;
|
2019-09-03 22:51:19 +02:00
|
|
|
_timer += frameTime;
|
|
|
|
|
if (_timer < 1f) return;
|
|
|
|
|
_timer = 0f;
|
|
|
|
|
_points += PointsPerSecond;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|