From 79f1f5e42302ce7df782d4feb3e88c7277a6ecee Mon Sep 17 00:00:00 2001 From: siimav Date: Fri, 30 Jun 2023 02:36:46 +0300 Subject: [PATCH] Log individual maintenance components & many more --- Source/CareerLog/CareerLog.cs | 58 ++++++++++++------- Source/CareerLog/CareerLogDto.cs | 18 ++++++ Source/CareerLog/LogPeriod.cs | 27 +++++++++ .../KerbalConstructionTime.cs | 2 +- .../Utilities/Utilities.cs | 19 +++--- Source/Maintenance/MaintenanceHandler.cs | 51 ++++++++++------ Source/UnlockCreditHandler.cs | 4 ++ 7 files changed, 131 insertions(+), 48 deletions(-) diff --git a/Source/CareerLog/CareerLog.cs b/Source/CareerLog/CareerLog.cs index b15ed822625..bac55625c56 100644 --- a/Source/CareerLog/CareerLog.cs +++ b/Source/CareerLog/CareerLog.cs @@ -68,6 +68,8 @@ public LogPeriod CurrentPeriod { get { + if (!IsEnabled) return null; + double time = KSPUtils.GetUT(); while (time > NextPeriodStart) { @@ -598,12 +600,21 @@ private CareerLogDto CreateLogDto(LogPeriod logPeriod) currentSci = logPeriod.CurrentSci, rndQueueLength = logPeriod.RnDQueueLength, scienceEarned = logPeriod.ScienceEarned, + salaryEngineers = logPeriod.SalaryEngineers, + salaryResearchers = logPeriod.SalaryResearchers, + salaryCrew = logPeriod.SalaryCrew, programFunds = logPeriod.ProgramFunds, otherFundsEarned = logPeriod.OtherFundsEarned, launchFees = logPeriod.LaunchFees, + vesselPurchase = logPeriod.VesselPurchase, + vesselRecovery = logPeriod.VesselRecovery, + lcMaintenance = logPeriod.LCMaintenance, + facilityMaintenance = logPeriod.FacilityMaintenance, maintenanceFees = logPeriod.MaintenanceFees, + trainingFees = logPeriod.TrainingFees, toolingFees = logPeriod.ToolingFees, entryCosts = logPeriod.EntryCosts, + spentUnlockCredit = logPeriod.SpentUnlockCredit, constructionFees = logPeriod.ConstructionFees, otherFees = logPeriod.OtherFees, subsidySize = logPeriod.SubsidySize, @@ -677,7 +688,7 @@ private void SettingsChanged() private void CurrenciesModified(CurrencyModifierQuery query) { - if (CareerEventScope.ShouldIgnore) return; + if (CareerEventScope.ShouldIgnore || !IsEnabled) return; float fundsDelta = query.GetTotal(Currency.Funds); if (fundsDelta != 0f) @@ -694,16 +705,11 @@ private void CurrenciesModified(CurrencyModifierQuery query) private void FundsChanged(float changeDelta, TransactionReasons reason) { + TransactionReasonsRP0 reasonRP0 = reason.RP0(); + _prevFundsChangeAmount = changeDelta; _prevFundsChangeReason = reason; - if (reason == TransactionReasons.ContractPenalty || reason == TransactionReasons.ContractDecline || - reason == TransactionReasons.ContractAdvance || reason == TransactionReasons.ContractReward) - { - CurrentPeriod.ContractRewards += changeDelta; - return; - } - if (reason == TransactionReasons.Mission) { CurrentPeriod.ProgramFunds += changeDelta; @@ -722,19 +728,31 @@ private void FundsChanged(float changeDelta, TransactionReasons reason) return; } - if (reason == TransactionReasons.VesselRollout || reason == TransactionReasons.VesselRecovery) + if (reasonRP0 == TransactionReasonsRP0.VesselPurchase) + { + CurrentPeriod.VesselPurchase -= changeDelta; + return; + } + + if (reasonRP0 == TransactionReasonsRP0.Rollouts) { CurrentPeriod.LaunchFees -= changeDelta; return; } - if (reason == TransactionReasonsRP0.PartOrUpgradeUnlock.Stock()) + if (reasonRP0 == TransactionReasonsRP0.VesselRecovery) + { + CurrentPeriod.VesselRecovery += changeDelta; + return; + } + + if (reasonRP0 == TransactionReasonsRP0.PartOrUpgradeUnlock) { CurrentPeriod.EntryCosts -= changeDelta; return; } - if (reason == TransactionReasons.StructureConstruction) + if (reasonRP0 == TransactionReasonsRP0.StructureConstruction) { CurrentPeriod.ConstructionFees -= changeDelta; return; @@ -761,7 +779,7 @@ private void RepChanged(float repDelta, TransactionReasons reason) private void ContractAccepted(Contract c) { - if (CareerEventScope.ShouldIgnore || c.AutoAccept) return; // Do not record the Accept event for record contracts + if (CareerEventScope.ShouldIgnore || !IsEnabled || c.AutoAccept) return; // Do not record the Accept event for record contracts _contractDict.Add(new ContractEvent(KSPUtils.GetUT()) { @@ -775,7 +793,7 @@ private void ContractAccepted(Contract c) private void ContractCompleted(Contract c) { - if (CareerEventScope.ShouldIgnore) return; + if (CareerEventScope.ShouldIgnore || !IsEnabled) return; _contractDict.Add(new ContractEvent(KSPUtils.GetUT()) { @@ -789,7 +807,7 @@ private void ContractCompleted(Contract c) private void ContractCancelled(Contract c) { - if (CareerEventScope.ShouldIgnore) return; + if (CareerEventScope.ShouldIgnore || !IsEnabled) return; // KSP first takes the contract penalty and then fires the contract events double fundsChange = 0; @@ -811,7 +829,7 @@ private void ContractCancelled(Contract c) private void ContractFailed(Contract c) { - if (CareerEventScope.ShouldIgnore) return; + if (CareerEventScope.ShouldIgnore || !IsEnabled) return; string internalName = GetContractInternalName(c); double ut = KSPUtils.GetUT(); @@ -848,7 +866,7 @@ public void ProgramCompleted(Program p) private void VesselSituationChange(GameEvents.HostedFromToAction ev) { - if (CareerEventScope.ShouldIgnore) return; + if (CareerEventScope.ShouldIgnore || !IsEnabled) return; // KJR can clobber the vessel back to prelaunch state in case of clamp wobble. Need to exclude such events. if (!_launched && ev.from == Vessel.Situations.PRELAUNCH && ev.host == FlightGlobals.ActiveVessel) @@ -870,7 +888,7 @@ private void VesselSituationChange(GameEvents.HostedFromToAction c.name == data.sender)) // Do not count tourist/test animal deaths { @@ -902,8 +920,6 @@ private float GetSciPointTotalFromKCT() private void OnKctTechCompleted(TechItem tech) { - if (CareerEventScope.ShouldIgnore) return; - AddTechEvent(tech); } @@ -987,7 +1003,7 @@ private void AddFacilityConstructionEvent(FacilityUpgrade data, ConstructionStat private void AddLCConstructionEvent(LCConstruction data, LCItem lc, ConstructionState state) { - if (CareerEventScope.ShouldIgnore) return; + if (CareerEventScope.ShouldIgnore || !IsEnabled) return; Guid modId = data?.modId ?? lc.ModID; // Should only happen when LCs are created through code and thus do not have Construction items if (!_lcs.Any(logLC => logLC.ModID == modId)) @@ -1007,7 +1023,7 @@ private void AddLCConstructionEvent(LCConstruction data, LCItem lc, Construction private void AddPadConstructionEvent(PadConstruction data, KCT_LaunchPad lp, ConstructionState state) { - if (CareerEventScope.ShouldIgnore) return; + if (CareerEventScope.ShouldIgnore || !IsEnabled) return; Guid id = data?.id ?? lp.id; LCItem lc = data?.LC ?? lp.LC; diff --git a/Source/CareerLog/CareerLogDto.cs b/Source/CareerLog/CareerLogDto.cs index 21844196f05..b941f47cf08 100644 --- a/Source/CareerLog/CareerLogDto.cs +++ b/Source/CareerLog/CareerLogDto.cs @@ -19,12 +19,21 @@ internal class CareerLogDto public double currentSci; public int rndQueueLength; public double scienceEarned; + public double salaryEngineers; + public double salaryResearchers; + public double salaryCrew; public double programFunds; public double otherFundsEarned; public double launchFees; + public double vesselPurchase; + public double vesselRecovery; + public double lcMaintenance; + public double facilityMaintenance; public double maintenanceFees; + public double trainingFees; public double toolingFees; public double entryCosts; + public double spentUnlockCredit; public double constructionFees; public double otherFees; public double subsidySize; @@ -49,12 +58,21 @@ public override string ToString() $"{nameof(currentSci)}: {currentSci}, " + $"{nameof(rndQueueLength)}: {rndQueueLength}, " + $"{nameof(scienceEarned)}: {scienceEarned}, " + + $"{nameof(salaryEngineers)}: {salaryEngineers}, " + + $"{nameof(salaryResearchers)}: {salaryResearchers}, " + + $"{nameof(salaryCrew)}: {salaryCrew}, " + $"{nameof(programFunds)}: {programFunds}, " + $"{nameof(otherFundsEarned)}: {otherFundsEarned}, " + $"{nameof(launchFees)}: {launchFees}, " + + $"{nameof(vesselPurchase)}: {vesselPurchase}, " + + $"{nameof(vesselRecovery)}: {vesselRecovery}, " + + $"{nameof(lcMaintenance)}: {lcMaintenance}, " + + $"{nameof(facilityMaintenance)}: {facilityMaintenance}, " + $"{nameof(maintenanceFees)}: {maintenanceFees}, " + + $"{nameof(trainingFees)}: {trainingFees}, " + $"{nameof(toolingFees)}: {toolingFees}, " + $"{nameof(entryCosts)}: {entryCosts}, " + + $"{nameof(spentUnlockCredit)}: {spentUnlockCredit}, " + $"{nameof(constructionFees)}: {constructionFees}, " + $"{nameof(otherFees)}: {otherFees}, " + $"{nameof(subsidySize)}: {subsidySize}, " + diff --git a/Source/CareerLog/LogPeriod.cs b/Source/CareerLog/LogPeriod.cs index 7e6f2445cfb..8adfcaaeedb 100644 --- a/Source/CareerLog/LogPeriod.cs +++ b/Source/CareerLog/LogPeriod.cs @@ -29,18 +29,45 @@ public class LogPeriod : IConfigNode [Persistent] public double ScienceEarned; + [Persistent] + public double SalaryEngineers; + + [Persistent] + public double SalaryResearchers; + + [Persistent] + public double SalaryCrew; + [Persistent] public double LaunchFees; + [Persistent] + public double VesselPurchase; + + [Persistent] + public double VesselRecovery; + + [Persistent] + public double LCMaintenance; + + [Persistent] + public double FacilityMaintenance; + [Persistent] public double MaintenanceFees; + [Persistent] + public double TrainingFees; + [Persistent] public double ToolingFees; [Persistent] public double EntryCosts; + [Persistent] + public double SpentUnlockCredit; + [Persistent] public double ConstructionFees; diff --git a/Source/KerbalConstructionTime/KerbalConstructionTime.cs b/Source/KerbalConstructionTime/KerbalConstructionTime.cs index 173429f952c..574b625d797 100644 --- a/Source/KerbalConstructionTime/KerbalConstructionTime.cs +++ b/Source/KerbalConstructionTime/KerbalConstructionTime.cs @@ -824,7 +824,7 @@ public static void PopUpVesselError(List errored) foreach (BuildListVessel blv in errored) { blv.RemoveFromBuildList(out _); - Utilities.AddFunds(blv.GetTotalCost(), TransactionReasons.VesselRollout); + Utilities.AddFunds(blv.GetTotalCost(), RP0.TransactionReasonsRP0.VesselPurchase); //remove any associated recon_rollout } }); diff --git a/Source/KerbalConstructionTime/Utilities/Utilities.cs b/Source/KerbalConstructionTime/Utilities/Utilities.cs index 4c27c1b6b06..8f5cd0915e8 100644 --- a/Source/KerbalConstructionTime/Utilities/Utilities.cs +++ b/Source/KerbalConstructionTime/Utilities/Utilities.cs @@ -397,11 +397,7 @@ public static double SpendFunds(double toSpend, TransactionReasons reason) public static double SpendFunds(double toSpend, TransactionReasonsRP0 reason) { - if (!CurrentGameIsCareer()) - return 0; - KCTDebug.Log($"Removing funds: {toSpend}, New total: {Funding.Instance.Funds - toSpend}"); - Funding.Instance.AddFunds(-toSpend, reason.Stock()); - return Funding.Instance.Funds; + return SpendFunds(toSpend, reason.Stock()); } public static double AddFunds(double toAdd, TransactionReasons reason) @@ -413,6 +409,11 @@ public static double AddFunds(double toAdd, TransactionReasons reason) return Funding.Instance.Funds; } + public static double AddFunds(double toAdd, TransactionReasonsRP0 reason) + { + return AddFunds(toAdd, reason.Stock()); + } + public static void ProcessSciPointTotalChange(float changeDelta) { // Earned point totals shouldn't decrease. This would only make sense when done through the cheat menu. @@ -540,7 +541,7 @@ public static void TryAddVesselToBuildList(BuildListVessel blv, bool skipPartChe public static void AddVesselToBuildList(BuildListVessel blv) { - SpendFunds(blv.GetTotalCost(), TransactionReasons.VesselRollout); + SpendFunds(blv.GetTotalCost(), TransactionReasonsRP0.VesselPurchase); if (blv.Type == BuildListVessel.ListType.SPH) blv.launchSite = "Runway"; @@ -596,11 +597,11 @@ public static void TrySaveShipEdits(BuildListVessel editableShip) usedShipsCost += v.GetTotalCost(); v.RemoveFromBuildList(out _); } - AddFunds(usedShipsCost, TransactionReasons.VesselRollout); + AddFunds(usedShipsCost, TransactionReasonsRP0.VesselPurchase); var validator = new VesselBuildValidator(); validator.SuccessAction = (postEditShip2) => SaveShipEdits(editableShip, postEditShip2); - validator.FailureAction = () => SpendFunds(usedShipsCost, TransactionReasons.VesselRollout); + validator.FailureAction = () => SpendFunds(usedShipsCost, TransactionReasonsRP0.VesselPurchase); validator.ProcessVessel(postEditShip); } @@ -1604,7 +1605,7 @@ public static void ScrapVessel(BuildListVessel b) { b.RemoveFromBuildList(out _); } - AddFunds(b.GetTotalCost(), TransactionReasons.VesselRollout); + AddFunds(b.GetTotalCost(), TransactionReasonsRP0.VesselPurchase); } public static void ChangeEngineers(LCItem currentLC, int delta) diff --git a/Source/Maintenance/MaintenanceHandler.cs b/Source/Maintenance/MaintenanceHandler.cs index ef01bd21f25..652075e0bca 100644 --- a/Source/Maintenance/MaintenanceHandler.cs +++ b/Source/Maintenance/MaintenanceHandler.cs @@ -499,29 +499,47 @@ public void FixedUpdate() // Best to deduct maintenance fees and add program funding at the same time ProgramHandler.Instance.ProcessFunding(); - using (new CareerEventScope(CareerEventType.Maintenance)) + double timeFactor = UTDiff * (1d / 86400d); + double subsidyForPassedTime = timeFactor * MaintenanceSubsidyPerDay; + + // We have to do some weird logic here. We have to get the resultant upkeep first, + // then add the subsidy, then actually subtract the upkeep. + // This is because we have to subtract piecemeal. + + double facilityMaintenance = CurrencyUtils.Funds(TransactionReasonsRP0.StructureRepair, -FacilityUpkeepPerDay) * timeFactor; + double lcMaintenance = CurrencyUtils.Funds(TransactionReasonsRP0.StructureRepairLC, -LCsCostPerDay) * timeFactor; + double salaryEngineers = CurrencyUtils.Funds(TransactionReasonsRP0.SalaryEngineers, -IntegrationSalaryPerDay) * timeFactor; + double salaryResearchers = CurrencyUtils.Funds(TransactionReasonsRP0.SalaryResearchers, -ResearchSalaryPerDay) * timeFactor; + double salaryCrew = CurrencyUtils.Funds(TransactionReasonsRP0.SalaryCrew, -NautBaseUpkeepPerDay - NautInFlightUpkeepPerDay) * timeFactor; + double crewTraining = CurrencyUtils.Funds(TransactionReasonsRP0.CrewTraining, -TrainingUpkeepPerDay) * timeFactor; + double totalUpkeep = facilityMaintenance + lcMaintenance + salaryEngineers + salaryResearchers + salaryCrew + crewTraining; + + LogPeriod logPeriod = CareerLog.Instance?.CurrentPeriod; + if (logPeriod != null) + { + logPeriod.FacilityMaintenance -= facilityMaintenance; + logPeriod.LCMaintenance -= lcMaintenance; + logPeriod.SalaryEngineers -= salaryEngineers; + logPeriod.SalaryResearchers -= salaryResearchers; + logPeriod.SalaryCrew -= salaryCrew; + logPeriod.TrainingFees -= crewTraining; + } + + using (new CareerEventScope(CareerEventType.Maintenance)) // TODO: remove this scope at a later date since all the components are now logged separately { - double timeFactor = UTDiff * (1d / 86400d); - double subsidyForPassedTime = timeFactor * MaintenanceSubsidyPerDay; - // We have to do some weird logic here. We have to get the resultant upkeep first, - // then add the subsidy, then actually subtract the upkeep. - // This is because we have to subtract piecemeal. double fundsOld = Funding.Instance.Funds; - double totalUpkeep = CurrencyUtils.Funds(TransactionReasonsRP0.StructureRepair, -FacilityUpkeepPerDay) - + CurrencyUtils.Funds(TransactionReasonsRP0.StructureRepairLC, -LCsCostPerDay) - + CurrencyUtils.Funds(TransactionReasonsRP0.SalaryEngineers, -IntegrationSalaryPerDay) - + CurrencyUtils.Funds(TransactionReasonsRP0.SalaryResearchers, -ResearchSalaryPerDay) - + CurrencyUtils.Funds(TransactionReasonsRP0.SalaryCrew, -NautBaseUpkeepPerDay - NautInFlightUpkeepPerDay) - + CurrencyUtils.Funds(TransactionReasonsRP0.CrewTraining, -TrainingUpkeepPerDay); // We have to add subsidy first, to be sure we have enough funds. - double netSubsidy = Math.Min(CurrencyUtils.Funds(TransactionReasonsRP0.Subsidy, subsidyForPassedTime), -totalUpkeep * timeFactor); + double netSubsidy = Math.Min(CurrencyUtils.Funds(TransactionReasonsRP0.Subsidy, subsidyForPassedTime), -totalUpkeep); if (netSubsidy > 0) { Funding.Instance.AddFunds(subsidyForPassedTime, TransactionReasonsRP0.Subsidy.Stock()); + if (logPeriod != null) + logPeriod.SubsidyPaidOut += netSubsidy; double overshoot = (Funding.Instance.Funds - fundsOld) - netSubsidy; if (overshoot > 0) Funding.Instance.AddFunds(-overshoot, TransactionReasons.None); } + double preMaint = Funding.Instance.Funds; Funding.Instance.AddFunds(-FacilityUpkeepPerDay * timeFactor, TransactionReasons.StructureRepair); Funding.Instance.AddFunds(-LCsCostPerDay * timeFactor, TransactionReasonsRP0.StructureRepairLC.Stock()); @@ -529,11 +547,10 @@ public void FixedUpdate() Funding.Instance.AddFunds(-ResearchSalaryPerDay * timeFactor, TransactionReasonsRP0.SalaryResearchers.Stock()); Funding.Instance.AddFunds(-(NautBaseUpkeepPerDay + NautInFlightUpkeepPerDay) * timeFactor, TransactionReasonsRP0.SalaryCrew.Stock()); Funding.Instance.AddFunds(-TrainingUpkeepPerDay * timeFactor, TransactionReasonsRP0.CrewTraining.Stock()); - RP0Debug.Log($"[RP-0] MaintenanceHandler removing {(-totalUpkeep * timeFactor - netSubsidy)} funds where upkeep is {-totalUpkeep} ({(preMaint - Funding.Instance.Funds)} for period) and subsidy {MaintenanceSubsidyPerDay} ({subsidyForPassedTime} for period). Delta = {(Funding.Instance.Funds - fundsOld)}"); - double delta = fundsOld + totalUpkeep * timeFactor + netSubsidy - Funding.Instance.Funds; + RP0Debug.Log($"[RP-0] MaintenanceHandler removing {(-totalUpkeep - netSubsidy)} funds where upkeep is {-totalUpkeep} ({(preMaint - Funding.Instance.Funds)} for period) and subsidy {MaintenanceSubsidyPerDay} ({subsidyForPassedTime} for period). Delta = {(Funding.Instance.Funds - fundsOld)}"); + double delta = fundsOld + totalUpkeep + netSubsidy - Funding.Instance.Funds; if (Math.Abs(delta) > 0.1) - Debug.LogError($"[RP-0] $$$$ Error! Fund mismatch from prediction in maintenance! Prediction:\nMaintenance: {totalUpkeep * timeFactor}\n Subsidy: {netSubsidy} subsidy\nTotal: {totalUpkeep * timeFactor + netSubsidy}\nbut real delta: {Funding.Instance.Funds - fundsOld} (diff {delta})"); - CareerLog.Instance.CurrentPeriod.SubsidyPaidOut += netSubsidy; + Debug.LogError($"[RP-0] $$$$ Error! Fund mismatch from prediction in maintenance! Prediction:\nMaintenance: {totalUpkeep}\n Subsidy: {netSubsidy} subsidy\nTotal: {totalUpkeep + netSubsidy}\nbut real delta: {Funding.Instance.Funds - fundsOld} (diff {delta})"); } // Finally, update all builds diff --git a/Source/UnlockCreditHandler.cs b/Source/UnlockCreditHandler.cs index 97612607c97..479c2efb6ee 100644 --- a/Source/UnlockCreditHandler.cs +++ b/Source/UnlockCreditHandler.cs @@ -110,12 +110,16 @@ public double SpendCredit(double cost) if (_totalCredit < cost) { excessCost = cost - _totalCredit; + if (CareerLog.Instance?.CurrentPeriod != null) + CareerLog.Instance.CurrentPeriod.SpentUnlockCredit += _totalCredit; _totalCredit = 0; } else { excessCost = 0d; _totalCredit -= cost; + if (CareerLog.Instance?.CurrentPeriod != null) + CareerLog.Instance.CurrentPeriod.SpentUnlockCredit += cost; } return excessCost; }