Skip to content

Commit

Permalink
Overhauled Volume Calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
pap1723 committed Jan 2, 2020
1 parent fc60ac8 commit 6ca3513
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 50 deletions.
Binary file modified GameData/ROLib/Plugins/ROLib.dll
Binary file not shown.
125 changes: 75 additions & 50 deletions Source/ROLib/Modules/ModuleROTank.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using UnityEngine;
using KSPShaderTools;
using static ROLib.ROLLog;
using static ROLib.ROLUtils;

namespace ROLib
{
Expand Down Expand Up @@ -104,7 +105,7 @@ public void OpenTankDimensionGUIEvent()
/// <summary>
/// Adjustment to the vertical-scale of v-scale compatible models/module-slots.
/// </summary>
[KSPField(isPersistant = true, guiActiveEditor = true, guiActive = false, guiName = "V.ScaleAdj", groupName = "ModuleROTank"),
[KSPField(isPersistant = true, guiActiveEditor = false, guiActive = false, guiName = "V.ScaleAdj", groupName = "ModuleROTank"),
UI_FloatEdit(sigFigs = 4, suppressEditorShipModified = true, minValue = -1, maxValue = 1, incrementLarge = 0.25f, incrementSmall = 0.05f, incrementSlide = 0.001f)]
public float currentVScale = 0f;

Expand Down Expand Up @@ -248,6 +249,14 @@ private ModelDefinitionVariantSet getVariantSet(ModelDefinitionLayoutOptions def

private DimensionWindow dimWindow;

private float effectiveVolume = 0f;
private float effectiveLength = 0f;
private float noseEffectiveLength = 0f;
private float mountEffectiveLength = 0f;
private float coreEffectiveLength = 0f;
private float noseAdditionalVol = 0f;
private float mountAdditionalVol = 0f;


#endregion Private Variables

Expand Down Expand Up @@ -477,6 +486,7 @@ private void initialize()
updateCost();
}
ROLStockInterop.updatePartHighlighting(part);
UpdateTankVolume(lengthWidth);
}

/// <summary>
Expand All @@ -501,6 +511,7 @@ public void initializeUI()
m.updateCost();
}
ROLModInterop.updateResourceVolume(m.part);
m.UpdateTankVolume(lengthWidth);
};

//set up the core variant UI control
Expand Down Expand Up @@ -855,9 +866,10 @@ private ROLModelModule<ModuleROTank> getModuleByName(string name)

private void SetModelFromDimensions()
{
currentVScale = 0.0f;

if (!lengthWidth)
{
currentVScale = 0.0f;
return;
}

Expand All @@ -876,131 +888,144 @@ private void SetModelFromDimensions()
{
m.updateCost();
}
ROLModInterop.updateResourceVolume(m.part);
m.UpdateTankVolume(lengthWidth);
};

float dimRatio, modelRatio = 0.0f;
string s = "1.0x-Kerolox";
dimRatio = currentLength / currentDiameter;

ROLLog.debug($"<color=green>dimRatio: {dimRatio}</color>");

//if (dimRatio < 0.375)
//{
// modelRatio = 0.25f;
// s = $"{modelRatio}x-{currentVariant}";
// ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
//}
if (dimRatio < 0.625)
{
modelRatio = 0.5f;
}
//else if (dimRatio < 0.875)
//{
// modelRatio = 0.75f;
// s = $"{modelRatio}x-{currentVariant}";
// ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
//}
else if (dimRatio < 1.25)
{
modelRatio = 1.0f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}
else if (dimRatio < 1.75)
{
modelRatio = 1.5f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}
else if (dimRatio < 2.25)
{
modelRatio = 2.0f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}
else if (dimRatio < 2.75)
{
modelRatio = 2.5f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}
else if (dimRatio < 3.25)
{
modelRatio = 3.0f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}
else if (dimRatio < 3.75)
{
modelRatio = 3.5f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}
else if (dimRatio < 4.25)
{
modelRatio = 4.0f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}
else if (dimRatio < 4.75)
{
modelRatio = 4.5f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}
else if (dimRatio < 5.25)
{
modelRatio = 5.0f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}
else if (dimRatio < 5.75)
{
modelRatio = 5.5f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}
else if (dimRatio < 6.25)
{
modelRatio = 6.0f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}
else if (dimRatio < 6.75)
{
modelRatio = 6.5f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}
else if (dimRatio < 7.25)
{
modelRatio = 7.0f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}
else if (dimRatio < 7.75)
{
modelRatio = 7.5f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}
else
{
modelRatio = 8.0f;
//s = $"{modelRatio}x-{currentVariant}";
//ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");
}

string ratioName = string.Format("{0:0.0}", modelRatio);
s = $"{ratioName}x-{currentVariant}";
ROLLog.debug($"dimRatio: {dimRatio}, modelRatio: {modelRatio}, string: {s}");

currentVScale = modelRatio * dimRatio;
currentVScale = (dimRatio / modelRatio) - 1;

ROLLog.debug($"currentVScale: {currentVScale}");
coreModule.modelSelected(s);
ROLLog.debug($"coreModule.name: {coreModule.definition.modelName}, currentDiameter: {currentDiameter}, currentVScale: {currentVScale}");
this.ROLactionWithSymmetry(modelChangedAction);
ROLStockInterop.fireEditorUpdate();
}

private void UpdateTankVolume(bool lw)
{
if (!lw)
{
return;
}

Action<ModuleROTank> modelChangedAction = (m) =>
{
m.updateModulePositions();
m.updateDimensions();
m.updateAttachNodes(true);
m.updateDragCubes();
if (scaleMass)
{
m.updateMass();
}
if (scaleCost)
{
m.updateCost();
}
ROLModInterop.realFuelsVolumeUpdate(m.part, m.effectiveVolume);
};

float horScale = currentDiameter / coreModule.definition.diameter;
float domeLength = currentDiameter / 2;
noseEffectiveLength = horScale * noseModule.definition.effectiveLength;
mountEffectiveLength = horScale * mountModule.definition.effectiveLength;
coreEffectiveLength = currentLength - domeLength;
effectiveLength = noseEffectiveLength + mountEffectiveLength + coreEffectiveLength;

// Set the minimum length based on domeLength
minLength = Math.Max(0.1f, domeLength - (noseEffectiveLength + mountEffectiveLength));

// Update the float controller to reset the proper minimum length
this.ROLupdateUIFloatEditControl(nameof(currentLength), minLength, maxLength, diameterLargeStep, diameterSmallStep, diameterSlideStep, true, currentLength);

// Set the tank length to be the same size as the minLength if it is currently smaller
if (currentLength < minLength)
{
currentLength = minLength;
}

// Calculate the new volume
// First, get the additional volume from the nose and mounts
float avgScale = (horScale + horScale + 1) / 3;
float volScale = Mathf.Pow(avgScale, 3);
noseAdditionalVol = volScale * noseModule.definition.additionalVolume * 1000f;
mountAdditionalVol = volScale * mountModule.definition.additionalVolume * 1000f;

// Calculate the volume of the main tank
float r = currentDiameter / 2;
effectiveVolume = (EllipsoidVolume(r, r, r/2) + CylinderVolume(r, effectiveLength)) * 1000f;
effectiveVolume += noseAdditionalVol + mountAdditionalVol;

this.ROLactionWithSymmetry(modelChangedAction);
ROLStockInterop.fireEditorUpdate();
}
Expand Down

0 comments on commit 6ca3513

Please sign in to comment.