public void setNetworkResources(pipelineShip v, ref xPipeLine pipeModule, double dt) { // foreach (var res in pipeModule.part.Resources.list) foreach(string res in pipeModule.totalDemand.Keys) { v.resourceDemand[res] = pipeModule.totalDemand[res]; } foreach (string res in pipeModule.totalSupply.Keys) { v.resourceSupply[res] = pipeModule.totalSupply[res]; } //double capacityScalar = res.amount; //print("addResource:" + capacityScalar.ToString()); //foreach (pipelineShip px in Networks[v.networkID]) // if (px.resourceMaximums.ContainsKey(res.info.name) && px.id != v.id) // { // double deltaRes = px.resourceMaximums[res.info.name] - px.resourceAmounts[res.info.name]; // deltaRes = Math.Min(capacityScalar, deltaRes); // px.resourceAmounts[res.info.name] += deltaRes; //distribute network resources evenly // capacityScalar -= deltaRes; // print("added " + deltaRes + " to go " + capacityScalar); // } // if(v.resourceSupply.ContainsKey(res.name)) // { // v.resourceDeltas[res.info.name] = (res.amount - v.resourceAmounts[res.info.name]) * dt; // v.resourceAmounts[res.info.name] = res.amount; // } //} }
public void applyNetworkDemand(double dt, pipelineShip p) { // double demandPerShip = dt / Networks[p.networkID].Count; //demand is in time! foreach (pipelineShip px in Networks[p.networkID]) { foreach(string k in px.resourceCapacity.Keys) { px.resourceCapacity[k] -= dt; } } }
public void getNetworkResources(pipelineShip p, ref xPipeLine pipeModule, double dt) { Dictionary<string, double> supply = new Dictionary<string, double>(); Dictionary<string, double> demand = new Dictionary<string, double>(); Dictionary<string, double> capacity = new Dictionary<string, double>(); // pipeModule.contributors = new Dictionary<string, Dictionary<string, double>>(); foreach(pipelineShip px in Networks[p.networkID]) { if (px.id == p.id) continue; foreach(string k in px.resourceSupply.Keys) { if (k == "pipeNetwork") continue; if (supply.ContainsKey(k)) { double capacityScalar = Math.Min(px.resourceCapacity[k], dt) / dt; supply[k] += px.resourceSupply[k] * capacityScalar; demand[k] += px.resourceDemand[k]; } else { double capacityScalar = Math.Min(px.resourceCapacity[k], dt) / dt; supply[k] = px.resourceSupply[k] * capacityScalar; demand[k] = px.resourceDemand[k]; } } } //pipeModule.part.Resources.list.Clear(); //foreach (string k in amounts.Keys) //{ // ConfigNode resourceNode = new ConfigNode("RESOURCE"); // resourceNode.AddValue("name", k); // resourceNode.AddValue("amount", amounts[k].ToString()); // resourceNode.AddValue("maxAmount", maximums[k].ToString()); // print("try to add resource..."); // try // { // var resource = pipeModule.part.AddResource(resourceNode); // print("resource defined " + resource.info.name + " " + resource.amount.ToString() + " " + resource.maxAmount.ToString()); // } // catch { } //} print("part has resources " + pipeModule.part.Resources.Count.ToString()); foreach (var res in pipeModule.part.Resources.list) print("resource " + res.info.name + " " + res.amount.ToString() + " " + res.maxAmount.ToString()); string recipeOutputs = ""; foreach (string k in demand.Keys) { recipeOutputs += k + "," + ( (supply[k] - demand[k])).ToString() + ",true;"; } recipeOutputs = recipeOutputs.TrimEnd(';'); print("recipe outputs = " + recipeOutputs); pipeModule.RecipeOutputs = recipeOutputs; pipeModule.reloadRecipe(); print("adjusting mass... resource mass = " + pipeModule.part.GetResourceMass().ToString()); print("part mass..." + pipeModule.part.mass.ToString()); p.needsResourceUpdate = false; }
public int shipsWithResource(List<pipelineShip> network, string resource, pipelineShip me, bool ignore) { int count = 0; foreach(var ship in network) if(ship.resourceSupply.ContainsKey(resource) && (ignore && ship.id != me.id)) count++; return count; }
public double requestNetworkResource(pipelineShip v, string resource, double amount) { try { pipelineShip p = v; double count =shipsWithResource(Networks[p.networkID], resource, p, true); if (count == 0) return 0; double totalRequested = 0; double requestPerShip = amount / count; //print("count = " + count.ToString()); // print("amount = " + amount.ToString()); // print("requestPerShip = " + requestPerShip.ToString()); foreach (pipelineShip px in Networks[p.networkID]) { if (px.id == p.id) continue; if (px.loaded) //lan ship.. we can draw resource directly from it { //print("lan resource draw pxloaded=" + px.loaded.ToString() ); Vessel loaded = FlightGlobals.Vessels.Find(x => x.id.ToString() == px.id); // print("found vessel? " + (loaded == null).ToString()); if (loaded != null) { px.loaded = loaded.loaded; var vesselResources = loaded.GetActiveResources(); foreach (var res in vesselResources) if (res.info.name == resource) { if (requestPerShip > 0) //drawing resource { //if supply is 1 and request is 2, return 1 double thisShipSupplied = Math.Min(res.amount, requestPerShip); totalRequested += thisShipSupplied; double consumed = loaded.parts[0].RequestResource(res.info.name, thisShipSupplied); var loadedPipeline = loaded.FindPartModulesImplementing<xPipeLine>(); for (int i = 0; i < loadedPipeline.Count; i++) if (loadedPipeline[i].localMaster) { loadedPipeline[i].setConsumed(res.info.name,consumed); } } if (requestPerShip < 0) //drawing resource { //if supply is 1 away from cap and request is 2, return 1 double thisShipSupplied = Math.Min(res.maxAmount - res.amount, -requestPerShip); totalRequested -= thisShipSupplied; double consumed = loaded.parts[0].RequestResource(res.info.name, -thisShipSupplied); var loadedPipeline = loaded.FindPartModulesImplementing<xPipeLine>(); for (int i = 0; i < loadedPipeline.Count; i++) if (loadedPipeline[i].localMaster) loadedPipeline[i].setConsumed(res.info.name, consumed); //we got here... there's a pipeline on the ship } break; } } } else { if (px.resourceCapacity.ContainsKey(resource)) { if (requestPerShip > 0) //drawing resource { //if supply is 1 and request is 2, return 1 double thisShipSupplied = Math.Min(px.resourceSupply[resource], requestPerShip); totalRequested += thisShipSupplied; px.resourceSupply[resource] -= thisShipSupplied; } if (requestPerShip < 0) //drawing resource { //if supply is 1 away from cap and request is 2, return 1 double thisShipSupplied = Math.Min(px.resourceCapacity[resource] - px.resourceSupply[resource], -requestPerShip); totalRequested -= thisShipSupplied; px.resourceSupply[resource] += thisShipSupplied; } //if request is 0, do nothing } } } return totalRequested; } catch { return 0; } //ship isn't in a network, return 0 }
public void addShipTonetwork(pipelineShip p, string id) { Networks[id].Add(p); p.networkID = id; }
List<string> networksInRange(pipelineShip p, double range) { List<string> ret = new List<string>(); foreach (string id in Networks.Keys) { if (networkContainsShip(id, p, range)) ret.Add(id); } return ret; }
bool networkContainsShip(string id, pipelineShip p, double range) { Vector3d pLoc = FlightGlobals.currentMainBody.GetWorldSurfacePosition(p.lat, p.lon, p.altitude); foreach(pipelineShip xp in Networks[id]) { Vector3d xploc = FlightGlobals.currentMainBody.GetWorldSurfacePosition(xp.lat, xp.lon, xp.altitude); if (Vector3d.Distance(xploc, pLoc) < range) return true; } return false; }
public static pipelineShip fromVessel(Vessel v, ref xPipeLine pipe) { pipelineShip ret = new pipelineShip(); ret.id = v.id.ToString(); ret.lat = v.latitude; ret.lon = v.longitude; ret.name = v.RevealName() ; ret.body = v.mainBody.name; ret.altitude = v.altitude; var modulelist = v.FindPartModulesImplementing<xPipeLine>(); ret.resourceSupply = new Dictionary<string, double>(); ret.resourceDemand = new Dictionary<string, double>(); ret.resourceCapacity = new Dictionary<string, double>(); ret.unfocusedProductivity = new Dictionary<string, double>(); ret.loaded = true; foreach(string res in pipe.totalSupply.Keys) { ret.resourceSupply[res] = pipe.totalSupply[res]; ret.resourceDemand[res] = pipe.totalDemand[res]; ret.resourceCapacity[res] = pipe.totalCapacity[res]; ret.unfocusedProductivity[res] = 0; } ret.networkDraw = new Dictionary<string,double>(); foreach(string res in pipe.networkDelta.Keys) ret.networkDraw[res] = pipe.networkDelta[res]; return ret; }
public static pipelineShip loadConfig(ConfigNode node) { try { pipelineShip ret = new pipelineShip(); ret.id = (node.GetValue("id")); ret.body = node.GetValue("body"); ret.name = node.GetValue("name"); ret.lat = double.Parse(node.GetValue("lat")); ret.lon = double.Parse(node.GetValue("lon")); ret.altitude = double.Parse(node.GetValue("altitude")); ret.resourceSupply = DictionaryFromString(node.GetValue("resourceSupply")); ret.resourceDemand = DictionaryFromString(node.GetValue("resourceDemand")); ret.resourceCapacity = DictionaryFromString(node.GetValue("resourceCapacity")); ret.networkDraw = DictionaryFromString(node.GetValue("networkDraw")); ret.unfocusedProductivity = DictionaryFromString(node.GetValue("unfocusedProductivity")); return ret; } catch { return null; } }