bool FindEjector(Part p, out ModuleKrEjectPilot mkep)
 {
     mkep = null;
     foreach (PartModule m in p.Modules)
     {
         if (m.moduleName == "ModuleKrEjectPilot")
         {
             mkep = (ModuleKrEjectPilot)m;
             Log.detail("EjectorFound on command pod");
             if (mkep.maxUses > 0)
             {
                 //ejectorFound = true;
                 mkep.maxUses--;
                 ejectionForce = mkep.ejectionForce;
                 forceCnt      = 60;
                 Log.detail("Max uses: {0}   EjectionForce: {1}", mkep.maxUses, ejectionForce);
                 return(true);
             }
         }
     }
     return(false);
 }
Пример #2
0
 bool FindEjector(Part p, out ModuleKrEjectPilot mkep)
 {
     mkep = null;
     foreach (PartModule m in p.Modules)
     {
         if (m.moduleName == "ModuleKrEjectPilot")
         {
             mkep = (ModuleKrEjectPilot)m;
             Log.Info("EjectorFound on command pod");
             if (mkep.maxUses > 0)
             {
                 //ejectorFound = true;
                 mkep.maxUses--;
                 ejectionForce = mkep.ejectionForce;
                 forceCnt      = 60;
                 Log.Info("Max uses: " + mkep.maxUses.ToString() + "   EjectionForce: " + ejectionForce.ToString());
                 return(true);
             }
         }
     }
     return(false);
 }
        void Update()
        {
            if (vessel == null)
            {
                return;
            }
            double d = Planetarium.GetUniversalTime();

            if (d < lastTime + DELAY)
            {
                return;
            }

            lastTime   = d;
            origVessel = vessel;
            ModuleKrEjectPilot mkep = null;

            foreach (Part p in vessel.parts)
            {
                Log.dbg("part: {0}", p.partInfo);
                if (p.protoModuleCrew.Count == 0)
                {
                    Log.detail("nobody inside");
                    continue;
                }
                bool ejectorFound = false;

                foreach (Part childp in p.FindChildParts <Part>(false))
                {
                    ejectorFound = FindEjector(childp, out mkep);

#if false
                    foreach (PartModule m in childp.Modules)
                    {
                        if (m.moduleName == "ModuleKrEjectPilot")
                        {
                            mkep = (ModuleKrEjectPilot)m;
                            Log.Info("EjectorFound on command pod");
                            if (mkep.maxUses > 0)
                            {
                                ejectorFound = true;
                                mkep.maxUses--;
                                ejectionForce = mkep.ejectionForce;
                                forceCnt      = 60;
                                Log.Info("Max uses: " + mkep.maxUses.ToString() + "   EjectionForce: " + ejectionForce.ToString());
                                break;
                            }
                        }
                    }
#endif
                    if (ejectorFound)
                    {
                        break;
                    }
                }
                // This is for the case where the ejection module has been added to the command pod directly
                if (!ejectorFound)
                {
                    // If an ejection part IS mounted, but is usedup, mkep will not be null
                    // This will give the ability to override a built-in ejection module
                    if (mkep == null)
                    {
                        ejectorFound = FindEjector(p, out mkep);
                    }
#if false
                    foreach (PartModule m in p.Modules)
                    {
                        if (m.moduleName == "ModuleKrEjectPilot")
                        {
                            mkep = (ModuleKrEjectPilot)m;
                            Log.Info("EjectorFound on command pod");
                            if (mkep.maxUses > 0)
                            {
                                ejectorFound = true;
                                mkep.maxUses--;
                                ejectionForce = mkep.ejectionForce;
                                forceCnt      = 60;
                                Log.Info("Max uses: " + mkep.maxUses.ToString() + "   EjectionForce: " + ejectionForce.ToString());
                                break;
                            }
                        }
                    }
#endif
                    if (!ejectorFound)
                    {
                        Log.warn("Ejector Not found on command pod");
                        continue;
                    }
                }

                // Look through all the available crew until we find one which can be ejected
                Log.trace("look for kerbal to eject");
                foreach (ProtoCrewMember kerbal in p.protoModuleCrew)
                {
                    KerbalEVA spawned = FlightEVA.fetch.spawnEVA(kerbal, p, p.airlock, true);
                    if (!spawned)
                    {
                        // if false, then the exit was blocked by something
                        allSpawned = false;
                        Log.trace("notSpawned");
                    }
                    else
                    {
                        spawned.autoGrabLadderOnStart = false;

                        // Look for the kerbal in all the vessels so we can
                        // add the parachute module and mark it as deployable
                        for (int i = FlightGlobals.Vessels.Count - 1; i >= 0; i--)
                        {
                            if (kerbal.name == FlightGlobals.Vessels[i].vesselName)
                            {
                                Log.detail("DoEjections.Update, adding parachute to {0}", kerbal.name);
                                bool b = true;
                                foreach (Part prt in FlightGlobals.Vessels[i].parts)
                                {
                                    // Check to see if this kerbal already has a parachute module, if it doesn't
                                    // then add it
                                    foreach (PartModule m in prt.Modules)
                                    {
                                        if (m.moduleName == "ModuleKrKerbalParachute")
                                        {
                                            b = false;
                                            break;
                                        }
                                    }
                                }
                                if (b)
                                {
                                    FlightGlobals.Vessels[i].rootPart.AddModule("ModuleKrKerbalParachute");
                                }

                                ModuleKrKerbalParachute mkkp = (ModuleKrKerbalParachute)FlightGlobals.Vessels[i].rootPart.Modules["ModuleKrKerbalParachute"];
                                mkkp.deployedDrag         = mkep.deployedDrag;
                                mkkp.minAirPressureToOpen = mkep.minAirPressureToOpen;
                                mkkp.semiDeployedFraction = mkep.semiDeployedFraction;
                                mkkp.deployTime           = mkep.deployTime;
                                mkkp.deployHeight         = mkep.deployHeight;
                                Log.trace("Update.deployHeight: {0}", mkep.deployHeight);
                                mkkp.deployed = true;

                                //mkkp.rigidbody = this.

                                Log.trace("mkep.selectedChute: {0}", mkep.selectedChute);
                                if (mkep.selectedChute >= 0 && mkep.selectedChute < mkep.arrChuteDir.Length)
                                {
                                    mkkp.chuteDir = mkep.arrChuteDir[mkep.selectedChute];
                                }

                                if (mkep.arrChuteDir[mkep.selectedChute] == "parasailChute")
                                {
                                    mkkp.parasail = true;
                                }

                                //Log.Info("mkkp.deployedDrag: " + mkkp.deployedDrag.ToString() + "   mkep.deployedDrag: " + mkep.deployedDrag.ToString());
                                //Log.Info("mkkp.minAirPressureToOpen: " + mkkp.minAirPressureToOpen.ToString() + "   mkep.minAirPressureToOpen: " + mkep.minAirPressureToOpen.ToString());
                                //Log.Info("mkkp.semiDeployedFraction: " + mkkp.semiDeployedFraction.ToString() + "   mkep.semiDeployedFraction: " + mkep.semiDeployedFraction.ToString());
                                //Log.Info("mkkp.deployTime: " + mkkp.deployTime.ToString() + "   mkep.deployTime: " + mkep.deployTime.ToString());

                                mkkp.DeployWhenAble(PartModule.StartState.Flying, origVessel, FlightGlobals.Vessels[i].rootPart.name);
                                ejectedKerbal = FlightGlobals.Vessels[i];
                                distance      = Vector3.Distance(ejectedKerbal.rootPart.transform.position, origVessel.rootPart.transform.position);

                                if (EjectionSound != null)
                                {
                                    if (!GameDatabase.Instance.ExistsAudioClip(ejectionSoundFile))
                                    {
                                        Debug.LogError("DoEjections: Audio file not found: " + ejectionSoundFile);
                                    }
                                    else
                                    {
                                        EjectionSound.audio.clip = GameDatabase.Instance.GetAudioClip(ejectionSoundFile);
                                        soundVolume = GameSettings.SHIP_VOLUME * ejectionVolume;
                                        //soundPitch = Mathf.Lerp(0.5f, 1f, 1f);
                                        soundPitch = 1f;
                                        EjectionSound.audio.pitch  = soundPitch;
                                        EjectionSound.audio.volume = mkep.Volume;
                                        //RcsSound.audio.loop = true;

                                        EjectionSound.audio.PlayOneShot(EjectionSound.audio.clip);
                                        // if (!RcsSound.audio.isPlaying)
                                        //RcsSound.audio.Play();
                                    }
                                }

                                return;
                            }
                        }
                    }
                }
            }
            if (allSpawned)
            {
                vessel = null;
            }
        }