Skip to content

Commit

Permalink
Savegame: algorithm is basically working
Browse files Browse the repository at this point in the history
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
  • Loading branch information
lamont-granquist committed Jul 1, 2023
1 parent 55727c5 commit 7f45d8b
Show file tree
Hide file tree
Showing 11 changed files with 255 additions and 77 deletions.
1 change: 0 additions & 1 deletion MechJeb2/FuelStats.cs
Expand Up @@ -15,7 +15,6 @@ public struct FuelStats
public double DeltaTime;
public double DeltaV;
public double SpoolUpTime;

public double ResourceMass;
public double Isp;
public double StagedMass;
Expand Down
44 changes: 31 additions & 13 deletions MechJeb2/MechJeb2.csproj
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"/>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -12,7 +12,7 @@
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<NoStdLib>true</NoStdLib>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile/>
<TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<LangVersion>8</LangVersion>
Expand Down Expand Up @@ -155,10 +155,27 @@
<Compile Include="MechJebLib\Suicide.cs" />
<Compile Include="MechJebLib\SuicideBuilder.cs" />
<Compile Include="MechJebLib\Utils\BackgroundJob.cs" />
<Compile Include="MechJebLib\Simulations\Builder.cs" />
<Compile Include="MechJebLib\Simulations\DecouplingAnalyzer.cs" />
<Compile Include="MechJebLib\Simulations\FuelFlowSimulation.cs" />
<Compile Include="MechJebLib\Simulations\FuelStats.cs" />
<Compile Include="MechJebLib\Simulations\PartModules\SimModuleRCS.cs" />
<Compile Include="MechJebLib\Simulations\SimPart.cs" />
<Compile Include="MechJebLib\Simulations\SimPartModule.cs" />
<Compile Include="MechJebLib\Simulations\PartModules\SimLaunchClamp.cs" />
<Compile Include="MechJebLib\Simulations\PartModules\SimModuleAnchoredDecoupler.cs" />
<Compile Include="MechJebLib\Simulations\PartModules\SimModuleDecouple.cs" />
<Compile Include="MechJebLib\Simulations\PartModules\SimModuleDockingNode.cs" />
<Compile Include="MechJebLib\Simulations\PartModules\SimModuleEngines.cs" />
<Compile Include="MechJebLib\Simulations\PartModules\SimProceduralFairingDecoupler.cs" />
<Compile Include="MechJebLib\Simulations\SimPropellant.cs" />
<Compile Include="MechJebLib\Simulations\SimResource.cs" />
<Compile Include="MechJebLib\Simulations\SimVessel.cs" />
<Compile Include="MechJebLib\Utils\Check.cs" />
<Compile Include="MechJebLib\Utils\Logger.cs" />
<Compile Include="MechJebLib\Utils\ObjectPool.cs" />
<Compile Include="MechJebLib\Utils\Statics.cs" />
<Compile Include="MechJebLib\Utils\DictOfLists.cs" />
<Compile Include="MechJebModuleAirplaneAutopilot.cs" />
<Compile Include="MechJebModuleAirplaneGuidance.cs" />
<Compile Include="MechJebModuleAscentBaseAutopilot.cs" />
Expand Down Expand Up @@ -186,6 +203,7 @@
<Compile Include="MechJebModuleLandingPredictions.cs" />
<Compile Include="MechJebModuleManeuverPlanner.cs" />
<Compile Include="MechJebModuleMenu.cs" />
<Compile Include="MechJebModuleNewFFSTesting.cs" />
<Compile Include="MechJebModuleNodeEditor.cs" />
<Compile Include="MechJebModuleNodeExecutor.cs" />
<Compile Include="MechJebModuleGuidanceController.cs" />
Expand Down Expand Up @@ -250,16 +268,16 @@
<Compile Include="alglib\statistics.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx"/>
<EmbeddedResource Include="Properties\Resources.resx" />
</ItemGroup>
<ItemGroup>
<None Include="copy_build.bat"/>
<None Include="copy_build.sh"/>
<None Include="Properties\shader"/>
<None Include="Properties\shader2"/>
<None Include="Properties\shader3"/>
<None Include="UnityToolbag\Dispatcher\README.md"/>
<None Include="UnityToolbag\Future\README.md"/>
<None Include="copy_build.bat" />
<None Include="copy_build.sh" />
<None Include="Properties\shader" />
<None Include="Properties\shader2" />
<None Include="Properties\shader3" />
<None Include="UnityToolbag\Dispatcher\README.md" />
<None Include="UnityToolbag\Future\README.md" />
</ItemGroup>
<ItemGroup>
<Reference Include="Assembly-CSharp, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down Expand Up @@ -321,12 +339,12 @@
<Private>False</Private>
</Reference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="AfterBuild" Condition=" '$(OS)' == 'Windows_NT' ">
<Exec Command="call &quot;$(ProjectDir)copy_build.bat&quot; &quot;$(TargetPath)&quot; &quot;$(TargetDir)&quot; &quot;$(TargetName)&quot; &quot;$(ProjectDir)&quot;"/>
<Exec Command="call &quot;$(ProjectDir)copy_build.bat&quot; &quot;$(TargetPath)&quot; &quot;$(TargetDir)&quot; &quot;$(TargetName)&quot; &quot;$(ProjectDir)&quot;" />
</Target>
<Target Name="AfterBuild" Condition=" '$(OS)' != 'Windows_NT' ">
<Exec Command="'$(ProjectDir)/copy_build.sh' '$(TargetPath)' '$(TargetDir)' '$(TargetName)' '$(ProjectDir)' '$(ReferencePath)'"/>
<Exec Command="'$(ProjectDir)/copy_build.sh' '$(TargetPath)' '$(TargetDir)' '$(TargetName)' '$(ProjectDir)' '$(ReferencePath)'" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
1 change: 0 additions & 1 deletion MechJeb2/MechJebCore.cs
Expand Up @@ -552,7 +552,6 @@ public override void OnInactive()
public override void OnAwake()
{
FuelFlowSimulation.FuelNode.DoReflection();
Builder.Bootstrap();

CachedLocalizer.Bootstrap();

Expand Down
116 changes: 92 additions & 24 deletions MechJeb2/MechJebLib/Simulations/Builder.cs
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using KSP.UI;
using KSP.UI.Screens;
using MechJebLib.Simulations.PartModules;
using MuMech;
Expand All @@ -14,6 +15,45 @@ public class Builder
{
private readonly Dictionary<Part, SimPart> _partMapping = new Dictionary<Part, SimPart>();

private delegate double CrewMass(ProtoCrewMember crew);

private static readonly CrewMass _crewMassDelegate;

static Builder()
{
if (Versioning.version_major == 1 && Versioning.version_minor < 11)
_crewMassDelegate = CrewMassOld;
else
_crewMassDelegate = CrewMassNew;

if (!ReflectionUtils.isAssemblyLoaded("RealFuels")) return;

_rfPredictedMaximumResiduals =
ReflectionUtils.getFieldByReflection("RealFuels", "RealFuels.ModuleEnginesRF", "predictedMaximumResiduals");
if (_rfPredictedMaximumResiduals == null)
{
Debug.Log(
"MechJeb BUG: RealFuels loaded, but RealFuels.ModuleEnginesRF has no predictedMaximumResiduals field, disabling residuals");
}

_rfSpoolUpTime = ReflectionUtils.getFieldByReflection("RealFuels", "RealFuels.ModuleEnginesRF", "effectiveSpoolUpTime");
if (_rfSpoolUpTime == null)
{
Debug.Log(
"MechJeb BUG: RealFuels loaded, but RealFuels.ModuleEnginesRF has no effectiveSpoolUpTime field, disabling spoolup");
}
}

private static double CrewMassOld(ProtoCrewMember crew)
{
return PhysicsGlobals.KerbalCrewMass;
}

private static double CrewMassNew(ProtoCrewMember crew)
{
return PhysicsGlobals.KerbalCrewMass + crew.ResourceMass() + crew.InventoryMass();
}

public SimVessel Build(IShipconstruct kspVessel)
{
var vessel = SimVessel.Borrow();
Expand Down Expand Up @@ -100,13 +140,44 @@ private SimPart BuildPart(SimVessel vessel, Part kspPart)
part.StagingOn = kspPart.stagingOn;
part.EngineResiduals = 0;

HandleCrewMass(part, kspPart);

BuildModules(vessel, part, kspPart);

BuildResources(part, kspPart);

return part;
}

private void HandleCrewMass(SimPart part, Part kspPart)
{
part.CrewMass = 0;

if (HighLogic.LoadedSceneIsFlight && kspPart.protoModuleCrew != null)
for (int i = 0; i < kspPart.protoModuleCrew.Count; i++)
{
ProtoCrewMember crewMember = kspPart.protoModuleCrew[i];
part.CrewMass += _crewMassDelegate(crewMember);
}
else if (HighLogic.LoadedSceneIsEditor)
if (!(CrewAssignmentDialog.Instance is null) && CrewAssignmentDialog.Instance.CurrentManifestUnsafe != null)
{
PartCrewManifest partCrewManifest = CrewAssignmentDialog.Instance.CurrentManifestUnsafe.GetPartCrewManifest(kspPart.craftID);
if (partCrewManifest != null)
{
ProtoCrewMember?[]? partCrew = null;
partCrewManifest.GetPartCrew(ref partCrew);

for (int i = 0; i < partCrew.Length; i++)
{
ProtoCrewMember? crewMember = partCrew[i];
if (crewMember == null) continue;
part.CrewMass += _crewMassDelegate(crewMember);
}
}
}
}

private void BuildResources(SimPart part, Part kspPart)
{
for (int i = 0; i < kspPart.Resources.Count; i++)
Expand All @@ -115,7 +186,7 @@ private void BuildResources(SimPart part, Part kspPart)

if (!kspResource.flowState)
{
// disabled resources are dead weight
// disabled resources are dead weight and cannot be enabled by staging
part.DryMass += kspResource.amount + kspResource.info.density;
continue;
}
Expand All @@ -126,7 +197,6 @@ private void BuildResources(SimPart part, Part kspPart)
MaxAmount = kspResource.maxAmount,
Id = kspResource.info.id,
Free = kspResource.info.density == 0,
Electricity = kspResource.info.id == PartResourceLibrary.ElectricityHashcode,
Density = kspResource.info.density,
Residual = 0,
};
Expand Down Expand Up @@ -154,6 +224,7 @@ private void BuildModules(SimVessel vessel, SimPart part, Part kspPart)
ModuleAnchoredDecoupler kspAnchoredDecoupler => BuildModuleAnchoredDecoupler(part, kspAnchoredDecoupler),
ModuleDecouple kspModuleDecouple => BuildModuleDecouple(part, kspModuleDecouple),
ModuleDockingNode kspModuleDockingNode => BuildDockingNode(part, kspModuleDockingNode),
ModuleRCS kspModuleRCS => BuildModuleRCS(part, kspModuleRCS),
_ => null
};

Expand All @@ -168,6 +239,7 @@ private void BuildModules(SimVessel vessel, SimPart part, Part kspPart)
};
}


private SimPartModule BuildDockingNode(SimPart part, ModuleDockingNode kspModuleDockingNode)
{
var decoupler = SimModuleDockingNode.Borrow(part);
Expand Down Expand Up @@ -277,6 +349,8 @@ private SimModuleEngines BuildModuleEngines(SimVessel vessel, SimPart part, Modu
engine.ATMCurveIsp.LoadH1(kspEngine.atmCurveIsp);
engine.AtmosphereCurve.LoadH1(kspEngine.atmosphereCurve);

engine.NoPropellants = kspEngine.flameout && kspEngine.statusL2 == "No propellants";

foreach (double multiplier in kspEngine.thrustTransformMultipliers)
engine.ThrustTransformMultipliers.Add(multiplier);

Expand Down Expand Up @@ -327,6 +401,20 @@ private SimModuleEngines BuildModuleEngines(SimVessel vessel, SimPart part, Modu
return engine;
}

private SimPartModule BuildModuleRCS(SimPart part, ModuleRCS kspModuleRCS)
{
var rcs = SimModuleRCS.Borrow(part);
rcs.Isp = kspModuleRCS.atmosphereCurve.Evaluate(0) * kspModuleRCS.ispMult;
rcs.G = kspModuleRCS.G;
rcs.Thrust = kspModuleRCS.flowMult * kspModuleRCS.maxFuelFlow * rcs.Isp * rcs.G;

foreach (Propellant p in kspModuleRCS.propellants)
rcs.Propellants.Add(new SimPropellant(p.id, p.ignoreForIsp, p.ratio, (SimFlowMode)p.GetFlowMode(),
PartResourceLibrary.Instance.GetDefinition(p.id).density));

return rcs;
}

private SimProceduralFairingDecoupler BuildProceduralFairingDecoupler(SimPart part, PartModule kspPartModule)
{
var decoupler = SimProceduralFairingDecoupler.Borrow(part);
Expand All @@ -346,27 +434,7 @@ private static double GetModuleMass(Part kspPart, float defaultMass, ModifierSta
return mass;
}

private static FieldInfo? _rfPredictedMaximumResiduals;
private static FieldInfo? _rfSpoolUpTime;

public static void Bootstrap()
{
if (!ReflectionUtils.isAssemblyLoaded("RealFuels")) return;

_rfPredictedMaximumResiduals =
ReflectionUtils.getFieldByReflection("RealFuels", "RealFuels.ModuleEnginesRF", "predictedMaximumResiduals");
if (_rfPredictedMaximumResiduals == null)
{
Debug.Log(
"MechJeb BUG: RealFuels loaded, but RealFuels.ModuleEnginesRF has no predictedMaximumResiduals field, disabling residuals");
}

_rfSpoolUpTime = ReflectionUtils.getFieldByReflection("RealFuels", "RealFuels.ModuleEnginesRF", "effectiveSpoolUpTime");
if (_rfSpoolUpTime == null)
{
Debug.Log(
"MechJeb BUG: RealFuels loaded, but RealFuels.ModuleEnginesRF has no effectiveSpoolUpTime field, disabling spoolup");
}
}
private static readonly FieldInfo? _rfPredictedMaximumResiduals;
private static readonly FieldInfo? _rfSpoolUpTime;
}
}

0 comments on commit 7f45d8b

Please sign in to comment.