public override void Render(TextWriter writer)
        {
            if (state == ControlPanelState.DragDrop)
            {
                if (ZoneName.IndexOfAny(new[] { '.', ',', ' ', '\'', '"', '\t', '\r', '\n' }) >= 0)
                {
                    throw new N2Exception("Zone '" + ZoneName + "' contains illegal characters.");
                }

                writer.Write("<div class='" + ZoneName + " dropZone'");
                writer.WriteAttribute(PartUtilities.PathAttribute, CurrentItem.Path)
                .WriteAttribute(PartUtilities.ZoneAttribute, ZoneName)
                .WriteAttribute(PartUtilities.AllowedAttribute, PartUtilities.GetAllowedNames(ZoneName, PartsAdapter.GetAllowedDefinitions(CurrentItem, ZoneName, Html.ViewContext.HttpContext.User)))
                .WriteAttribute("title", ZoneTitle ?? DroppableZone.GetToolTip(Html.ResolveService <IDefinitionManager>().GetDefinition(CurrentItem), ZoneName))
                .Write(">");

                RenderPreview(writer);

                writer.Write("</div>");
            }
            else if (state == ControlPanelState.Previewing)
            {
                RenderPreview(writer);
            }
            else
            {
                base.Render(writer);
            }
        }
示例#2
0
        protected override void CreateItems(Control container)
        {
            if (State.IsFlagSet(ControlPanelState.DragDrop) && (AllowExternalManipulation || CurrentItem == CurrentPage || Find.ClosestPage(CurrentItem) == CurrentPage))
            {
                if (ZoneName.IndexOfAny(new[] { '.', ',', ' ', '\'', '"', '\t', '\r', '\n' }) >= 0)
                {
                    throw new N2Exception("Zone '" + ZoneName + "' contains illegal characters.");
                }

                Panel zoneContainer = AddPanel(this, ZoneName + " dropZone");
                if (CurrentItem.ID != 0 && !CurrentItem.VersionOf.HasValue)
                {
                    zoneContainer.Attributes[PartUtilities.PathAttribute] = CurrentItem.Path;
                }
                else
                {
                    zoneContainer.Attributes[PartUtilities.PathAttribute] = Find.ClosestPage(CurrentItem).Path;
                    zoneContainer.Attributes["data-versionKey"]           = CurrentItem.GetVersionKey();
                    zoneContainer.Attributes["data-versionIndex"]         = CurrentItem.VersionIndex.ToString();
                }
                zoneContainer.Attributes[PartUtilities.ZoneAttribute]    = ZoneName;
                zoneContainer.Attributes[PartUtilities.AllowedAttribute] = PartUtilities.GetAllowedNames(ZoneName, PartsAdapter.GetAllowedDefinitions(CurrentItem, ZoneName, Page.User));

                zoneContainer.ToolTip = GetToolTip(GetDefinition(CurrentItem), ZoneName);
                base.CreateItems(zoneContainer);
            }
            else
            {
                base.CreateItems(this);
            }
        }
示例#3
0
        public void SpawnVessel()
        {
            //var allowedSituations = Vessel.Situations.LANDED | Vessel.Situations.PRELAUNCH | Vessel.Situations.SPLASHED;
            //if (allowedSituations != (allowedSituations & FlightGlobals.ActiveVessel.situation))
            //{
            //    throw new Exception(_notLandedErrorText);
            //}

            if (string.IsNullOrEmpty(_selectedCraftFilePath))
            {
                throw new Exception(_noVesselSelectedErrorText);
            }

            PartUtilities.ConsumeResources(_cachedCostData);

            // Backup the ship config from the VAB/SPH, load the selected .craft file
            //   and restore the cached config from the VAB/SPH
            var constructBak = ShipConstruction.ShipConfig;
            var construct    = ShipConstruction.LoadShip(_selectedCraftFilePath);

            ShipConstruction.ShipConfig = constructBak;

            ShipConstruction.PutShipToGround(construct, transform);
            ShipConstruction.AssembleForLaunch(
                construct,
                vessel.landedAt,
                vessel.displaylandedAt,
                construct.missionFlag,
                HighLogic.CurrentGame,
                new VesselCrewManifest());

            _window.CloseWindow();
        }
示例#4
0
        public override void Render(TextWriter writer)
        {
            if (writer == null)
            {
                throw new NullReferenceException("writer cannot be null");
            }

            if (state.IsFlagSet(ControlPanelState.DragDrop))
            {
                if (String.IsNullOrWhiteSpace(ZoneName))
                {
                    throw new N2Exception("Zone name cannot be null, empty, or consist only of whitespace.");
                }
                if (ZoneName.IndexOfAny(new[] { '.', ',', ' ', '\'', '"', '\t', '\r', '\n' }) >= 0)
                {
                    throw new N2Exception("Zone '" + ZoneName + "' contains illegal characters.");
                }
                if (CurrentItem == null)
                {
                    throw new N2Exception("CurrentItem cannot be null");
                }
                var dm = Html.ResolveService <IDefinitionManager>();
                if (dm == null)
                {
                    throw new N2Exception("Failed to resolve the definition manager.");
                }



                writer.Write("<div class=\"" + ZoneName + " dropZone\"");
                if (CurrentItem.ID != 0 && !CurrentItem.VersionOf.HasValue)
                {
                    writer.WriteAttribute("data-id", CurrentItem.ID.ToString());
                    writer.WriteAttribute(PartUtilities.PathAttribute, CurrentItem.Path);
                }
                else
                {
                    writer.WriteAttribute(PartUtilities.PathAttribute, (Find.ClosestPage(CurrentItem) ?? CurrentItem).Path);
                    writer.WriteAttribute("data-versionKey", CurrentItem.GetVersionKey());
                    writer.WriteAttribute("data-versionIndex", CurrentItem.VersionIndex.ToString());
                }
                writer.WriteAttribute(PartUtilities.ZoneAttribute, ZoneName)
                .WriteAttribute(PartUtilities.AllowedAttribute, PartUtilities.GetAllowedNames(ZoneName, PartsAdapter.GetAllowedDefinitions(CurrentItem, ZoneName, Html.ViewContext.HttpContext.User)))
                .WriteAttribute("title", ZoneTitle ?? DroppableZone.GetToolTip(dm.GetDefinition(CurrentItem), ZoneName));
                writer.Write(">");

                RenderPreview(writer);

                writer.Write("</div>");
            }
            else if (state.IsFlagSet(ControlPanelState.Previewing))
            {
                RenderPreview(writer);
            }
            else
            {
                base.Render(writer);
            }
        }
        protected List <KonstructorResourceMetadata> GetResourceCosts()
        {
            if (_cachedProtoVessel == null)
            {
                return(null);
            }

            if (_cachedCostData == null)
            {
                var availableParts   = PartLoader.LoadedPartsList;
                var persistenceLayer = _scenario.ServiceManager
                                       .GetService <KonstructionPersistance>();
                var costData = new List <CostData>();
                foreach (var partSnapshot in _cachedProtoVessel.protoPartSnapshots)
                {
                    var part = availableParts.FirstOrDefault(p => p.name == partSnapshot.partName);
                    if (part != null)
                    {
                        var costs = PartUtilities.GetPartCost(part, persistenceLayer);
                        costData.AddRange(costs);
                    }
                }
                _cachedCostData = costData
                                  .GroupBy(c => c.Resource.name)
                                  .Select(g =>
                {
                    var resource = g.First().Resource;
                    var quantity = g.Sum(c => c.Quantity);
                    return(new CostData
                    {
                        Quantity = quantity,
                        Resource = resource,
                    });
                })
                                  .ToList();
            }
            if (_cachedResources == null)
            {
                _cachedResources = _cachedCostData
                                   .Select(c =>
                {
                    var available = PartUtilities.GetResourceQty(c.Resource.name);
                    return(new KonstructorResourceMetadata(c.Resource.displayName, available, c.Quantity));
                })
                                   .ToDictionary(c => c.Name);
            }
            else
            {
                foreach (var resource in _cachedResources)
                {
                    resource.Value.Available = PartUtilities.GetResourceQty(resource.Key);
                }
            }

            _nextRefreshTime = Planetarium.GetUniversalTime() + 1d;

            return(_cachedResources.Select(r => r.Value).ToList());
        }
示例#6
0
        private List <string> LoadRecyclerParts()
        {
            var ret = new List <string>();

            totVol   = 0;
            totMass  = 0;
            totParts = 0;
            totCost  = new List <CostData>();
            var inv = _module.part.FindModuleImplementing <ModuleInventoryPart>();

            for (int z = 0; z < inv.InventorySlots; z++)
            {
                if (!inv.IsSlotEmpty(z))
                {
                    var invPart = GetPartByName(inv.storedParts[z].partName);
                    ret.Add(invPart.title);
                    totParts++;
                    totVol  += invPart.partPrefab.FindModuleImplementing <ModuleCargoPart>().packedVolume;
                    totMass += invPart.partPrefab.mass;
                    totMass += invPart.partPrefab.resourceMass;

                    //Add our cost data
                    var cost = PartUtilities.GetPartCost(invPart, _persistence);
                    foreach (var c in cost)
                    {
                        var cTot = totCost.Where(x => x.Resource.name == c.Resource.name).FirstOrDefault();
                        if (cTot == null)
                        {
                            cTot          = new CostData();
                            cTot.Resource = c.Resource;
                            totCost.Add(cTot);
                        }
                        cTot.Quantity += c.Quantity;
                    }
                }
            }

            //Account for recycle ratio
            foreach (var cost in totCost)
            {
                double adjQuantity = Math.Floor(cost.Quantity * _module.DekonstructRatio);
                cost.Quantity = (int)adjQuantity;
            }

            ret.Sort();
            return(ret);
        }
示例#7
0
        protected override void CreateItems(Control container)
        {
            if (State == ControlPanelState.DragDrop && (AllowExternalManipulation || CurrentItem == CurrentPage))
            {
                if (ZoneName.IndexOfAny(new[] { '.', ',', ' ', '\'', '"', '\t', '\r', '\n' }) >= 0)
                {
                    throw new N2Exception("Zone '" + ZoneName + "' contains illegal characters.");
                }

                Panel zoneContainer = AddPanel(this, ZoneName + " dropZone");
                zoneContainer.Attributes[PartUtilities.PathAttribute]    = CurrentItem.Path;
                zoneContainer.Attributes[PartUtilities.ZoneAttribute]    = ZoneName;
                zoneContainer.Attributes[PartUtilities.AllowedAttribute] = PartUtilities.GetAllowedNames(ZoneName, PartsAdapter.GetAllowedDefinitions(CurrentItem, ZoneName, Page.User));
                zoneContainer.ToolTip = GetToolTip(GetDefinition(CurrentItem), ZoneName);
                base.CreateItems(zoneContainer);
            }
            else
            {
                base.CreateItems(this);
            }
        }
示例#8
0
        private void RecycleInventoryParts()
        {
            //Destroy Items
            var inv = _module.part.FindModuleImplementing <ModuleInventoryPart>();

            for (int z = 0; z < inv.InventorySlots; z++)
            {
                if (!inv.IsSlotEmpty(z))
                {
                    inv.ClearPartAtSlot(z);
                }
            }
            //Refund materials
            foreach (var cost in totCost)
            {
                if (cost.Quantity > 0)
                {
                    PartUtilities.AddResource(cost.Resource, cost.Quantity);
                    ScreenMessages.PostScreenMessage(String.Format("Refunding {0} {1}", cost.Quantity, cost.Resource.displayName), 5f, ScreenMessageStyle.UPPER_CENTER);
                }
            }
        }
示例#9
0
        protected override void DrawWindowContents(int windowId)
        {
            GUILayout.BeginVertical();
            try
            {
                //*****************
                //*   SETUP
                //*****************
                List <string> recycleParts = LoadRecyclerParts();

                //*********************
                //*   MAIN WORK AREA
                //*********************
                GUILayout.BeginHorizontal();

                //*****************
                //*   PARTS
                //*****************
                GUILayout.BeginVertical();
                GUILayout.Label(string.Format("<color=#ffd900>Parts to Recycle</color>"), _labelStyle, GUILayout.Width(120));
                scrollPosPart = GUILayout.BeginScrollView(scrollPosPart, _scrollStyle, GUILayout.Width(380), GUILayout.Height(480));
                var itemCol = "ffffff";

                foreach (var item in recycleParts)
                {
                    GUILayout.Label(string.Format("<color=#{0}>{1}</color>", itemCol, item), _labelStyle, GUILayout.Width(340));
                }
                GUILayout.EndScrollView();
                GUILayout.EndVertical();

                //*****************
                //*   ACTION WINDOW
                //*****************
                //   Part Count:    10
                //   Mass:          0.05 t
                //   Volume:        124 L
                //
                //   Recycler Output:
                //      100 Material Kits
                //      50 Specialized Parts
                //
                //   [ Recycle! ]
                //
                GUILayout.BeginVertical();
                GUILayout.Label(string.Format(" "), _labelStyle, GUILayout.Width(120)); //Spacer

                GUILayout.BeginHorizontal();
                GUILayout.Label(string.Format("<color=#ffd900>Parts:</color>"), _labelStyle, GUILayout.Width(60));
                GUILayout.Label(string.Format("<color=#ffffff>{0} t</color>", totParts), _labelStyle, GUILayout.Width(200));
                GUILayout.EndHorizontal();


                GUILayout.BeginHorizontal();
                GUILayout.Label(string.Format("<color=#ffd900>Mass:</color>"), _labelStyle, GUILayout.Width(60));
                GUILayout.Label(string.Format("<color=#ffffff>{0} t</color>", totMass), _labelStyle, GUILayout.Width(200));
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                GUILayout.Label(string.Format("<color=#ffd900>Volume:</color>"), _labelStyle, GUILayout.Width(60));
                GUILayout.Label(string.Format("<color=#ffffff>{0} L</color>", totVol), _labelStyle, GUILayout.Width(200));
                GUILayout.EndHorizontal();

                GUILayout.Label(string.Format(" "), _labelStyle, GUILayout.Width(120));                 //Spacer
                GUILayout.Label(string.Format("Recycler Output: "), _labelStyle, GUILayout.Width(120)); //Spacer

                var valRes = true;

                foreach (var cost in totCost)
                {
                    var space      = PartUtilities.GetStorageSpace(cost.Resource.name);
                    var valThisRes = space >= cost.Quantity;
                    var resColor   = "ffffff";
                    if (!valThisRes)
                    {
                        resColor = "ff6e69";
                        valRes   = false;
                    }

                    GUILayout.BeginHorizontal();
                    GUILayout.Label(String.Format("<color=#{0}>    - {1} {2}/{3}</color>", resColor, cost.Resource.name, cost.Quantity, space), _detailStyle, GUILayout.Width(250));
                    GUILayout.EndHorizontal();
                }


                if (valRes)
                {
                    if (GUILayout.Button("Dekonstruct Parts", GUILayout.Width(300), GUILayout.Height(50)))
                    {
                        RecycleInventoryParts();
                    }
                }
                if (!valRes)
                {
                    GUILayout.Label(string.Format("<color=#ff6e69>Insufficient space for resources.</color>"), _labelStyle, GUILayout.Width(320));
                }

                GUILayout.Label(string.Format(" "), _labelStyle, GUILayout.Width(50)); //Spacer
                if (GUILayout.Button("Close Window"))
                {
                    SetVisible(false);
                }

                GUILayout.EndVertical();
                //*********************
                //*  CLEAN UP
                //*********************
                GUILayout.EndHorizontal();
            }
            catch (Exception ex)
            {
                Debug.Log(ex.StackTrace);
            }
            finally
            {
                GUILayout.EndVertical();
                GUI.DragWindow();
            }
        }
        public override void LaunchVessel()
        {
            if (FlightGlobals.ActiveVessel.situation != Vessel.Situations.ORBITING)
            {
                throw new Exception(_notInOrbitErrorText);
            }

            if (string.IsNullOrEmpty(_selectedCraftFilePath))
            {
                throw new Exception(_noVesselSelectedErrorText);
            }

            if (LogisticsTools.AnyNearbyVessels(100d, FlightGlobals.ActiveVessel))
            {
                throw new Exception(_nearbyVesselsErrorText);
            }

            PartUtilities.ConsumeResources(_cachedCostData);

            var vesselOrbit = FlightGlobals.ActiveVessel.orbit;
            var now         = Planetarium.GetUniversalTime();

            vesselOrbit.GetOrbitalStateVectorsAtUT(
                now,
                out Vector3d position,
                out Vector3d velocity);
            position.x += 50d;
            var orbit = new Orbit(vesselOrbit);

            orbit.UpdateFromStateVectors(
                position,
                velocity,
                vesselOrbit.referenceBody,
                now);

            var partNodes = _cachedProtoVessel.protoPartSnapshots
                            .Select(s =>
            {
                var node = new ConfigNode("PART");
                s.Save(node);
                return(node);
            })
                            .ToArray();
            var type = VesselType.Ship;

            _craftConfigNode.TryGetEnum("type", ref type, VesselType.Ship);

            var vesselConfigNode = ProtoVessel.CreateVesselNode(
                _cachedProtoVessel.GetDisplayName(),
                type,
                orbit,
                0,
                partNodes);

            var spawnedProtoVessel = new ProtoVessel(vesselConfigNode, HighLogic.CurrentGame);

            spawnedProtoVessel.Load(HighLogic.CurrentGame.flightState);

            _window.CloseWindow();

            var spawnedVessel = FlightGlobals.Vessels.Last();

            spawnedVessel.protoVessel.stage = int.MaxValue;
            FlightGlobals.SetActiveVessel(spawnedVessel);
        }