Jump to content

Fuel Switch


KSRe-dev

Recommended Posts

There's a basic fuel switch in KSRe that someone else may be interested in as a starting point. I don't have time to develop this further, and it's definitely worthy of a separate mod.

 

inside the spacewarp plugin:

private GameInstance game;
private ResourceContainerChangedMessage lastResMsg;
private ushort fuelContIDVal;
internal static List<ResourceDefinitionID> fuelList;

public override void OnInitialized() {
	game = GameManager.Instance.Game;
	game.Messages.PersistentSubscribe<GameStateChangedMessage>(ChangedGameState);
	game.Messages.PersistentSubscribe<UISliderReleasedMessage>(ReleasedUISlider);
	game.Messages.PersistentSubscribe<ResourceContainerChangedMessage>(ChangedResCont);
}

  public void Update() {
            if (game?.GlobalGameState?.GetGameState().GameState != GameState.FlightView) return;
            if (!game?.ResourceManager?.IsVisible ?? false) return;
            foreach (PartComponent part in game?.ViewController?.GetActiveVehicle()?.GetSimVessel()?.SimulationObject.PartOwner.Parts) {
                if (part == null) continue;
                ResourceContainer resCont = part.PartResourceContainer;
                if (resCont == null) continue;
                if (resCont.GetResourcesContainedCount() == 0 || part.PartResourceContainer.GetStoredResourcesTotalMass() == 0) continue;
                if (!fuelList.All(f => resCont.IsResourceContained(f))) continue;
                if (fuelList.Where(r => resCont.GetResourceStoredMass(r) > 0).Count() > 1)
                    resCont.DumpAllResources();
            }
        }
  
  private void ChangedGameState(MessageCenterMessage m) {
            if (fuelContIDVal != 0) return;
            if (game.ResourceDefinitionDatabase.GetAllResourceIDs().Count() == 0) return;
            // stock bug (v0.2.0): game.ResourceDefinitionDatabase.GetResourceIDFromName()
            ResourceDefinitionID recipeID = game.ResourceDefinitionDatabase.GetAllResourceIDs().FirstOrDefault(r => game.ResourceDefinitionDatabase.GetDefinitionData(r).name == "FuelContainer");
            if (recipeID.Value == 0) return;
            fuelContIDVal = recipeID.Value;
            fuelList = game.ResourceDefinitionDatabase.GetDefinitionData(recipeID).recipeProperties.IngredientsResourceIDs.ToList();
        }
  
  private void ChangedResCont(MessageCenterMessage m) {
            if (game.GlobalGameState.GetGameState().GameState != GameState.VehicleAssemblyBuilder) return;
            ResourceContainerChangedMessage resMsg = (ResourceContainerChangedMessage)m;
            if (resMsg == null) return;
            lastResMsg = resMsg;
        }
  
        private void ReleasedUISlider(MessageCenterMessage m) {
            if (game.GlobalGameState.GetGameState().GameState != GameState.VehicleAssemblyBuilder) return;
            if (lastResMsg == null) return;
            if (!game.ResourceDefinitionDatabase.GetRecipesForIngredient(lastResMsg.ResourceId, out List<ResourceDefinitionID> recipeList)) return;
            if (!recipeList.Any(r => r.Value == fuelContIDVal)) return;
            fuelList.Where(r => r != lastResMsg.ResourceId).ToList().ForEach(r => lastResMsg.Container.DumpResource(r));
            lastResMsg = null;
        }
  
  
  

 

harmony patch for ui


  
  [HarmonyPatch(typeof(PartInfoOverlay), "PopulateResourceInfoFromPart")]
  [HarmonyPostfix]
  public static void FixFuelContainerTotal(List<KeyValuePair<string, string>> dict, IObjectAssemblyResource[] resourceArray) {
            if (!resourceArray.Any(r => r.Name == "FuelContainer")) return;
            dict[dict.Count - 1] = new KeyValuePair<string, string>(dict.Last().Key, $"~{resourceArray.First(r => r.Name == "FuelContainer").Capacity} {Units.SymbolTonne}");
        }

patch manager patch for the container recipe, and an example to add it to tanks (will need some customization there)

@new("FuelContainer",true)
:resources {
	displayNameKey: "Resource/DisplayName/FuelContainer";
	abbreviationKey: "Resource/Abbreviation/FC";
	+Oxidizer { unitsPerRecipeUnit: 1.0; }
	+Monopropellant { unitsPerRecipeUnit: 1.2; }
	+Methane { unitsPerRecipeUnit: 0.8; }
	+Hydrogen { unitsPerRecipeUnit: 0.0625; }
	vfxFuelType: "Pressurized";
}
#fueltank_* {
		resourceContainers[0]: $value:set(name,"FuelContainer");
		resourceContainers[0]: $value:set(initialUnits,0.0);
	}
// .. etc

 

The way this works in game is by creating a container with 4 fuels. When one is filled, the others are emptied.

1xxmeXc.gif

it also dumps the other fuels in the same way during resource transfers in flight.

so currently only one fuel can be used in a container, that's just what i needed with this design. the remaining issue is that the fuel bar in the stage stack is never filled completely as it contains the empty containers.

 

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