/// <summary> /// Called by the IKITModule to produce resources on a vessel.It automatically converts the amount by the appropriate value to /// give a per-second resource production. /// </summary> /// <param name="resource">Resource to produce</param> /// <param name="amount">Amount you are providing</param> /// <param name="max">Maximum amount that could be provided</param> public double Produce(ResourceData data, ResourceName resource, double amount, double max = -1) { KITResourceSettings.ValidateResource(resource); if (!_inExecuteKITModules) { Debug.Log( $"[KITResourceManager.Produce] don't{(_inExecuteKITModules ? " use outside of IKITModules" : "")} {(amount < 0 ? " produce negative amounts" : "")}"); return(0); } // Debug.Log($"[Produce] called with resource = {KITResourceSettings.ResourceToName(resource)}, amount = {amount} and max as {max}"); var trackResourceUsage = TrackableResource(resource) && data.ModConsumption != null && data.ModProduction != null && data.ResourceProductionStats != null; if (trackResourceUsage) { data.ResourceProductionStats[(int)resource].InternalCurrentlySupplied += amount; var lastMod = data.ModsCurrentlyRunning.Last(); if (!data.ModProduction[resource].ContainsKey(lastMod)) { var tmpPPRI = new PerPartResourceInformation { Amount = amount, MaxAmount = (max == -1) ? 0 : max }; data.ModProduction[resource][lastMod] = tmpPPRI; } else { var tmpPPRI = data.ModProduction[resource][lastMod]; tmpPPRI.Amount += amount; tmpPPRI.MaxAmount += (max == -1) ? 0 : max; data.ModProduction[resource][lastMod] = tmpPPRI; } } if (resource == ResourceName.WasteHeat && data.CheatOptions().IgnoreMaxTemperature) { return(0); } if (!data.AvailableResources.ContainsKey(resource)) { data.AvailableResources[resource] = 0; } data.AvailableResources[resource] += amount * _fixedDeltaTime; return(amount); }
/// <summary> /// Called by the IKITModule to consume resources present on a vessel. It automatically converts the wanted amount by the appropriate value to /// give you a per-second resource consumption. /// </summary> /// <param name="data">Resource Data to consume against</param> /// <param name="resource">Resource to consume</param> /// <param name="wanted">How much you want</param> /// <returns>How much you got</returns> public double Consume(ResourceData data, ResourceName resource, double wanted) { KITResourceSettings.ValidateResource(resource); PerPartResourceInformation tmpPPRI; if (!_inExecuteKITModules || wanted < 0) { Debug.Log( $"[KITResourceManager.Consume] don't{(_inExecuteKITModules ? " use outside of IKITModules" : "")} {(wanted < 0 ? " consume negative amounts" : "")}"); return(0); } tmpPPRI.Amount = tmpPPRI.MaxAmount = 0; var lastMod = data.ModsCurrentlyRunning.Last(); var trackResourceUsage = TrackableResource(resource) && data.ModConsumption != null && data.ModProduction != null && data.ResourceProductionStats != null; if (trackResourceUsage) { data.ResourceProductionStats[(int)ResourceName.ElectricCharge].InternalCurrentlyRequested += wanted; if (!data.ModConsumption[resource].ContainsKey(lastMod)) { data.ModConsumption[resource][lastMod] = new PerPartResourceInformation(); } tmpPPRI = data.ModConsumption[resource][lastMod]; tmpPPRI.MaxAmount += wanted; } if (data.CheatOptions().InfiniteElectricity&& resource == ResourceName.ElectricCharge) { tmpPPRI.Amount += wanted; data.ModConsumption[resource][lastMod] = tmpPPRI; return(wanted); } if (!data.AvailableResources.ContainsKey(resource)) { data.AvailableResources[resource] = 0; } double obtainedAmount = 0; double modifiedAmount = wanted * _fixedDeltaTime; var tmp = Math.Min(data.AvailableResources[resource], modifiedAmount); obtainedAmount += tmp; data.AvailableResources[resource] -= tmp; if (trackResourceUsage && tmp > 0) { data.ResourceProductionStats[(int)resource].InternalCurrentlySupplied += tmp; } if (obtainedAmount >= modifiedAmount) { tmpPPRI.Amount += wanted; if (trackResourceUsage) { data.ModConsumption[resource][lastMod] = tmpPPRI; } return(wanted); } double surplusWanted = 0; if (resource != ResourceName.ChargedParticle) { data.MaxResources.TryGetValue(resource, out surplusWanted); } // Convert to seconds obtainedAmount = wanted * (obtainedAmount / modifiedAmount); // Debug.Log($"[Consume] calling variable suppliers for {KITResourceSettings.ResourceToName(resource)}, already have {obtainedAmount}, want a total of {wanted}, with a surplus of {surplusWanted}"); obtainedAmount = CallVariableSuppliers(data, resource, obtainedAmount, wanted, surplusWanted); // We do not need to account for InternalCurrentlySupplied here, as the modules called above will call // Produce which credits the InternalCurrentlySupplied field here. // is it close enough to being fully requested? (accounting for precision issues) var result = (obtainedAmount < (wanted * fudgeFactor)) ? wanted * (obtainedAmount / wanted) : wanted; // Debug.Log($"[Consume] after calling variable suppliers, obtainedAmount is {obtainedAmount}, fudged value is {wanted * fudgeFactor}, and result is {result}"); tmpPPRI.Amount += result; if (trackResourceUsage) { data.ModConsumption[resource][lastMod] = tmpPPRI; } return(result); }