bool change_tank_type() { //check if the tank is in use if (tank_type != null && current_resource != null && current_resource.amount > 0) { if (HighLogic.LoadedSceneIsEditor) { current_resource.amount = 0; } else { Utils.Message("Cannot change tank type while tank is in use"); TankType = tank_type.name; return(false); } } //setup new tank type tank_type = null; if (init_tank_type() && switch_resource()) { init_res_control(); return(true); } return(false); }
void init_supported_types() { exclude = Utils.ParseLine(ExcludeTankTypes, Utils.Comma); include = Utils.ParseLine(IncludeTankTypes, Utils.Comma); SupportedTypes = SwitchableTankType.TankTypeNames(include, exclude); SupportedTypes.AddRange(VolumeConfigsLibrary.AllConfigNames(include, exclude)); if (SupportedTypes.Count > 0) { selected_tank_type = SupportedTypes[0]; } }
public static TankVolume FromResource(PartResource res) { var tank = new TankVolume(); var tank_type = SwitchableTankType.FindTankType(res.resourceName); if (tank_type == null) { return(null); } tank.TankType = tank_type.name; tank.CurrentResource = res.resourceName; tank.Volume = (float)(res.maxAmount / tank_type.Resources[res.resourceName].UnitsPerLiter / 1000 / tank_type.UsefulVolumeRatio); tank.InitialAmount = (float)(res.amount / res.maxAmount); return(tank); }
public override string GetInfo() { var info = ""; init_supported_types(); if (ChooseTankType) { info += SwitchableTankType.TypesInfo(include, exclude); } if (!init_tank_type()) { return(info); } info += tank_type.Info; info += "Tank Volume: " + Utils.formatVolume(Volume); return(info); }
bool init_tank_type() { if (Volume < 0) { Volume = Metric.Volume(part); } if (tank_type != null) { return(true); } boiloff = null; //if tank type is not provided, use the first one from the library if (string.IsNullOrEmpty(TankType)) { TankType = SwitchableTankType.TankTypeNames(include, exclude)[0]; } //select tank type from the library if (!SwitchableTankType.TankTypes.TryGetValue(TankType, out tank_type)) { Utils.Message(6, "No \"{0}\" tank type in the library.\n" + "Configuration of \"{1}\" is INVALID.", TankType, this.Title()); } if (tank_type == null) { return(false); } //initialize current resource if (CurrentResource == string.Empty || !tank_type.Resources.ContainsKey(CurrentResource)) { CurrentResource = tank_type.DefaultResource.Name; } //initialize boiloff/cooling if (tank_type.Boiloff || tank_type.Cooling) { boiloff = tank_type.Boiloff? new ResourceBoiloff(this) : new ActiveCooling(this); if (ModuleSave != null) { boiloff.LoadFrom(ModuleSave); } cooler = boiloff as ActiveCooling; } return(true); }
public string GetInfo(ConfigNode node) { base.Load(node); init_supported_types(); var info = ""; if (TypeChangeEnabled) { info += SwitchableTankType.TypesInfo(include, exclude); } var volumes = ConfigNodeObject.FromConfig <VolumeConfiguration>(node); if (volumes.Valid) { info = string.Concat(info, "Preconfigured Tanks:\n", volumes.Info()); } return(info); }
void add_tank_gui() { if (!AddRemoveEnabled) { return; } GUILayout.BeginVertical(); //tank properties GUILayout.BeginHorizontal(); GUILayout.Label("Type:", GUILayout.ExpandWidth(false)); selected_tank_type = Utils.LeftRightChooser <string>(selected_tank_type, SupportedTypes, SwitchableTankType.GetTankTypeInfo(selected_tank_type), 160); GUILayout.Label("Volume:", GUILayout.Width(50)); volume_field = GUILayout.TextField(volume_field, GUILayout.ExpandWidth(true), GUILayout.MinWidth(50)); if (GUILayout.Button(new GUIContent(percent? "%" : "m3", "Change between Volume (m3) and Percentage (%)"), Styles.normal_button, GUILayout.Width(30))) { percent = !percent; } float volume = -1; var volume_valid = float.TryParse(volume_field, out volume); if (volume_valid) { var vol = add_tank(selected_tank_type, volume, percent); if (!vol.Equals(volume)) { volume = vol; volume_field = volume.ToString("R"); } } GUILayout.EndHorizontal(); //warning label GUILayout.BeginHorizontal(); if (!volume_valid) { GUILayout.Label("Volume should be a number.", Styles.red); } GUILayout.EndHorizontal(); GUILayout.EndVertical(); }
/// <summary> /// Adds a tank of the provided type and value to the part, if possible. /// </summary> /// <returns><c>true</c>, if tank was added, <c>false</c> otherwise.</returns> /// <param name="tank_type">Tank type.</param> /// <param name="volume">Tank volume.</param> /// <param name="resource">Current resource name.</param> /// <param name="amount">Initial amount of a resource in the tank: [0, 1]</param> /// <param name="update_counterparts">If counterparts are to be updated.</param> public bool AddTank(string tank_type, float volume, string resource = "", float amount = 0, bool update_counterparts = true) { if (!AddRemoveEnabled) { return(false); } if (!SwitchableTankType.HaveTankType(tank_type)) { Utils.Log("SwitchableTankManager: no such tank type: {}", tank_type); return(false); } var tank = part.AddModule(typeof(ModuleSwitchableTank).Name) as ModuleSwitchableTank; if (tank == null) { return(false); } tank.id = ++max_id; tank.managed = true; tank.Volume = volume; tank.TankType = tank_type; tank.EnablePartControls = EnablePartControls; tank.IncludeTankTypes = IncludeTankTypes; tank.ExcludeTankTypes = ExcludeTankTypes; tank.InitialAmount = HighLogic.LoadedSceneIsEditor? Mathf.Clamp01(amount) : 0; if (!string.IsNullOrEmpty(resource)) { tank.CurrentResource = resource; } tank.OnStart(part.StartState()); tanks.ForEach(t => t.Tank.RegisterOtherTank(tank)); tanks.Add(new TankWrapper(tank, this)); total_volume = -1; if (update_counterparts) { update_symmetry_managers(m => m.AddTank(tank_type, volume, resource, amount, false)); } return(true); }
/// <summary> /// Forces the switch of the current resource, even if the new resource belongs to another /// tank type, in which case the type is also switched. If the amount of current resource /// is not zero, it is discarded. After the switch the tank remains empty. /// </summary> /// <returns><c>true</c>, if resource was successfully switched, <c>false</c> otherwise.</returns> /// <param name="new_resource">New resource name.</param> public bool ForceSwitchResource(string new_resource) { //if nothing to do, return true if (current_resource != null && current_resource.resourceName == new_resource) { return(true); } //if the new resource is in the current tank type if (tank_type != null && tank_type.Resources.ContainsKey(new_resource)) { if (current_resource != null) { current_resource.amount = 0; } CurrentResource = new_resource; if (switch_resource()) { update_res_control(); return(true); } return(false); } else //try to find the tank type for the new resource { var new_type = SwitchableTankType.FindTankType(new_resource); if (new_type == null) { return(false); } if (current_resource != null) { current_resource.amount = 0; } TankType = new_type.name; CurrentResource = new_resource; return(change_tank_type()); } }
public static List <string> AllConfigNames(string[] include, string[] exclude) { var names = new List <string>(); if (include != null && include.Length > 0) { exclude = SwitchableTankType.TankTypeNames(null, include).ToArray(); } if (exclude != null && exclude.Length > 0) { names.AddRange(from cfg in PresetConfigs where cfg.Value.ContainsTypes(exclude) select cfg.Value.name); names.AddRange(from cfg in UserConfigs where cfg.Value.ContainsTypes(exclude) select cfg.Value.name); } else { names.AddRange(PresetConfigs.Keys); names.AddRange(UserConfigs.Keys); } return(names); }
void init_supported_types() { include = Utils.ParseLine(IncludeTankTypes, Utils.Comma); exclude = Utils.ParseLine(ExcludeTankTypes, Utils.Comma); SupportedTypes = SwitchableTankType.TankTypeNames(include, exclude); }