clean up infinite researchsystem shitcode (#13136)

* clean up infinite researchsystem shitcode

* fml some more shit

* make syncing work logically

* naming naming naming
This commit is contained in:
Nemanja
2022-12-25 16:22:23 -05:00
committed by GitHub
parent 9afbdf6a17
commit 4eee1ee9b2
23 changed files with 497 additions and 293 deletions

View File

@@ -1,30 +1,25 @@
using System.Linq;
using Content.Server.Research.Components;
using Content.Shared.Research.Components;
using Content.Shared.Research.Prototypes;
using Robust.Shared.GameStates;
namespace Content.Server.Research.Systems;
public sealed partial class ResearchSystem
{
/// <summary>
/// Synchronizes this database against other,
/// adding all technologies from the other that
/// this one doesn't have.
/// Syncs the primary entity's database to that of the secondary entity's database.
/// </summary>
/// <param name="component"></param>
/// <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)
public void Sync(EntityUid primaryUid, EntityUid otherUid, TechnologyDatabaseComponent? primaryDb = null, TechnologyDatabaseComponent? otherDb = null)
{
otherDatabase.TechnologyIds = otherDatabase.TechnologyIds.Union(component.TechnologyIds).ToList();
otherDatabase.RecipeIds = otherDatabase.RecipeIds.Union(component.RecipeIds).ToList();
if (!Resolve(primaryUid, ref primaryDb) || !Resolve(otherUid, ref otherDb))
return;
if (twoway)
Sync(otherDatabase, component, false);
primaryDb.TechnologyIds = otherDb.TechnologyIds;
primaryDb.RecipeIds = otherDb.RecipeIds;
Dirty(component);
Dirty(primaryDb);
var ev = new TechnologyDatabaseModifiedEvent();
RaiseLocalEvent(primaryDb.Owner, ref ev);
}
/// <summary>
@@ -33,46 +28,74 @@ public sealed partial class ResearchSystem
/// 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)
public bool SyncClientWithServer(EntityUid uid, TechnologyDatabaseComponent? databaseComponent = null, ResearchClientComponent? clientComponent = null)
{
if (!Resolve(component.Owner, ref clientComponent, false))
return false;
if (!TryComp<TechnologyDatabaseComponent>(clientComponent.Server?.Owner, out var clientDatabase))
if (!Resolve(uid, ref databaseComponent, ref clientComponent, false))
return false;
Sync(component, clientDatabase);
if (!TryComp<TechnologyDatabaseComponent>(clientComponent.Server, out var serverDatabase))
return false;
Sync(uid, clientComponent.Server.Value, databaseComponent, serverDatabase);
return true;
}
/// <summary>
/// If possible, unlocks a technology on this database.
/// Tries to add a technology to a database, checking if it is able to
/// </summary>
/// <param name="component"></param>
/// <param name="technology"></param>
/// <returns></returns>
public bool UnlockTechnology(TechnologyDatabaseComponent component, TechnologyPrototype technology)
/// <returns>If the technology was successfully added</returns>
public bool UnlockTechnology(EntityUid client, string prototypeid, ResearchClientComponent? component = null,
TechnologyDatabaseComponent? databaseComponent = null)
{
if (!CanUnlockTechnology(component.Owner, technology, component)) return false;
if (!_prototypeManager.TryIndex<TechnologyPrototype>(prototypeid, out var prototype))
{
Logger.Error("invalid technology prototype");
return false;
}
return UnlockTechnology(client, prototype, component, databaseComponent);
}
AddTechnology(component, technology.ID);
/// <summary>
/// Tries to add a technology to a database, checking if it is able to
/// </summary>
/// <returns>If the technology was successfully added</returns>
public bool UnlockTechnology(EntityUid client, TechnologyPrototype prototype, ResearchClientComponent? component = null,
TechnologyDatabaseComponent? databaseComponent = null)
{
if (!Resolve(client, ref component, ref databaseComponent, false))
return false;
if (!CanUnlockTechnology(client, prototype, databaseComponent))
return false;
if (component.Server is not { } server)
return false;
AddTechnology(server, prototype.ID);
AddPointsToServer(server, -prototype.RequiredPoints);
return true;
}
/// <summary>
/// Adds a technology to the database without checking if it could be unlocked.
/// </summary>
/// <param name="component"></param>
/// <param name="technology"></param>
public void AddTechnology(TechnologyDatabaseComponent component, string technology)
public void AddTechnology(EntityUid uid, string technology, TechnologyDatabaseComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
if (!_prototypeManager.TryIndex<TechnologyPrototype>(technology, out var prototype))
return;
AddTechnology(component, prototype);
AddTechnology(uid, prototype, component);
}
public void AddTechnology(TechnologyDatabaseComponent component, TechnologyPrototype technology)
/// <summary>
/// Adds a technology to the database without checking if it could be unlocked.
/// </summary>
public void AddTechnology(EntityUid uid, TechnologyPrototype technology, TechnologyDatabaseComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
component.TechnologyIds.Add(technology.ID);
foreach (var unlock in technology.UnlockedRecipes)
{
@@ -82,23 +105,63 @@ public sealed partial class ResearchSystem
}
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);
}
var ev = new TechnologyDatabaseModifiedEvent();
RaiseLocalEvent(uid, ref ev);
}
public void AddLatheRecipe(TechnologyDatabaseComponent component, string recipe, bool dirty = true)
/// <summary>
/// Adds a lathe recipe to the specified technology database
/// without checking if it can be unlocked.
/// </summary>
public void AddLatheRecipe(EntityUid uid, string recipe, TechnologyDatabaseComponent? component = null, bool dirty = true)
{
if (!Resolve(uid, ref component))
return;
if (component.RecipeIds.Contains(recipe))
return;
component.RecipeIds.Add(recipe);
if (dirty)
Dirty(component);
var ev = new TechnologyDatabaseModifiedEvent();
RaiseLocalEvent(uid, ref ev);
}
/// <summary>
/// Returns whether a technology can be unlocked on this database,
/// taking parent technologies into account.
/// </summary>
/// <returns>Whether it could be unlocked or not</returns>
public bool CanUnlockTechnology(EntityUid uid, string technology, TechnologyDatabaseComponent? database = null, ResearchClientComponent? client = null)
{
if (!_prototypeManager.TryIndex<TechnologyPrototype>(technology, out var prototype))
return false;
return CanUnlockTechnology(uid, prototype, database, client);
}
/// <summary>
/// Returns whether a technology can be unlocked on this database,
/// taking parent technologies into account.
/// </summary>
/// <returns>Whether it could be unlocked or not</returns>
public bool CanUnlockTechnology(EntityUid uid, TechnologyPrototype technology, TechnologyDatabaseComponent? database = null, ResearchClientComponent? client = null)
{
if (!Resolve(uid, ref database, ref client))
return false;
if (!TryGetClientServer(uid, out _, out var serverComponent, client))
return false;
if (serverComponent.Points < technology.RequiredPoints)
return false;
if (IsTechnologyUnlocked(uid, technology, database))
return false;
if (!ArePrerequesitesUnlocked(uid, technology, database))
return false;
return true;
}
}