예제 #1
0
        /// <summary>
        /// Use this whenever possible. Note this cannot be used during deployment.
        /// </summary>
        internal static DeployedScienceCluster GetScienceCluster(Vessel v)
        {
            if (!Enabled)
            {
                return(null);
            }

            foreach (var cluster in DeployedScience.Instance.DeployedScienceClusters.Values)
            {
                if (v.loaded)
                {
                    Part part = null;
                    if (FlightGlobals.FindLoadedPart(cluster.ControlModulePartId, out part))
                    {
                        if (part.vessel == v)
                        {
                            return(cluster);
                        }
                    }
                }
                else
                {
                    ProtoPartSnapshot snapshot;
                    if (FlightGlobals.FindUnloadedPart(cluster.ControlModulePartId, out snapshot))
                    {
                        if (snapshot.pVesselRef == v.protoVessel)
                        {
                            return(cluster);
                        }
                    }
                }
            }

            return(null);
        }
        static bool Prefix(DeployedScienceExperiment __instance, ref bool __result)
        {
            // get private vars
            ScienceSubject subject                = Lib.ReflectionValue <ScienceSubject>(__instance, "subject");
            float          storedScienceData      = Lib.ReflectionValue <float>(__instance, "storedScienceData");
            float          transmittedScienceData = Lib.ReflectionValue <float>(__instance, "transmittedScienceData");
            Vessel         ControllerVessel       = Lib.ReflectionValue <Vessel>(__instance, "ControllerVessel");

            //Lib.Log("SendDataToComms!: " + subject.title);
            if (__instance.Experiment != null && !(__instance.ExperimentVessel == null) && subject != null && !(__instance.Cluster == null) && __instance.sciencePart.Enabled && !(storedScienceData <= 0f) && __instance.ExperimentSituationValid)
            {
                if (!__instance.TimeToSendStoredData())
                {
                    __result = true;
                    return(false);
                }
                if (ControllerVessel == null && __instance.Cluster != null)
                {
                    Lib.ReflectionCall(__instance, "SetControllerVessel");
                    ControllerVessel = Lib.ReflectionValue <Vessel>(__instance, "ControllerVessel");
                }
                Part control;
                FlightGlobals.FindLoadedPart(__instance.Cluster.ControlModulePartId, out control);
                if (control == null)
                {
                    //Lib.Log("DeployedScienceExperiment: couldn't find control module");
                    __result = true;
                    return(false);
                }
                List <Drive> drives = Drive.GetDrives(control.vessel, false);
                foreach (Drive drive in drives)
                {
                    //Lib.Log(Lib.BuildString("BREAKING GROUND -- ", subject.id, " | ", storedScienceData.ToString()));
                    if (drive.Record_file(subject.id, storedScienceData, true))
                    {
                        //Lib.Log("BREAKING GROUND -- file recorded!");
                        Lib.ReflectionValue <float>(__instance, "transmittedScienceData", transmittedScienceData + storedScienceData);
                        Lib.ReflectionValue <float>(__instance, "storedScienceData", 0f);
                        break;
                    }
                    else
                    {
                        //Lib.Log("BREAKING GROUND -- file NOT recorded!");
                        __result = true;
                        return(false);
                    }
                }
                __result = false;
            }
            return(false);            // always return false so we don't continue to the original code
        }
예제 #3
0
        public static void CreateMissingPartsInCurrentProtoVessel(Vessel vessel, ProtoVessel protoVessel)
        {
            //TODO: This is old code where we created parts dinamically but it's quite buggy. It create parts in the CURRENT vessel so it wont work for other vessels

            //We've run trough all the vessel parts and removed the ones that don't exist in the definition.
            //Now run trough the parts in the definition and add the parts that don't exist in the vessel.
            var partsToInit = new List <ProtoPartSnapshot>();

            foreach (var partSnapshot in protoVessel.protoPartSnapshots)
            {
                if (partSnapshot.FindModule("ModuleDockingNode") != null)
                {
                    //We are in a docking port part so remove it from our own vessel if we have it
                    if (FlightGlobals.FindLoadedPart(partSnapshot.persistentId, out var vesselPart))
                    {
                        vesselPart.Die();
                    }
                }

                //Skip parts that already exists
                if (FlightGlobals.FindLoadedPart(partSnapshot.persistentId, out _))
                {
                    continue;
                }

                var newPart = partSnapshot.Load(vessel, false);
                vessel.parts.Add(newPart);
                partsToInit.Add(partSnapshot);
            }

            //Init new parts. This must be done in another loop as otherwise new parts won't have their correct attachment parts.
            foreach (var partSnapshot in partsToInit)
            {
                partSnapshot.Init(vessel);
            }

            vessel.RebuildCrewList();
            MainSystem.Singleton.StartCoroutine(CallbackUtil.DelayedCallback(0.25f, () => { if (FlightGlobals.ActiveVessel)
                                                                                            {
                                                                                                FlightGlobals.ActiveVessel.SpawnCrew();
                                                                                            }
                                                                             }));
            MainSystem.Singleton.StartCoroutine(CallbackUtil.DelayedCallback(0.5f, () => { if (KerbalPortraitGallery.Instance)
                                                                                           {
                                                                                               KerbalPortraitGallery.Instance.SetActivePortraitsForVessel(FlightGlobals.ActiveVessel);
                                                                                           }
                                                                             }));
        }