Skip to content

Commit d858c70

Browse files
committed
FIX: Tech Transfer being lost on SECOND flight with new engines
FIX: Maximum tech transfer is now respected NEW: Completely reworked how flight data is stored. It is not split into multiple pools, for "tech transfer", "research", and "flight". This simplifies many aspects and fixes a lot of bugs regarding RnD and Tech Transfer.
1 parent 675aaae commit d858c70

File tree

6 files changed

+96
-19
lines changed

6 files changed

+96
-19
lines changed
1.5 KB
Binary file not shown.
1 KB
Binary file not shown.
0 Bytes
Binary file not shown.
2.5 KB
Binary file not shown.

TestFlightCore/TestFlightCore/TestFlight.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,22 @@ public float GetFlightDataForPartName(string partName)
796796
else
797797
return -1f;
798798
}
799+
// This is a utility method that will return the "transferData" value directly or -1 if not found
800+
public float GetTransferDataForPartName(string partName)
801+
{
802+
if (partData.ContainsKey(partName))
803+
return partData[partName].GetFloat("transferData");
804+
else
805+
return -1f;
806+
}
807+
// This is a utility method that will return the "researchData" value directly or -1 if not found
808+
public float GetResearchDataForPartName(string partName)
809+
{
810+
if (partData.ContainsKey(partName))
811+
return partData[partName].GetFloat("researchData");
812+
else
813+
return -1f;
814+
}
799815

800816
// New noscope Format
801817
// This is a utility method that sets the "flightData" value directly
@@ -811,6 +827,32 @@ public void SetFlightDataForPartName(string partName, float data)
811827
partData.Add(partName, newData);
812828
}
813829
}
830+
// This is a utility method that sets the "transferData" value directly
831+
public void SetTransferDataForPartName(string partName, float data)
832+
{
833+
if (partData.ContainsKey(partName))
834+
partData[partName].SetValue("transferData", data.ToString());
835+
else
836+
{
837+
TestFlightPartData newData = new TestFlightPartData();
838+
newData.PartName = partName;
839+
newData.SetValue("transferData", data.ToString());
840+
partData.Add(partName, newData);
841+
}
842+
}
843+
// This is a utility method that sets the "researchData" value directly
844+
public void SetResearchDataForPartName(string partName, float data)
845+
{
846+
if (partData.ContainsKey(partName))
847+
partData[partName].SetValue("researchData", data.ToString());
848+
else
849+
{
850+
TestFlightPartData newData = new TestFlightPartData();
851+
newData.PartName = partName;
852+
newData.SetValue("researchData", data.ToString());
853+
partData.Add(partName, newData);
854+
}
855+
}
814856
// This is a utility method that adds the "flightData" value directly
815857
public void AddFlightDataForPartName(string partName, float data)
816858
{
@@ -824,6 +866,32 @@ public void AddFlightDataForPartName(string partName, float data)
824866
partData.Add(partName, newData);
825867
}
826868
}
869+
// This is a utility method that adds the "transferData" value directly
870+
public void AddTransferDataForPartName(string partName, float data)
871+
{
872+
if (partData.ContainsKey(partName))
873+
partData[partName].AddValue("transferData", data);
874+
else
875+
{
876+
TestFlightPartData newData = new TestFlightPartData();
877+
newData.PartName = partName;
878+
newData.SetValue("transferData", data);
879+
partData.Add(partName, newData);
880+
}
881+
}
882+
// This is a utility method that adds the "researchData" value directly
883+
public void AddResearchDataForPartName(string partName, float data)
884+
{
885+
if (partData.ContainsKey(partName))
886+
partData[partName].AddValue("researchData", data);
887+
else
888+
{
889+
TestFlightPartData newData = new TestFlightPartData();
890+
newData.PartName = partName;
891+
newData.SetValue("researchData", data);
892+
partData.Add(partName, newData);
893+
}
894+
}
827895

828896
public override void OnLoad(ConfigNode node)
829897
{

TestFlightCore/TestFlightCore/TestFlightCore.cs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public class TestFlightCore : PartModuleExtended, ITestFlightCore
5050
[KSPField(isPersistant=true)]
5151
public double lastMET;
5252
[KSPField(isPersistant=true)]
53-
public bool initialized = false;
54-
[KSPField(isPersistant=true)]
5553
public float currentFlightData;
5654
[KSPField(isPersistant=true)]
5755
public float initialFlightData;
@@ -66,6 +64,10 @@ public class TestFlightCore : PartModuleExtended, ITestFlightCore
6664
public ConfigNode currentConfig;
6765
public string configNodeData;
6866

67+
bool initialized = false;
68+
float transferData;
69+
float researchData;
70+
6971
private double baseFailureRate;
7072
// We store the base, or initial, flight data for calculation of Base Failure Rate
7173
// Momentary Failure Rates are calculated based on modifiers. Those modifiers
@@ -558,10 +560,9 @@ public double FailureRateToMTBF(double failureRate, TestFlightUtil.MTBFUnits uni
558560
// New v1.3 noscope
559561
public float GetFlightData()
560562
{
561-
if (currentFlightData > maxData)
562-
currentFlightData = maxData;
563-
564-
return currentFlightData;
563+
var data = currentFlightData + researchData + transferData;
564+
data = Mathf.Min(data, maxData);
565+
return data;
565566
}
566567
public float GetInitialFlightData()
567568
{
@@ -629,7 +630,7 @@ public float ModifyFlightData(float modifier, bool additive)
629630
// The flight data as it stands before modification
630631
float existingData = currentFlightData;
631632
// Amount of data stored in the scenario store
632-
float existingStoredFlightData = TestFlightManagerScenario.Instance.GetFlightDataForPartName(Alias);
633+
float existingStoredFlightData = Mathf.Max(0,TestFlightManagerScenario.Instance.GetFlightDataForPartName(Alias));
633634

634635
// Calculate the new flight data
635636
if (additive)
@@ -986,6 +987,8 @@ public override void OnDestroy()
986987

987988
public override void OnAwake()
988989
{
990+
initialized = false;
991+
989992
if (!string.IsNullOrEmpty(configNodeData))
990993
{
991994
var node = ConfigNode.Parse(configNodeData);
@@ -1039,19 +1042,19 @@ public void InitializeFlightData(float flightData)
10391042
{
10401043
if (initialized)
10411044
return;
1042-
1043-
if (flightData == 0f)
1044-
flightData = AttemptTechTransfer();
1045-
1046-
if (startFlightData > flightData)
1047-
{
1048-
TestFlightManagerScenario.Instance.AddFlightDataForPartName(Alias, startFlightData);
1049-
flightData = startFlightData;
1050-
}
10511045

1046+
researchData = Mathf.Min(TestFlightManagerScenario.Instance.GetResearchDataForPartName(Alias), rndMaxData);
1047+
if (researchData < 0f) researchData = 0f;
1048+
transferData = AttemptTechTransfer();
1049+
if (transferData < 0f) transferData = 0f;
1050+
TestFlightManagerScenario.Instance.SetTransferDataForPartName(Alias, transferData);
1051+
1052+
flightData = Mathf.Max(startFlightData, flightData);
1053+
10521054
currentFlightData = flightData;
1053-
initialFlightData = flightData;
1055+
initialFlightData = flightData + researchData + transferData;
10541056

1057+
TestFlightManagerScenario.Instance.SetFlightDataForPartName(Alias, flightData);
10551058
missionStartTime = Planetarium.GetUniversalTime();
10561059

10571060
initialized |= HighLogic.LoadedSceneIsFlight;
@@ -1069,13 +1072,16 @@ internal float AttemptTechTransfer()
10691072
// multiple branches can be specified with the & character
10701073
// multiple parts in a branch can be specified by separating them with a comma
10711074
// for each branch the first part listed is considered the closest part, and each part after is considered to be one generation removed. An optional generation penalty is added for each level
1072-
// for each branch, the flight data from each part is added together including any generation penalties, to create a total for that branch, modifed by the transfer amount for that branch
1075+
// for each branch, the flight data from each part is added together including any generation penalties, to create a total for that branch, modified by the transfer amount for that branch
10731076
// if multiple branches are specified, each branch is then added together
10741077
// an optional maximum data can be enforced for each scope (global setting but applied to each scope, not total)
10751078
// Example
10761079
// techTransfer = rs-27a,rs-27,h1-b,h1:10&lr-89-na-7,lr-89-na-6,lr-89-na-5:25
10771080
// defines two branches, one from the RS-27 branch and one from the LR-89 branch.
10781081

1082+
Debug.Log($"[TestFlight] Attempting TechTransfer for part {Configuration}");
1083+
Debug.Log($"[TestFlight] techTransfer: {techTransfer.Trim()}");
1084+
10791085
if (techTransfer.Trim() == "")
10801086
return 0f;
10811087

@@ -1088,6 +1094,7 @@ internal float AttemptTechTransfer()
10881094
for (int i = 0, branchesLength = branches.Length; i < branchesLength; i++)
10891095
{
10901096
string branch = branches[i];
1097+
Debug.Log($"[TestFlight] Processing Branch {branch}");
10911098
modifiers = branch.Split(new char[1] {
10921099
':'
10931100
});
@@ -1103,14 +1110,16 @@ internal float AttemptTechTransfer()
11031110
{
11041111
string partName = partsInBranch[j];
11051112
float partFlightData = TestFlightManagerScenario.Instance.GetFlightDataForPartName(partName);
1113+
Debug.Log($"[TestFlight] Existing data for {partName}: {partFlightData}");
11061114
if (partFlightData == 0f)
11071115
continue;
11081116
dataToTransfer = dataToTransfer + ((partFlightData - (partFlightData * generation * techTransferGenerationPenalty)) * branchModifier);
11091117
generation++;
11101118
}
11111119
}
11121120

1113-
return dataToTransfer;
1121+
Debug.Log($"[TestFlight] Data to Transfer: {dataToTransfer}");
1122+
return Mathf.Min(dataToTransfer, techTransferMax);
11141123
}
11151124

11161125
public float ForceRepair(ITestFlightFailure failure)

0 commit comments

Comments
 (0)