Skip to content

Commit

Permalink
Fix the annoying strategia +0.0 science spam.
Browse files Browse the repository at this point in the history
This is done by introducing a deferred science value cache for transmitted
data with very little value.
  • Loading branch information
SirMortimer committed Jun 15, 2019
1 parent 109c2c0 commit 0eb0c46
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* Support tilted magnetic fields and radiation belts with offsets (Sir Mortimer)
* Updated the RSS radiation model according to http://evildrganymede.net/work/magfield.htm (Sir Mortimer)
* Fixed an error in new science support for DMOS (Sir Mortimer)
* 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)

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

Expand Down
6 changes: 3 additions & 3 deletions src/Kerbalism/Science/Drive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void Delete_file(string subject_id, double amount, ProtoVessel pv)

if(file.buff > double.Epsilon && pv != null)
{
Science.Credit(subject_id, file.buff, true, pv);
Science.Credit(subject_id, file.buff, true, pv, true);
file.buff = 0;
}

Expand Down Expand Up @@ -494,7 +494,7 @@ public static void Purge(ProtoVessel proto_vessel)
if (p.Value.buff > double.Epsilon)
{
Lib.Log("Purge, crediting " + p.Key + " of " + p.Value.buff);
Science.Credit(p.Key, p.Value.buff, true, proto_vessel);
Science.Credit(p.Key, p.Value.buff, true, proto_vessel, true);
}
}
}
Expand Down Expand Up @@ -528,7 +528,7 @@ public static void Purge(Vessel vessel)
{
if(p.Value.buff > double.Epsilon)
{
Science.Credit(p.Key, p.Value.buff, true, vessel.protoVessel);
Science.Credit(p.Key, p.Value.buff, true, vessel.protoVessel, true);
}
}
}
Expand Down
62 changes: 61 additions & 1 deletion src/Kerbalism/Science/Science.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,58 @@ public static string Transmitting(Vessel v, bool linked)
return string.Empty;
}

public static void ClearDeferred()
{
deferredCredit.Clear();
}

public static void CreditAllDeferred()
{
foreach(var deferred in deferredCredit.Values)
{
Credit(deferred.subject_id, deferred.size, true, deferred.pv, true);
}
deferredCredit.Clear();
}

private static void CreditDeferred(string subject_id, double size, ProtoVessel pv)
{
if (deferredCredit.ContainsKey(subject_id))
{
var deferred = deferredCredit[subject_id];
deferred.size += size;
deferred.pv = pv;

var credits = Value(subject_id, deferred.size);
if(credits >= buffer_science_value)
{
deferredCredit.Remove(subject_id);
Credit(subject_id, deferred.size, true, pv, true);
}
}
else
{
deferredCredit.Add(subject_id, new DeferredCreditValues(subject_id, size, pv));
}
}

// credit science for the experiment subject specified
public static float Credit(string subject_id, double size, bool transmitted, ProtoVessel pv)
public static float Credit(string subject_id, double size, bool transmitted, ProtoVessel pv, bool enforced_credit = false)
{
var credits = Value(subject_id, size);

if(!enforced_credit && transmitted && credits < buffer_science_value) {
CreditDeferred(subject_id, size, pv);
return credits;
}

if(deferredCredit.ContainsKey(subject_id)) {
var deferred = deferredCredit[subject_id];
size += deferred.size;
deferred.size = 0;
credits = Value(subject_id, size);
}

// credit the science
var subject = ResearchAndDevelopment.GetSubjectByID(subject_id);
if(subject == null)
Expand Down Expand Up @@ -651,6 +697,20 @@ public static double GetSampleMass(string experiment_id)
static readonly Dictionary<string, ExperimentInfo> experiments = new Dictionary<string, ExperimentInfo>();
readonly static Dictionary<string, double> sampleMasses = new Dictionary<string, double>();

private class DeferredCreditValues {
internal string subject_id;
internal double size;
internal ProtoVessel pv;

public DeferredCreditValues(string subject_id, double size, ProtoVessel pv)
{
this.subject_id = subject_id;
this.size = size;
this.pv = pv;
}
}

static readonly Dictionary<string, DeferredCreditValues> deferredCredit = new Dictionary<string, DeferredCreditValues>();
}

} // KERBALISM
Expand Down
8 changes: 4 additions & 4 deletions src/Kerbalism/System/Callbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Callbacks()
GameEvents.onGUILaunchScreenSpawn.Add((_) => visible = false);
GameEvents.onGUILaunchScreenDespawn.Add(() => visible = true);

GameEvents.onGameSceneSwitchRequested.Add((_) => { visible = false; Cache.PurgeObjects(); });
GameEvents.onGameSceneSwitchRequested.Add((_) => { visible = false; Cache.PurgeObjects(); Science.CreditAllDeferred(); });
GameEvents.onGUIApplicationLauncherReady.Add(() => visible = true);

GameEvents.CommNet.OnNetworkInitialized.Add(() => Kerbalism.Fetch.StartCoroutine(NetworkInitialized()));
Expand Down Expand Up @@ -193,7 +193,7 @@ void VesselRecoveryProcessing(ProtoVessel v, MissionRecoveryDialog dialog, float
ScienceSubject subject = ResearchAndDevelopment.GetSubjectByID(filename);

// credit science
float credits = Science.Credit(filename, file.size, false, v);
float credits = Science.Credit(filename, file.size, false, v, true);

// create science widged
ScienceSubjectWidget widged = ScienceSubjectWidget.Create
Expand Down Expand Up @@ -223,7 +223,7 @@ void VesselRecoveryProcessing(ProtoVessel v, MissionRecoveryDialog dialog, float
ScienceSubject subject = ResearchAndDevelopment.GetSubjectByID(filename);

// credit science
float credits = Science.Credit(filename, sample.size, false, v);
float credits = Science.Credit(filename, sample.size, false, v, true);

// create science widged
ScienceSubjectWidget widged = ScienceSubjectWidget.Create
Expand Down Expand Up @@ -369,7 +369,7 @@ void PartDestroyed(Part p)
{
if(pair.Value.buff > double.Epsilon)
{
Science.Credit(pair.Key, pair.Value.buff, true, p.vessel.protoVessel);
Science.Credit(pair.Key, pair.Value.buff, true, p.vessel.protoVessel, true);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Kerbalism/System/Kerbalism.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,14 @@ public override void OnLoad(ConfigNode node)
// remember savegame id
savegame_uid = DB.uid;
}

Science.ClearDeferred();
}

public override void OnSave(ConfigNode node)
{
// serialize data
Science.CreditAllDeferred();
DB.Save(node);
}

Expand Down

0 comments on commit 0eb0c46

Please sign in to comment.