private static void Reset(RCSSim engineSim) { engineSim.resourceConsumptions.Reset(); engineSim.resourceFlowModes.Reset(); engineSim.partSim = null; engineSim.actualThrust = 0; engineSim.isActive = false; engineSim.isp = 0; for (int i = 0; i < engineSim.appliedForces.Count; i++) { engineSim.appliedForces[i].Release(); } engineSim.appliedForces.Clear(); engineSim.thrust = 0; engineSim.maxMach = 0f; engineSim.isFlamedOut = false; }
public void CreateRCSSims(List <RCSSim> allRCS, double atmosphere, double mach, bool vectoredThrust, bool fullThrust, LogMsg log) { if (log != null) { log.AppendLine("CreateRCSSims for ", this.name); } List <ModuleRCS> cacheModuleRCS = part.FindModulesImplementing <ModuleRCS>(); try { if (cacheModuleRCS.Count > 0) { //find first active engine, assuming that two are never active at the same time foreach (ModuleRCS engine in cacheModuleRCS) { if (engine.isEnabled) { if (log != null) { log.AppendLine("Module: ", engine.moduleName); } RCSSim engineSim = RCSSim.New( this, engine, atmosphere, (float)mach, vectoredThrust, fullThrust, log); allRCS.Add(engineSim); } } } } catch { Debug.Log("[KER] Error Catch in CreateRCSSims"); } }
public static RCSSim New(PartSim theEngine, ModuleRCS engineMod, double atmosphere, float machNumber, bool vectoredThrust, bool fullThrust, LogMsg log) { double maxFuelFlow = engineMod.maxFuelFlow; // double minFuelFlow = engineMod.minFuelFlow; float thrustPercentage = engineMod.thrustPercentage; List <Transform> thrustTransforms = engineMod.thrusterTransforms; // List<float> thrustTransformMultipliers = engineMod.th Vector3 vecThrust = CalculateThrustVector(vectoredThrust ? thrustTransforms : null, log); FloatCurve atmosphereCurve = engineMod.atmosphereCurve; // bool atmChangeFlow = engineMod.at // FloatCurve atmCurve = engineMod.useAtmCurve ? engineMod.atmCurve : null; // FloatCurve velCurve = engineMod.useVelCurve ? engineMod.velCurve : null; // float currentThrottle = engineMod.currentThrottle; double IspG = engineMod.G; // bool throttleLocked = engineMod.throttleLocked || fullThrust; List <Propellant> propellants = engineMod.propellants; bool active = engineMod.moduleIsEnabled; // float resultingThrust = engineMod.resultingThrust; bool isFlamedOut = engineMod.flameout; RCSSim engineSim = pool.Borrow(); engineSim.isp = 0.0; engineSim.maxMach = 0.0f; engineSim.actualThrust = 0.0; engineSim.partSim = theEngine; engineSim.isActive = active; engineSim.thrustVec = vecThrust; engineSim.isFlamedOut = isFlamedOut; engineSim.resourceConsumptions.Reset(); engineSim.resourceFlowModes.Reset(); engineSim.appliedForces.Clear(); double flowRate = 0.0; if (engineSim.partSim.hasVessel) { if (log != null) { log.AppendLine("hasVessel is true"); } engineSim.isp = atmosphereCurve.Evaluate((float)atmosphere); engineSim.thrust = GetThrust(maxFuelFlow, engineSim.isp); engineSim.actualThrust = engineSim.isActive ? engineSim.thrust : 0.0; if (log != null) { log.buf.AppendFormat("isp = {0:g6}\n", engineSim.isp); log.buf.AppendFormat("thrust = {0:g6}\n", engineSim.thrust); log.buf.AppendFormat("actual = {0:g6}\n", engineSim.actualThrust); } if (true) { if (log != null) { log.AppendLine("throttleLocked is true, using thrust for flowRate"); } flowRate = GetFlowRate(engineSim.thrust, engineSim.isp); } else { // if (currentThrottle > 0.0f && engineSim.partSim.isLanded == false) // { //// TODO: This bit doesn't work for RF engines //if (log != null) log.AppendLine("throttled up and not landed, using actualThrust for flowRate"); // flowRate = GetFlowRate(engineSim.actualThrust, engineSim.isp); // } // else // { // if (log != null) log.AppendLine("throttled down or landed, using thrust for flowRate"); // flowRate = GetFlowRate(engineSim.thrust, engineSim.isp); // } } } else { if (log != null) { log.buf.AppendLine("hasVessel is false"); } engineSim.isp = atmosphereCurve.Evaluate((float)atmosphere); engineSim.thrust = GetThrust(maxFuelFlow, engineSim.isp); engineSim.actualThrust = 0d; if (log != null) { log.buf.AppendFormat("isp = {0:g6}\n", engineSim.isp); log.buf.AppendFormat("thrust = {0:g6}\n", engineSim.thrust); log.buf.AppendFormat("actual = {0:g6}\n", engineSim.actualThrust); log.AppendLine("no vessel, using thrust for flowRate"); } flowRate = GetFlowRate(engineSim.thrust, engineSim.isp); } if (log != null) { log.buf.AppendFormat("flowRate = {0:g6}\n", flowRate); } float flowMass = 0f; for (int i = 0; i < propellants.Count; ++i) { Propellant propellant = propellants[i]; if (!propellant.ignoreForIsp) { flowMass += propellant.ratio * ResourceContainer.GetResourceDensity(propellant.id); } } if (log != null) { log.buf.AppendFormat("flowMass = {0:g6}\n", flowMass); } for (int i = 0; i < propellants.Count; ++i) { Propellant propellant = propellants[i]; if (propellant.name == "ElectricCharge" || propellant.name == "IntakeAir") { continue; } double consumptionRate = propellant.ratio * flowRate / flowMass; if (log != null) { log.buf.AppendFormat( "Add consumption({0}, {1}:{2:d}) = {3:g6}\n", ResourceContainer.GetResourceName(propellant.id), theEngine.name, theEngine.partId, consumptionRate); } engineSim.resourceConsumptions.Add(propellant.id, consumptionRate); engineSim.resourceFlowModes.Add(propellant.id, (double)propellant.GetFlowMode()); } for (int i = 0; i < thrustTransforms.Count; i++) { Transform thrustTransform = thrustTransforms[i]; Vector3d direction = thrustTransform.forward.normalized; Vector3d position = thrustTransform.position; AppliedForce appliedForce = AppliedForce.New(direction * engineSim.thrust, position); engineSim.appliedForces.Add(appliedForce); } return(engineSim); }
// This function activates the next stage // currentStage must be updated before calling this function private void ActivateStage() { // Build a set of all the parts that will be decoupled decoupledParts.Clear(); for (int i = 0; i < allParts.Count; ++i) { PartSim partSim = allParts[i]; if (partSim.decoupledInStage >= currentStage) { decoupledParts.Add(partSim); } } foreach (PartSim partSim in decoupledParts) { // Remove it from the all parts list allParts.Remove(partSim); partSim.Release(); if (partSim.isEngine) { // If it is an engine then loop through all the engine modules and remove all the ones from this engine part for (int i = allEngines.Count - 1; i >= 0; i--) { EngineSim engine = allEngines[i]; if (engine.partSim == partSim) { allEngines.RemoveAt(i); engine.Release(); } } } if (partSim.isRCS) { // If it is an engine then loop through all the engine modules and remove all the ones from this engine part for (int i = allRCS.Count - 1; i >= 0; i--) { RCSSim engine = allRCS[i]; if (engine.partSim == partSim) { allRCS.RemoveAt(i); engine.Release(); } } } // If it is a fuel line then remove it from the list of all fuel lines if (partSim.isFuelLine) { allFuelLines.Remove(partSim); } } // Loop through all the (remaining) parts for (int i = 0; i < allParts.Count; ++i) { // Ask the part to remove all the parts that are decoupled allParts[i].RemoveAttachedParts(decoupledParts); } // Now we loop through all the engines and activate those that are ignited in this stage for (int i = 0; i < allEngines.Count; ++i) { EngineSim engine = allEngines[i]; if (engine.partSim.inverseStage == currentStage) { engine.isActive = true; } } }