Browse Source

find_nearest_selling.

We removed some things from other similar code.  We don't care
if there's a warp back to the sector we came from.
Steve Thielemann 3 years ago
parent
commit
e25357306b
1 changed files with 62 additions and 1 deletions
  1. 62 1
      galaxy.cpp

+ 62 - 1
galaxy.cpp

@@ -948,6 +948,67 @@ trade_type_result Galaxy::trade_type_info(sector_type port1, sector_type port2,
   return trade_type_result{NONE, inv2};
 }
 
-sector_type Galaxy::find_nearest_selling(int sector, int product, int selling){
+sector_type Galaxy::find_nearest_selling(int sector, int product, int selling) {
+  BUGZ_LOG(fatal) << "find_closest_trade(" << sector << ")";
+  std::set<sector_type> seen;
+  std::set<sector_type> current;
+  current.insert(sector);
+  bool found = false;
+  bool added_new = false;
+  int depth = 0;
+
+  // check current sector
+  auto port = ports.find(sector);
+  if (port != ports.end()) {
+    // ok, port exists in starting sector
+    if (port->second.amount[product] >= selling) {
+      return port->first;
+    }
+  }
+
+  while (!found) {
+    ++depth;
+    BUGZ_LOG(info) << "depth: " << depth << " current:" << current.size()
+                   << " seen: " << seen.size();
+
+    // search current for trades
+    for (auto const &c : current) {
+      auto port_warps = warps.find(c);
+      if (port_warps == warps.end()) continue;
+      for (auto const &s : port_warps->second.warps) {
+        // I don't care if there's a way back the way we came
+
+        auto possible_port = ports.find(s);
+        if (possible_port == ports.end()) continue;
+        if (possible_port->second.type == 0) continue;
+        // check this port out
+        if (possible_port->second.amount[product] >= selling) {
+          return possible_port->first;
+        }
+      }
+    }
+    added_new = false;
+
+    // update the seen
+    seen.insert(current.begin(), current.end());
+    auto look_in = current;
+    current.clear();
+    for (auto const &li : look_in) {
+      auto wi = warps.find(li);
+      if (wi == warps.end()) continue;
+      for (auto const &w : wi->second.warps) {
+        // have we already seen this sector?
+        if (seen.find(w) != seen.end()) continue;
+        // I don't care if it has a warp back or not.
+        current.insert(w);
+        added_new = true;
+      }
+    }
+    if (!added_new) {
+      BUGZ_LOG(warning) << "No new sectors added.  We're done!";
+      return 0;
+    }
+  }
+
   return 0;
 };