private float ReclaimResource(string resource, double amount, string vessel_name, string name = null) { PartResourceDefinition res_def; res_def = PartResourceLibrary.Instance.GetDefinition(resource); VesselResources recycler = new VesselResources(vessel); if (res_def == null) { return(0); } if (name == null) { name = resource; } double remain = amount; // any resources that can't be pumped or don't flow just "evaporate" // FIXME: should this be a little smarter and convert certain such // resources into rocket parts? if (res_def.resourceTransferMode != ResourceTransferMode.NONE && res_def.resourceFlowMode != ResourceFlowMode.NO_FLOW) { remain = recycler.TransferResource(resource, amount); } Debug.Log(String.Format("[EL] {0}-{1}: {2} taken {3} reclaimed, {4} lost", vessel_name, name, amount, amount - remain, remain)); return((float)(amount * res_def.density)); }
internal void SetupCraftResources(Vessel vsl) { craftResources = new VesselResources(vsl); foreach (var br in buildCost.optional) { var amount = craftResources.ResourceAmount(br.name); craftResources.TransferResource(br.name, -amount); } }
void onVesselWasModified(Vessel v) { if (v == vessel) { padResources = new VesselResources(vessel); if (craftRoot != null && craftRoot.vessel != vessel) { craftRoot = null; ReleaseVessel(); } } }
internal void FindVesselResources() { padResources = new VesselResources(vessel); var craft_parts = CraftParts(); if (craft_parts.Count > 0) { craftResources = new VesselResources(); } foreach (var part in craft_parts) { padResources.RemovePart(part); craftResources.AddPart(part); } if (craftResources == null && craftConfig != null) { getBuildCost(craftConfig); } }
public float RecycleVessel(Vessel v) { float ConversionEfficiency = 0.8f; double amount; VesselResources scrap = new VesselResources(v); string target_resource; PartResourceDefinition rp_def; if (ExLaunchPad.kethane_present) { target_resource = "Metal"; } else { target_resource = "RocketParts"; } rp_def = PartResourceLibrary.Instance.GetDefinition(target_resource); if (FlightGlobals.ActiveVessel == v) { FlightGlobals.ForceSetActiveVessel(this.vessel); } float mass = 0; foreach (var crew in v.GetVesselCrew()) { mass += RecycleKerbal(crew, null); } foreach (string resource in scrap.resources.Keys) { amount = scrap.ResourceAmount(resource); mass += ReclaimResource(resource, amount, v.name); scrap.TransferResource(resource, -amount); } float hull_mass = v.GetTotalMass(); amount = hull_mass * ConversionEfficiency / rp_def.density; mass += ReclaimResource(target_resource, amount, v.name, "hull"); v.Die(); return(mass); }
public BuildCost.CostReport getBuildCost(ConfigNode craft) { ShipConstruct ship = new ShipConstruct(); ship.LoadShip(craft); GameObject ro = ship.parts[0].localRoot.gameObject; Vessel dummy = ro.AddComponent <Vessel>(); dummy.Initialize(true); craftResources = new VesselResources(dummy); BuildCost resources = new BuildCost(); foreach (Part p in ship.parts) { resources.addPart(p); } dummy.Die(); return(resources.cost); }
// ===================================================================================================================================================== // UI Functions private void UseResources(Vessel craft) { VesselResources craftResources = new VesselResources(craft); // Remove all resources that we might later fill (hull resources will not be touched) HashSet <string> resources_to_remove = new HashSet <string>(uis.requiredresources.Keys); craftResources.RemoveAllResources(resources_to_remove); // remove rocket parts required for the hull and solid fuel padResources.TransferResource("RocketParts", -uis.hullRocketParts); // use resources foreach (KeyValuePair <string, double> pair in uis.requiredresources) { // If resource is "JetFuel", rename to "LiquidFuel" string res = pair.Key; if (pair.Key == "JetFuel") { res = "LiquidFuel"; if (pair.Value == 0) { continue; } } if (!uis.resourcesliders.ContainsKey(pair.Key)) { Debug.Log(String.Format("[EL] missing slider {0}", pair.Key)); continue; } // Calculate resource cost based on slider position - note use pair.Key NOT res! we need to use the position of the dedicated LF slider not the LF component of LFO slider double tot = pair.Value * uis.resourcesliders[pair.Key]; // Transfer the resource from the vessel doing the building to the vessel being built padResources.TransferResource(res, -tot); craftResources.TransferResource(res, tot); } }
public float RecycleVessel(Vessel v) { float ConversionEfficiency = 0.8f; double amount; VesselResources scrap = new VesselResources(v); PartResourceDefinition rp_def; rp_def = PartResourceLibrary.Instance.GetDefinition("RocketParts"); float mass = 0; foreach (string resource in scrap.resources.Keys) { amount = scrap.ResourceAmount(resource); mass += ReclaimResource(resource, amount, v.name); } float hull_mass = v.GetTotalMass(); amount = hull_mass * ConversionEfficiency / rp_def.density; mass += ReclaimResource("RocketParts", amount, v.name, "hull"); v.Die(); return(mass); }
void onVesselWasModified(Vessel v) { if (v == vessel) { padResources = new VesselResources (vessel); if (craftRoot != null && craftRoot.vessel != vessel) { craftRoot = null; ReleaseVessel (); } } }
internal void SetupCraftResources(Vessel vsl) { craftResources = new VesselResources (vsl); foreach (var br in buildCost.optional) { var amount = craftResources.ResourceAmount (br.name); craftResources.TransferResource (br.name, -amount); } }
internal void FindVesselResources() { padResources = new VesselResources (vessel); var craft_parts = CraftParts (); if (craft_parts.Count > 0) { craftResources = new VesselResources (); } foreach (var part in craft_parts) { padResources.RemovePart (part); craftResources.AddPart (part); } if (craftResources == null && craftConfig != null) { getBuildCost (craftConfig); } }
public BuildCost.CostReport getBuildCost(ConfigNode craft) { ShipConstruct ship = new ShipConstruct (); ship.LoadShip (craft); GameObject ro = ship.parts[0].localRoot.gameObject; Vessel dummy = ro.AddComponent<Vessel>(); dummy.Initialize (true); craftResources = new VesselResources (dummy); BuildCost resources = new BuildCost (); foreach (Part p in ship.parts) { resources.addPart (p); } dummy.Die (); return resources.cost; }
private void WindowGUI(int windowID) { Styles.Init(); /* * ToDo: * can extend FileBrowser class to see currently highlighted file? * rslashphish says: public myclass(arg1, arg2) : base(arg1, arg2); * KSPUtil.ApplicationRootPath - gets KSPO root * expose m_files and m_selectedFile? * fileBrowser = new FileBrowser(new Rect(Screen.width / 2, 100, 350, 500), title, callback, true); */ EditorLogic editor = EditorLogic.fetch; if (editor) return; if (!uis.builduiactive) return; if (padResources != null && padPartsCount != vessel.Parts.Count) { // something docked or undocked, so rebuild the pad's resouces info padResources = null; } if (padResources == null) { padPartsCount = vessel.Parts.Count; padResources = new VesselResources(vessel); } GUILayout.BeginVertical(); GUILayout.BeginHorizontal("box"); GUILayout.FlexibleSpace(); // VAB / SPH selection if (GUILayout.Toggle(uis.ct == crafttype.VAB, "VAB", GUILayout.Width(80))) { uis.ct = crafttype.VAB; } if (GUILayout.Toggle(uis.ct == crafttype.SPH, "SPH", GUILayout.Width(80))) { uis.ct = crafttype.SPH; } if (GUILayout.Toggle(uis.ct == crafttype.SUB, "SubAss", GUILayout.Width(160))) { uis.ct = crafttype.SUB; } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); string strpath = HighLogic.SaveFolder; if (GUILayout.Button("Select Craft", Styles.normal, GUILayout.ExpandWidth(true))) { string [] dir = new string[] {"SPH", "VAB", "../Subassemblies"}; bool stock = HighLogic.CurrentGame.Parameters.Difficulty.AllowStockVessels; if (uis.ct == crafttype.SUB) HighLogic.CurrentGame.Parameters.Difficulty.AllowStockVessels = false; //GUILayout.Button is "true" when clicked uis.craftlist = new CraftBrowser(new Rect(Screen.width / 2, 100, 350, 500), dir[(int)uis.ct], strpath, "Select a ship to load", craftSelectComplete, craftSelectCancel, HighLogic.Skin, EditorLogic.ShipFileImage, true); uis.showcraftbrowser = true; HighLogic.CurrentGame.Parameters.Difficulty.AllowStockVessels = stock; } if (uis.craftselected) { GUILayout.Box("Selected Craft: " + uis.craftnode.GetValue("ship"), Styles.white); // Resource requirements GUILayout.Label("Resources required to build:", Styles.label, GUILayout.Width(600)); // Link LFO toggle uis.linklfosliders = GUILayout.Toggle(uis.linklfosliders, "Link RocketFuel sliders for LiquidFuel and Oxidizer"); uis.resscroll = GUILayout.BeginScrollView(uis.resscroll, GUILayout.Width(600), GUILayout.Height(300)); GUILayout.BeginHorizontal(); // Headings GUILayout.Label("Resource", Styles.label, GUILayout.Width(120)); GUILayout.Label("Fill Percentage", Styles.label, GUILayout.Width(300)); GUILayout.Label("Required", Styles.label, GUILayout.Width(75)); GUILayout.Label("Available", Styles.label, GUILayout.Width(75)); GUILayout.EndHorizontal(); uis.canbuildcraft = true; // default to can build - if something is stopping us from building, we will set to false later if (!uis.requiredresources.ContainsKey("RocketParts")) { // if the craft to be built has no rocket parts storage, then the amount to use is not adjustable string resname = "RocketParts"; double available = padResources.ResourceAmount(resname); ResourceLine(resname, resname, 1.0F, uis.hullRocketParts, uis.hullRocketParts, available); } // Cycle through required resources foreach (KeyValuePair<string, double> pair in uis.requiredresources) { string resname = pair.Key; // Holds REAL resource name. May need to translate from "JetFuel" back to "LiquidFuel" string reslabel = resname; // Resource name for DISPLAY purposes only. Internally the app uses pair.Key if (reslabel == "JetFuel") { if (pair.Value == 0f) { // Do not show JetFuel line if not being used continue; } //resname = "JetFuel"; resname = "LiquidFuel"; } if (!uis.resourcesliders.ContainsKey(pair.Key)) { uis.resourcesliders.Add(pair.Key, 1); } // If in link LFO sliders mode, rename Oxidizer to LFO (Oxidizer) and LiquidFuel to LFO (LiquidFuel) if (reslabel == "Oxidizer") { reslabel = "RocketFuel (Ox)"; } if (reslabel == "LiquidFuel") { reslabel = "RocketFuel (LF)"; } double minAmount = 0.0; double maxAmount = uis.requiredresources[resname]; if (resname == "RocketParts") { minAmount += uis.hullRocketParts; maxAmount += uis.hullRocketParts; } double available = padResources.ResourceAmount(resname); // If LFO LiquidFuel exists and we are on LiquidFuel (Non-LFO), then subtract the amount used by LFO(LiquidFuel) from the available amount if (pair.Key == "JetFuel") { available -= uis.requiredresources["LiquidFuel"] * uis.resourcesliders["LiquidFuel"]; if (available < 0.0) available = 0.0; } uis.resourcesliders[pair.Key] = ResourceLine(reslabel, pair.Key, uis.resourcesliders[pair.Key], minAmount, maxAmount, available); if (uis.linklfosliders) { float tmp = uis.resourcesliders[pair.Key]; if (pair.Key == "Oxidizer") { uis.resourcesliders["LiquidFuel"] = tmp; } else if (pair.Key == "LiquidFuel") { uis.resourcesliders["Oxidizer"] = tmp; } } } GUILayout.EndScrollView(); // Build button if (uis.canbuildcraft) { if (GUILayout.Button("Build", Styles.normal, GUILayout.ExpandWidth(true))) { BuildAndLaunchCraft(); // Reset the UI uis.craftselected = false; uis.requiredresources = null; uis.resourcesliders = new Dictionary<string, float>();; uis.builduiactive = false; UpdateGUIState(); } } else { GUILayout.Box("You do not have the resources to build this craft", Styles.red); } } else { GUILayout.Box("You must select a craft before you can build", Styles.red); } GUILayout.EndVertical(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Close")) { HideBuildMenu(); } uis.showbuilduionload = GUILayout.Toggle(uis.showbuilduionload, "Show on StartUp"); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); //DragWindow makes the window draggable. The Rect specifies which part of the window it can by dragged by, and is //clipped to the actual boundary of the window. You can also pass no argument at all and then the window can by //dragged by any part of it. Make sure the DragWindow command is AFTER all your other GUI input stuff, or else //it may "cover up" your controls and make them stop responding to the mouse. GUI.DragWindow(new Rect(0, 0, 10000, 20)); }
public float RecycleVessel(Vessel v) { float ConversionEfficiency = 0.8f; double amount; VesselResources scrap = new VesselResources (v); string target_resource; PartResourceDefinition rp_def; if (ExLaunchPad.kethane_present) { target_resource = "Metal"; } else { target_resource = "RocketParts"; } rp_def = PartResourceLibrary.Instance.GetDefinition (target_resource); if (FlightGlobals.ActiveVessel == v) FlightGlobals.ForceSetActiveVessel (this.vessel); float mass = 0; foreach (var crew in v.GetVesselCrew ()) { mass += RecycleKerbal (crew, null); } foreach (string resource in scrap.resources.Keys) { amount = scrap.ResourceAmount (resource); mass += ReclaimResource (resource, amount, v.name); scrap.TransferResource (resource, -amount); } float hull_mass = v.GetTotalMass (); amount = hull_mass * ConversionEfficiency / rp_def.density; mass += ReclaimResource (target_resource, amount, v.name, "hull"); v.Die (); return mass; }
// ===================================================================================================================================================== // UI Functions private void UseResources(Vessel craft) { VesselResources craftResources = new VesselResources(craft); // Remove all resources that we might later fill (hull resources will not be touched) HashSet<string> resources_to_remove = new HashSet<string>(uis.requiredresources.Keys); craftResources.RemoveAllResources(resources_to_remove); // remove rocket parts required for the hull and solid fuel padResources.TransferResource("RocketParts", -uis.hullRocketParts); // use resources foreach (KeyValuePair<string, double> pair in uis.requiredresources) { // If resource is "JetFuel", rename to "LiquidFuel" string res = pair.Key; if (pair.Key == "JetFuel") { res = "LiquidFuel"; if (pair.Value == 0) continue; } if (!uis.resourcesliders.ContainsKey(pair.Key)) { Debug.Log(String.Format("[EL] missing slider {0}", pair.Key)); continue; } // Calculate resource cost based on slider position - note use pair.Key NOT res! we need to use the position of the dedicated LF slider not the LF component of LFO slider double tot = pair.Value * uis.resourcesliders[pair.Key]; // Transfer the resource from the vessel doing the building to the vessel being built padResources.TransferResource(res, -tot); craftResources.TransferResource(res, tot); } }
private float ReclaimResource(string resource, double amount, string vessel_name, string name=null) { PartResourceDefinition res_def; res_def = PartResourceLibrary.Instance.GetDefinition(resource); VesselResources recycler = new VesselResources(vessel); if (res_def == null) { return 0; } if (name == null) { name = resource; } double remain = amount; // any resources that can't be pumped or don't flow just "evaporate" // FIXME: should this be a little smarter and convert certain such // resources into rocket parts? if (res_def.resourceTransferMode != ResourceTransferMode.NONE && res_def.resourceFlowMode != ResourceFlowMode.NO_FLOW) { remain = recycler.TransferResource(resource, amount); } Debug.Log(String.Format("[EL] {0}-{1}: {2} taken {3} reclaimed, {4} lost", vessel_name, name, amount, amount - remain, remain)); return (float) (amount * res_def.density); }
public float RecycleVessel(Vessel v) { float ConversionEfficiency = 0.8f; double amount; VesselResources scrap = new VesselResources(v); PartResourceDefinition rp_def; rp_def = PartResourceLibrary.Instance.GetDefinition("RocketParts"); float mass = 0; foreach (string resource in scrap.resources.Keys) { amount = scrap.ResourceAmount (resource); mass += ReclaimResource(resource, amount, v.name); } float hull_mass = v.GetTotalMass(); amount = hull_mass * ConversionEfficiency / rp_def.density; mass += ReclaimResource("RocketParts", amount, v.name, "hull"); v.Die(); return mass; }
private void WindowGUI(int windowID) { Styles.Init(); /* * ToDo: * can extend FileBrowser class to see currently highlighted file? * rslashphish says: public myclass(arg1, arg2) : base(arg1, arg2); * KSPUtil.ApplicationRootPath - gets KSPO root * expose m_files and m_selectedFile? * fileBrowser = new FileBrowser(new Rect(Screen.width / 2, 100, 350, 500), title, callback, true); */ EditorLogic editor = EditorLogic.fetch; if (editor) { return; } if (!uis.builduiactive) { return; } if (padResources != null && padPartsCount != vessel.Parts.Count) { // something docked or undocked, so rebuild the pad's resouces info padResources = null; } if (padResources == null) { padPartsCount = vessel.Parts.Count; padResources = new VesselResources(vessel); } GUILayout.BeginVertical(); GUILayout.BeginHorizontal("box"); GUILayout.FlexibleSpace(); // VAB / SPH selection if (GUILayout.Toggle(uis.ct == crafttype.VAB, "VAB", GUILayout.Width(80))) { uis.ct = crafttype.VAB; } if (GUILayout.Toggle(uis.ct == crafttype.SPH, "SPH", GUILayout.Width(80))) { uis.ct = crafttype.SPH; } if (GUILayout.Toggle(uis.ct == crafttype.SUB, "SubAss", GUILayout.Width(160))) { uis.ct = crafttype.SUB; } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); string strpath = HighLogic.SaveFolder; if (GUILayout.Button("Select Craft", Styles.normal, GUILayout.ExpandWidth(true))) { string [] dir = new string[] { "SPH", "VAB", "../Subassemblies" }; bool stock = HighLogic.CurrentGame.Parameters.Difficulty.AllowStockVessels; if (uis.ct == crafttype.SUB) { HighLogic.CurrentGame.Parameters.Difficulty.AllowStockVessels = false; } //GUILayout.Button is "true" when clicked uis.craftlist = new CraftBrowser(new Rect(Screen.width / 2, 100, 350, 500), dir[(int)uis.ct], strpath, "Select a ship to load", craftSelectComplete, craftSelectCancel, HighLogic.Skin, EditorLogic.ShipFileImage, true); uis.showcraftbrowser = true; HighLogic.CurrentGame.Parameters.Difficulty.AllowStockVessels = stock; } if (uis.craftselected) { GUILayout.Box("Selected Craft: "+ uis.craftnode.GetValue("ship"), Styles.white); // Resource requirements GUILayout.Label("Resources required to build:", Styles.label, GUILayout.Width(600)); // Link LFO toggle uis.linklfosliders = GUILayout.Toggle(uis.linklfosliders, "Link RocketFuel sliders for LiquidFuel and Oxidizer"); uis.resscroll = GUILayout.BeginScrollView(uis.resscroll, GUILayout.Width(600), GUILayout.Height(300)); GUILayout.BeginHorizontal(); // Headings GUILayout.Label("Resource", Styles.label, GUILayout.Width(120)); GUILayout.Label("Fill Percentage", Styles.label, GUILayout.Width(300)); GUILayout.Label("Required", Styles.label, GUILayout.Width(75)); GUILayout.Label("Available", Styles.label, GUILayout.Width(75)); GUILayout.EndHorizontal(); uis.canbuildcraft = true; // default to can build - if something is stopping us from building, we will set to false later if (!uis.requiredresources.ContainsKey("RocketParts")) { // if the craft to be built has no rocket parts storage, then the amount to use is not adjustable string resname = "RocketParts"; double available = padResources.ResourceAmount(resname); ResourceLine(resname, resname, 1.0F, uis.hullRocketParts, uis.hullRocketParts, available); } // Cycle through required resources foreach (KeyValuePair <string, double> pair in uis.requiredresources) { string resname = pair.Key; // Holds REAL resource name. May need to translate from "JetFuel" back to "LiquidFuel" string reslabel = resname; // Resource name for DISPLAY purposes only. Internally the app uses pair.Key if (reslabel == "JetFuel") { if (pair.Value == 0f) { // Do not show JetFuel line if not being used continue; } //resname = "JetFuel"; resname = "LiquidFuel"; } if (!uis.resourcesliders.ContainsKey(pair.Key)) { uis.resourcesliders.Add(pair.Key, 1); } // If in link LFO sliders mode, rename Oxidizer to LFO (Oxidizer) and LiquidFuel to LFO (LiquidFuel) if (reslabel == "Oxidizer") { reslabel = "RocketFuel (Ox)"; } if (reslabel == "LiquidFuel") { reslabel = "RocketFuel (LF)"; } double minAmount = 0.0; double maxAmount = uis.requiredresources[resname]; if (resname == "RocketParts") { minAmount += uis.hullRocketParts; maxAmount += uis.hullRocketParts; } double available = padResources.ResourceAmount(resname); // If LFO LiquidFuel exists and we are on LiquidFuel (Non-LFO), then subtract the amount used by LFO(LiquidFuel) from the available amount if (pair.Key == "JetFuel") { available -= uis.requiredresources["LiquidFuel"] * uis.resourcesliders["LiquidFuel"]; if (available < 0.0) { available = 0.0; } } uis.resourcesliders[pair.Key] = ResourceLine(reslabel, pair.Key, uis.resourcesliders[pair.Key], minAmount, maxAmount, available); if (uis.linklfosliders) { float tmp = uis.resourcesliders[pair.Key]; if (pair.Key == "Oxidizer") { uis.resourcesliders["LiquidFuel"] = tmp; } else if (pair.Key == "LiquidFuel") { uis.resourcesliders["Oxidizer"] = tmp; } } } GUILayout.EndScrollView(); // Build button if (uis.canbuildcraft) { if (GUILayout.Button("Build", Styles.normal, GUILayout.ExpandWidth(true))) { BuildAndLaunchCraft(); // Reset the UI uis.craftselected = false; uis.requiredresources = null; uis.resourcesliders = new Dictionary <string, float>();; uis.builduiactive = false; UpdateGUIState(); } } else { GUILayout.Box("You do not have the resources to build this craft", Styles.red); } } else { GUILayout.Box("You must select a craft before you can build", Styles.red); } GUILayout.EndVertical(); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Close")) { HideBuildMenu(); } uis.showbuilduionload = GUILayout.Toggle(uis.showbuilduionload, "Show on StartUp"); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); //DragWindow makes the window draggable. The Rect specifies which part of the window it can by dragged by, and is //clipped to the actual boundary of the window. You can also pass no argument at all and then the window can by //dragged by any part of it. Make sure the DragWindow command is AFTER all your other GUI input stuff, or else //it may "cover up" your controls and make them stop responding to the mouse. GUI.DragWindow(new Rect(0, 0, 10000, 20)); }
public BuildCost() { resources = new VesselResources(); }