示例#1
0
 public PadConstructionStorageItem FromPadConstruction(PadConstruction pc)
 {
     launchpadID      = pc.LaunchpadIndex;
     name             = pc.Name;
     progress         = pc.Progress;
     BP               = pc.BP;
     cost             = pc.Cost;
     upgradeProcessed = pc.UpgradeProcessed;
     return(this);
 }
示例#2
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);
        }