Jump to content

[Plugin] [Design Stage] Modular Mass Modifier - A better system for modifying part mass


blowfish

Recommended Posts

The current options for modifying a part's mass aren't very good. You can implement IPartMassModifier, but that mass doesn't affect physics at all. You can modify the part's mass, but if you have more than one module trying to do that, you're going to run into trouble. Therefore I am proposing a new system for part mass modification:

  • A single module will handle all mass modifications and update part.mass
  • Other modules that wish to modify a part's mass will implement an interface similar to IPartMassModifier
  • The part's mass will be automatically updated on part update events, but modules may request a manual update if necessary

I've got the basic implementation details worked out in code below. Still a lot of work to be done, but most of the major stuff is there


public interface IPartMassModifier2
{
float GetModuleMass(float baseMass);
}

public class PartMassModifierModule : PartModule, IPartMassModifier
{
public float BaseMass { get; private set; }
public float ModuleMass { get; private set; }
public float Mass { get { return BaseMass + ModuleMass; } }

public override void OnStart(PartModule.StartState state)
{
base.OnStart(state);

BaseMass = part.GetPrefab().mass;

if (state == PartModule.StartState.Editor)
GameEvents.onEditorPartEvent.Add(OnEditorPartUpdate);
else
GameEvents.onVesselUpdate.Add(OnVesselUpdate);
}

private void OnDestroy()
{
GameEvents.onEditorPartEvent.Remove(OnEditorPartUpdate);
GameEvents.onVesselUpdate.Remove(OnVesselUpdate);
}

[KSPEvent]
public void UpdateMass()
{
ModuleMass = 0f;
foreach (PartModule m in part.Modules)
{
if (m is IPartMassModifier2)
ModuleMass += (m as IPartMassModifier2).GetModuleMass(BaseMass);
}

part.mass = Mass;
}

private void OnEditorPartUpdate(ConstructionEventType eventType, Part part)
{
if (object.ReferenceEquals(part, this.part))
UpdateMass();
}

private void OnVesselUpdate(Vessel vessel)
{
if (object.ReferenceEquals(vessel, this.part.vessel))
UpdateMass();
}

public float GetModuleMass(float baseMass)
{
if (baseMass == BaseMass)
return ModuleMass;
else if (baseMass == part.mass)
return 0f;
else
{
Debug.LogWarning("Warning: unrecognized mass detected");
return 0f;
}
}
}

So to all the plugin developers out there, I ask: does this seem like a reasonable system? Would you use it? Is there anything you feel is missing?

Link to comment
Share on other sites

You forgot about the resource mass. ;)

wet mass = (dry) part mass + module mass(es) + resource mass

does this seem like a reasonable system?

IMHO you replicate what the game already does. If a programmer wants to add mass, he can just add a module and set a mass attribute. KSP will take care of all calculations.

Would you use it?

Probably not. It's too much of a hassle to search the forums for small things like your interface. If there's a bundle which incorporates all the snippets I'll think about it.

Edited by *Aqua*
Link to comment
Share on other sites

You forgot about the resource mass. ;)

wet mass = (dry) part mass + module mass(es) + resource mass

KSP deals with resource mass correctly, so it doesn't need to be added

IMHO you replicate what the game already does. If a programmer wants to add mass, he can just add a module and set a mass attribute. KSP will take care of all calculations.

Currently, you can have multiple modules affecting mass (but that mass won't count toward physics), OR you can affect physics (but only one module can do it). The goal here is to allow both.

Link to comment
Share on other sites

I never heard of that. Are you sure?

I'm sure. IPartMassModifier has no effect on physics. You could possibly devise a system where multiple modules would carefully add their mass to part.mass then keep track of changes in that mass, but it would be messy and prone to error.

Link to comment
Share on other sites

  • 1 year later...
On 11/19/2016 at 4:45 PM, Blackline said:

Sorry for necro-ing, but is the mass behavior still tihs way? Are there any "new" ways since 1.2 for a module to have its own mass (which counts towards total part.mass)?

Cheers

There's nothing new in this regard in 1.2, but since 1.0? We have IPartMassModifier (and IPartCostModifier for cost), which, if implemented on a PartModule, will tell KSP to add some mass to the part (or subtract even).

Link to comment
Share on other sites

7 hours ago, blowfish said:

There's nothing new in this regard in 1.2, but since 1.0? We have IPartMassModifier (and IPartCostModifier for cost), which, if implemented on a PartModule, will tell KSP to add some mass to the part (or subtract even).

But are those additional masses used for physics calculations? Because you said in the OP they are not.

Link to comment
Share on other sites

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...