Skip to content

Commit

Permalink
Add automated staff hiring (#2352)
Browse files Browse the repository at this point in the history
  • Loading branch information
siimav authored May 11, 2024
1 parent 52dc2a9 commit dadc6ba
Show file tree
Hide file tree
Showing 11 changed files with 323 additions and 64 deletions.
1 change: 1 addition & 0 deletions Source/RP0/RP0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<Compile Include="Harmony\ModuleRCS.cs" />
<Compile Include="ModIntegrations\TFInterop.cs" />
<Compile Include="Singletons\LeaderNotifications.cs" />
<Compile Include="SpaceCenter\Projects\HireStaffProject.cs" />
<Compile Include="SpaceCenter\Projects\VesselRepairProject.cs" />
<Compile Include="UI\ProceduralAvionicsWindow.cs" />
<Compile Include="CareerLog\CareerEvent.cs" />
Expand Down
9 changes: 0 additions & 9 deletions Source/RP0/Settings/SpaceCenterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,6 @@ public void ResetBools()
nautUpkeepTrainingBools[i] = false;
}

public int GetResearcherCap(int lvl = -1)
{
return -1;

/*if (lvl == -1)
lvl = Utilities.GetBuildingUpgradeLevel(SpaceCenterFacility.ResearchAndDevelopment);
return GeneralSettings.ResearcherCaps[lvl];*/
}

public int GetStartingPersonnel(Game.Modes mode)
{
if (mode == Game.Modes.CAREER)
Expand Down
2 changes: 2 additions & 0 deletions Source/RP0/SpaceCenter/LaunchComplex/LaunchComplex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ public void Delete()
SpaceCenterManagement.Instance.UnregsiterLP(lp);

SpaceCenterManagement.Instance.UnregisterLC(this);
if (SpaceCenterManagement.Instance.staffTarget.LCID == ID)
SpaceCenterManagement.Instance.staffTarget.Clear();

int index = KSC.LaunchComplexes.IndexOf(this);
KSC.LaunchComplexes.RemoveAt(index);
Expand Down
7 changes: 5 additions & 2 deletions Source/RP0/SpaceCenter/Projects/FundTargetProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,17 @@ public ProjectType GetProjectType()

public double GetTimeLeft()
{
double baseFunds = Funding.Instance.Funds;
return EstimateTimeToFunds(Funding.Instance.Funds, targetFunds, epsilonTime);
}

public static double EstimateTimeToFunds(double baseFunds, double targetFunds, double epsilonTime)
{
if (targetFunds - baseFunds <= 0.001d)
return 0d;

double timeLower = MinTime;
double timeUpper = MaxTime;

double bestTime = -1d;
double lastFunds = 0d;
bool lastDir = false;
Expand Down
133 changes: 133 additions & 0 deletions Source/RP0/SpaceCenter/Projects/HireStaffProject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using ROUtils.DataTypes;
using System;

namespace RP0
{
public class HireStaffProject : ConfigNodePersistenceBase, ISpaceCenterProject, IConfigNode
{
[Persistent]
private double reserveFunds = 0d;

[Persistent]
private int startCrewCount = 0;

[Persistent]
private int targetCrewCount = 0;

[Persistent]
private Guid _lcID = Guid.Empty;
public Guid LCID
{
get
{
return _lcID;
}
set
{
_lcID = value;
if (_lcID == Guid.Empty)
_lc = null;
else
_lc = SpaceCenterManagement.Instance.LC(_lcID);
}
}

private LaunchComplex _lc = null;
public LaunchComplex LC
{
get
{
if (_lc == null && LCID != Guid.Empty)
{
_lc = SpaceCenterManagement.Instance.LC(_lcID);
}
return _lc;
}
set
{
_lc = value;
if (_lc == null)
_lcID = Guid.Empty;
else
_lcID = _lc.ID;
}
}

public bool IsValid => targetCrewCount > 0;

public HireStaffProject() { }

public HireStaffProject(int startCrewCount, int targetCrewCount, double reserveFunds, LaunchComplex lc = null)
{
LC = lc;
this.startCrewCount = startCrewCount;
this.targetCrewCount = targetCrewCount;
this.reserveFunds = reserveFunds;
}

public void Clear()
{
reserveFunds = 0d;
startCrewCount = targetCrewCount = 0;
LCID = Guid.Empty;
}

public double GetBuildRate()
{
return 1d;
}

public double GetFractionComplete()
{
int total = targetCrewCount - startCrewCount;
if (total <= 0) return 0d;

return (CurrentAmount - startCrewCount) / total;
}

public string GetItemName()
{
return $"Reach {targetCrewCount} {(IsResearch ? "researchers" : "engineers")}";
}

public ProjectType GetProjectType()
{
return ProjectType.None;
}

public double GetTimeLeft()
{
double modifiedHireCost = -CurrencyUtils.Funds(IsResearch ? TransactionReasonsRP0.HiringResearchers : TransactionReasonsRP0.HiringEngineers, -Database.SettingsSC.HireCost);
double curFunds = Funding.Instance.Funds;
double fundsNeeded = reserveFunds + NumLeftToHire * modifiedHireCost;
return FundTargetProject.EstimateTimeToFunds(curFunds, fundsNeeded, 60);
}

public double GetTimeLeftEst(double offset)
{
return GetTimeLeft();
}

public double IncrementProgress(double UTDiff)
{
double modifiedHireCost = -CurrencyUtils.Funds(IsResearch ? TransactionReasonsRP0.HiringResearchers : TransactionReasonsRP0.HiringEngineers, -Database.SettingsSC.HireCost);
double nextHireAt = reserveFunds + modifiedHireCost;
if (SpaceCenterManagement.Instance.Applicants > 0 || Funding.Instance.Funds > nextHireAt)
{
int numCanHire = (int)((Funding.Instance.Funds - reserveFunds) / modifiedHireCost);
numCanHire = SpaceCenterManagement.Instance.Applicants + numCanHire;
KCTUtilities.HireStaff(IsResearch, Math.Min(numCanHire, NumLeftToHire), LC);
}

return 0d;
}

public bool IsResearch => LCID == Guid.Empty;

public bool IsComplete() => NumLeftToHire <= 0;

public int NumLeftToHire => targetCrewCount - CurrentAmount;

public int CurrentAmount => IsResearch ? SpaceCenterManagement.Instance.Researchers : LC.Engineers;
}
}
11 changes: 11 additions & 0 deletions Source/RP0/SpaceCenter/SpaceCenterManagement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public static void ClearVesselEditMode()
[KSPField(isPersistant = true)]
public FundTargetProject fundTarget = new FundTargetProject();

[KSPField(isPersistant = true)]
public HireStaffProject staffTarget = new HireStaffProject();

#endregion

#region Fields
Expand Down Expand Up @@ -1691,6 +1694,14 @@ public void ProgressBuildTime(double UTDiff)
if (fundTarget.IsValid && fundTarget.GetTimeLeft() < 0.5d)
fundTarget.Clear();
}

if (staffTarget.IsValid)
{
staffTarget.IncrementProgress(UTDiff);
if (staffTarget.IsComplete())
staffTarget.Clear();
}

Profiler.EndSample();
}

Expand Down
15 changes: 13 additions & 2 deletions Source/RP0/UI/KCT/GUI_BuildList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public enum RenameType { None, Vessel, Pad, LaunchComplex };
private static double _accumulatedTimeBefore;

private static GUIStyle _redText, _yellowText, _greenText, _blobText, _yellowButton, _redButton, _greenButton;
private static GUIContent _settingsTexture, _planeTexture, _rocketTexture, _techTexture, _constructTexture,
private static GUIContent _emptyTexture, _settingsTexture, _planeTexture, _rocketTexture, _techTexture, _constructTexture,
_reconTexture, _rolloutTexture, _rollbackTexture, _airlaunchTexture, _recoveryTexture, _hangarTexture, _repairTexture;
private const int _width1 = 120;
private const int _width2 = 100;
Expand Down Expand Up @@ -115,6 +115,7 @@ public static void InitBuildListVars()
_settingsTexture = new GUIContent(GameDatabase.Instance.GetTexture("RP-1/Resources/KCT_settings16", false));
_techTexture = new GUIContent(GameDatabase.Instance.GetTexture("RP-1/Resources/KCT_tech16", false));
_repairTexture = new GUIContent(GameDatabase.Instance.GetTexture("RP-1/Resources/KCT_repair", false));
_emptyTexture = new GUIContent("");
}

public static void DrawBuildListWindow(int windowID)
Expand Down Expand Up @@ -653,6 +654,9 @@ private static void RenderCombinedList()
if (SpaceCenterManagement.Instance.fundTarget.IsValid)
_allItems.Add(SpaceCenterManagement.Instance.fundTarget);

if (SpaceCenterManagement.Instance.staffTarget.IsValid)
_allItems.Add(SpaceCenterManagement.Instance.staffTarget);

// Precalc times and then sort
foreach (var b in _allItems)
_estTimeForItem[b] = b.GetTimeLeftEst(_timeBeforeItem.ValueOrDefault(b));
Expand All @@ -673,6 +677,13 @@ private static void RenderCombinedList()
continue;

GUILayout.BeginHorizontal();
if ((t is HireStaffProject || t is FundTargetProject) &&
GUILayout.Button("X", GUILayout.Width(_butW)))
{
(t as HireStaffProject)?.Clear();
(t as FundTargetProject)?.Clear();
}

DrawTypeIcon(t);
VesselProject vp;
if (t is ReconRolloutProject r)
Expand Down Expand Up @@ -833,7 +844,7 @@ private static GUIContent GetTypeIcon(ISpaceCenterProject b)
return _repairTexture;
}

return _constructTexture;
return _emptyTexture;
}

private static void DrawTypeIcon(ISpaceCenterProject b)
Expand Down
2 changes: 2 additions & 0 deletions Source/RP0/UI/KCT/GUI_NewLC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ private static void ProcessNewLC(bool isModify, double curPadCost, double totalC
if (isModify)
{
lc = activeLC;
if (SpaceCenterManagement.Instance.staffTarget.LCID == lc.ID)
SpaceCenterManagement.Instance.staffTarget.Clear();
KCTUtilities.ChangeEngineers(lc, -engineers);
SpaceCenterManagement.Instance.ActiveSC.SwitchToPrevLaunchComplex();

Expand Down
Loading

0 comments on commit dadc6ba

Please sign in to comment.