private void PushResources(string resourceName) { if (!part.Resources.Contains(resourceName)) { return; } var brokRes = part.Resources[resourceName]; //Put remaining parts in warehouses var wh = LogisticsTools.GetRegionalWarehouses(vessel, "USI_ModuleCleaningBin"); var count = wh.Count; for (int i = 0; i < count; ++i) { var p = wh[i]; if (p.Resources.Contains(resourceName)) { var partRes = p.Resources[resourceName]; var partNeed = partRes.maxAmount - partRes.amount; if (partNeed > 0 && brokRes.amount > 0) { if (partNeed > brokRes.amount) { partNeed = brokRes.amount; } partRes.amount += partNeed; brokRes.amount -= partNeed; } } } }
private void ConsumeResource(ResourceRatio resourceRatio, double percentage) { var resourceName = resourceRatio.ResourceName; var needed = resourceRatio.Ratio * percentage; var sourceParts = LogisticsTools.GetRegionalWarehouses(vessel, nameof(USI_ModuleResourceWarehouse)); foreach (var sourcePart in sourceParts) { if (sourcePart != part) { var warehouse = sourcePart.FindModuleImplementing <USI_ModuleResourceWarehouse>(); if (warehouse != null && warehouse.localTransferEnabled && sourcePart.Resources.Contains(resourceName)) { var resource = sourcePart.Resources[resourceName]; if (resource.flowState) { if (resource.amount >= needed) { resource.amount -= needed; needed = 0; break; } else { needed -= resource.amount; resource.amount = 0; } } } } } }
public List <Vessel> GetPowerDistributors() { var distributors = new List <Vessel>(); var nearbyVessels = LogisticsTools.GetNearbyVessels(LogisticsTools.PHYSICS_RANGE, false, vessel, true); var count = nearbyVessels.Count; for (int i = 0; i < count; ++i) { var v = nearbyVessels[i]; var range = LogisticsTools.GetRange(vessel, v); var pCount = v.parts.Count; for (int x = 0; x < pCount; ++x) { var p = v.parts[x]; var mod = p.FindModuleImplementing <ModulePowerDistributor>(); if (mod == null) { continue; } if (mod.ActiveDistributionRange >= range) { distributors.Add(v); } } } return(distributors); }
public List <Vessel> GetResourceStockpiles() { var depots = new List <Vessel>(); var potDeps = LogisticsTools.GetNearbyVessels(LogisticsSetup.Instance.Config.ScavangeRange, false, vessel, true); var count = potDeps.Count; for (int i = 0; i < count; ++i) { if (potDeps[i].FindPartModulesImplementing <USI_ModuleResourceWarehouse>().Count > 0) { depots.Add(potDeps[i]); } } var nearbyVesselList = LogisticsTools.GetNearbyVessels(LogisticsTools.PHYSICS_RANGE, true, vessel, true); count = nearbyVesselList.Count; for (int i = 0; i < count; ++i) { var v = nearbyVesselList[i]; var range = LogisticsTools.GetRange(vessel, v); var pCount = v.parts.Count; for (int q = 0; q < pCount; ++q) { var p = v.parts[q]; if (p.FindModuleImplementing <ModuleResourceDistributor>() == null) { continue; } if (!LogisticsTools.HasCrew(p.vessel, "Pilot")) { continue; } var m = p.FindModuleImplementing <ModuleResourceDistributor>(); if (range <= m.ResourceDistributionRange) { //Now find ones adjacent to our depot. var potStock = LogisticsTools.GetNearbyVessels(m.ResourceDistributionRange, false, vessel, true); var potCount = potStock.Count; for (int z = 0; z < potCount; ++z) { var s = potStock[z]; if (s.FindPartModulesImplementing <USI_ModuleResourceWarehouse>().Count == 0) { continue; } if (!depots.Contains(s)) { depots.Add(s); } } } } } return(depots); }
private void GrabResources(string resourceName) { if (!part.Resources.Contains(resourceName)) { return; } var brokRes = part.Resources[resourceName]; var needed = brokRes.maxAmount - brokRes.amount; //Pull in from warehouses var whpList = LogisticsTools.GetRegionalWarehouses(vessel, "USI_ModuleResourceWarehouse"); var count = whpList.Count; for (int i = 0; i < count; ++i) { var whp = whpList[i]; if (whp == part) { continue; } var whc = whp.FindModulesImplementing <BaseConverter>(); if (whc.Count > 0) { continue; } if (whp.Modules.Contains("USI_ModuleResourceWarehouse")) { var wh = whp.FindModuleImplementing <USI_ModuleResourceWarehouse>(); if (!wh.localTransferEnabled) { continue; } } if (whp.Resources.Contains(resourceName)) { var res = whp.Resources[resourceName]; if (res.amount >= needed) { brokRes.amount += needed; res.amount -= needed; needed = 0; break; } else { brokRes.amount += res.amount; needed -= res.amount; res.amount = 0; } } } }
private bool HasResource(ResourceRatio resInfo) { var resourceName = resInfo.ResourceName; var costMultiplier = USI_ConverterOptions.ConverterSwapCostMultiplierValue; if (costMultiplier <= ResourceUtilities.FLOAT_TOLERANCE) { return(true); } var needed = resInfo.Ratio * costMultiplier; var whpList = LogisticsTools.GetRegionalWarehouses(vessel, "USI_ModuleResourceWarehouse"); //EC we're a lot less picky... if (resInfo.ResourceName == "ElectricCharge") { whpList.AddRange(part.vessel.parts); } var count = whpList.Count; for (int i = 0; i < count; ++i) { var whp = whpList[i]; if (whp == part) { continue; } if (resInfo.ResourceName != "ElectricCharge") { var wh = whp.FindModuleImplementing <USI_ModuleResourceWarehouse>(); if (wh != null) { if (!wh.localTransferEnabled) { continue; } } } if (whp.Resources.Contains(resourceName)) { var res = whp.Resources[resourceName]; if (res.amount >= needed) { needed = 0; break; } else { needed -= res.amount; } } } return(needed < ResourceUtilities.FLOAT_TOLERANCE); }
private void TakeResources(ResourceRatio resInfo) { var resourceName = resInfo.ResourceName; var costMultiplier = USI_ConverterOptions.ConverterSwapCostMultiplierValue; if (costMultiplier > 0) { var needed = resInfo.Ratio * costMultiplier; //Pull in from warehouses var whpList = LogisticsTools.GetRegionalWarehouses(vessel, "USI_ModuleResourceWarehouse"); var count = whpList.Count; for (int i = 0; i < count; ++i) { var whp = whpList[i]; if (whp == part) { continue; } var wh = whp.FindModuleImplementing <USI_ModuleResourceWarehouse>(); if (wh != null) { if (!wh.localTransferEnabled) { continue; } } if (whp.Resources.Contains(resourceName)) { var res = whp.Resources[resourceName]; if (res.amount >= needed) { res.amount -= needed; needed = 0; break; } else { needed -= res.amount; res.amount = 0; } } } } }
private void SwapResources(string fetchName, string storeName) { if (!part.Resources.Contains(storeName)) { return; } var brokRes = part.Resources[storeName]; var needed = brokRes.maxAmount - brokRes.amount; //Pull in from warehouses var whpList = LogisticsTools.GetRegionalWarehouses(vessel, "USI_ModuleResourceWarehouse"); var count = whpList.Count; for (int i = 0; i < count; ++i) { var whp = whpList[i]; var wh = whp.FindModuleImplementing <USI_ModuleResourceWarehouse>(); if (wh != null) { if (!wh.localTransferEnabled) { continue; } } if (whp.Resources.Contains(fetchName)) { print("Found " + fetchName); var res = whp.Resources[fetchName]; if (res.amount >= needed) { brokRes.amount += needed; res.amount -= needed; needed = 0; break; } else { brokRes.amount += res.amount; needed -= res.amount; res.amount = 0; } } } }
private void PushResources(string resourceName, double amount) { var vessels = LogisticsTools.GetNearbyVessels(2000, true, vessel, false); var count = vessels.Count; for (int i = 0; i < count; ++i) { var v = vessels[i]; //Put recycled stuff into recycleable places var parts = v.parts; var pCount = parts.Count; for (int x = 0; x < pCount; ++x) { var p = parts[x]; if (p == part || !p.Modules.Contains("USI_ModuleRecycleBin")) { continue; } if (!p.Resources.Contains(resourceName)) { continue; } var partRes = p.Resources[resourceName]; var partNeed = partRes.maxAmount - partRes.amount; if (partNeed > 0 && amount > 0) { if (partNeed > amount) { partNeed = amount; } partRes.amount += partNeed; amount -= partNeed; } } } if (amount > 1f) { ScreenMessages.PostScreenMessage(String.Format("{0:0} units of {1} were lost due to lack of recycle space", amount, ResourceName), 5f, ScreenMessageStyle.UPPER_CENTER); } }
protected double FindResourceAvailablePercentage(ResourceRatio resource) { var resourceName = resource.ResourceName; var needed = resource.Ratio; if (needed < ResourceUtilities.FLOAT_TOLERANCE) { return(1d); } var available = 0d; var warehouses = LogisticsTools.GetRegionalModules <USI_ModuleResourceWarehouse>(vessel); if (warehouses == null || warehouses.Count < 1) { return(available); } foreach (var warehouse in warehouses) { if (warehouse.vessel != vessel && !warehouse.localTransferEnabled && resourceName != "ElectricCharge") { continue; } if (warehouse.part.Resources.Contains(resourceName)) { var partResource = warehouse.part.Resources[resourceName]; if (partResource.flowState) { available += warehouse.part.Resources[resourceName].amount; if (available >= needed) { return(1d); } } } } return(available / needed); }
private void ConsumeResource(ResourceRatio resInfo, double percentage) { var resourceName = resInfo.ResourceName; var needed = resInfo.Ratio * percentage; //Pull in from warehouses var sourceParts = LogisticsTools.GetRegionalWarehouses(vessel, "USI_ModuleResourceWarehouse"); foreach (var sourcePart in sourceParts) { if (sourcePart == part) { continue; } var warehouse = sourcePart.FindModuleImplementing <USI_ModuleResourceWarehouse>(); if (warehouse != null) { if (!warehouse.localTransferEnabled) { continue; } } if (sourcePart.Resources.Contains(resourceName)) { var res = sourcePart.Resources[resourceName]; if (res.amount >= needed) { res.amount -= needed; needed = 0; break; } else { needed -= res.amount; res.amount = 0; } } } }
private double FindResources(ResourceRatio resInfo) { var resourceName = resInfo.ResourceName; var needed = resInfo.Ratio; if (needed < ResourceUtilities.FLOAT_TOLERANCE) { return(1d); } var available = 0d; var sourceParts = LogisticsTools.GetRegionalWarehouses(vessel, "USI_ModuleResourceWarehouse"); foreach (var sourcePart in sourceParts) { if (sourcePart == part) { continue; } var warehouse = sourcePart.FindModuleImplementing <USI_ModuleResourceWarehouse>(); if (resInfo.ResourceName != "ElectricCharge" && warehouse != null) //EC we're a lot less picky... { if (!warehouse.localTransferEnabled) { continue; } } if (sourcePart.Resources.Contains(resourceName)) { available += sourcePart.Resources[resourceName].amount; if (available >= needed) { return(1d); } } } return(available / needed); }