Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental hab-cabin time implementation #317

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ LIFE_SUPPORT_SETTINGS
BaseHabTime = 0.25 //How long can 1 crew capacity support 1 Kerbal, expressed in Kerbal Months
ReplacementPartAmount = 0 //How fast life support equipment and habs 'wears out'
HabRange = 150 //How close we need to be to use other vessel's habitation modules and recyclers.
RecoverySpeed = 1 //Rate at which Kerbals recover cabin time while in a habitat (seconds per second)
EnableRecyclers = true //Use resource recyclers? Not the same as resource converteres like greenhouses!
VetNames = Jebediah,Valentina,Bill,Bob
ScoutHabTime = 9180000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override string GetInfo()
var output = new StringBuilder();
output.AppendLine();
output.AppendLine(base.GetInfo());
output.AppendLine(string.Format("Kerbal-Months: {0}", BaseKerbalMonths + part.CrewCapacity));
output.AppendLine(string.Format("Kerbal-Months: {0}", BaseKerbalMonths));
output.AppendLine(string.Format("Crew Affected: {0}", CrewCapacity));
output.AppendLine(string.Format("Hab Multipler: {0}", BaseHabMultiplier));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ public override void PostProcess(ConverterResults result, double deltaTime)
{
var k = kerbals[i];
var lsKerbal = LifeSupportManager.Instance.FetchKerbal(k);
if (AffectsHomeTimer)
lsKerbal.MaxOffKerbinTime += timePerKerbal;
if (AffectsHabTimer)
lsKerbal.TimeEnteredVessel += timePerKerbal;

// Experimental patch.
// I don't like changing LastAtHome but the alternative is that we add another variable...
if (AffectsHomeTimer || AffectsHabTimer)
lsKerbal.LastAtHome += timePerKerbal;

LifeSupportManager.Instance.TrackKerbal(lsKerbal);
}
Expand Down
1 change: 1 addition & 0 deletions Source/USILifeSupport/LifeSupportConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class LifeSupportConfig
public double BaseHabTime { get; set; }
public bool EnableRecyclers { get; set; }
public double HabRange { get; set; }
public float RecoverySpeed { get; set; }
public double ScoutHabTime { get; set; }
public double PermaHabTime { get; set; }
}
Expand Down
6 changes: 2 additions & 4 deletions Source/USILifeSupport/LifeSupportManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ public LifeSupportStatus FetchKerbal(ProtoCrewMember crew)
k.LastEC = Planetarium.GetUniversalTime();
k.LastAtHome = Planetarium.GetUniversalTime();
k.LastSOIChange = Planetarium.GetUniversalTime();
k.MaxOffKerbinTime = Planetarium.GetUniversalTime() + 648000;
k.TimeEnteredVessel = Planetarium.GetUniversalTime();
k.CurrentVesselId = "?UNKNOWN?";
k.PreviousVesselId = "??UNKNOWN??";
k.RemainingCabinTime = LifeSupportScenario.Instance.settings.GetSettings().BaseHabTime
* LifeSupportUtilities.SecondsPerMonth();
k.LastUpdate = Planetarium.GetUniversalTime();
k.IsGrouchy = false;
k.OldTrait = crew.experienceTrait.Config.Name;
Expand Down Expand Up @@ -391,7 +391,6 @@ internal static double GetHabChecksum(VesselSupplyStatus sourceVessel, Vessel vs

double totHabSpace = sourceVessel.ExtraHabSpace;
double totHabMult = CalculateVesselHabMultiplier(vsl, 1);
totHabSpace += (LifeSupportScenario.Instance.settings.GetSettings().BaseHabTime * totMaxCrew);

var hCount = hList.Count;
for (int i = 0; i < hCount; ++i)
Expand Down Expand Up @@ -439,7 +438,6 @@ internal static double GetTotalHabTime(VesselSupplyStatus sourceVessel, Vessel v
}
double totHabSpace = sourceVessel.ExtraHabSpace;
double totHabMult = CalculateVesselHabMultiplier(vessel,totCurCrew);
totHabSpace += (LifeSupportScenario.Instance.settings.GetSettings().BaseHabTime * totMaxCrew);

var hCount = hList.Count;
for (int i = 0; i < hCount; ++i)
Expand Down
80 changes: 53 additions & 27 deletions Source/USILifeSupport/LifeSupportMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,37 @@ private LifeSupportVesselDisplayStat GetEvaKerbalStats(Vessel kerbal)
vstat.LastUpdate = kerbal.missionTime;
var sitString = "(EVA)";

var evaKerbal = kerbal.GetVesselCrew()[0];
var kerbalStatus = LifeSupportManager.Instance.FetchKerbal(evaKerbal);
var cabinTime = kerbalStatus.RemainingCabinTime;
var cabinTimeString = LifeSupportUtilities.SmartDurationDisplay(Math.Max(0, cabinTime));

var remEVATime = LifeSupportScenario.Instance.settings.GetSettings().EVATime - kerbal.missionTime;
var timeString = LifeSupportUtilities.SmartDurationDisplay(Math.Max(0, remEVATime));

var secondsPerDay = LifeSupportUtilities.SecondsPerDay();


var lblCabin = "6FFF00";
if (cabinTime < secondsPerDay * 3)
{
lblCabin = "FFE100";
}
if (cabinTime < secondsPerDay * 1)
{
lblCabin = "FFAE00";
}

if (remEVATime > 0)
{
vstat.SummaryLabel = String.Format(
"<color=#3DB1FF>{0}/{1} - </color><color=#9EE4FF>{2}</color><color=#3DB1FF> time remaining</color>"
"<color=#3DB1FF>{0}/{1} - </color><color=#9EE4FF>{2}</color><color=#3DB1FF> time remaining - </color>" +
"<color=#{3}>{4}</color><color=#3DB1FF> cabin time</color>"
, kerbal.mainBody.bodyName
, sitString
, timeString.Substring(timeString.IndexOf(':') + 1));
, timeString
, lblCabin
, cabinTimeString);
}
else
{
Expand Down Expand Up @@ -190,9 +211,12 @@ private LifeSupportVesselDisplayStat GetVesselStats(VesselSupplyStatus vsl)
LifeSupportScenario.Instance.settings.GetSettings().HabRange);
}

var numCrew = vsl.NumCrew;
var crewCap = vsl.CrewCap;
var vesselHabMultiplier = vsl.VesselHabMultiplier;
foreach (var c in thisVessel.GetVesselCrew())
{
var crewStat = GetCrewStat(c, thisVessel, suppliesTimeLeft, ecTimeLeft, ecAmount, habTime);
var crewStat = GetCrewStat(c, thisVessel, suppliesTimeLeft, ecTimeLeft, ecAmount, habTime, numCrew, crewCap, vesselHabMultiplier);
vstat.crew.Add(crewStat);
}
vstat.crew = vstat.crew.OrderBy(crewStat => crewStat.EarliestExpiration).ToList();
Expand Down Expand Up @@ -284,8 +308,8 @@ private void DisplayCrewStats(LifeSupportCrewDisplayStat c)
GUILayout.Label(c.ECLabel, _labelStyle, GUILayout.Width(145));
GUILayout.Label("<color=#EDEDED>hab:</color>", _labelStyle, GUILayout.Width(40));
GUILayout.Label(c.HabLabel, _labelStyle, GUILayout.Width(145));
GUILayout.Label("<color=#EDEDED>home:</color>", _labelStyle, GUILayout.Width(40));
GUILayout.Label(c.HomeLabel, _labelStyle, GUILayout.Width(145));
GUILayout.Label("<color=#EDEDED>cabin:</color>", _labelStyle, GUILayout.Width(40));
GUILayout.Label(c.CabinLabel, _labelStyle, GUILayout.Width(145));
GUILayout.EndHorizontal();
}

Expand Down Expand Up @@ -320,7 +344,7 @@ private string GetSituationString(Vessel vessel)
return sitString;
}

private LifeSupportCrewDisplayStat GetCrewStat(ProtoCrewMember c, Vessel vessel, double vesselSuppliesTimeLeft, double vesselEcTimeLeft, double vesselEcAmount, double vesselHabTime)
private LifeSupportCrewDisplayStat GetCrewStat(ProtoCrewMember c, Vessel vessel, double vesselSuppliesTimeLeft, double vesselEcTimeLeft, double vesselEcAmount, double vesselHabTime, int numCrew, int crewCap, double vesselHabMultiplier)
{
var cls = LifeSupportManager.Instance.FetchKerbal(c);
//Guard clause in case we just changed vessels
Expand All @@ -329,7 +353,6 @@ private LifeSupportCrewDisplayStat GetCrewStat(ProtoCrewMember c, Vessel vessel,
{
cls.PreviousVesselId = cls.CurrentVesselId;
cls.CurrentVesselId = vessel.id.ToString();
cls.TimeEnteredVessel = Planetarium.GetUniversalTime();
LifeSupportManager.Instance.TrackKerbal(cls);
}

Expand All @@ -345,7 +368,7 @@ private LifeSupportCrewDisplayStat GetCrewStat(ProtoCrewMember c, Vessel vessel,
cStat.ComputeEc(ecTimeLeft, c);
cStat.ComputeSupply(vesselSuppliesTimeLeft, c);
cStat.ComputeHab(vesselHabTime, c, cls);
cStat.ComputeHome(c, cls);
cStat.ComputeCabin(c, cls, numCrew, crewCap, vesselHabMultiplier);

LifeSupportManager.Instance.TrackKerbal(cls);
return cStat;
Expand Down Expand Up @@ -399,7 +422,7 @@ public class LifeSupportCrewDisplayStat
public string ECLabel { get; set; }
public string SupplyLabel { get; set; }
public string HabLabel { get; set; }
public string HomeLabel { get; set; }
public string CabinLabel { get; set; }
public double EarliestExpiration { get; set; }

public LifeSupportCrewDisplayStat()
Expand All @@ -424,7 +447,7 @@ internal void ComputeHab(double vesselHabTime, ProtoCrewMember c, LifeSupportSta
if (useHabPenalties)
{
UpdateEarliestExpiration(vesselHabTime);
var habTimeLeft = vesselHabTime - (Planetarium.GetUniversalTime() - cls.TimeEnteredVessel);
var habTimeLeft = vesselHabTime - (Planetarium.GetUniversalTime() - cls.LastAtHome);
var isScout = c.HasEffect("ExplorerSkill") && habTimeLeft >= LifeSupportScenario.Instance.settings.GetSettings().ScoutHabTime;
var isPermaHab = habTimeLeft >= LifeSupportScenario.Instance.settings.GetSettings().PermaHabTime;

Expand Down Expand Up @@ -454,43 +477,46 @@ internal void ComputeHab(double vesselHabTime, ProtoCrewMember c, LifeSupportSta
HabLabel = String.Format("<color=#{0}>{1}</color>", lblHab, crewHabString);
}

internal void ComputeHome(ProtoCrewMember c, LifeSupportStatus cls)
internal void ComputeCabin(ProtoCrewMember c, LifeSupportStatus cls, int numCrew, int crewCap, double vesselHabMultiplier)
{
var crewHomeString = "indefinite";
var lblHome = "6FFF00";
var crewCabinString = "indefinite";
var lblCabin = "6FFF00";
var useHabPenalties = LifeSupportManager.GetNoHomeEffect(c.name) > 0;
if (useHabPenalties)
{
var homeTimeLeft = cls.MaxOffKerbinTime - Planetarium.GetUniversalTime();
UpdateEarliestExpiration(homeTimeLeft);
var adjustedCabinTimeLeft = cls.RemainingCabinTime * ((double)crewCap / (double)numCrew) * (1 + vesselHabMultiplier);
UpdateEarliestExpiration(adjustedCabinTimeLeft);

var isScout = c.HasEffect("ExplorerSkill") && homeTimeLeft >= LifeSupportScenario.Instance.settings.GetSettings().ScoutHabTime;
var isPermaHab = homeTimeLeft >= LifeSupportScenario.Instance.settings.GetSettings().PermaHabTime;
// Debug.Log(String.Format("{0} has {1} remaining CabinTime", cls.KerbalName, cabinTimeLeft ));

var isScout = c.HasEffect("ExplorerSkill") && adjustedCabinTimeLeft >= LifeSupportScenario.Instance.settings.GetSettings().ScoutHabTime;
var isPermaHab = adjustedCabinTimeLeft >= LifeSupportScenario.Instance.settings.GetSettings().PermaHabTime;

if (isScout || isPermaHab)
{
crewHomeString = "indefinite";
crewCabinString = "indefinite";
}
else if (homeTimeLeft < 0)
else if (adjustedCabinTimeLeft < 0)
{
lblHome = "FF5E5E";
crewHomeString = "expired";
lblCabin = "FF5E5E";
crewCabinString = "expired";
}
else
{
crewHomeString = LifeSupportUtilities.SmartDurationDisplay(homeTimeLeft);

crewCabinString = LifeSupportUtilities.SmartDurationDisplay(adjustedCabinTimeLeft);
var secondsPerDay = LifeSupportUtilities.SecondsPerDay();
if (homeTimeLeft < secondsPerDay * 30) //15 days
if (adjustedCabinTimeLeft < secondsPerDay * 3)
{
lblHome = "FFE100";
lblCabin = "FFE100";
}
if (homeTimeLeft < secondsPerDay * 15)
if (adjustedCabinTimeLeft < secondsPerDay * 1)
{
lblHome = "FFAE00";
lblCabin = "FFAE00";
}
}
}
HomeLabel = String.Format("<color=#{0}>{1}</color>", lblHome, crewHomeString);
CabinLabel = String.Format("<color=#{0}>{1}</color>", lblCabin, crewCabinString);
}

private string GetRemainingTimeWithGraceLabel(double timeLeft, double graceTime, string graceTimeDisplay, string inGraceTimeMessage, int effectWhenExpires)
Expand Down
Loading