Browse Source

ScriptExplore uses sort, also can find unknown

  use_nearest_unknown, "Explorer will find nearest unknown when out of known sectors to explore"
david 3 years ago
parent
commit
4c55ad2853
2 changed files with 90 additions and 75 deletions
  1. 89 75
      scripts.cpp
  2. 1 0
      scripts.h

+ 89 - 75
scripts.cpp

@@ -347,11 +347,21 @@ void ScriptExplore::init() {
     director.galaxy.config["prefer_ports"] = "Y";
     prefer_ports = true;
   } else {
-    prefer_ports = json_str(director.galaxy.config["prefer_ports"]) == "Y";
+    prefer_ports = startswith(json_str(director.galaxy.config["prefer_ports"]), "Y");
   }
   if (!director.galaxy.meta["help"].contains("prefer_ports")) {
     director.galaxy.meta["help"]["prefer_ports"] =
-        "Explorer prefers to find ports.";
+        "Explorer prefers to find ports";
+  }
+  if (!director.galaxy.config.contains("use_nearest_unknown")) {
+    director.galaxy.config["use_nearest_unknown"] = "Y";
+    use_nearest_unknown = true;
+  } else {
+    use_nearest_unknown = startswith(json_str(director.galaxy.config["use_nearest_unknown"]), "Y");
+  }
+  if (!director.galaxy.meta["help"].contains("use_nearest_unknown")) {
+    director.galaxy.meta["help"]["use_nearest_unknown"] =
+        "Explorer will find nearest unknown when out of known sectors to explore";
   }
   BUGZ_LOG(warning) << "Prefer Ports: " + prefer_ports;
 }
@@ -398,9 +408,8 @@ void ScriptExplore::move_notify() {
       target = unknown_warps.top();
       unknown_warps.pop();
       std::string message = "UNSAFE DESTINATION";
-      std::string indenter = "    ";
       ANSIColor alert(COLOR::WHITE, COLOR::RED, ATTR::BOLD);
-      to_client(indenter + alert() + message + reset() + "\n\r");
+      to_client("     " + alert() + message + reset() + "\n\r");
       BUGZ_LOG(warning) << "Target = " << target;
       next();
       //deactivate();
@@ -447,6 +456,32 @@ void ScriptExplore::input_notify() {
   state = 3;
 }
 
+bool has_port(const density d) {
+  int den = d.density;
+  if(d.navhaz <= 5) {
+    den -= 21 * d.navhaz;
+  }
+  return den == 100 || den == 101;
+}
+
+bool sort_ports(const density a, const density b) {
+  if(has_port(a) && has_port(b)) {
+      return a.warps > b.warps;
+  } else {
+    if((has_port(a) && !has_port(b)) || (!has_port(a) && has_port(b))) {
+      return has_port(a);
+    }
+    return a.warps > b.warps;
+  }
+}
+
+bool sort_warps(const density a, const density b) {
+  if((has_port(a) && !has_port(b)) || (!has_port(a) && has_port(b))) {
+    return has_port(a) && a.warps > b.warps;
+  }
+  return a.warps > b.warps;
+}
+
 void ScriptExplore::next() {
   if (loops <= 0 && !infinite) {
     to_client("The exploration ends, for now.\n\r");
@@ -459,91 +494,70 @@ void ScriptExplore::next() {
   density_scan &ds = director.galaxy.dscan;
   density best_sector;
   best_sector.sector = 0;
+  std::array<density, 6> sectors = ds.d;
+  for(int x = 0; x < 6; ++x) {
+    if(sectors[x].known || !density_clear(sectors[x].sector, sectors[x].density, sectors[x].navhaz)) {
+      sectors[x].sector = 0;
+      sectors[x].density = 0;
+      sectors[x].warps = 0;
+      sectors[x].navhaz = 0;
+      sectors[x].known = false;
+      sectors[x].anomaly = false;
+    }
+  }
+  if(prefer_ports) {
+    std::sort(sectors.begin(), sectors.end(), sort_ports);
+  } else {
+    std::sort(sectors.begin(), sectors.end(), sort_warps);
+  }
+  for(int x = 0; x < 6; ++x) {
+    BUGZ_LOG(info) << "["
+                   << sectors[x].sector << ", "
+                   << sectors[x].density << ", "
+                   << sectors[x].warps << ", "
+                   << sectors[x].anomaly << ", "
+                   << sectors[x].known << "]";
+  }
+  BUGZ_LOG(warning) << "Best Sector: " << sectors[0].sector;
+  best_sector = sectors[0];
+  for(int x = 1; x < 6; ++x) {
+    if(sectors[x].sector != 0 && sectors[x].sector != best_sector.sector) {
+      unknown_warps.push(sectors[x].sector);
+      BUGZ_LOG(info) << "Added " << sectors[x].sector << " to unknown_warps (" << unknown_warps.size() << ")";
+    }
+  }
   if (target != 0) {
     BUGZ_LOG(info) << "Using: " << target;
     best_sector.sector = target;
     target = 0;
-  } else {
-    for (int x = 0; x < ds.pos; ++x) {
-      if (best_sector.sector != 0)
-        BUGZ_LOG(info) << "Comparing: " << ds.d[x].sector << " ("
-                       << ds.d[x].density << ", " << ds.d[x].known << ") to "
-                       << best_sector.sector << " (" << best_sector.density
-                       << ", " << best_sector.known << ")";
-      /* Is this sector prefered over others?
-       * Warp Counts (Number of warps)
-       * Density Check (Is this sector clear, does it contain a port)
-       * NavHaz Check (Avoid sectors with navhaz above X%)
-       */
-      if (!ds.d[x].known) {
-        BUGZ_LOG(info) << "Subject: " << ds.d[x].sector;
-        // Compare, Warp counts
-        if (best_sector.sector != 0) {
-          if (prefer_ports) {
-            if ((ds.d[x].density == 100 || ds.d[x].density == 101) ||
-                (ds.d[x].warps >= best_sector.warps)) {
-              if (density_clear(ds.d[x].sector, ds.d[x].density,
-                                ds.d[x].navhaz)) {
-                if (best_sector.sector != 0) {
-                  BUGZ_LOG(info)
-                      << "Storing previous best " << best_sector.sector;
-                  unknown_warps.push(best_sector.sector);
-                }
-                best_sector = ds.d[x];
-              }
-            } else {
-              if (density_clear(ds.d[x].sector, ds.d[x].density,
-                                ds.d[x].navhaz)) {
-                BUGZ_LOG(info)
-                    << "Added " << ds.d[x].sector << " to unknown_warps ("
-                    << unknown_warps.size() << ")";
-                unknown_warps.push(ds.d[x].sector);
-              }
-            }
-          } else {
-            if ((ds.d[x].warps >= best_sector.warps)) {
-              if (density_clear(ds.d[x].sector, ds.d[x].density,
-                                ds.d[x].navhaz)) {
-                if (best_sector.sector != 0) {
-                  BUGZ_LOG(info)
-                      << "Storing previous best " << best_sector.sector;
-                  unknown_warps.push(best_sector.sector);
-                }
-                best_sector = ds.d[x];
-              }
-            } else {
-              if (density_clear(ds.d[x].sector, ds.d[x].density,
-                                ds.d[x].navhaz)) {
-                BUGZ_LOG(info)
-                    << "Added " << ds.d[x].sector << " to unknown_warps ("
-                    << unknown_warps.size() << ")";
-                unknown_warps.push(ds.d[x].sector);
-              }
-            }
-          }
-        } else {
-          BUGZ_LOG(info) << "No-Op " << ds.d[x].sector << " is best.";
-          best_sector = ds.d[x];
-        }
-        // Check density for possible port
-      }
-      // Check density for possible port
-    }
   }
+
   BUGZ_LOG(warning) << "Unknown Warps: " << unknown_warps.size();
   if (best_sector.sector == 0) {
     if (unknown_warps.size() == 0) {
-      to_client("No unknown warps.");
-      deactivate();
-      return;
+      if (!use_nearest_unknown) {
+        to_client("No unknown warps.");
+        deactivate();
+        return;
+      } else {
+        BUGZ_LOG(warning) << "Seeking nearest unknown";
+        std::string message = "Finding nearest unexplored";
+        ANSIColor alert(COLOR::WHITE, COLOR::RED, ATTR::BOLD);
+        to_client("     " + alert() + message + reset() + "\n\r");
+        best_sector.sector = director.galaxy.find_nearest_unexplored(director.current_sector);
+        if(best_sector.sector == 0) {
+          to_client("No unknown warps in the galaxy!");
+          deactivate();
+          return;
+        }
+      }
     } else {
       BUGZ_LOG(warning) << "Seeking previous unexplored";
       best_sector.sector = unknown_warps.top();
       unknown_warps.pop();
       std::string message = "DEAD END";
-      std::string indenter = "    ";
       ANSIColor alert(COLOR::WHITE, COLOR::RED, ATTR::BOLD);
-      to_client(indenter + alert() + message + reset() + "\n\r");
+      to_client("     " + alert() + message + reset() + "\n\r");
     }
   }
   BUGZ_LOG(warning) << "Targeting sector: " << best_sector.sector;

+ 1 - 0
scripts.h

@@ -75,6 +75,7 @@ class ScriptExplore : public Dispatch {
     int loops;
     bool infinite;
     bool prefer_ports;
+    bool use_nearest_unknown;
     int state;
     int target;
     std::stack<sector_type> unknown_warps;