private bool crewAvailable(Offering Off)
        {
            int availableCrew = 0;
            foreach (ProtoCrewMember cr in HighLogic.CurrentGame.CrewRoster.Crew)
            {
                if (cr.rosterStatus == ProtoCrewMember.RosterStatus.Available)
                {
                    availableCrew++;
                }
            }

            if (availableCrew < Off.MinimumCrew)
            {
                ScreenMessages.PostScreenMessage("not enough crew was available for mission", 4, ScreenMessageStyle.UPPER_CENTER);
                return false;
            }

            return true;
        }
        private void dockStage1()
        {
            //print("stage1 " + Planetarium.GetUniversalTime());

            loadOfferings();

            //print(missionFolderName);
            foreach (Offering Off in OfferingsList)
            {
                //print(Off.folderName);
                if (missionFolderName == Off.folderName)
                {
                    missionOffering = Off;
                    //print("got ehm");
                }
            }
            if (missionOffering == null) { abortArrival(); return; }

            //load Offering of current mission
            //missionOffering = new Offering();
            //missionOffering.folderName = missionFolderName;
            //if (File.Exists(GamePath + CommercialOfferingsPath + "/Offerings/" + missionOffering.folderName + "/info.txt"))
            //{
            //    missionOffering.loadOffering(GamePath + CommercialOfferingsPath + "/Offerings/" + missionOffering.folderName + "/info.txt");
            //}

            if (offeringAllowed(missionOffering) && crewAvailable(missionOffering))
            {
                nextLogicTime = Planetarium.GetUniversalTime();
                ArrivalStage = 2;
            }
            else
            {
                Funding.Instance.AddFunds(missionOffering.Price, TransactionReasons.VesselRecovery);
                missionUnderway = false;
                completeArrival = false;

                nextLogicTime = 0;
                ArrivalStage = -1;
            }
        }
        public void procureOffering(Offering Off,bool repeat)
        {
            if (missionUnderway == true) { ScreenMessages.PostScreenMessage("already a mission underway", 4, ScreenMessageStyle.UPPER_CENTER); return; }
            if (!offeringAllowed(Off)) { ScreenMessages.PostScreenMessage("not rated for this orbit", 4, ScreenMessageStyle.UPPER_CENTER); return; }

            if (HighLogic.CurrentGame.Mode == Game.Modes.CAREER)
            {
                if (Funding.Instance.Funds > Off.Price)
                {
                    Funding.Instance.AddFunds(-Off.Price, TransactionReasons.VesselRollout);
                }
                else
                {
                    ScreenMessages.PostScreenMessage("insufficient funds", 4, ScreenMessageStyle.UPPER_CENTER);
                    return;
                }
            }

            missionUnderway = true;
            missionFolderName = Off.folderName;
            if (!repeat)
            {
                missionArrivalTime = (float)(Planetarium.GetUniversalTime() + Off.Time);
                missionCrewCount = intCrewCount;
            }
            else
            {
                missionArrivalTime = (float)(Planetarium.GetUniversalTime() + (Off.Time > (missionRepeatDelay * 21600) ? Off.Time : (missionRepeatDelay * 21600)));
            }

            SMAsave = (float)vessel.orbit.semiMajorAxis;
            ECCsave = (float)vessel.orbit.eccentricity;
            INCsave = (float)vessel.orbit.inclination;

            selectedOffering = Off;
            closeGUIMission();
            closeGUIOffering();
        }
 private bool autoDepartAllowed(Offering Off)
 {
     if (offeringAllowed(Off))
     {
         return (true);
     }
     else
     {
         ScreenMessages.PostScreenMessage("not rated for this orbit", 4, ScreenMessageStyle.UPPER_CENTER);
         return (false);
     }
 }
        public void loadOfferings()
        {
            OfferingsList.Clear();

            //print("loading offerings");
            //load standard offerings
            //            string[] directoryOfferings = Directory.GetDirectories(GamePath + CommercialOfferingsPath + "/Offerings");
            //
            //            foreach (String dir in directoryOfferings)
            //            {
            //                //print(dir);
            //                Offering Off = new Offering();
            //
            //                Off.folderName = Path.GetFileName(dir);
            //                if (File.Exists(GamePath + CommercialOfferingsPath + "/Offerings/" + Off.folderName + "/info.txt"))
            //                {
            //                    Off.loadOffering(GamePath + CommercialOfferingsPath + "/Offerings/" + Off.folderName + "/info.txt");
            //
            //                    if (offeringAllowed(Off))
            //                    {
            //                        OfferingsList.Add(Off);
            //                        //print("loaded " + Off.Name);
            //                    }
            //                }
            //            }

            //print("aaa " + GamePath);

            loadOfferingsDirectory(GamePath + "/GameData");

            if (missionFolderName != "")
            {
                foreach (Offering Off in OfferingsList)
                {
                    if (Off.folderName == missionFolderName)
                    {
                        selectedOffering = Off;
                    }
                }
            }
        }
        /// <summary>
        /// GUI Offering
        /// </summary>
        /// <param name="windowID"></param>
        private void WindowGUIOffering(int windowID)
        {
            GUI.DragWindow(new Rect(0, 0, 500, 30));
            GUILayout.BeginVertical();

            if (!isACampaign(GUIOffering))
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("Company:", labelStyle, GUILayout.Width(100));
                GUILayout.Label(GUIOffering.CompanyName, labelStyle, GUILayout.Width(200));
                GUILayout.EndHorizontal();
            }

            GUILayout.BeginHorizontal();
            GUILayout.Label("Vehicle:", labelStyle, GUILayout.Width(100));
            GUILayout.Label(GUIOffering.VehicleName, labelStyle, GUILayout.Width(200));
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Launch System:", labelStyle, GUILayout.Width(100));
            GUILayout.Label(GUIOffering.LaunchSystemName, labelStyle, GUILayout.Width(200));
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Price:", labelStyle, GUILayout.Width(100));
            GUILayout.Label(GUIOffering.Price.ToString() + ((GUIOffering.VehicleReturnFee > 0) ? " (" + (GUIOffering.Price - GUIOffering.VehicleReturnFee).ToString() + " with vehicle return)" : ""), labelStyle, GUILayout.Width(250));
            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Arrival in:", labelStyle, GUILayout.Width(100));
            GUILayout.Label(timeString(GUIOffering.Time), labelStyle, GUILayout.Width(200));
            GUILayout.EndHorizontal();
            if (GUIOffering.MinimumCrew > 0)
            {
                if (GUIOffering.MinimumCrew < GUIOffering.MaximumCrew)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("Minimal crew required:", labelStyle, GUILayout.Width(150));
                    GUILayout.Label(GUIOffering.MinimumCrew.ToString(), labelStyle, GUILayout.Width(200));
                    GUILayout.EndHorizontal();

                    GUILayout.BeginHorizontal();
                    GUILayout.Label("Maximum crew capacity:", labelStyle, GUILayout.Width(150));
                    GUILayout.Label(GUIOffering.MaximumCrew.ToString(), labelStyle, GUILayout.Width(200));
                    GUILayout.EndHorizontal();
                }
                else
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Label("Crew:", labelStyle, GUILayout.Width(100));
                    GUILayout.Label(GUIOffering.MinimumCrew.ToString(), labelStyle, GUILayout.Width(200));
                    GUILayout.EndHorizontal();
                }
            }
            else if (GUIOffering.MinimumCrew < GUIOffering.MaximumCrew)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("Crew capacity:", labelStyle, GUILayout.Width(100));
                GUILayout.Label(GUIOffering.MaximumCrew.ToString(), labelStyle, GUILayout.Width(200));
                GUILayout.EndHorizontal();
            }
            else if (GUIOffering.MinimumCrew == 0 && GUIOffering.MaximumCrew == 0)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("Unmanned", labelStyle, GUILayout.Width(100));
                GUILayout.EndHorizontal();
            }
            if (!GUIOffering.ReturnEnabled)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("No return mission", labelStyle, GUILayout.Width(300));
                GUILayout.EndHorizontal();
            }
            else if (!GUIOffering.SafeReturn)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("No safe return mission", labelStyle, GUILayout.Width(300));
                GUILayout.EndHorizontal();
            }
            if (GUIOffering.ReturnResources != "")
            {
                GUILayout.Label("Required return resources: ", labelStyle, GUILayout.Width(300));
                string[] SplitArray = GUIOffering.ReturnResources.Split(',');
                foreach (String st in SplitArray)
                {
                    string[] SplatArray = st.Split(':');
                    string resourceName = SplatArray[0].Trim();
                    double amount = Convert.ToDouble(SplatArray[1].Trim());
                    GUILayout.Label("   " + resourceName + ": " + RoundToSignificantDigits(amount,4).ToString(), labelStyle, GUILayout.Width(300));
                }
            }

            if (GUIOffering.ReturnCargoMass != 0.0)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("Return cargo mass: " + RoundToSignificantDigits(GUIOffering.ReturnCargoMass,3).ToString(), labelStyle, GUILayout.Width(300));
                GUILayout.EndHorizontal();
            }
            GUILayout.BeginHorizontal();
            GUILayout.Label("", redlabelStyle, GUILayout.Width(430));
            if (GUILayout.Button("Delete", buttonStyle, GUILayout.Width(70), GUILayout.Height(22)))
            {
                if (Directory.Exists(GamePath + GUIOffering.folderName))
                {
                    DeleteDirectory(GamePath + GUIOffering.folderName);
                    closeGUIOffering();
                    loadOfferings();
                }
            }

            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            if (!missionUnderway)
            {
                if (GUILayout.Button("Procure", buttonStyle, GUILayout.Width(300), GUILayout.Height(22)))
                {
                    if (!missionUnderway)
                    {
                        GUIMission = GUIOffering;
                        openGUIMission();
                    }
                }
            }
            if (GUILayout.Button("Close", buttonStyle, GUILayout.Width(200), GUILayout.Height(22)))
            {
                closeGUIOffering();
            }
            GUILayout.EndHorizontal();

            GUILayout.EndVertical();
        }
        private bool returnResourcesAvailable(Offering Off)
        {
            if (Off.ReturnResources == "") { return (true); }

            string[] SplitArray = Off.ReturnResources.Split(',');

            string[] arrResource = new string[0];
            getProppellantArray(ref arrResource);

            foreach (String st in SplitArray)
            {
                string[] SplatArray = st.Split(':');
                string resourceName = SplatArray[0].Trim();
                double amount = Convert.ToDouble(SplatArray[1].Trim());

                if (!arrResource.Contains(resourceName)) { ScreenMessages.PostScreenMessage("vessel in unknown configuration for return", 4, ScreenMessageStyle.UPPER_CENTER); return (false); }

                if (amount * 0.99 > readResource(vessel, resourceName)) { ScreenMessages.PostScreenMessage("not enough resources for return", 4, ScreenMessageStyle.UPPER_CENTER); return (false); }
            }

            return (true);
        }
        /// <summary>
        /// GUI Main
        /// </summary>
        /// <param name="windowID"></param>
        private void WindowGUIMain(int windowID)
        {
            GUI.DragWindow(new Rect(0, 0, 200, 30));
            GUILayout.BeginVertical();

            GUILayout.BeginHorizontal();

            GUILayout.BeginVertical();
            GUILayout.Label("Current:", labelStyle, GUILayout.Width(200));
            if (GUILayout.Button(selectedOffering.Name, buttonStyle, GUILayout.Width(200), GUILayout.Height(22)))
            {
                GUIOffering = selectedOffering;
                intCrewCount = missionCrewCount;
                openGUIOffering();
            }
            if (missionUnderway)
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label("Underway", labelStyle, GUILayout.Width(100));
                if (GUILayout.Button("Cancel", buttonStyle, GUILayout.Width(100), GUILayout.Height(22)))
                {
                    if (HighLogic.CurrentGame.Mode == Game.Modes.CAREER)
                    {
                        if (Planetarium.GetUniversalTime() < missionArrivalTime - selectedOffering.Time + 3600)
                        {
                            if (Funding.Instance.Funds > GUIOffering.Price)
                            {
                                Funding.Instance.AddFunds(GUIOffering.Price, TransactionReasons.VesselRecovery);
                            }
                        }
                    }
                    missionUnderway = false;
                }
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                GUILayout.Label("ETA:", labelStyle, GUILayout.Width(35));
                GUILayout.Label(timeETAString(missionArrivalTime - Planetarium.GetUniversalTime()), labelStyle, GUILayout.Width(165));
                GUILayout.EndHorizontal();
            }
            else
            {
                GUILayout.Label("", labelStyle, GUILayout.Width(100));
            }
            GUILayout.EndVertical();

            GUILayout.Label("  ", labelStyle, GUILayout.Width(10));

            GUILayout.BeginVertical();
            missionRepeat = GUILayout.Toggle(missionRepeat, "Repeat");
            GUILayout.Label("Repeat Delay:", labelStyle, GUILayout.Width(80));
            GUILayout.BeginHorizontal();

            if (GUILayout.Button("<", buttonStyle, GUILayout.Width(15), GUILayout.Height(15)))
            {
                if (missionRepeatDelay >= 2) { missionRepeatDelay -= (missionRepeatDelay / 10 > 1 ? missionRepeatDelay/10 : 1); }
            }
            GUILayout.Label( missionRepeatDelay.ToString(), labelStyle, GUILayout.Width(25));
            if (GUILayout.Button(">", buttonStyle, GUILayout.Width(15), GUILayout.Height(15)))
            {
                if (missionRepeatDelay <= 999) { missionRepeatDelay += (missionRepeatDelay / 10 > 1 ? missionRepeatDelay / 10 : 1); }
                if (missionRepeatDelay > 999) { missionRepeatDelay = 999; }
            }
            GUILayout.Label("days", labelStyle, GUILayout.Width(30));

            GUILayout.EndHorizontal();
            GUILayout.EndVertical();

            GUILayout.EndHorizontal();

            scrollPositionAvailableCommercialOfferings = GUILayout.BeginScrollView(scrollPositionAvailableCommercialOfferings, false, true, GUILayout.Width(200), GUILayout.Height(300));
            foreach (Offering Off in OfferingsList)
            {
                if (GUILayout.Button(Off.Name, buttonStyle, GUILayout.Width(165), GUILayout.Height(22)))
                {
                    GUIOffering = Off;
                    if (GUIOffering.MinimumCrew > 0)
                    {
                        if (GUIOffering.MinimumCrew < GUIOffering.MaximumCrew)
                        {
                            intCrewCount = GUIOffering.MaximumCrew;
                        }
                        else
                        {
                            intCrewCount = GUIOffering.MinimumCrew;
                        }
                    }
                    else if (GUIOffering.MinimumCrew < GUIOffering.MaximumCrew)
                    {
                        intCrewCount = GUIOffering.MaximumCrew;
                    }
                    else if (GUIOffering.MinimumCrew == 0 && GUIOffering.MaximumCrew == 0)
                    {
                        intCrewCount = 0;
                    }

                    openGUIOffering();
                }
            }
            GUILayout.EndScrollView();

            //development mode buttons
            if (DevMode)
            {
                if (GUILayout.Button("Docking Stage 2"))
                {
                    dockStage2();
                }

                if (GUILayout.Button("Docking Stage 3"))
                {
                    dockStage3();
                }
                if (GUILayout.Button("Save vessel"))
                {
                    ConfigNode savenode = new ConfigNode();
                    vessel.BackupVessel();
                    vessel.protoVessel.Save(savenode);
                    savenode.Save(GamePath + CommercialOfferingsPath + "/Missions/DevMode/vesselfile");
                }
            }
            //

            if (GUILayout.Button("Close", buttonStyle, GUILayout.Width(200), GUILayout.Height(22)))
            {
                closeGUIMain();
            }
            GUILayout.EndVertical();
        }
 private bool offeringAllowed(Offering Off)
 {
     if (vessel.mainBody.name == Off.Body && (vesselOrbitAltitude() < Off.MaxOrbitAltitude || Off.MaxOrbitAltitude == 0) && isCurrentCampaign(Off))
     {
         return (true);
     }
     else
     {
         return (false);
     }
 }
 private bool returnCargoMassNotExceeded(Offering Off)
 {
     if (Off.ReturnCargoMass == 0.0 || getCargoMass() <= Off.ReturnCargoMass * 1.1)
     {
         return true;
     }
     else
     {
         ScreenMessages.PostScreenMessage("cargo mass exceeds rated amount", 4, ScreenMessageStyle.UPPER_CENTER);
         return false;
     }
 }
 private bool minimalCrewPresent(Offering Off)
 {
     if (Off.MinimumCrew == 0) { return (true); }
     if (Off.MinimumCrew > astronautCrewCount(vessel)) { ScreenMessages.PostScreenMessage("not enough crew for return", 4, ScreenMessageStyle.UPPER_CENTER); return (false); }
     if (!Off.SafeReturn && crewCount(vessel) > 0) { ScreenMessages.PostScreenMessage("not rated for safe crew return", 4, ScreenMessageStyle.UPPER_CENTER); return (false); }
     return (true);
 }
        private void loadOfferingsDirectory(string searchDirectory)
        {
            if (File.Exists(searchDirectory + "/CommercialOfferingsPackMarkerFile"))
            {
                string[] directoryOfferings = Directory.GetDirectories(searchDirectory);

                foreach (String dir in directoryOfferings)
                {

                    Offering Off = new Offering();

                    Off.folderName = dir.Substring(GamePath.ToString().Length, dir.Length - GamePath.ToString().Length);

                    if (File.Exists(GamePath + Off.folderName + "/info.txt"))
                    {
                        Off.loadOffering(GamePath + Off.folderName + "/info.txt");

                        if (offeringAllowed(Off))
                        {
                            OfferingsList.Add(Off);
                            //print("loaded " + Off.Name);
                        }
                    }
                }
            }
            else
            {
                string[] searchDirectories = Directory.GetDirectories(searchDirectory);

                foreach (String dir in searchDirectories)
                {
                    loadOfferingsDirectory(dir);
                }
            }
        }
 private bool isCurrentCampaign(Offering off)
 {
     if (isACampaign(off))
     {
         if (off.CompanyName.Length >= 14 && off.CompanyName.Substring(14) == HighLogic.SaveFolder)
         {
             return (true);
         }
     }
     else
     {
         return (true);
     }
     return (false);
 }
 private bool isACampaign(Offering off)
 {
     if (off.CompanyName.Length >= 14 && off.CompanyName.Substring(0, 14) == "KSPCAMPAIGN:::")
         return (true);
     return (false);
 }
        private bool handleCommercialVehicleMode()
        {
            if (commercialvehiclemode && !commercialvehicleOfferingLoaded && commercialvehicleFolderName != "")
            {
                loadOfferings();

                foreach (Offering Off in OfferingsList)
                {
                    if (commercialvehicleFolderName == Off.folderName)
                    {
                        commercialvehicleOffering = Off;
                        commercialvehicleOfferingLoaded = true;
                        return false;
                    }
                }

                //commercialvehicleOffering.folderName = commercialvehicleFolderName;
                //if (File.Exists(GamePath + CommercialOfferingsPath + "/Offerings/" + commercialvehicleOffering.folderName + "/info.txt"))
                //{
                //    commercialvehicleOffering.loadOffering(GamePath + CommercialOfferingsPath + "/Offerings/" + commercialvehicleOffering.folderName + "/info.txt");
                //}
                //else
                //{
                //    print("Commercial Offerings Plugin unable to load " + commercialvehicleOffering.folderName);
                //    return;
                //}
                return (true);
            }

            if (commercialvehiclemode)
            {
                if (vehicleAutoDepart && !checkDocked())
                {
                    if (!vessel.isActiveVessel)
                    {
                        handleAutoDepart(); return false;
                    }
                    else
                    {
                        foreach (Vessel ves in FlightGlobals.Vessels)
                        {
                            if (!ves.packed && ves.loaded && ves.id.ToString() != vessel.id.ToString())
                            {
                                FlightGlobals.SetActiveVessel(ves);
                                nextLogicTime = Planetarium.GetUniversalTime() + 1;
                                return false;
                            }
                        }
                        vehicleAutoDepart = false;
                        nextLogicTime = 0;
                        ScreenMessages.PostScreenMessage("no other asset in vicinity", 4, ScreenMessageStyle.UPPER_CENTER);
                    }
                }
            }
            return true;
        }