/// <summary> /// Check whether the saves of the part are consistent with the resources in the part /// </summary> private bool checkSaveConsistency() { if ((initialized) && (switchableResources.Count > 0)) { //when the saved index is not valid if ((selectedResourceID < 0) || (selectedResourceID >= switchableResources.Count)) { selectedResourceID = -1; if (replaceDefaultResources) { part.Resources.Clear(); } else { //remove all resources that are not default resources int numResources = part.Resources.Count; List <PartResource> resourcesToRemove = new List <PartResource>(); for (int i = 0; i < numResources; i++) { KerbetrotterDefaultResource res = getResourceFromList(part.Resources[i].resourceName, defaultResources); //when the resource is not a default resource, remove it if (res == null) { resourcesToRemove.Add(part.Resources[i]); } //else change its value to the default ones else { part.Resources[i].amount = UtilMath.Min(res.maxAmount, part.Resources[i].amount); part.Resources[i].maxAmount = res.maxAmount; part.Resources[i].isTweakable = res.isTweakable; } } //remove all resources that are scheduled to be removed for (int i = 0; i < resourcesToRemove.Count; i++) { part.RemoveResource(resourcesToRemove[i]); } } Debug.LogWarning("[KerbetrotterResourceSwitch] " + moduleName + ": Resources of part are inconsistent with switchable resource, resetting to default!"); return(false); } } return(true); }
/// <summary> /// Check if the tank is empty /// </summary> /// <param name="currentPart">The current part</param> /// <returns>True when tank is empty, else false</returns> private bool isTankEmpty(Part currentPart) { //when we have an invalid id for the current resource, assume that the tank is empty if (selectedResourceID == -1) { return(true); } int numResources = currentPart.Resources.Count; for (int i = 0; i < numResources; i++) { //when the current tank is not empty (more then 0.1% is in the tank), check it further if (currentPart.Resources[i].amount > currentPart.Resources[i].maxAmount / 1000) { if (replaceDefaultResources) { return(false); } else { KerbetrotterDefaultResource defaultResource = getResourceFromList(currentPart.Resources[i].resourceName, defaultResources); //when this resource is also a default resource if (defaultResource != null) { if (currentPart.Resources[i].amount > defaultResource.maxAmount) { return(false); } } //when this resource is not a default resource else { return(false); } } } } return(true); }
/// <summary> /// Switch the resources from the parts /// </summary> /// <param name="currentPart">The part which resources have to be switched</param> /// <param name="newResourceID">The ID of the new resource</param> private void switchResources(Part currentPart, int newResourceID) { if ((initialized) && (switchableResources.Count > 0)) { //only switch when a valid resourceID is set if ((newResourceID >= 0) && (newResourceID < switchableResources.Count)) { //List<LynxDefaultResource> removedDefaultResources = new List<LynxDefaultResource>(); //remove the previous resources from the part if (selectedResourceID != -1) { //when the default resources should be replaced if (replaceDefaultResources) { currentPart.Resources.Clear(); } else { //get the list of resources from the last setup KerbetrotterSwitchableResource oldResources = switchableResources[selectedResourceID]; List <PartResource> resourcesToRemove = new List <PartResource>(); //remove all of the resources that are not default, update the default ones int numResources = currentPart.Resources.Count; for (int i = 0; i < numResources; i++) { PartResource partResource = currentPart.Resources[i]; KerbetrotterDefaultResource defaultResource = getResourceFromList(partResource.resourceName, defaultResources); //When the part containes this resource as a default resource, change its values if (defaultResource != null) { double amount = getDefaultResourceAmount(defaultResource.name, defaultResource.maxAmount, currentPart); partResource.amount = amount; partResource.maxAmount = defaultResource.maxAmount; partResource.isTweakable = defaultResource.isTweakable; } //else add resource to remove list else { resourcesToRemove.Add(partResource); } } //remove the resources that are scheduled to be removed for (int i = 0; i < resourcesToRemove.Count; i++) { currentPart.RemoveResource(resourcesToRemove[i]); } } } //update the new resource id selectedResourceID = newResourceID; KerbetrotterResourceDefinition[] newResources = switchableResources[selectedResourceID].resources; //update costs and weights resourceCostsModifier = (float)switchableResources[selectedResourceID].costModifier; resourceMassModifier = (float)switchableResources[selectedResourceID].massModifier; //add all the defined resources to the part for (int i = 0; i < newResources.Length; i++) { //Skip resources with the name Structural (why?) if (newResources[i].name == "Structural") { continue; } double maxAmount = newResources[i].maxAmount; double amount = 0.0; //when in editor, we will set the configured amount if (HighLogic.LoadedSceneIsEditor) { amount = newResources[i].amount; } //get the data of the default resource if available KerbetrotterDefaultResource defaultResource = getResourceFromList(newResources[i].name, defaultResources); //when we have a default resource and do not replace them, update their data if ((!replaceDefaultResources) && (defaultResource != null)) { PartResource partResource = currentPart.Resources[defaultResource.name]; partResource.maxAmount += newResources[i].maxAmount; partResource.amount += amount; partResource.isTweakable = newResources[i].isTweakable; } //else create and add a new resource else { ConfigNode newResourceNode = new ConfigNode("RESOURCE"); newResourceNode.AddValue("name", newResources[i].name); newResourceNode.AddValue("maxAmount", newResources[i].maxAmount); newResourceNode.AddValue("isTweakable", newResources[i].isTweakable); newResourceNode.AddValue("amount", amount); //when we are in the editor, fill the tank with the new amount if (HighLogic.LoadedSceneIsEditor) { newResourceNode.AddValue("amount", newResources[i].amount); } //else the tank is empty else { newResourceNode.AddValue("amount", 0.0f); } currentPart.AddResource(newResourceNode); } } } } }