public void Awake() { DontDestroyOnLoad(this); GameEvents.onGameSceneSwitchRequested.Add(OnGameSceneSwitchRequested); GameEvents.onGUILaunchScreenSpawn.Add(OnGUILaunchScreenSpawn); GameEvents.onLevelWasLoadedGUIReady.Add(OnLevelWasLoadedGUIReady); GameEvents.onEditorShipModified.Add(OnEditorShipModified); //GameEvents.onGUIApplicationLauncherReady.Add(OnGUIAppLauncherReady); GameEvents.onGUIApplicationLauncherDestroyed.Add(OnGUIApplicationLauncherDestroyed); GameEvents.onVesselRecovered.Add(OnVesselRecovered); partCostRules = new List <XenoIndustryLaunchCostsPartRule>(); resourceCostRules = new List <XenoIndustryLaunchCostsResourceRule>(); JSONNode[] launchCostRuleNodes = JSONUtil.ReadJSONFile(MOD_PATH + "launchCosts.json"); if (launchCostRuleNodes != null) { foreach (JSONNode ruleNode in launchCostRuleNodes) { if (ruleNode["type"] == null) { Debug.Log("XenoIndustryLaunchCosts: cost rule is lacking type, skipping"); continue; } if (ruleNode["type"] == "part") { XenoIndustryLaunchCostsPartRule rule = new XenoIndustryLaunchCostsPartRule(); Debug.Log("XenoIndustryLaunchCosts: adding new part rule"); if (ruleNode["name"] != null) { Debug.Log(String.Format("XenoIndustryLaunchCosts: adding name {0} to rule", ruleNode["name"])); rule.partName = ruleNode["name"]; } if (ruleNode["moduleName"] != null) { Debug.Log(String.Format("XenoIndustryLaunchCosts: adding moduleName {0} to rule", ruleNode["moduleName"])); rule.moduleName = ruleNode["moduleName"]; } if (ruleNode["containsResource"] != null) { Debug.Log(String.Format("XenoIndustryLaunchCosts: adding containsResource {0} to rule", ruleNode["containsResource"])); rule.containsResource = ruleNode["containsResource"]; } if (ruleNode["crewedOnly"] != null) { Debug.Log(String.Format("XenoIndustryLaunchCosts: adding crewedOnly {0} to rule", ruleNode["crewedOnly"])); rule.crewedOnly = ruleNode["crewedOnly"].AsBool; } if (ruleNode["nonCrewedOnly"] != null) { Debug.Log(String.Format("XenoIndustryLaunchCosts: adding nonCrewedOnly {0} to rule", ruleNode["nonCrewedOnly"])); rule.nonCrewedOnly = ruleNode["nonCrewedOnly"].AsBool; } if (ruleNode["itemCosts"] != null) { if (!ruleNode["itemCosts"].IsObject) { Debug.Log("XenoIndustryLaunchCosts: rule itemCosts are not valid, must be object"); } else { foreach (KeyValuePair <string, JSONNode> itemNode in ruleNode["itemCosts"].AsObject) { string name = itemNode.Key; int count = itemNode.Value.AsInt; rule.itemCosts[name] = count; Debug.Log(String.Format("XenoIndustryLaunchCosts: adding item cost {0} of item {1} to rule", count, name)); } } } partCostRules.Add(rule); } else if (ruleNode["type"] == "resource") { XenoIndustryLaunchCostsResourceRule rule = new XenoIndustryLaunchCostsResourceRule(); Debug.Log("XenoIndustryLaunchCosts: adding new resource rule"); if (ruleNode["name"] != null) { Debug.Log(String.Format("XenoIndustryLaunchCosts: adding name {0} to rule", ruleNode["name"])); rule.resourceName = ruleNode["name"]; } if (ruleNode["itemCosts"] != null) { if (!ruleNode["itemCosts"].IsObject) { Debug.Log("XenoIndustryLaunchCosts: rule itemCosts are not valid, must be object"); } else { foreach (KeyValuePair <string, JSONNode> itemNode in ruleNode["itemCosts"].AsObject) { string name = itemNode.Key; int count = itemNode.Value.AsInt; rule.itemCosts[name] = count; Debug.Log(String.Format("XenoIndustryLaunchCosts: adding item cost {0} of item {1} to rule", count, name)); } } } resourceCostRules.Add(rule); } else { Debug.Log(String.Format("XenoIndustryLaunchCosts: cost rule has invalid type {0}, skipping", ruleNode["type"])); } } } windowRect = new Rect(Screen.width / 2 - 150, Screen.height / 2 - 150, 300, 100); clusterioInventory = new Dictionary <string, int>(); latestLaunchCosts = new Dictionary <string, int>(); }
private void ApplyPartCostRules(AvailablePart part, float cost, ref Dictionary <string, int> launchCosts) { XenoIndustryLaunchCostsPartRule finalRule = null; foreach (XenoIndustryLaunchCostsPartRule rule in partCostRules) { if (rule.partName != null && part.name != rule.partName) { continue; } if (rule.moduleName != null) { bool hasModule = false; foreach (PartModule module in part.partPrefab.Modules) { if (module.ClassName == rule.moduleName) { hasModule = true; break; } } if (!hasModule) { continue; } } if (rule.containsResource != null) { bool hasResource = false; foreach (PartResource resource in part.partPrefab.Resources) { if (resource.resourceName == rule.containsResource) { hasResource = true; break; } } if (!hasResource) { continue; } } if (part.partPrefab.CrewCapacity > 0 && rule.nonCrewedOnly) { continue; } else if (part.partPrefab.CrewCapacity == 0 && rule.crewedOnly) { continue; } finalRule = rule; } if (finalRule == null) { Debug.Log("XenoIndustryLaunchCosts: no rule has passed, part cannot be counted into final cost"); return; } foreach (KeyValuePair <string, int> kvPair in finalRule.itemCosts) { string itemName = kvPair.Key; int itemCost = kvPair.Value; if (launchCosts.ContainsKey(itemName)) { launchCosts[itemName] += (int)Math.Ceiling(cost / (float)itemCost); } else { launchCosts[itemName] = (int)Math.Ceiling(cost / (float)itemCost); } } }