Skip to content

Commit

Permalink
Same process in multiple slots (#115)
Browse files Browse the repository at this point in the history
* Lib: Add SetResource method

* Configure: Tidy indents

* Configure: Allow slots to have same processes

* Reliability: Remove RT broken output msg to console log

* Configure: Fix cost and mass for slots with same process

* Configure: Fix resources for slots with same process

* Profile: Fix MRE process

Regolith also contains CO2 which is released in the pre-heating stage, so added a CO2 output.
MRE would not run if shielding on the vessel was full or did not exist, so set shielding to dump.

* CHANGELOG.md: Add MRE and Configurable slots change details

* Build: Release dll (v.1.7.2)
  • Loading branch information
PiezPiedPy authored and steamport committed Jul 8, 2018
1 parent cb27601 commit 397528e
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 44 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

### Changes since the last release

* Configurable parts can now contain the same process in multiple slots (PiezPiedPy)
* Fixed MRE not running when shielding is full or does not exist on a vessel (PiezPiedPy)
* MRE process now outputs a small amount of CO2 (PiezPiedPy)
* GeigerCounter science experiment fixes for OPM and NewHorizons. Also SEP support fixes (Gordon Dry)
* Rebalanced Sabatier and Anthraquinone processes to output LiquidFuel and Oxidizer at Stock ratio of 9:11 (PiezPiedPy)
* Rebalanced H2+O2 and LH2+O2 fuel cells to output more realistic EC levels (PiezPiedPy)
* Some tooltip colors changed from a nasty hard to see red to a nice gold (PiezPiedPy)
Expand Down
Binary file modified GameData/Kerbalism/Kerbalism.dll
Binary file not shown.
11 changes: 6 additions & 5 deletions GameData/Kerbalism/Profiles/Default.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ Profile
{
name = sabatier process // LiquidFuel output balanced to match Anthraquinone Oxidizer output
modifier = _Sabatier
input = ElectricCharge@0.002556692 // calculated using 1.03 J/(g K) (sustained heating to 300c)
input = ElectricCharge@0.002556692 // calculated using 1.03 J/(g K) (sustained heating to 575K)
input = CarbonDioxide@3.490273221
input = Hydrogen@13.87826691
output = Water@0.005574954
Expand All @@ -383,7 +383,7 @@ Profile
{
name = sabatier process water priority // LiquidFuel output balanced to match Anthraquinone Oxidizer output
modifier = _SabatierH2OPriority
input = ElectricCharge@0.002556692 // calculated using 1.03 J/(g K) (sustained heating to 300c)
input = ElectricCharge@0.002556692 // calculated using 1.03 J/(g K) (sustained heating to 575K)
input = CarbonDioxide@3.490273221
input = Hydrogen@13.87826691
output = Water@0.005574954
Expand Down Expand Up @@ -435,9 +435,10 @@ Profile
modifier = _MRE
input = ElectricCharge@2.0 // 2 kW MRE reactor at 2300 K with heat corrosion resistant crucible
input = Ore@0.00006342
output = Oxygen@0.088843
output = Shielding@0.000000112
// Slag is wasted
output = Oxygen@0.088843 // 42% of regolith is O2
output = CarbonDioxide@0.00444215 // approx 2% of regolith is CO2, released in the pre-heating stage @950K
output = Shielding@0.000000112 // makes Shielding out of waste Slag
dump = Shielding,CarbonDioxide
}

Process
Expand Down
16 changes: 16 additions & 0 deletions src/Lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,22 @@ public static void SetResourceCapacity(Part p, string res_name, double capacity)
res.amount = Math.Min(res.amount, capacity);
}

// note: the resource must exist
public static void SetResource(Part p, string res_name, double amount, double capacity)
{
// if the resource is not in the part, log a warning and do nothing
if (!p.Resources.Contains(res_name))
{
Lib.Log(Lib.BuildString("error while setting capacity for ", res_name, ": the resource is not in the part"));
return;
}

// set capacity and clamp amount
var res = p.Resources[res_name];
res.maxAmount = capacity;
res.amount = Math.Min(amount, capacity);
}


// set flow of a resource in the specified part
// do nothing if the resource don't exist in the part
Expand Down
67 changes: 35 additions & 32 deletions src/Modules/Configure.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using KSP.Localization;

Expand All @@ -9,22 +8,24 @@ namespace KERBALISM
{


// Modules can implement this interface in case they need to do something
// when enabled/disabled by Configure. This is the case, for example, for
// all those modules that add resources dynamically (like Process or Habitat).
/// <summary>
/// Modules can implement this interface in case they need to do something
/// when enabled/disabled by Configure. This is the case, for example, for
/// all those modules that add resources dynamically (like Process or Habitat).
/// </summary>
public interface IConfigurable
{
// configure the module
void Configure(bool enable);
void Configure(bool enable, int multiple = 1);
}


public sealed class Configure : PartModule, IPartCostModifier, IPartMassModifier, IModuleInfo, ISpecifics
{
// config
[KSPField] public string title = string.Empty; // short description
[KSPField] public string data = string.Empty; // store setups as serialized data
[KSPField] public uint slots = 1; // how many setups can be selected
[KSPField] public string title = string.Empty; // short description
[KSPField] public string data = string.Empty; // store setups as serialized data
[KSPField] public uint slots = 1; // how many setups can be selected
[KSPField] public string reconfigure = string.Empty; // true if it can be reconfigured in flight

// persistence
Expand All @@ -36,12 +37,12 @@ public sealed class Configure : PartModule, IPartCostModifier, IPartMassModifier
// part copy/symmetry serialization can see them
List<ConfigureSetup> setups; // all setups
List<ConfigureSetup> unlocked; // unlocked setups
public List<string> selected; // selected setups names
public List<string> prev_selected; // previously selected setups names
double extra_cost; // extra cost for selected setups, including resources
double extra_mass; // extra mass for selected setups, excluding resources
bool initialized; // keep track of first configuration
CrewSpecs reconfigure_cs; // in-flight reconfiguration crew specs
public List<string> selected; // selected setups names
public List<string> prev_selected; // previously selected setups names
double extra_cost; // extra cost for selected setups, including resources
double extra_mass; // extra mass for selected setups, excluding resources
bool initialized; // keep track of first configuration
CrewSpecs reconfigure_cs; // in-flight reconfiguration crew specs
Dictionary<int, int> changes; // store 'deferred' changes to avoid problems with unity gui

// used to avoid infinite recursion when dealing with symmetry group
Expand Down Expand Up @@ -152,7 +153,7 @@ public void DoConfigure()

// make sure configuration include all available slots
// this also create default configuration
// - we don it only in the editor
// - we do it only in the editor
// - we avoid corner case when cfg was never set up (because craft was never in VAB)
if (Lib.IsEditor() || selected.Count == 0)
{
Expand All @@ -165,11 +166,17 @@ public void DoConfigure()
// for each setup
foreach (ConfigureSetup setup in setups)
{
// detect if the setup is selected in multiple slots
int count = (selected.FindAll(x => x == setup.name)).Count;

// detect if the setup is selected
bool active = selected.Contains(setup.name);
bool active = count > 0;

// detect if the setup was previously selected in multiple slots
int prev_count = (prev_selected.FindAll(x => x == setup.name)).Count;

// detect if the setup was previously selected
bool prev_active = prev_selected.Contains(setup.name);
bool prev_active = prev_count > 0;

// for each module specification in the setup
foreach (ConfigureModule cm in setup.modules)
Expand All @@ -182,7 +189,7 @@ public void DoConfigure()
{
// call configure/deconfigure functions on module if available
if (m is IConfigurable configurable_module)
configurable_module.Configure(active);
configurable_module.Configure(active, count);

// enable/disable the module
m.isEnabled = active;
Expand All @@ -204,36 +211,34 @@ public void DoConfigure()
double capacity = Lib.Parse.ToDouble(cr.maxAmount);

// add/remove resource
if ((prev_active != (active && capacity > 0.0)) || (reconfigure_cs && initialized))
if ((prev_active != (active && capacity > 0.0)) || (reconfigure_cs && initialized) || (count != prev_count))
{
// if previously selected
if (prev_active)
{
// remove the resources
Lib.RemoveResource(part, cr.name, amount, capacity);
prev_count = prev_count == 0 ? 1 : prev_count;
Lib.RemoveResource(part, cr.name, amount * prev_count, capacity * prev_count);
}

// if selected
if (active && capacity > 0.0)
{
// add the resources
// - in flight, do not add amount
Lib.AddResource(part, cr.name, Lib.IsFlight() ? 0.0 : amount, capacity);
Lib.AddResource(part, cr.name, Lib.IsFlight() ? 0.0 : amount * count, capacity * count);
}
}

// add resource cost
if (active)
{
extra_cost += amount * unit_cost;
}
if (active) extra_cost += amount * unit_cost * count;
}

// add setup extra cost and mass
if (active)
{
extra_cost += setup.cost;
extra_mass += setup.mass;
extra_cost += setup.cost * count;
extra_mass += setup.mass * count;
}
}

Expand Down Expand Up @@ -517,11 +522,9 @@ void Render_panel(Panel p, ConfigureSetup setup, int selected_i, int setup_i)
// utility, used as callback in panel select
void Change_setup(int change, int selected_i, ref int setup_i)
{
do
{
setup_i = (setup_i + change + unlocked.Count) % unlocked.Count;
}
while (selected.Contains(unlocked[setup_i].name));
if (setup_i + change == unlocked.Count) setup_i = 0;
else if (setup_i + change < 0) setup_i = unlocked.Count - 1;
else setup_i += change;
changes.Add(selected_i, setup_i);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Modules/Habitat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void Set_pressurized(bool pressurized)
}
}

public void Configure(bool enable)
public void Configure(bool enable, int multiple = 1)
{
if (enable)
{
Expand Down
32 changes: 27 additions & 5 deletions src/Modules/ProcessController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@ public sealed class ProcessController : PartModule, IModuleInfo, IAnimatedModule
[KSPField] public string title = string.Empty; // name to show on ui
[KSPField] public string desc = string.Empty; // description to show on tooltip
[KSPField] public double capacity = 1.0; // amount of associated pseudo-resource
[KSPField] public bool toggle = true; // show the enable/disable toggle
[KSPField] public bool toggle = true; // show the enable/disable toggle

// persistence/config
// note: the running state doesn't need to be serialized, as it can be deduced from resource flow
// but we find useful to set running to true in the cfg node for some processes, and not others
[KSPField(isPersistant = true)] public bool running;

// amount of times to multiply capacity, used so configure can have the same process in more than one slot
[KSPField(isPersistant = true)] public int multiple = 1;


public void Start()
{
// don't break tutorial scenarios
if (Lib.DisableScenario(this)) return;

// configure on start
Configure(true);
Configure(true, multiple);

// set action group ui
Actions["Action"].guiName = Lib.BuildString("Start/Stop ", title);
Expand All @@ -42,8 +45,11 @@ public void Start()
if (!toggle) running = true;
}

public void Configure(bool enable)
public void Configure(bool enable, int multiple = 1)
{
// make sure multiple is not zero
multiple = multiple == 0 ? 1 : multiple;

if (enable)
{
// if never set
Expand All @@ -53,12 +59,28 @@ public void Configure(bool enable)
{
// add the resource
// - always add the specified amount, even in flight
Lib.AddResource(part, resource, capacity, capacity);
Lib.AddResource(part, resource, capacity * multiple, capacity * multiple);
}
// has multiple changed
else if (this.multiple != multiple)
{
// multiple has increased
if (this.multiple < multiple)
{
Lib.AddResource(part, resource, capacity * (multiple - this.multiple), capacity * (multiple - this.multiple));
}
// multiple has decreased
else
{
Lib.RemoveResource(part, resource, 0.0, capacity * (this.multiple - multiple));
}
}
this.multiple = multiple;
}
else
{
Lib.RemoveResource(part, resource, 0.0, capacity);
Lib.RemoveResource(part, resource, 0.0, capacity * this.multiple);
this.multiple = 1;
}
}

Expand Down
1 change: 0 additions & 1 deletion src/Modules/Reliability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,6 @@ void Apply(bool b)
{
if (m.moduleName == "ModuleRTAntenna" || m.moduleName == "ModuleRTAntennaPassive")
{
Lib.Log("RemoteTech: Setting IsRTBroken to " + b + " for " + m.moduleName);
Lib.ReflectionValue(m, "IsRTBroken", b);
}
}
Expand Down

0 comments on commit 397528e

Please sign in to comment.