Browse Source

find_closest_trade

Steve Thielemann 3 years ago
parent
commit
6253517a68
2 changed files with 110 additions and 8 deletions
  1. 101 0
      galaxy.cpp
  2. 9 8
      galaxy.h

+ 101 - 0
galaxy.cpp

@@ -580,6 +580,15 @@ std::vector<port_pair_type> Galaxy::find_best_trades(void) {
   return pptv;
 }
 
+/**
+ * Find_closest trade
+ *
+ * This works by getting all of the ports we know of.
+ * Then, we find the nearest.
+ *
+ * @param sector
+ * @return port_pair_type
+ */
 port_pair_type Galaxy::find_closest(int sector) {
   auto trades = find_best_trades();
   // int type, sector_type s1, s2;
@@ -645,6 +654,98 @@ port_pair_type Galaxy::find_closest(int sector) {
   return ppt;
 }
 
+port_pair_type Galaxy::find_closest_trade(int sector, int lowest_trade_type) {
+  // int type, sector_type s1, s2;
+  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;
+
+  while (!found) {
+    /*
+    for (auto const &t : trades) {
+      if (t.type > 2) continue;
+      if (current.find(t.s1) != current.end()) {
+        // found one!
+        return t;
+      }
+      if (current.find(t.s2) != current.end()) {
+        return t;
+      }
+    }
+    */
+    ++depth;
+    BUGZ_LOG(info) << "depth: " << depth << " current:" << current.size()
+                   << " seen: " << seen.size();
+
+    // search current for trades
+    for (auto const &c : current) {
+      auto port = ports.find(c);
+      if (port == ports.end()) continue;
+      if (port->second.type == 0) continue;
+      auto port_warps = warps.find(c);
+      if (port_warps == warps.end()) continue;
+      for (auto const &s : port_warps->second.warps) {
+        // verify we have a way back
+        {
+          auto warpback = warps.find(s);
+          if (warpback == warps.end()) continue;
+          if (warpback->second.warps.find(c) == warpback->second.warps.end())
+            continue;
+        }
+
+        auto possible_port = ports.find(s);
+        if (possible_port == ports.end()) continue;
+        if (possible_port->second.type == 0) continue;
+        trade_type_result ttr = trade_type_info(c, s);
+        if ((ttr.type == NONE) || (ttr.type > lowest_trade_type)) continue;
+        // Ok! we found a trade that fits the criteria!
+        return port_pair_type{ttr.type, ttr.trades, c, s};
+      }
+    }
+    added_new = false;
+
+    if (!found) {
+      // 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;
+
+          // does it have a warp back to the original sector?
+          auto warp_back = warps.find(w);
+          if (warp_back != warps.end()) {
+            if (warp_back->second.warps.find(li) !=
+                warp_back->second.warps.end()) {
+              // Ok, this links back to the original sector...
+              current.insert(w);
+              added_new = true;
+            }
+          }
+        }
+      }
+      if (!added_new) {
+        BUGZ_LOG(warning) << "No new sectors added.  We're done!";
+        port_pair_type ppt;
+        ppt.type = 0;
+        return ppt;
+      }
+    }
+  }
+  port_pair_type ppt;
+  ppt.type = 0;
+  return ppt;
+}
+
 trade_type_result Galaxy::trade_type_info(sector_type port1, sector_type port2,
                                           int burnt_percent) {
   BUGZ_LOG(fatal) << "Trade_type_info(" << port1 << "," << port2 << ")";

+ 9 - 8
galaxy.h

@@ -53,12 +53,12 @@ struct sector_warps {
 // int trade_type(port_type port1, port_type port2);
 
 enum trade_types {
-  NONE, 
-  BEST,   // OE PAIR
-  OK,     // PAIR
-  FAIR_E, // BS E
-  FAIR_O, // BS O
-  FAIR_F, // BS F
+  NONE,
+  BEST,    // OE PAIR
+  OK,      // PAIR
+  FAIR_E,  // BS E
+  FAIR_O,  // BS O
+  FAIR_F,  // BS F
 };
 
 struct trade_type_result {
@@ -142,10 +142,11 @@ class Galaxy {
   std::vector<port_pair_type> find_best_trades(void);
   std::vector<port_pair_type> find_trades(sector_type sector,
                                           bool highest = true);
-  trade_type_result trade_type_info(sector_type port1, sector_type port2, int burnt_percent = 20);
+  trade_type_result trade_type_info(sector_type port1, sector_type port2,
+                                    int burnt_percent = 20);
   void sort_port_pair_type(std::vector<port_pair_type>& pptv);
   port_pair_type find_closest(int sector);
-
+  port_pair_type find_closest_trade(int sector, int lowest_trade_type);
   char game;
   std::string username;
 };