Jump to content

[PLUGIN, 0.14.3] Multiversal Mechatronics - Miscellaneous Modules


r4m0n

Recommended Posts

I think I\'m already using new public bool GetFuel(), without effect (I changed several things like the gauge and die-if-empty code and it still behaves like the original, can\'t check from phone)

Link to comment
Share on other sites

I should probably point out that the actual problem with the VariableTanks was that they serialize the current fuel value, but not the max fuel value.

Someone noticed that when vessel switching, this causes the gauges on the stack icons to reload with the current working max set to the old current fuel amt. So the tanks still have the correct value, but the bars reload to 'full', and empty themselves proportionally faster. It also means the tanks cannot be refilled past whatever value they held when they got serialized.

**____End of 0.14 posts____**

Link to comment
Share on other sites

  • 2 weeks later...

May I propose a solution to the tank problem?

The changes are:

1) fullFuel is set before loading the fuel amount from save (check added to the start procedure to ensure it doesn\'t get overwritten if it was already set, but gets set if it wasn\'t)

2) Empty tank now can accept fuel.

3) Tank mass is set properly when starting not full tank.

using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class MuMechVariableTank : MuMechPart
{
public string type = 'Generic';
public float fuel = 100;
public float dryMass = 1;
public float emptyExplosionPotential = 0;
public float fullExplosionPotential = 1;

public float fullMass;
public float fullFuel=0;
protected VInfoBox fuelBox = null;

public override void onFlightStateSave(Dictionary<string, KSPParseable> partDataCollection)
{
partDataCollection['fuel'] = new KSPParseable(fuel, KSPParseable.Type.FLOAT);

base.onFlightStateSave(partDataCollection);
}

public override void onFlightStateLoad(Dictionary<string, KSPParseable> parsedData)
{
fullFuel = fuel;

if (parsedData.ContainsKey('fuel')) fuel = parsedData['fuel'].value_float;

base.onFlightStateLoad(parsedData);
}

protected override void onPartStart()
{
stackIcon.SetIcon(DefaultIcons.FUEL_TANK);
stackIconGrouping = StackIconGrouping.SAME_TYPE;

fullMass = mass;
if (fullFuel < fuel)
{
fullFuel = fuel;
}
else
{
mass = Mathf.Lerp(dryMass, fullMass, fuel / fullFuel);
}

base.onPartStart();
}

protected override void onFlightStart()
{
stackIcon.SetIconColor((state == PartStates.IDLE) || (state == PartStates.ACTIVE) ? XKCDColors.BrightTeal : XKCDColors.SlateGrey);

base.onFlightStart();
}

public override void OnDrawStats()
{
GUILayout.TextArea('Fuel type: ' + type + '\nCapacity: ' + fuel + '\nDry mass: ' + dryMass, GUILayout.ExpandHeight(true));
}

public bool getFuel(string fuelType, float amount)
{
if ( (fuelType != type) || (((state == PartStates.DEAD) || (fuel <= 0))&&(amount>=0)))
{
return false;
}

if (state != PartStates.ACTIVE)
{
force_activate();
}

fuel = Mathf.Clamp(fuel - amount, 0, fullFuel);
mass = Mathf.Lerp(dryMass, fullMass, fuel / fullFuel);
explosionPotential = Mathf.Lerp(emptyExplosionPotential, fullExplosionPotential, fuel / fullFuel);

if (fuelBox == null)
{
fuelBox = stackIcon.DisplayInfo();
fuelBox.SetLength(2.0F);
XKCDColors.NextColorAlpha = 0.6F;
fuelBox.SetMsgBgColor(XKCDColors.DarkLime);
fuelBox.SetMsgTextColor(XKCDColors.ElectricLime);
fuelBox.SetMessage(type);
fuelBox.SetProgressBarBgColor(XKCDColors.DarkLime);
fuelBox.SetProgressBarColor(XKCDColors.Yellow);
XKCDColors.NextColorAlpha = 1.0F;
}

fuelBox.SetValue(fuel / fullFuel);

if (fuel <= 0)
{
if (state == PartStates.ACTIVE)
{
deactivate();
}

stackIcon.SetIconColor(XKCDColors.SlateGrey);

if (fuelBox != null)
{
stackIcon.ClearInfoBoxes();
fuelBox = null;
}
}

return true;
}
}

Link to comment
Share on other sites

May I propose a solution to the tank problem?

The changes are:

1) fullFuel is set before loading the fuel amount from save (check added to the start procedure to ensure it doesn\'t get overwritten if it was already set, but gets set if it wasn\'t)

2) Empty tank now can accept fuel.

3) Tank mass is set properly when starting not full tank.

using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class MuMechVariableTank : MuMechPart
{
public string type = 'Generic';
public float fuel = 100;
public float dryMass = 1;
public float emptyExplosionPotential = 0;
public float fullExplosionPotential = 1;

public float fullMass;
public float fullFuel=0;
protected VInfoBox fuelBox = null;

public override void onFlightStateSave(Dictionary<string, KSPParseable> partDataCollection)
{
partDataCollection['fuel'] = new KSPParseable(fuel, KSPParseable.Type.FLOAT);

base.onFlightStateSave(partDataCollection);
}

public override void onFlightStateLoad(Dictionary<string, KSPParseable> parsedData)
{
fullFuel = fuel;

if (parsedData.ContainsKey('fuel')) fuel = parsedData['fuel'].value_float;

base.onFlightStateLoad(parsedData);
}

protected override void onPartStart()
{
stackIcon.SetIcon(DefaultIcons.FUEL_TANK);
stackIconGrouping = StackIconGrouping.SAME_TYPE;

fullMass = mass;
if (fullFuel < fuel)
{
fullFuel = fuel;
}
else
{
mass = Mathf.Lerp(dryMass, fullMass, fuel / fullFuel);
}

base.onPartStart();
}

protected override void onFlightStart()
{
stackIcon.SetIconColor((state == PartStates.IDLE) || (state == PartStates.ACTIVE) ? XKCDColors.BrightTeal : XKCDColors.SlateGrey);

base.onFlightStart();
}

public override void OnDrawStats()
{
GUILayout.TextArea('Fuel type: ' + type + '\nCapacity: ' + fuel + '\nDry mass: ' + dryMass, GUILayout.ExpandHeight(true));
}

public bool getFuel(string fuelType, float amount)
{
if ( (fuelType != type) || (((state == PartStates.DEAD) || (fuel <= 0))&&(amount>=0)))
{
return false;
}

if (state != PartStates.ACTIVE)
{
force_activate();
}

fuel = Mathf.Clamp(fuel - amount, 0, fullFuel);
mass = Mathf.Lerp(dryMass, fullMass, fuel / fullFuel);
explosionPotential = Mathf.Lerp(emptyExplosionPotential, fullExplosionPotential, fuel / fullFuel);

if (fuelBox == null)
{
fuelBox = stackIcon.DisplayInfo();
fuelBox.SetLength(2.0F);
XKCDColors.NextColorAlpha = 0.6F;
fuelBox.SetMsgBgColor(XKCDColors.DarkLime);
fuelBox.SetMsgTextColor(XKCDColors.ElectricLime);
fuelBox.SetMessage(type);
fuelBox.SetProgressBarBgColor(XKCDColors.DarkLime);
fuelBox.SetProgressBarColor(XKCDColors.Yellow);
XKCDColors.NextColorAlpha = 1.0F;
}

fuelBox.SetValue(fuel / fullFuel);

if (fuel <= 0)
{
if (state == PartStates.ACTIVE)
{
deactivate();
}

stackIcon.SetIconColor(XKCDColors.SlateGrey);

if (fuelBox != null)
{
stackIcon.ClearInfoBoxes();
fuelBox = null;
}
}

return true;
}
}

I\'ll be migrating those modules to use PartModule and the resource system shortly. Still not sure if I will even keep the VariableTank, as I think ModuleResource covers that function...

If I keep it, I\'ll keep this fix in mind.

Link to comment
Share on other sites

  • 4 weeks later...
MuMechVariableTank

The base module for new fuel types. Works exactly as a normal FuelTank, with just one new CFG property:

fuel - A string defining the name of the type of fuel contained in the tank.

Is 'fuel' a typo?

In a normal FuelTank the CFG property 'fuel' is already used for the amount of fuel contained in the tank...

Link to comment
Share on other sites

  • 1 month later...
  • 3 weeks later...
This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...