Skip to content

Commit

Permalink
Prep for cosmic radiation stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Adderley authored and Chris Adderley committed Sep 9, 2016
1 parent ab9b36f commit c34620e
Show file tree
Hide file tree
Showing 14 changed files with 687 additions and 291 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Radioactivity0_1_1.zip
2 changes: 1 addition & 1 deletion GameData/Radioactivity/Parts/Science/geiger-counter-1.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
@hideUIwhenUnavailable = True
@rerunnable = True
}

}
2 changes: 1 addition & 1 deletion GameData/Radioactivity/RadioactivityScienceDefs.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ EXPERIMENT_DEFINITION
LaytheInSpaceHigh =The clicking is annoying, so you turn the volume down.
LaytheSrfLanded =

VallInSpaceLow =
VallInSpaceLow =
VallInSpaceHigh =The clicking is annoying, so you turn the volume down.
VallSrfLanded =

Expand Down
257 changes: 0 additions & 257 deletions Source/Radioactivity/KerbalDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,270 +127,13 @@ internal void Save(ConfigNode node)
ConfigNode kNode = kerbal.Value.Save(dbNode);
kNode.AddValue("KerbalName",kerbal.Key);
}

Utils.Log("Kerbal Database: Saving completed!");

}

public void PropagateExposure()
{
double curTime = Planetarium.GetUniversalTime();
for (int i = 0; i < Kerbals.Count ;i++)
{
//Kerbals[i].IrradiateOverTime(curTime);
}
}
}

public enum RadioactivityKerbalState {
Healthy, Sick, Dead, Home
}

// The concept for some of these functions is taken from RosterManager
public class RadioactivityKerbal
{
public double LastUpdate;
public ProtoCrewMember.RosterStatus Status = ProtoCrewMember.RosterStatus.Available;
public ProtoCrewMember.KerbalType CrewType = ProtoCrewMember.KerbalType.Crew;
public Guid VesselID = Guid.Empty;
public Vessel CurrentVessel;
public string VesselName = " ";
public uint PartId; //Probably not required - currently not used.
public int SeatIdx; //Probably not required - currently not used.
public string SeatName = string.Empty; //Probably not required - currently not used.

// Kerbal's total exposure
public double TotalExposure;
// Kerbal's current exposure
public double CurrentExposure;
// Kerbal's health state
public RadioactivityKerbalState HealthState;

public ProtoCrewMember Kerbal { get; set; }
public bool IsNew { get; set; }
public string Name;

public RadioactivityKerbal(string name)
{
Name = name;
TotalExposure = 0d;
CurrentExposure = 0d;
}
// Add radiation to a kerbal
public void Irradiate(Vessel curVessel, double amt)
{
LastUpdate = Planetarium.GetUniversalTime();
CurrentVessel = curVessel;
//TotalExposure = TotalExposure + amt;
CurrentExposure = amt;
}
// Add radiation to a kerbal based on the latest exposure metrics
public void IrradiateOverTime(double curTime)
{
double catchupSeconds = curTime - LastUpdate;
double catchupExposure = catchupSeconds*CurrentExposure;
TotalExposure = TotalExposure + catchupSeconds;
}

public void Simulate(float timeStep)
{
if (Kerbal.rosterStatus == ProtoCrewMember.RosterStatus.Dead || Kerbal.rosterStatus == ProtoCrewMember.RosterStatus.Missing)
{
CurrentVessel = null;
KerbalTracking.Instance.KerbalDB.RemoveKerbal(this);

return;
}
if (Kerbal.rosterStatus == ProtoCrewMember.RosterStatus.Available)
{

CurrentVessel = null;
//HealthState = RadioactivityKerbalState.Home;
TotalExposure = TotalExposure - RadioactivitySettings.kerbalHealRateKSC*timeStep;
if (TotalExposure < 0d)
TotalExposure = 0d;

} else
{
if (CurrentExposure <= RadioactivitySettings.kerbalHealThreshold)
{
TotalExposure = TotalExposure - RadioactivitySettings.kerbalHealRate*timeStep;
if (TotalExposure < 0d)
TotalExposure = 0d;
}
else
{
TotalExposure = TotalExposure + CurrentExposure*timeStep;
}
}
if (RadioactivitySettings.enableKerbalEffects)
HandleExposure();
}


void HandleExposure()
{
if (TotalExposure >= RadioactivitySettings.kerbalSicknessThreshold)
{
if (HealthState != RadioactivityKerbalState.Sick && HealthState != RadioactivityKerbalState.Dead)
{
Sicken();
return;
}
//Utils.LogWarning(Name + " died of radiation exposure");
}
if (TotalExposure >= RadioactivitySettings.kerbalDeathThreshold)
{
if (HealthState != RadioactivityKerbalState.Dead)
{
Die();
return;
}
//Utils.LogWarning(Name + " got radiation sickness");
}
if (TotalExposure < RadioactivitySettings.kerbalSicknessThreshold)
{
if (HealthState == RadioactivityKerbalState.Sick)
{
Heal();
}
//Utils.LogWarning(Name + " got radiation sickness");

}
}

void Sicken()
{
HealthState = RadioactivityKerbalState.Sick;
ScreenMessages.PostScreenMessage(new ScreenMessage(String.Format("{0} now has radiation sickness", Name), 4.0f, ScreenMessageStyle.UPPER_CENTER));
if (RadioactivitySettings.debugKerbalEvents)
Utils.LogWarning(String.Format("Kerbals: {0} got radiation sickness", Name));
}
void Heal()
{
HealthState = RadioactivityKerbalState.Healthy;
ScreenMessages.PostScreenMessage(new ScreenMessage(String.Format("{0} recovered from radiation sickness", Name), 4.0f, ScreenMessageStyle.UPPER_CENTER));
if (RadioactivitySettings.debugKerbalEvents)
Utils.LogWarning(String.Format("Kerbals: {0} recovered from radiation sickness", Name));
}

/// "kills" a kerbal
void Die()
{

HealthState = RadioactivityKerbalState.Dead;
KerbalTracking.Instance.KerbalDB.RemoveKerbal(this);
ScreenMessages.PostScreenMessage(new ScreenMessage(String.Format("{0} has died of radiation exposure", Name), 4.0f, ScreenMessageStyle.UPPER_CENTER));

if (CurrentVessel != null && CurrentVessel.isEVA)
{
// If we are EVA, have to handle this more carefully
CurrentVessel.rootPart.Die();
if (RadioactivitySettings.enableKerbalDeath)
{
// Do nothing
} else
{
if (HighLogic.CurrentGame.Parameters.Difficulty.MissingCrewsRespawn)
{
Kerbal.StartRespawnPeriod(2160000.0); // 100 Kerbin days
}
}
if (RadioactivitySettings.debugKerbalEvents)
Utils.LogWarning(String.Format("Kerbals: {0} died on EVA of radiation exposure", Name));
}
else
{
if (Kerbal.rosterStatus == ProtoCrewMember.RosterStatus.Available)
{
Kerbal.Die();
if (HighLogic.CurrentGame.Parameters.Difficulty.MissingCrewsRespawn)
{
Kerbal.StartRespawnPeriod(2160000.0); // 100 Kerbin days
}
if (RadioactivitySettings.debugKerbalEvents)
Utils.LogWarning(String.Format("Kerbals: {0} died at home of radiation exposure", Name));
}
else
{

Part part = CurrentVessel.Parts.Find(p => p.protoModuleCrew.Contains(Kerbal));
if (part != null)
{
part.RemoveCrewmember(Kerbal);
Kerbal.Die();
if (HighLogic.CurrentGame.Parameters.Difficulty.MissingCrewsRespawn)
{
Kerbal.StartRespawnPeriod(2160000.0); // 100 Kerbin days
}
if (RadioactivitySettings.debugKerbalEvents)
Utils.LogWarning(String.Format("Kerbals: {0} died in his vessel of radiation exposure", Name));
//HighLogic.CurrentGame.CrewRoster.RemoveDead(Kerbal);
}
}
}



}

// Load from confignode
public void Load(ConfigNode config, string name)
{

Name = name;
var crewList = HighLogic.CurrentGame.CrewRoster.Crew.Concat(HighLogic.CurrentGame.CrewRoster.Applicants).Concat(HighLogic.CurrentGame.CrewRoster.Tourist).Concat(HighLogic.CurrentGame.CrewRoster.Unowned).ToList();
Kerbal = crewList.FirstOrDefault(a => a.name == name);
//newKerbal.CrewType = Utils.GetValue(config, "Type", ProtoCrewMember.KerbalType.Crew);
LastUpdate = Utils.GetValue(config, "LastUpdate", 0d);
TotalExposure = Utils.GetValue(config, "TotalExposure", 0d);
CurrentExposure = Utils.GetValue(config, "CurrentExposure", 0d);

HealthState = (RadioactivityKerbalState)Enum.Parse(typeof(RadioactivityKerbalState), Utils.GetValue(config, "HealthState", "Healthy"));


VesselID = Utils.GetValue(config, "VesselID", Guid.Empty);
if (Guid.Empty.Equals(VesselID))
{
CurrentVessel = null;
}
else
{

Vessel tryVessel = FlightGlobals.Vessels.FirstOrDefault(a => a.id == VesselID);
if (tryVessel != null && tryVessel.loaded)
{
CurrentVessel = tryVessel;

}
}
}
public void Load(ProtoCrewMember crewMember)
{
Name = crewMember.name;
Kerbal = crewMember;
//Status = crewMember
//newKerbal.CrewType = Utils.GetValue(config, "Type", ProtoCrewMember.KerbalType.Crew);
TotalExposure = 0d;
CurrentExposure = 0d;
HealthState = RadioactivityKerbalState.Healthy;
}

public ConfigNode Save(ConfigNode config)
{
ConfigNode node = config.AddNode(RadioactivitySettings.kerbalConfigNodeName);
node.AddValue("lastUpdate", LastUpdate);
//node.AddValue("Name", Name);
node.AddValue("Status", Status);
node.AddValue("Type", CrewType);
node.AddValue("TotalExposure", TotalExposure);
node.AddValue("CurrentExposure", CurrentExposure);
node.AddValue("HealthState",HealthState.ToString());
node.AddValue("VesselID",VesselID.ToString());
return node;
}


}

}
35 changes: 31 additions & 4 deletions Source/Radioactivity/KerbalTracking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public override void OnLoad(ConfigNode node)
RadioactivitySettings.Load();
Utils.Log("Kerbal Tracking: Done Loading");
databaseReady = true;
// Update the kerbals from the DB if time has passed
//KerbalDB.PropagateExposure();

}

public override void OnSave(ConfigNode node)
Expand All @@ -58,14 +57,42 @@ public void SimulateKerbals(float time)
}
}

public List<RadioactivityKerbal> GetKerbals(List<ProtoCrewMember> crew)
{
List<RadioactivityKerbal> toReturn = new List<RadioactivityKerbal>();

foreach (KeyValuePair<string,RadioactivityKerbal> kerbal in KerbalDB.Kerbals)
{
foreach (ProtoCrewMember crewMember in crew)
{
if (crew == kerbal.Value.Kerbal)
toReturn.Add(kerbal.Value);
}
}
return toReturn;
}

// Irradiates a kerbal
public void IrradiateKerbal(ProtoCrewMember crew, Vessel crewVessel, double amount)
public void IrradiateKerbal(ProtoCrewMember crew, Vessel crewVessel, double pointAmount)
{
foreach (KeyValuePair<string,RadioactivityKerbal> kerbal in KerbalDB.Kerbals)
{
if (crew == kerbal.Value.Kerbal)
kerbal.Value.Irradiate(crewVessel, amount);
kerbal.Value.IrradiatePoint(crewVessel, pointAmount);
}
}
public void IrradiateKerbal(ProtoCrewMember crew, Vessel crewVessel, double pointAmount, double bodyFraction, double skyFraction, double partFraction)
{
foreach (KeyValuePair<string,RadioactivityKerbal> kerbal in KerbalDB.Kerbals)
{
if (crew == kerbal.Value.Kerbal)
{
kerbal.Value.IrradiatePoint(crewVessel, amount);
kerbal.Value.SetAmbientExposure(bodyFraction, skyFraction, partFraction);
}
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ protected void IrradiateCrew(float amt)
for (int i = 0; i < this.part.protoModuleCrew.Count ;i++)
{
KerbalTracking.Instance.IrradiateKerbal(this.part.protoModuleCrew[i], part.vessel, (double)amt);

}
}
}
Expand Down
Loading

0 comments on commit c34620e

Please sign in to comment.