Skip to content

Commit

Permalink
Fixed the part symmetry problem. (#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirMortimer authored and steamport committed Apr 8, 2019
1 parent c3658ea commit d503ee8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 44 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* Hide Sickbay from automation if it is unavailable (Sir Mortimer)
* New Science system (Sir Mortimer, Arthur)
* Reverted the part-specific process handling introduced with PR #280 as it causes other issues (Sir Mortimer)
* Fixed the issue with placing parts in a symmetry group > 2 in the editor (Sir Mortimer)

------------------------------------------------------------------------------------------------------

Expand Down
105 changes: 61 additions & 44 deletions src/Kerbalism/Lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1207,66 +1207,83 @@ public static double Level( Part part, string resource_name, bool ignore_flow =

/// <summary> Adds the specified resource amount and capacity to a part,
/// the resource is created if it doesn't already exist </summary>
public static void AddResource( Part p, string res_name, double amount, double capacity )
// poached from https://github.com/blowfishpro/B9PartSwitch/blob/master/B9PartSwitch/Extensions/PartExtensions.cs
public static PartResource AddResource(Part part, string res_name, double amount, double capacity)
{
// if the resource is already in the part
if (p.Resources.Contains( res_name ))
var reslib = PartResourceLibrary.Instance.resourceDefinitions;
// if the resource is not known, log a warning and do nothing
if (!reslib.Contains(res_name))
{
// add amount and capacity
var res = p.Resources[res_name];
res.amount += amount;
res.maxAmount += capacity;
Lib.Log(Lib.BuildString("error while adding ", res_name, ": the resource doesn't exist"));
return null;
}
// if the resource is not already in the part
else
{
// shortcut to resource library
var reslib = PartResourceLibrary.Instance.resourceDefinitions;
var resourceDefinition = reslib[res_name];

// if the resource is not known, log a warning and do nothing
if (!reslib.Contains( res_name ))
{
Lib.Log( Lib.BuildString( "error while adding ", res_name, ": the resource doesn't exist" ) );
return;
}
amount = Math.Min(amount, capacity);
amount = Math.Max(amount, 0);

// get resource definition
var def = reslib[res_name];
PartResource resource = part.Resources[resourceDefinition.name];

if (resource == null)
{
resource = new PartResource(part);
resource.SetInfo(resourceDefinition);
resource.maxAmount = capacity;
resource.amount = amount;
resource.flowState = true;
resource.isTweakable = resourceDefinition.isTweakable;
resource.isVisible = resourceDefinition.isVisible;
resource.hideFlow = false;
resource.flowMode = PartResource.FlowMode.Both;
part.Resources.dict.Add(resourceDefinition.name.GetHashCode(), resource);

PartResource simulationResource = new PartResource(resource);
simulationResource.simulationResource = true;
part.SimulationResources?.dict.Add(resourceDefinition.name.GetHashCode(), simulationResource);

GameEvents.onPartResourceListChange.Fire(part);
}
else
{
resource.maxAmount = capacity;

// create the resource
ConfigNode res = new ConfigNode( "RESOURCE" );
res.AddValue( "name", res_name );
res.AddValue( "amount", amount );
res.AddValue( "maxAmount", capacity );
PartResource simulationResource = part.SimulationResources?[resourceDefinition.name];
if (simulationResource != null) simulationResource.maxAmount = capacity;

// add it to the part
p.Resources.Add( res );
resource.amount = amount;
}

return resource;
}

/// <summary> Removes the specified resource amount and capacity from a part,
/// the resource is removed completely if the capacity reaches zero </summary>
public static void RemoveResource( Part p, string res_name, double amount, double capacity )
public static void RemoveResource(Part part, string res_name, double amount, double capacity)
{
// if the resource is not already in the part, do nothing
if (p.Resources.Contains( res_name ))
{
// get the resource
var res = p.Resources[res_name];
// if the resource is not in the part, do nothing
if (!part.Resources.Contains(res_name))
return;

// reduce amount and capacity
res.amount -= amount;
res.maxAmount -= capacity;
// get the resource
var res = part.Resources[res_name];

// clamp amount to capacity just in case
res.amount = Math.Min( res.amount, res.maxAmount );
// reduce amount and capacity
res.amount -= amount;
res.maxAmount -= capacity;

// if the resource is empty
if (res.maxAmount <= 0.005) //< deal with precision issues
{
// remove it
p.Resources.Remove( res );
}
// clamp amount to capacity just in case
res.amount = Math.Min(res.amount, res.maxAmount);

// if the resource is empty
if (res.maxAmount <= 0.005) //< deal with precision issues
{
var reslib = PartResourceLibrary.Instance.resourceDefinitions;
var resourceDefinition = reslib[res_name];

part.Resources.dict.Remove(resourceDefinition.name.GetHashCode());
part.SimulationResources?.dict.Remove(resourceDefinition.name.GetHashCode());

GameEvents.onPartResourceListChange.Fire(part);
}
}

Expand Down

0 comments on commit d503ee8

Please sign in to comment.