public FacilityUpgrade ToFacilityUpgrade()
        {
            // KSP doesn't support automatically persisting nullable enum values.
            // The following code is a workaround for that.
            SpaceCenterFacility?facilityType = null;

            try
            {
                if (!string.IsNullOrEmpty(sFacilityType))
                {
                    facilityType = (SpaceCenterFacility)Enum.Parse(typeof(SpaceCenterFacility), sFacilityType);
                }
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
            }

            var ret = new FacilityUpgrade
            {
                FacilityType     = facilityType,
                UpgradeLevel     = upgradeLevel,
                CurrentLevel     = currentLevel,
                LaunchpadID      = launchpadID,
                Id               = id,
                CommonName       = commonName,
                Progress         = progress,
                BP               = BP,
                Cost             = cost,
                UpgradeProcessed = UpgradeProcessed,
                IsLaunchpad      = isLaunchpad
            };

            return(ret);
        }
 public FacilityUpgradeStorageItem FromFacilityUpgrade(FacilityUpgrade fu)
 {
     sFacilityType    = fu.FacilityType?.ToString();
     upgradeLevel     = fu.UpgradeLevel;
     currentLevel     = fu.CurrentLevel;
     launchpadID      = fu.LaunchpadID;
     id               = fu.Id;
     commonName       = fu.CommonName;
     progress         = fu.Progress;
     BP               = fu.BP;
     cost             = fu.Cost;
     UpgradeProcessed = fu.UpgradeProcessed;
     isLaunchpad      = fu.IsLaunchpad;
     return(this);
 }
Esempio n. 3
0
        public static void DrawNewPadWindow(int windowID)
        {
            if (_padCosts == null || _padLvlOptions == null)
            {
                LoadPadNamesAndCosts();
            }

            GUILayout.BeginVertical();
            GUILayout.Label("Name:");
            _newName = GUILayout.TextField(_newName);

            GUILayout.Label("Pad level:");
            _selectedPadIdx = GUILayout.SelectionGrid(_selectedPadIdx, _padLvlOptions, 1);

            double curPadCost      = _padCosts[_selectedPadIdx];
            double curPadBuildTime = FacilityUpgrade.CalculateBuildTime(curPadCost, SpaceCenterFacility.LaunchPad);
            string sBuildTime      = KSPUtil.PrintDateDelta(curPadBuildTime, true);

            GUILayout.Label($"It will cost {Math.Round(curPadCost, 2):N} funds to build the new launchpad. " +
                            $"Estimated construction time is {sBuildTime}. Would you like to build it?");

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Yes"))
            {
                if (string.IsNullOrEmpty(_newName))
                {
                    ScreenMessages.PostScreenMessage("Enter a name for the new launchpad");
                    return;
                }

                for (int i = 0; i < KCTGameStates.ActiveKSC.LaunchPads.Count; i++)
                {
                    var lp = KCTGameStates.ActiveKSC.LaunchPads[i];
                    if (string.Equals(lp.name, _newName, StringComparison.OrdinalIgnoreCase))
                    {
                        ScreenMessages.PostScreenMessage("Another launchpad with the same name already exists");
                        return;
                    }
                }

                GUIStates.ShowNewPad          = false;
                _centralWindowPosition.height = 1;
                _centralWindowPosition.width  = 150;
                _centralWindowPosition.x      = (Screen.width - 150) / 2;
                GUIStates.ShowBuildList       = true;

                if (!Utilities.CurrentGameIsCareer())
                {
                    KCTDebug.Log("Building new launchpad!");
                    KCTGameStates.ActiveKSC.LaunchPads.Add(new KCT_LaunchPad(_newName, _selectedPadIdx));
                }
                else if (Funding.CanAfford((float)curPadCost))
                {
                    KCTDebug.Log("Building new launchpad!");
                    Utilities.SpendFunds(curPadCost, TransactionReasons.StructureConstruction);
                    KCTGameStates.ActiveKSC.LaunchPads.Add(new KCT_LaunchPad(_newName, -1));
                    FacilityUpgrade newPad = new FacilityUpgrade
                    {
                        FacilityType = SpaceCenterFacility.LaunchPad,
                        Id           = KCT_LaunchPad.LPID,
                        IsLaunchpad  = true,
                        LaunchpadID  = KCTGameStates.ActiveKSC.LaunchPads.Count - 1,
                        UpgradeLevel = _selectedPadIdx,
                        CurrentLevel = -1,
                        Cost         = curPadCost,
                        CommonName   = _newName
                    };
                    newPad.SetBP(curPadCost);
                    try
                    {
                        KCTEvents.OnFacilityUpgradeQueued?.Fire(newPad);
                    }
                    catch (Exception ex)
                    {
                        Debug.LogException(ex);
                    }
                    KCTGameStates.ActiveKSC.KSCTech.Add(newPad);
                }
                else
                {
                    ScreenMessages.PostScreenMessage("Not enough funds to build this launchpad.");
                }

                _padCosts      = null;
                _padLvlOptions = null;
                _costOfNewLP   = int.MinValue;
            }
            if (GUILayout.Button("No"))
            {
                _centralWindowPosition.height = 1;
                _centralWindowPosition.width  = 150;
                _centralWindowPosition.x      = (Screen.width - 150) / 2;
                _padCosts               = null;
                _padLvlOptions          = null;
                GUIStates.ShowNewPad    = false;
                GUIStates.ShowBuildList = true;
            }
            GUILayout.EndHorizontal();
            GUILayout.EndVertical();
            CenterWindow(ref _centralWindowPosition);
        }
Esempio n. 4
0
        public static void DrawNewPadWindow(int windowID)
        {
            if (_padCosts == null || _padLvlOptions == null)
            {
                LoadPadNamesAndCosts();
            }

            GUILayout.BeginVertical();
            GUILayout.Label("Name:");
            _newName = GUILayout.TextField(_newName);

            GUILayout.Label("Pad level:");
            _selectedPadIdx = GUILayout.SelectionGrid(_selectedPadIdx, _padLvlOptions, 2);

            Vector3 unlimitedSizeThreshold = new Vector3(70, 130, 70);

            double  curPadCost;
            float   fractionalPadLvl = -1;
            float   tonnageLimit     = 0;
            Vector3 curPadSize       = Vector3.zero;

            int  customPadIdx = _padLvlOptions.Length - 1;
            bool isCustom     = _selectedPadIdx == customPadIdx;

            if (isCustom)
            {
                curPadCost = 0;

                GUILayout.Label("Tonnage limit:");
                _tonnageLimit = GUILayout.TextField(_tonnageLimit);
                if (float.TryParse(_tonnageLimit, out tonnageLimit) && tonnageLimit >= _padTons[0])
                {
                    float unlimitedTonnageThreshold = PresetManager.Instance.ActivePreset.GeneralSettings.PadUnlimitedTonnageThreshold;
                    if (tonnageLimit >= unlimitedTonnageThreshold)
                    {
                        int padLvl = _padLvlOptions.Length - 2;
                        tonnageLimit     = unlimitedTonnageThreshold;
                        curPadSize       = _padSizes[padLvl];
                        curPadCost       = _padCosts[padLvl];
                        fractionalPadLvl = padLvl;
                    }
                    else
                    {
                        for (int i = 1; i < _padTons.Length; i++)
                        {
                            if (tonnageLimit < _padTons[i])
                            {
                                float lowerBound          = _padTons[i - 1];
                                float upperBound          = Math.Min(_padTons[i], unlimitedTonnageThreshold);
                                float fractionOverFullLvl = (tonnageLimit - lowerBound) / (upperBound - lowerBound);
                                fractionalPadLvl = (i - 1) + fractionOverFullLvl;

                                var s1 = _padSizes[i - 1];
                                var s2 = Vector3.Min(_padSizes[i], unlimitedSizeThreshold);
                                curPadSize = s1 + (s2 - s1) * fractionOverFullLvl;

                                var c1 = _padCosts[i - 1];
                                var c2 = _padCosts[i];
                                curPadCost = c1 + (c2 - c1) * fractionOverFullLvl;
                                break;
                            }
                        }
                    }
                }
            }
            else
            {
                fractionalPadLvl = _selectedPadIdx;
                curPadSize       = _padSizes[_selectedPadIdx];
                curPadCost       = _padCosts[_selectedPadIdx];
                tonnageLimit     = _padTons[_selectedPadIdx];
            }

            if (curPadSize != Vector3.zero)
            {
                if (curPadSize.y == float.MaxValue)
                {
                    GUILayout.Label($"Size limit: unlimited");
                }
                else
                {
                    GUILayout.Label($"Size limit: {curPadSize.x:#.#}x{curPadSize.y:#.#}m");
                }
            }

            if (curPadCost > 0)
            {
                double curPadBuildTime = FacilityUpgrade.CalculateBuildTime(curPadCost, SpaceCenterFacility.LaunchPad);
                string sBuildTime      = KSPUtil.PrintDateDelta(curPadBuildTime, includeTime: false);
                GUILayout.Label($"It will cost {Math.Round(curPadCost):N} funds to build the new launchpad. " +
                                $"Estimated construction time is {sBuildTime}.");
            }

            GUILayout.Label("Would you like to build it?");
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Yes") && ValidatePadCreationParameters(_newName, fractionalPadLvl, tonnageLimit, curPadSize))
            {
                GUIStates.ShowNewPad          = false;
                _centralWindowPosition.height = 1;
                _centralWindowPosition.width  = 150;
                _centralWindowPosition.x      = (Screen.width - 150) / 2;
                GUIStates.ShowBuildList       = true;

                if (!Utilities.CurrentGameIsCareer())
                {
                    KCTDebug.Log("Building new launchpad!");
                    KCTGameStates.ActiveKSC.LaunchPads.Add(new KCT_LaunchPad(_newName, fractionalPadLvl, tonnageLimit, curPadSize));
                }
                else if (Funding.CanAfford((float)curPadCost))
                {
                    KCTDebug.Log("Building new launchpad!");
                    Utilities.SpendFunds(curPadCost, TransactionReasons.StructureConstruction);
                    var lp = new KCT_LaunchPad(_newName, fractionalPadLvl, tonnageLimit, curPadSize);
                    KCTGameStates.ActiveKSC.LaunchPads.Add(lp);

                    var padConstr = new PadConstruction
                    {
                        LaunchpadIndex = KCTGameStates.ActiveKSC.LaunchPads.Count - 1,
                        Cost           = curPadCost,
                        Name           = _newName
                    };
                    padConstr.SetBP(curPadCost);
                    KCTGameStates.ActiveKSC.PadConstructions.Add(padConstr);

                    try
                    {
                        KCTEvents.OnPadConstructionQueued?.Fire(padConstr, lp);
                    }
                    catch (Exception ex)
                    {
                        Debug.LogException(ex);
                    }
                }
                else
                {
                    ScreenMessages.PostScreenMessage("Not enough funds to build this launchpad.");
                }

                _padCosts      = null;
                _padLvlOptions = null;
                _costOfNewLP   = int.MinValue;
            }

            if (GUILayout.Button("No"))
            {
                _centralWindowPosition.height = 1;
                _centralWindowPosition.width  = 150;
                _centralWindowPosition.x      = (Screen.width - 150) / 2;
                _padCosts               = null;
                _padLvlOptions          = null;
                GUIStates.ShowNewPad    = false;
                GUIStates.ShowBuildList = true;
            }

            GUILayout.EndHorizontal();
            GUILayout.EndVertical();
            CenterWindow(ref _centralWindowPosition);
        }