Skip to content

Commit

Permalink
For completed contracts, also check completed programs to see if this…
Browse files Browse the repository at this point in the history
… contract was optional. NOTE: There's a complication, where a contract is optional in program A, required in program B, and program A is completed and program B is active. The problem is that contract state is set _before_ awarding rewards, so we have to be careful about that case and not check completed programs then. Close #2082
  • Loading branch information
NathanKell committed Jul 2, 2023
1 parent 31714cd commit 56daec0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Source/Harmony/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal static bool Postfix_TextAward(ref string __result, string title, string
if (_contract == null)
return true;

if (Programs.ProgramHandler.Instance != null && Programs.ProgramHandler.Instance.RepToConfidenceForContract(_contract) is float repToConf && repToConf > 0f)
if (Programs.ProgramHandler.Instance != null && Programs.ProgramHandler.Instance.RepToConfidenceForContract(_contract, _isReward) is float repToConf && repToConf > 0f)
{
var cmq = CurrencyModifierQueryRP0.RunQuery(TransactionReasonsRP0.ContractReward, 0d, 0d, 0d, _contract.ReputationCompletion * repToConf, 0d);
value += $"<color={CurrencyModifierQueryRP0.CurrencyColor(CurrencyRP0.Confidence)}>{CurrencyModifierQueryRP0.SpriteString(CurrencyRP0.Confidence)} {cmq.GetTotal(CurrencyRP0.Confidence):N0} {cmq.GetEffectDeltaText(CurrencyRP0.Confidence, "N0", CurrencyModifierQuery.TextStyling.OnGUI)} </color>";
Expand Down
4 changes: 2 additions & 2 deletions Source/Harmony/ContractParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static void Prefix_SendStateMessage(ContractParameter __instance, ref s
if (icon != MessageSystemButton.ButtonIcons.COMPLETE || !__instance.Optional)
return;

if (__instance.Root is ContractConfigurator.ConfiguredContract cc && Programs.ProgramHandler.Instance != null && Programs.ProgramHandler.Instance.RepToConfidenceForContract(cc) is float repToConf && repToConf > 0f)
if (__instance.Root is ContractConfigurator.ConfiguredContract cc && Programs.ProgramHandler.Instance != null && Programs.ProgramHandler.Instance.RepToConfidenceForContract(cc, true) is float repToConf && repToConf > 0f)
{
var cmq = CurrencyModifierQueryRP0.RunQuery(TransactionReasonsRP0.ContractReward, 0d, 0d, 0d, _storedRep * repToConf, 0d);
message += $"<color={CurrencyModifierQueryRP0.CurrencyColor(CurrencyRP0.Confidence)}>{CurrencyModifierQueryRP0.SpriteString(CurrencyRP0.Confidence)} {cmq.GetTotal(CurrencyRP0.Confidence):N0} {cmq.GetEffectDeltaText(CurrencyRP0.Confidence, "N0", CurrencyModifierQuery.TextStyling.OnGUI)} </color>";
Expand All @@ -30,7 +30,7 @@ internal static void Prefix_AwardCompletion(ContractParameter __instance)
{
//KSP sets the rewards to 0 as part of this!
//So we have to intercept and store the value.
if (__instance.Root is ContractConfigurator.ConfiguredContract cc && Programs.ProgramHandler.Instance != null && Programs.ProgramHandler.Instance.RepToConfidenceForContract(cc) is float repToConf && repToConf > 0f)
if (__instance.Root is ContractConfigurator.ConfiguredContract cc && Programs.ProgramHandler.Instance != null && Programs.ProgramHandler.Instance.RepToConfidenceForContract(cc, true) is float repToConf && repToConf > 0f)
{
_storedRep = __instance.ReputationCompletion;
}
Expand Down
15 changes: 13 additions & 2 deletions Source/Programs/ProgramHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ internal void OnGUI()
}
}

public float RepToConfidenceForContract(ConfiguredContract cc)
public float RepToConfidenceForContract(ConfiguredContract cc, bool isAwarding)
{
foreach (Program p in ActivePrograms)
{
Expand All @@ -224,6 +224,17 @@ public float RepToConfidenceForContract(ConfiguredContract cc)
return p.RepToConfidence;
}
}
if (isAwarding || cc.ContractState != Contract.State.Completed)
return 0f;

// Since it's completed (and this is not part of the awarding step), check completed programs too.
foreach (Program p in CompletedPrograms)
{
if (p.optionalContracts.Contains(cc.contractType.name))
{
return p.RepToConfidence;
}
}

return 0f;
}
Expand Down Expand Up @@ -263,7 +274,7 @@ private IEnumerator ContractCompleteRoutine(Contract data)
KerbalConstructionTime.KerbalConstructionTimeData.Instance.Applicants += applicants;

// Handle Confidence
float repToConf = RepToConfidenceForContract(cc);
float repToConf = RepToConfidenceForContract(cc, true);
if (repToConf > 0f)
{
float rep = 0;
Expand Down

0 comments on commit 56daec0

Please sign in to comment.