public static double ParseBuildRateFormula(KCT_BuildListVessel.ListType type, int index, KCT_KSC KSC, bool UpgradedRates = false) { //N = num upgrades, I = rate index, L = VAB/SPH upgrade level, R = R&D level int level = 0, upgrades = 0; Dictionary<string, string> variables = new Dictionary<string, string>(); if (type == KCT_BuildListVessel.ListType.VAB) { level = KCT_Utilities.BuildingUpgradeLevel(SpaceCenterFacility.VehicleAssemblyBuilding); if (KSC.VABUpgrades.Count > index) upgrades = KSC.VABUpgrades[index]; } else if (type == KCT_BuildListVessel.ListType.SPH) { level = KCT_Utilities.BuildingUpgradeLevel(SpaceCenterFacility.SpaceplaneHangar); if (KSC.SPHUpgrades.Count > index) upgrades = KSC.SPHUpgrades[index]; } if (UpgradedRates) upgrades++; variables.Add("L", level.ToString()); variables.Add("N", upgrades.ToString()); variables.Add("I", index.ToString()); variables.Add("R", KCT_Utilities.BuildingUpgradeLevel(SpaceCenterFacility.ResearchAndDevelopment).ToString()); return GetStandardFormulaValue("BuildRate", variables); }
public override void OnLoad(ConfigNode node) { base.OnLoad(node); LoadTree(); #if KSP1_4 // 1.4 Addition if (KCT_Utilities.CurrentGameIsMission()) { return; } #endif KCTDebug.Log("Reading from persistence."); KCT_GameStates.KSCs.Clear(); KCT_GameStates.ActiveKSC = null; //KCT_Utilities.SetActiveKSC("Stock"); KCT_GameStates.InitAndClearTechList(); KCT_GameStates.TechUpgradesTotal = 0; KCT_GameStates.SciPointsTotal = -1; KCT_DataStorage kctVS = new KCT_DataStorage(); ConfigNode CN = node.GetNode(kctVS.GetType().Name); if (CN != null) { ConfigNode.LoadObjectFromConfig(kctVS, CN); } foreach (ConfigNode ksc in node.GetNodes("KSC")) { string name = ksc.GetValue("KSCName"); KCT_KSC loaded_KSC = new KCT_KSC(name); loaded_KSC.FromConfigNode(ksc); if (loaded_KSC != null && loaded_KSC.KSCName != null && loaded_KSC.KSCName.Length > 0) { loaded_KSC.RDUpgrades[1] = KCT_GameStates.TechUpgradesTotal; if (KCT_GameStates.KSCs.Find(k => k.KSCName == loaded_KSC.KSCName) == null) { KCT_GameStates.KSCs.Add(loaded_KSC); } } } KCT_Utilities.SetActiveKSCToRSS(); ConfigNode tmp = node.GetNode("TechList"); if (tmp != null) { foreach (ConfigNode techNode in tmp.GetNodes("Tech")) { KCT_TechStorageItem techStorageItem = new KCT_TechStorageItem(); ConfigNode.LoadObjectFromConfig(techStorageItem, techNode); KCT_TechItem techItem = techStorageItem.ToTechItem(); techItem.protoNode = new ProtoTechNode(techNode.GetNode("ProtoNode")); KCT_GameStates.TechList.Add(techItem); } } KCT_GUI.CheckToolbar(); KCT_GameStates.erroredDuringOnLoad.OnLoadFinish(); //KerbalConstructionTime.DelayedStart(); }
public static double CalculateBuildTime(double cost, KCT_KSC KSC = null) { double bp = CalculateBP(cost); double rateTotal = KCT_Utilities.GetBothBuildRateSum(KSC ?? KCT_GameStates.ActiveKSC); return(bp / rateTotal); }
public override void OnLoad(ConfigNode node) { KCTDebug.Log("Reading from persistence."); base.OnLoad(node); KCT_GameStates.KSCs.Clear(); KCT_GameStates.ActiveKSC = null; //KCT_Utilities.SetActiveKSC("Stock"); KCT_GameStates.TechList.Clear(); KCT_GameStates.TechUpgradesTotal = 0; KCT_DataStorage kctVS = new KCT_DataStorage(); ConfigNode CN = node.GetNode(kctVS.GetType().Name); if (CN != null) { ConfigNode.LoadObjectFromConfig(kctVS, CN); } foreach (ConfigNode ksc in node.GetNodes("KSC")) { string name = ksc.GetValue("KSCName"); KCT_KSC loaded_KSC = new KCT_KSC(name); loaded_KSC.FromConfigNode(ksc); if (loaded_KSC != null && loaded_KSC.KSCName != null && loaded_KSC.KSCName.Length > 0) { loaded_KSC.RDUpgrades[1] = KCT_GameStates.TechUpgradesTotal; if (KCT_GameStates.KSCs.Find(k => k.KSCName == loaded_KSC.KSCName) == null) { KCT_GameStates.KSCs.Add(loaded_KSC); } } } //KCT_Utilities.SetActiveKSCToRSS(); KCT_Utilities.SetActiveKSC(KCT_GameStates.activeKSCName); ConfigNode tmp = node.GetNode("TechList"); if (tmp != null) { foreach (ConfigNode techNode in tmp.GetNodes("Tech")) { KCT_TechStorageItem techStorageItem = new KCT_TechStorageItem(); ConfigNode.LoadObjectFromConfig(techStorageItem, techNode); KCT_TechItem techItem = techStorageItem.ToTechItem(); techItem.protoNode = new ProtoTechNode(techNode.GetNode("ProtoNode")); KCT_GameStates.TechList.Add(techItem); } } //KCT_GameStates.ActiveKSC.AsConfigNode().Save(KSPUtil.ApplicationRootPath + "/KSC.node"); KerbalConstructionTime.DelayedStart(); KCT_GUI.CheckToolbar(); KCT_GameStates.erroredDuringOnLoad.OnLoadFinish(); }
public bool RemoveFromBuildList() { string typeName = ""; bool removed = false; KCT_KSC theKSC = this.KSC; if (theKSC == null) { KCTDebug.Log("Could not find the KSC to remove vessel!"); return(false); } if (type == ListType.SPH) { if (theKSC.SPHWarehouse.Contains(this)) { removed = theKSC.SPHWarehouse.Remove(this); } else if (theKSC.SPHList.Contains(this)) { removed = theKSC.SPHList.Remove(this); } typeName = "SPH"; } else if (type == ListType.VAB) { if (theKSC.VABWarehouse.Contains(this)) { removed = theKSC.VABWarehouse.Remove(this); } else if (theKSC.VABList.Contains(this)) { removed = theKSC.VABList.Remove(this); } typeName = "VAB"; } KCTDebug.Log("Removing " + shipName + " from " + typeName + " storage/list."); if (!removed) { KCTDebug.Log("Failed to remove ship from list! Performing direct comparison of ids..."); foreach (KCT_BuildListVessel blv in theKSC.SPHWarehouse) { if (blv.id == this.id) { KCTDebug.Log("Ship found in SPH storage. Removing..."); removed = theKSC.SPHWarehouse.Remove(blv); break; } } if (!removed) { foreach (KCT_BuildListVessel blv in theKSC.VABWarehouse) { if (blv.id == this.id) { KCTDebug.Log("Ship found in VAB storage. Removing..."); removed = theKSC.VABWarehouse.Remove(blv); break; } } } if (!removed) { foreach (KCT_BuildListVessel blv in theKSC.VABList) { if (blv.id == this.id) { KCTDebug.Log("Ship found in VAB List. Removing..."); removed = theKSC.VABList.Remove(blv); break; } } } if (!removed) { foreach (KCT_BuildListVessel blv in theKSC.SPHList) { if (blv.id == this.id) { KCTDebug.Log("Ship found in SPH list. Removing..."); removed = theKSC.SPHList.Remove(blv); break; } } } } if (removed) { KCTDebug.Log("Sucessfully removed ship from storage."); } else { KCTDebug.Log("Still couldn't remove ship!"); } return(removed); }
public static double ParseBuildRateFormula(KCT_BuildListVessel.ListType type, int index, KCT_KSC KSC, bool UpgradedRates = false) { return(ParseBuildRateFormula(type, index, KSC, UpgradedRates ? 1 : 0)); }
/* public static double ParseMath(string input, Dictionary<string, string> variables) { // KCTDebug.Log("Input_raw: " + input); string raw = input; foreach (KeyValuePair<string, string> kvp in variables) { if (input.Contains("[" + kvp.Key + "]")) { input = input.Replace("[" + kvp.Key + "]", kvp.Value); } } //KCTDebug.Log("Input_replaced: " + input); double currentVal = 0; string stack = ""; string lastOp = "+"; string[] ops = { "+", "-", "*", "/", "%", "^", "(", "e", "E" }; string[] functions = { "min", "max", "l", "L", "abs", "sign" }; for (int i = 0; i < input.Length; ++i) { string ch = input[i].ToString(); bool isOp = false, isFunction = false; // KCTDebug.Log(ch); foreach (string op in ops) { if (op == ch) { isOp = true; break; } } if (!isOp) { foreach (string fun in functions) { if (fun[0] == input[i]) { isFunction = true; break; } } } if (isOp) { if (ch == "-" && (stack.Trim() == "")) { stack += ch; } else if (ch == "e" || ch == "E") { int index=i+2; for (index = i+2; index < input.Length; ++index) { string ch2 = input[index].ToString(); if (ops.Contains(ch2)) break; } string sub = input.Substring(i + 1, index - i - 1); double exp = ParseMath(sub, variables); double newVal = double.Parse(stack) * Math.Pow(10, exp); currentVal = DoMath(currentVal, lastOp, newVal.ToString()); stack = "0"; lastOp = "+"; i = index - 1; } else if (ch == "(") { int j = FindEndParenthesis(input, i)[0]; string sub = input.Substring(i + 1, j - i - 1); string val = ParseMath(sub, variables).ToString(); input = input.Substring(0, i) + val + input.Substring(j + 1); --i; } else { currentVal = DoMath(currentVal, lastOp, stack); lastOp = ch; stack = ""; } } else if (isFunction) { int subStart = input.IndexOf('(', i)+1; string function = input.Substring(i, subStart - i - 1); //KCTDebug.Log(function); int[] parenComma = FindEndParenthesis(input, subStart-1); int j = parenComma[0]; int comma = parenComma[1]; string sub = input.Substring(subStart, j - subStart); KCTDebug.Log("fn: "+function+" sub: "+sub); double val = 0.0; if (function == "l") { val = ParseMath(sub, variables); val = Math.Log(val); } else if (function == "L") { val = ParseMath(sub, variables); val = Math.Log10(val); } else if (function == "max" || function == "min") { string[] parts = new string[2]; parts[0] = input.Substring(subStart, parenComma[1] - subStart); parts[1] = input.Substring(parenComma[1] + 1, j - parenComma[1] - 1); double sub1 = ParseMath(parts[0], variables); double sub2 = ParseMath(parts[1], variables); if (function == "max") val = Math.Max(sub1, sub2); else if (function == "min") val = Math.Min(sub1, sub2); } else if (function == "sign") { val = ParseMath(sub, variables); if (val >= 0) val = 1; else val = -1; } else if (function == "abs") { val = ParseMath(sub, variables); val = Math.Abs(val); } input = input.Substring(0, i) + val.ToString() + input.Substring(j + 1); i--; } else { stack += ch; } } currentVal = DoMath(currentVal, lastOp, stack); KCTDebug.Log("(" + raw + ")=(" + input + ")=" + currentVal); return currentVal; } private static int[] FindEndParenthesis(string str, int curPos) { int depth = 0; int j = 0, commaPos = -1; for (j = curPos + 1; j < str.Length; ++j) { if (str[j] == '(') depth++; if (str[j] == ')') depth--; if (str[j] == ',' && depth == 0) commaPos = j; if (depth < 0) { break; } } return new int[] {j, commaPos}; } private static double DoMath(double currentVal, string operation, string newVal) { double newValue = 0; if (String.IsNullOrEmpty(newVal)) return currentVal; if (!double.TryParse(newVal, out newValue)) { Debug.LogError("[KCT] Tried to parse a non-double value: " + newVal); return currentVal; } switch (operation) { case "+": currentVal += newValue; break; case "-": currentVal -= newValue; break; case "*": currentVal *= newValue; break; case "/": currentVal /= newValue; break; case "%": currentVal = currentVal % long.Parse(newVal); break; case "^": currentVal = Math.Pow(currentVal, newValue); break; default: break; } return currentVal; }*/ public static double ParseBuildRateFormula(KCT_BuildListVessel.ListType type, int index, KCT_KSC KSC, bool UpgradedRates = false) { //N = num upgrades, I = rate index, L = VAB/SPH upgrade level, R = R&D level int level = 0, upgrades = 0; Dictionary<string, string> variables = new Dictionary<string, string>(); if (type == KCT_BuildListVessel.ListType.VAB) { level = KCT_Utilities.BuildingUpgradeLevel(SpaceCenterFacility.VehicleAssemblyBuilding); if (KSC.VABUpgrades.Count > index) upgrades = KSC.VABUpgrades[index]; } else if (type == KCT_BuildListVessel.ListType.SPH) { level = KCT_Utilities.BuildingUpgradeLevel(SpaceCenterFacility.SpaceplaneHangar); if (KSC.SPHUpgrades.Count > index) upgrades = KSC.SPHUpgrades[index]; } if (UpgradedRates) upgrades++; variables.Add("L", level.ToString()); variables.Add("N", upgrades.ToString()); variables.Add("I", index.ToString()); variables.Add("R", KCT_Utilities.BuildingUpgradeLevel(SpaceCenterFacility.ResearchAndDevelopment).ToString()); //int numNodes = RDController.Instance != null ? RDController.Instance.nodes.FindAll(n => n.IsResearched).Count : 0; int numNodes = 0; if (ResearchAndDevelopment.Instance != null) numNodes = ResearchAndDevelopment.Instance.snapshot.GetData().GetNodes("Tech").Length; variables.Add("S", numNodes.ToString()); AddCrewVariables(variables); return GetStandardFormulaValue("BuildRate", variables); }
public static double GetBuildRate(int index, KCT_BuildListVessel.ListType type, KCT_KSC KSC, bool UpgradedRate = false) { if (KSC == null) KSC = KCT_GameStates.ActiveKSC; double ret = 0; if (type == KCT_BuildListVessel.ListType.VAB) { if (!UpgradedRate && KSC.VABRates.Count > index) { return KSC.VABRates[index]; } else if (UpgradedRate && KSC.UpVABRates.Count > index) { return KSC.UpVABRates[index]; } else { return 0; } } else if (type == KCT_BuildListVessel.ListType.SPH) { if (!UpgradedRate && KSC.SPHRates.Count > index) { return KSC.SPHRates[index]; } else if (UpgradedRate && KSC.UpSPHRates.Count > index) { return KSC.UpSPHRates[index]; } else { return 0; } } else if (type == KCT_BuildListVessel.ListType.TechNode) { ret = KCT_GameStates.TechList[index].BuildRate; } return ret; }
public static List<double> BuildRatesVAB(KCT_KSC KSC) { if (KSC == null) KSC = KCT_GameStates.ActiveKSC; return KSC.VABRates; }
public static int TotalSpentUpgrades(KCT_KSC ksc) { if (ksc == null) ksc = KCT_GameStates.ActiveKSC; int spentPoints = 0; if (KCT_PresetManager.Instance.ActivePreset.generalSettings.SharedUpgradePool) { foreach (KCT_KSC KSC in KCT_GameStates.KSCs) { foreach (int i in KSC.VABUpgrades) spentPoints += i; foreach (int i in KSC.SPHUpgrades) spentPoints += i; spentPoints += KSC.RDUpgrades[0]; } spentPoints += ksc.RDUpgrades[1]; //only count this once, all KSCs share this value } else { foreach (int i in ksc.VABUpgrades) spentPoints += i; foreach (int i in ksc.SPHUpgrades) spentPoints += i; foreach (int i in ksc.RDUpgrades) spentPoints += i; } return spentPoints; }
public static bool ReconditioningActive(KCT_KSC KSC) { if (KSC == null) KSC = KCT_GameStates.ActiveKSC; KCT_Recon_Rollout recon = KSC.GetReconditioning(); return (recon != null); }
public static bool ReconditioningActive(KCT_KSC KSC, string launchSite = "LaunchPad") { if (KSC == null) KSC = KCT_GameStates.ActiveKSC; KCT_Recon_Rollout recon = KSC.GetReconditioning(launchSite); return (recon != null); }
public static void MoveVesselToWarehouse(int ListIdentifier, int index, KCT_KSC KSC) { if (KSC == null) KSC = KCT_GameStates.ActiveKSC; if (KCT_GameStates.kctToolbarButton != null) { KCT_GameStates.kctToolbarButton.Important = true; //Show the button if it is hidden away startedFlashing = DateTime.Now; //Set the time to start flashing } StringBuilder Message = new StringBuilder(); Message.AppendLine("The following vessel is complete:"); KCT_BuildListVessel vessel = null; if (ListIdentifier == 0) //VAB list { vessel = KSC.VABList[index]; KSC.VABList.RemoveAt(index); KSC.VABWarehouse.Add(vessel); Message.AppendLine(vessel.shipName); Message.AppendLine("Please check the VAB Storage at "+KSC.KSCName+" to launch it."); } else if (ListIdentifier == 1)//SPH list { vessel = KSC.SPHList[index]; KSC.SPHList.RemoveAt(index); KSC.SPHWarehouse.Add(vessel); Message.AppendLine(vessel.shipName); Message.AppendLine("Please check the SPH Storage at " + KSC.KSCName + " to launch it."); } if ((KCT_GameStates.settings.ForceStopWarp || vessel == KCT_GameStates.targetedItem) && TimeWarp.CurrentRateIndex != 0) { TimeWarp.SetRate(0, true); KCT_GameStates.warpInitiated = false; } //Assign science based on science rate double rate = KCT_MathParsing.GetStandardFormulaValue("Research", new Dictionary<string, string>() { { "N", KSC.RDUpgrades[0].ToString() }, { "R", KCT_Utilities.BuildingUpgradeLevel(SpaceCenterFacility.ResearchAndDevelopment).ToString() } }); if (CurrentGameHasScience() && !vessel.cannotEarnScience && rate > 0) AddScienceWithMessage((float)(rate*vessel.buildPoints), TransactionReasons.None); //Add parts to the tracker if (!vessel.cannotEarnScience) { List<string> trackedParts = new List<string>(); foreach (ConfigNode p in vessel.ExtractedPartNodes) { if (!trackedParts.Contains(PartNameFromNode(p)+GetTweakScaleSize(p))) { AddPartToTracker(PartNameFromNode(p) + GetTweakScaleSize(p)); trackedParts.Add(PartNameFromNode(p) + GetTweakScaleSize(p)); } } } string stor = ListIdentifier == 0 ? "VAB" : "SPH"; KCTDebug.Log("Moved vessel " + vessel.shipName + " to " +KSC.KSCName + "'s " + stor + " storage."); foreach (KCT_KSC KSC_iterator in KCT_GameStates.KSCs) { foreach (KCT_BuildListVessel blv in KSC_iterator.VABList) { double newTime = KCT_Utilities.GetBuildTime(blv.ExtractedPartNodes, true, blv.InventoryParts); //Use only the parts that were originally used when recalculating if (newTime < blv.buildPoints) { blv.buildPoints = blv.buildPoints - ((blv.buildPoints - newTime) * (100 - blv.ProgressPercent()) / 100.0); //If progress=0% then set to new build time, 100%=no change, 50%=half of difference. } } foreach (KCT_BuildListVessel blv in KSC_iterator.SPHList) { double newTime = KCT_Utilities.GetBuildTime(blv.ExtractedPartNodes, true, blv.InventoryParts); if (newTime < blv.buildPoints) { blv.buildPoints = blv.buildPoints - ((blv.buildPoints - newTime) * (100 - blv.ProgressPercent()) / 100.0); //If progress=0% then set to new build time, 100%=no change, 50%=half of difference. } } } KCT_GUI.ResetBLWindow(false); if (!KCT_GameStates.settings.DisableAllMessages) DisplayMessage("Vessel Complete!", Message, MessageSystemButton.MessageButtonColor.GREEN, MessageSystemButton.ButtonIcons.COMPLETE); }
public override void OnLoad(ConfigNode node) { KCTDebug.Log("Reading from persistence."); base.OnLoad(node); KCT_GameStates.KSCs.Clear(); KCT_GameStates.ActiveKSC = null; //KCT_Utilities.SetActiveKSC("Stock"); KCT_GameStates.TechList.Clear(); KCT_GameStates.TechUpgradesTotal = 0; KCT_DataStorage kctVS = new KCT_DataStorage(); ConfigNode CN = node.GetNode(kctVS.GetType().Name); if (CN != null) ConfigNode.LoadObjectFromConfig(kctVS, CN); foreach (ConfigNode ksc in node.GetNodes("KSC")) { string name = ksc.GetValue("KSCName"); KCT_KSC loaded_KSC = new KCT_KSC(name); loaded_KSC.FromConfigNode(ksc); if (loaded_KSC != null && loaded_KSC.KSCName != null && loaded_KSC.KSCName.Length > 0) { loaded_KSC.RDUpgrades[1] = KCT_GameStates.TechUpgradesTotal; if (KCT_GameStates.KSCs.Find(k => k.KSCName == loaded_KSC.KSCName) == null) KCT_GameStates.KSCs.Add(loaded_KSC); } } KCT_Utilities.SetActiveKSCToRSS(); //KCT_Utilities.SetActiveKSC(KCT_GameStates.activeKSCName); ConfigNode tmp = node.GetNode("TechList"); if (tmp != null) { foreach (ConfigNode techNode in tmp.GetNodes("Tech")) { KCT_TechStorageItem techStorageItem = new KCT_TechStorageItem(); ConfigNode.LoadObjectFromConfig(techStorageItem, techNode); KCT_TechItem techItem = techStorageItem.ToTechItem(); techItem.protoNode = new ProtoTechNode(techNode.GetNode("ProtoNode")); KCT_GameStates.TechList.Add(techItem); } } //KCT_GameStates.ActiveKSC.AsConfigNode().Save(KSPUtil.ApplicationRootPath + "/KSC.node"); KerbalConstructionTime.DelayedStart(); KCT_GUI.CheckToolbar(); KCT_GameStates.erroredDuringOnLoad.OnLoadFinish(); }
public static double ParseBuildRateFormula(KCT_BuildListVessel.ListType type, int index, KCT_KSC KSC, bool UpgradedRates = false) { //N = num upgrades, I = rate index, L = VAB/SPH upgrade level, R = R&D level int level = 0, upgrades = 0; Dictionary <string, string> variables = new Dictionary <string, string>(); if (type == KCT_BuildListVessel.ListType.VAB) { level = KCT_Utilities.BuildingUpgradeLevel(SpaceCenterFacility.VehicleAssemblyBuilding); if (KSC.VABUpgrades.Count > index) { upgrades = KSC.VABUpgrades[index]; } } else if (type == KCT_BuildListVessel.ListType.SPH) { level = KCT_Utilities.BuildingUpgradeLevel(SpaceCenterFacility.SpaceplaneHangar); if (KSC.SPHUpgrades.Count > index) { upgrades = KSC.SPHUpgrades[index]; } } if (UpgradedRates) { upgrades++; } variables.Add("L", level.ToString()); variables.Add("N", upgrades.ToString()); variables.Add("I", index.ToString()); variables.Add("R", KCT_Utilities.BuildingUpgradeLevel(SpaceCenterFacility.ResearchAndDevelopment).ToString()); return(GetStandardFormulaValue("BuildRate", variables)); }
public static int TotalSpentUpgrades(KCT_KSC ksc) { if (ksc == null) ksc = KCT_GameStates.ActiveKSC; int spentPoints = 0; foreach (int i in ksc.VABUpgrades) spentPoints += i; foreach (int i in ksc.SPHUpgrades) spentPoints += i; foreach (int i in ksc.RDUpgrades) spentPoints += i; return spentPoints; }
/* public static double ParseMath(string input, Dictionary<string, string> variables) * { * // KCTDebug.Log("Input_raw: " + input); * string raw = input; * foreach (KeyValuePair<string, string> kvp in variables) * { * if (input.Contains("[" + kvp.Key + "]")) * { * input = input.Replace("[" + kvp.Key + "]", kvp.Value); * } * } * //KCTDebug.Log("Input_replaced: " + input); * * double currentVal = 0; * string stack = ""; * string lastOp = "+"; * string[] ops = { "+", "-", "*", "/", "%", "^", "(", "e", "E" }; * string[] functions = { "min", "max", "l", "L", "abs", "sign" }; * for (int i = 0; i < input.Length; ++i) * { * string ch = input[i].ToString(); * bool isOp = false, isFunction = false; * // KCTDebug.Log(ch); * foreach (string op in ops) * { * if (op == ch) * { * isOp = true; * break; * } * } * if (!isOp) * { * foreach (string fun in functions) * { * if (fun[0] == input[i]) * { * isFunction = true; * break; * } * } * } * * if (isOp) * { * if (ch == "-" && (stack.Trim() == "")) * { * stack += ch; * } * else if (ch == "e" || ch == "E") * { * int index=i+2; * for (index = i+2; index < input.Length; ++index) * { * string ch2 = input[index].ToString(); * if (ops.Contains(ch2)) * break; * } * string sub = input.Substring(i + 1, index - i - 1); * double exp = ParseMath(sub, variables); * double newVal = double.Parse(stack) * Math.Pow(10, exp); * currentVal = DoMath(currentVal, lastOp, newVal.ToString()); * stack = "0"; * lastOp = "+"; * i = index - 1; * } * else if (ch == "(") * { * int j = FindEndParenthesis(input, i)[0]; * string sub = input.Substring(i + 1, j - i - 1); * string val = ParseMath(sub, variables).ToString(); * input = input.Substring(0, i) + val + input.Substring(j + 1); * --i; * } * else * { * currentVal = DoMath(currentVal, lastOp, stack); * lastOp = ch; * stack = ""; * } * } * else if (isFunction) * { * int subStart = input.IndexOf('(', i)+1; * string function = input.Substring(i, subStart - i - 1); * //KCTDebug.Log(function); * int[] parenComma = FindEndParenthesis(input, subStart-1); * int j = parenComma[0]; * int comma = parenComma[1]; * string sub = input.Substring(subStart, j - subStart); * KCTDebug.Log("fn: "+function+" sub: "+sub); * double val = 0.0; * * if (function == "l") * { * val = ParseMath(sub, variables); * val = Math.Log(val); * } * else if (function == "L") * { * val = ParseMath(sub, variables); * val = Math.Log10(val); * } * else if (function == "max" || function == "min") * { * string[] parts = new string[2]; * parts[0] = input.Substring(subStart, parenComma[1] - subStart); * parts[1] = input.Substring(parenComma[1] + 1, j - parenComma[1] - 1); * double sub1 = ParseMath(parts[0], variables); * double sub2 = ParseMath(parts[1], variables); * if (function == "max") * val = Math.Max(sub1, sub2); * else if (function == "min") * val = Math.Min(sub1, sub2); * } * else if (function == "sign") * { * val = ParseMath(sub, variables); * if (val >= 0) * val = 1; * else * val = -1; * } * else if (function == "abs") * { * val = ParseMath(sub, variables); * val = Math.Abs(val); * } * * input = input.Substring(0, i) + val.ToString() + input.Substring(j + 1); * i--; * } * else * { * stack += ch; * } * } * currentVal = DoMath(currentVal, lastOp, stack); * KCTDebug.Log("(" + raw + ")=(" + input + ")=" + currentVal); * return currentVal; * } * * private static int[] FindEndParenthesis(string str, int curPos) * { * int depth = 0; * int j = 0, commaPos = -1; * for (j = curPos + 1; j < str.Length; ++j) * { * if (str[j] == '(') depth++; * if (str[j] == ')') depth--; * * if (str[j] == ',' && depth == 0) * commaPos = j; * * if (depth < 0) * { * break; * } * } * return new int[] {j, commaPos}; * } * * private static double DoMath(double currentVal, string operation, string newVal) * { * double newValue = 0; * if (String.IsNullOrEmpty(newVal)) * return currentVal; * if (!double.TryParse(newVal, out newValue)) * { * Debug.LogError("[KCT] Tried to parse a non-double value: " + newVal); * return currentVal; * } * switch (operation) * { * case "+": currentVal += newValue; break; * case "-": currentVal -= newValue; break; * case "*": currentVal *= newValue; break; * case "/": currentVal /= newValue; break; * case "%": currentVal = currentVal % long.Parse(newVal); break; * case "^": currentVal = Math.Pow(currentVal, newValue); break; * default: break; * } * * return currentVal; * }*/ public static double ParseBuildRateFormula(KCT_BuildListVessel.ListType type, int index, KCT_KSC KSC, bool UpgradedRates = false) { //N = num upgrades, I = rate index, L = VAB/SPH upgrade level, R = R&D level int level = 0, upgrades = 0; Dictionary <string, string> variables = new Dictionary <string, string>(); if (type == KCT_BuildListVessel.ListType.VAB) { level = KCT_Utilities.BuildingUpgradeLevel(SpaceCenterFacility.VehicleAssemblyBuilding); if (KSC.VABUpgrades.Count > index) { upgrades = KSC.VABUpgrades[index]; } } else if (type == KCT_BuildListVessel.ListType.SPH) { level = KCT_Utilities.BuildingUpgradeLevel(SpaceCenterFacility.SpaceplaneHangar); if (KSC.SPHUpgrades.Count > index) { upgrades = KSC.SPHUpgrades[index]; } } if (UpgradedRates) { upgrades++; } variables.Add("L", level.ToString()); variables.Add("N", upgrades.ToString()); variables.Add("I", index.ToString()); variables.Add("R", KCT_Utilities.BuildingUpgradeLevel(SpaceCenterFacility.ResearchAndDevelopment).ToString()); //int numNodes = RDController.Instance != null ? RDController.Instance.nodes.FindAll(n => n.IsResearched).Count : 0; int numNodes = 0; if (ResearchAndDevelopment.Instance != null) { numNodes = ResearchAndDevelopment.Instance.snapshot.GetData().GetNodes("Tech").Length; } variables.Add("S", numNodes.ToString()); AddCrewVariables(variables); return(GetStandardFormulaValue("BuildRate", variables)); }
public static void SetActiveKSC(string site) { if (site == "") site = "Stock"; if (KCT_GameStates.ActiveKSC == null || site != KCT_GameStates.ActiveKSC.KSCName) { KCTDebug.Log("Setting active site to " + site); KCT_KSC setActive = KCT_GameStates.KSCs.FirstOrDefault(ksc => ksc.KSCName == site); if (setActive != null) { KCT_GameStates.ActiveKSC = setActive; } else { setActive = new KCT_KSC(site); if (CurrentGameIsCareer()) setActive.ActiveLPInstance.level = 0; KCT_GameStates.KSCs.Add(setActive); KCT_GameStates.ActiveKSC = setActive; } } KCT_GameStates.activeKSCName = site; }