Skip to content

Commit

Permalink
Storm warnings now have a probability + accuracy range. Defaults to p…
Browse files Browse the repository at this point in the history
…erfect accuracy but that can be changed via API
  • Loading branch information
SirMortimer committed Jun 17, 2019
1 parent 6edd14a commit e1599c2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Breaking Grounds DLC: Fixed the science value indication when the transmission is complete (Sir Mortimer)
* Crediting of zero science is now reduced to a minimum. This will fix the constant science updates with mods like Strategia (Sir Mortimer)
* Added mod support to query the radiation model, as well as toggle visibility of belts and magnetopause (Sir Mortimer)
* Added mod support for CME prediction accuracy that influences the probability that you get an advanced warning, and its accuracy (Sir Mortimer)

## v3.0.2 for all versions of KSP from 1.4.0 to 1.7.x

Expand Down
30 changes: 26 additions & 4 deletions src/Kerbalism/Storm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace KERBALISM

public static class Storm
{
public static float sun_observation_quality = 1.0f;

public static void Update(CelestialBody body, double elapsed_s)
{
// do nothing if storms are disabled
Expand Down Expand Up @@ -65,10 +67,14 @@ public static void Update(CelestialBody body, double elapsed_s)
}
else if (bd.msg_storm < 1 && bd.storm_state == 1)
{
if (Body_is_relevant(body))
// show warning message only if you're lucky...
if(Lib.RandomFloat() < sun_observation_quality)
{
Message.Post(Severity.warning, Lib.BuildString("Our observatories report a coronal mass ejection directed toward <b>", body.name, "</b> system"),
Lib.BuildString("Time to impact: ", Lib.HumanReadableDuration(TimeBeforeCME(bd.storm_time, bd.storm_age))));
if (Body_is_relevant(body))
{
Message.Post(Severity.warning, Lib.BuildString("Our observatories report a coronal mass ejection directed toward <b>", body.name, "</b> system"),
Lib.BuildString("Time to impact: ", Lib.HumanReadableDuration(TimeBeforeCME(bd.storm_time, bd.storm_age))));
}
}
bd.msg_storm = 1;
}
Expand Down Expand Up @@ -279,7 +285,23 @@ public static double TimeBeforeCME(Vessel v)
// return time left until CME is over
static double TimeLeftCME(double storm_time, double storm_age)
{
return Math.Max(0.0, storm_time - storm_age);

// get a pseudo-random number ranging from 0 to 1. pseudo-random because it needs
// to be the same number every time we get a remaining time for the same storm
double uncertainity = (storm_time % 100) / 100.0;

// allow under-estimation of storm duration, but tend towards over-estimation
uncertainity -= 0.3;

// accurate sun observation -> less uncertainity
uncertainity *= (1.0 - sun_observation_quality);

double timeLeft = Math.Max(0.0, storm_time - storm_age);

// add uncertainity to returned value
timeLeft += uncertainity * PreferencesStorm.Instance.StormMaxTime;

return timeLeft;
}


Expand Down
20 changes: 20 additions & 0 deletions src/Kerbalism/System/API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,26 @@ public static bool Blackout(Vessel v)
return Cache.VesselInfo(v).blackout;
}

/// <summary>
/// Returns the current sun observation quality (ranges from 0 to 1). this is
/// the probability that the player will get a warning for an incoming CME
/// </summary>
/// <returns>The observation quality.</returns>
public static float StormObservationQuality()
{
return Storm.sun_observation_quality;
}

/// <summary>
/// Set the current sun observation quality (ranges from 0 to 1). this is
/// the probability that the player will get a warning for an incoming CME
/// </summary>
/// <param name="quality">Quality.</param>
public static void SetStormObservationQuality(float quality)
{
Storm.sun_observation_quality = Lib.Clamp(quality, 0.0f, 1.0f);
}

// --- RELIABILITY ----------------------------------------------------------

// return true if at least a component has malfunctioned, or had a critical failure
Expand Down

0 comments on commit e1599c2

Please sign in to comment.