Skip to content

Commit

Permalink
KSP 1.3.1 Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Angel-125 committed Oct 7, 2017
1 parent 75413ad commit ba02f0e
Show file tree
Hide file tree
Showing 23 changed files with 211 additions and 123 deletions.
5 changes: 5 additions & 0 deletions GameData/WildBlueIndustries/Snacks/MiniAVC.xml
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<AddonSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AllowCheck>true</AllowCheck>
<FirstRun>false</FirstRun>
</AddonSettings>
Expand Up @@ -74,6 +74,17 @@
FlowMode = STAGE_PRIORITY_FLOW
}
}

//Because we are directly monitoring converters,
//ModuleQualityControl has to appear in the config
//AFTER all the converters in the part.
MODULE:NEEDS[BARIS]
{
name = ModuleQualityControl
quality = 65
mtbf = 100
monitorConverters = true //WARNING: this is a performance hit.
}
}

@PART[crewCabin,mk3Cockpit_Shuttle,mk2CrewCabin]
Expand Down Expand Up @@ -122,6 +133,17 @@
}
}

//Because we are directly monitoring converters,
//ModuleQualityControl has to appear in the config
//AFTER all the converters in the part.
MODULE:NEEDS[BARIS]
{
name = ModuleQualityControl
quality = 65
mtbf = 100
monitorConverters = true //WARNING: this is a performance hit.
}

RESOURCE
{
name = Soil
Expand Down
Expand Up @@ -70,15 +70,15 @@ PART
RESOURCE
{
name = Snacks
amount = 500
maxAmount = 500
amount = 250
maxAmount = 250
}
RESOURCE
{
name = Soil
amount = 0
maxAmount = 500
maxAmount = 250
}
}
Expand Down
5 changes: 5 additions & 0 deletions GameData/WildBlueIndustries/Snacks/Readme.txt
Expand Up @@ -24,6 +24,11 @@ Copy the files in the zip folder over to GameData/Snacks

REVISION HISTORY

1.8.5
- Fixed background processing of snacks and soil issues with WBI mods (Pathfinder, Buffalo, etc.).
NOTE: Be sure to visit your spacecraft at least once to ensure that the changes take effect.
- Updated to KSP 1.3.1.

1.8.0
- Time estimates are now measured in years and days; months, though accurate, was getting too confusing.
- Snack processors and soil recyclers now run in the background when vessels aren't loaded.
Expand Down
4 changes: 2 additions & 2 deletions GameData/WildBlueIndustries/Snacks/Snacks.version
Expand Up @@ -12,12 +12,12 @@
{
"MAJOR":1,
"MINOR":8,
"PATCH":0
"PATCH":5
},
"KSP_VERSION":
{
"MAJOR":1,
"MINOR":3,
"PATCH":0
"PATCH":1
}
}
Binary file removed Releases/Snacks_1_5_1.zip
Binary file not shown.
Binary file removed Releases/Snacks_1_5_2.zip
Binary file not shown.
Binary file removed Releases/Snacks_1_5_3.zip
Binary file not shown.
Binary file removed Releases/Snacks_1_5_5.zip
Binary file not shown.
Binary file removed Releases/Snacks_1_5_6.zip
Binary file not shown.
Binary file removed Releases/Snacks_1_5_7.zip
Binary file not shown.
Binary file removed Releases/Snacks_1_6_0.zip
Binary file not shown.
Binary file removed Releases/Snacks_1_6_2.zip
Binary file not shown.
Binary file removed Releases/Snacks_1_6_5.zip
Binary file not shown.
Binary file removed Releases/Snacks_1_7_0.zip
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions Snacks/GUI/SnackAppView.cs
Expand Up @@ -159,6 +159,12 @@ public void drawSpaceCenterWindow()
exemptKerbals = string.Empty;
exemptKerbals = GUILayout.TextField(exemptKerbals);

if (SnackController.debugMode)
{
if (GUILayout.Button("Snack Time!"))
SnackController.Instance.EatSnacks();
}

drawFlightWindow();
}

Expand Down
128 changes: 91 additions & 37 deletions Snacks/LifeSupport/SnackConsumer.cs
Expand Up @@ -35,6 +35,12 @@ class SnackConsumer
{
private static System.Random random = new System.Random();

protected void Log(string message)
{
if (SnackController.debugMode)
Debug.Log("[SnackConsumer] - " + message);
}

public static bool hasUnownedCrew(Vessel vessel)
{
int crewCount = 0;
Expand Down Expand Up @@ -176,34 +182,41 @@ public double AddSoilResource(List<ProtoPartSnapshot> protoPartSnapshots, double
* */
public double ConsumeAndGetDeficit(Vessel vessel)
{
Log("ConsumeAndGetDeficit called for vessel:" + vessel.vesselName);
double demand = 0;
double fed = 0;
double crewCount = SnacksScenario.Instance.GetNonExemptCrewCount(vessel);
Log("Non-exempt crewCount: " + crewCount);

//Calculate for loaded vessel
if (vessel.loaded)
{
demand = vessel.GetCrewCount() * SnacksProperties.SnacksPerMeal + calculateExtraSnacksRequired(vessel.GetVesselCrew());
demand = crewCount * SnacksProperties.SnacksPerMeal + calculateExtraSnacksRequired(vessel.GetVesselCrew());
Log("(Loaded vessel) demand: " + demand);

if (demand <= 0)
return 0;

fed = vessel.rootPart.RequestResource(SnacksProperties.SnacksResourceName, demand, ResourceFlowMode.ALL_VESSEL);
Log("(Loaded vessel) fed: " + fed);
}

//Calculate for proto vessel
else
{
//Unloaded vessels need to run their recyclers and snack processors.
Log("Calling runConverters");
runConverters(vessel.protoVessel);

//Now calculate demand.
demand = vessel.protoVessel.GetVesselCrew().Count * SnacksProperties.SnacksPerMeal + calculateExtraSnacksRequired(vessel.protoVessel.GetVesselCrew());
demand = crewCount * SnacksProperties.SnacksPerMeal + calculateExtraSnacksRequired(vessel.protoVessel.GetVesselCrew());
Log("(Unloaded vessel) demand: " + demand);

if (demand <= 0)
return 0;

fed = GetSnackResource(vessel.protoVessel.protoPartSnapshots, demand);
Log("(Unloaded vessel) fed: " + fed);
}

//Fire consume snacks event
Expand All @@ -212,6 +225,7 @@ public double ConsumeAndGetDeficit(Vessel vessel)
snackConsumption.demand = demand;
snackConsumption.fed = fed;
snackConsumption.vessel = vessel;
Log("Fired event onConsumeSnacks");
SnackController.onConsumeSnacks.Fire(snackConsumption);

//Request resource (loaded vessel)
Expand All @@ -222,7 +236,10 @@ public double ConsumeAndGetDeficit(Vessel vessel)

//If recycling is enabled then produce soil.
if (SnacksProperties.RecyclersEnabled)
{
vessel.rootPart.RequestResource(SnacksProperties.SoilResourceName, -fed, ResourceFlowMode.ALL_VESSEL);
Log("Produced soil: " + fed);
}
}

//Request resource (unloaded vessel)
Expand All @@ -235,59 +252,87 @@ public double ConsumeAndGetDeficit(Vessel vessel)

//If recycling is enabled then produce soil.
if (SnacksProperties.RecyclersEnabled)
{
AddSoilResource(vessel.protoVessel.protoPartSnapshots, fed);
Log("Produced soil: " + fed);
}
}

return demand - fed;
}

protected void getResourceRatios(string className, ProtoPartSnapshot protoPartSnapshot, Dictionary<string, double> inputs, Dictionary<string, double> outputs, out double recyclerCapacity)
protected void getResourceRatios(ProtoPartSnapshot protoPartSnapshot, Dictionary<string, double> inputs, Dictionary<string, double> outputs, out double recyclerCapacity)
{
recyclerCapacity = 0f;
ConfigNode[] nodes = protoPartSnapshot.partInfo.partConfig.GetNodes("MODULE");
ConfigNode processorNode = null;
ConfigNode node = null;
string moduleName;
string resourceName;
double resourceRatio;

//Get the processor config node.
for (int index = 0; index < nodes.Length; index++)
ProtoPartModuleSnapshot[] moduleSnapshots;
ProtoPartModuleSnapshot moduleSnapshot;
string resourceRatios = string.Empty;

//Go through all the snapshots and see if we find a recycler or snack processor.
moduleSnapshots = protoPartSnapshot.modules.ToArray();
for (int moduleIndex = 0; moduleIndex < moduleSnapshots.Length; moduleIndex++)
{
node = nodes[index];
if (node.HasValue("name"))
moduleSnapshot = moduleSnapshots[moduleIndex];

//Resource ratio string.
if (moduleSnapshot.moduleValues.HasValue("resourceRatios"))
resourceRatios = moduleSnapshot.moduleValues.GetValue("resourceRatios");

//Snack Processor
if (moduleSnapshot.moduleName == "SnackProcessor")
{
moduleName = node.GetValue("name");
if (moduleName == className)
if (string.IsNullOrEmpty(resourceRatios))
resourceRatios = SnackProcessor.DefaultProcessorRatios;
}

//Soil Recycler
if (moduleSnapshot.moduleName == "SoilRecycler")
{
if (string.IsNullOrEmpty(resourceRatios))
resourceRatios = SoilRecycler.DefaultRecyclerRatios;

//Get recycler capacity
if (moduleSnapshot.moduleValues.HasValue("RecyclerCapacity"))
{
processorNode = node;
break;
recyclerCapacity = double.Parse(moduleSnapshot.moduleValues.GetValue("RecyclerCapacity"));
Log("Recycler supports " + recyclerCapacity + " kerbals.");
}

//Estimate based on part crew capacity.
else if (protoPartSnapshot.partInfo != null && protoPartSnapshot.partInfo.partConfig != null)
{
if (protoPartSnapshot.partInfo.partConfig.HasValue("CrewCapacity"))
{
recyclerCapacity = int.Parse(protoPartSnapshot.partInfo.partConfig.GetValue("CrewCapacity"));
Log("Estimated recyclerCapacity: " + recyclerCapacity + " kerbals.");
}
}
}
}
if (processorNode == null)
return;

//Get the nodes we're interested in
nodes = processorNode.GetNodes("INPUT_RESOURCE");
for (int index = 0; index < nodes.Length; index++)
//Now parse the inputs and outputs.
string[] inputOutputArray = resourceRatios.Split('|');

//Inputs
string[] resources = inputOutputArray[0].Split(';');
string[] resourceValues;
foreach (string inputResource in resources)
{
resourceName = nodes[index].GetValue("ResourceName");
resourceRatio = double.Parse(nodes[index].GetValue("Ratio"));
inputs.Add(resourceName, resourceRatio);
resourceValues = inputResource.Split(',');

//First index is the resource name, second index is the amount.
inputs.Add(resourceValues[0], double.Parse(resourceValues[1]));
}

nodes = processorNode.GetNodes("OUTPUT_RESOURCE");
for (int index = 0; index < nodes.Length; index++)
//Outputs
resources = inputOutputArray[1].Split(';');
foreach (string outputResource in resources)
{
resourceName = nodes[index].GetValue("ResourceName");
resourceRatio = double.Parse(nodes[index].GetValue("Ratio"));
outputs.Add(resourceName, resourceRatio);
}
resourceValues = outputResource.Split(',');

//Recycler: get capacity
if (processorNode.HasValue("RecyclerCapacity"))
recyclerCapacity = double.Parse(processorNode.GetValue("RecyclerCapacity"));
//First index is the resource name, second index is the amount.
outputs.Add(resourceValues[0], double.Parse(resourceValues[1]));
}
}

protected void runConverters(ProtoVessel vessel)
Expand Down Expand Up @@ -356,11 +401,12 @@ protected void runConverters(ProtoVessel vessel)

if (recycler != null)
{
Log("Running soil recycler...");
lastUpdateTime = double.Parse(recycler.moduleValues.GetValue("lastUpdateTime"));
elapsedTime = currentTime - lastUpdateTime;

//Get the resource ratios
getResourceRatios("SoilRecycler", pps, inputs, outputs, out recyclerCapacity);
getResourceRatios(pps, inputs, outputs, out recyclerCapacity);

//Calculate base demand
baseDemand = elapsedTime * recyclerCapacity * SnacksProperties.SnacksPerMeal * SnacksProperties.MealsPerDay;
Expand Down Expand Up @@ -406,14 +452,15 @@ protected void runConverters(ProtoVessel vessel)

if (snackProcessor != null)
{
Log("Running snack processor...");
lastUpdateTime = double.Parse(recycler.moduleValues.GetValue("lastUpdateTime"));
elapsedTime = currentTime - lastUpdateTime;

//Get the resource ratios
recyclerCapacity = 0f;
inputs.Clear();
outputs.Clear();
getResourceRatios("SnackProcessor", pps, inputs, outputs, out recyclerCapacity);
getResourceRatios(pps, inputs, outputs, out recyclerCapacity);

//Calculate base demand
baseDemand = elapsedTime * SnacksProperties.ProductionEfficiency;
Expand Down Expand Up @@ -468,7 +515,11 @@ private static bool getRandomChance(double prob)
private static double calculateExtraSnacksRequired(List<ProtoCrewMember> crew)
{
if (SnacksProperties.EnableRandomSnacking == false)
{
if (SnackController.debugMode)
Debug.Log("[SnackConsumer] - Extra Snacks: 0");
return 0;
}

double extra = 0;
foreach (ProtoCrewMember pc in crew)
Expand All @@ -484,6 +535,9 @@ private static double calculateExtraSnacksRequired(List<ProtoCrewMember> crew)
if (pc.isBadass && getRandomChance(.2))
extra -= SnacksProperties.SnacksPerMeal;
}

if (SnackController.debugMode)
Debug.Log("[SnackConsumer] - Extra Snacks: " + extra);
return extra;
}

Expand Down

0 comments on commit ba02f0e

Please sign in to comment.