Quellcode durchsuchen

Storing density_scan in galaxy.

Just the most recent scan.
Still to do:  Remember "high level" density values.
Check the density scans before moving (1 move away).
See if we can't remove the clrscr/cursor home when
getting Auto Pilot Engaging.
Steve Thielemann vor 3 Jahren
Ursprung
Commit
b86677ea93
6 geänderte Dateien mit 124 neuen und 22 gelöschten Zeilen
  1. 13 5
      director.cpp
  2. 20 8
      dispatchers.cpp
  3. 1 1
      dispatchers.h
  4. 38 0
      galaxy.cpp
  5. 28 2
      galaxy.h
  6. 24 6
      test-director.cpp

+ 13 - 5
director.cpp

@@ -215,7 +215,10 @@ void Director::server_line(const std::string &line,
     if (startswith(line, " Items     Status  Trading % of max OnBoard"))
       SL_parser = SF_portline;
     */
-    if (endswith(line, "Relative Density Scan")) SL_parser = SF_densityline;
+    if (endswith(line, "Relative Density Scan")) { 
+      galaxy.dscan.reset(current_sector);
+      SL_parser = SF_densityline;
+    }
     if (startswith(line, "Sector  : ")) SL_parser = SF_sectorline;
     if (line == ": ") SL_parser = SF_cimline;
     if (line == "<Info>") SL_parser = SF_infoline;
@@ -883,11 +886,16 @@ void Director::SL_densityline(const std::string &line) {
     replace(work, "%", "");
     auto dense = split(work);
     // Parse our data
-    int sector = std::stoi(dense.at(1));
-    int density = std::stoi(dense.at(3));
-    int warps = std::stoi(dense.at(5));
-    int navhaz = std::stoi(dense.at(7));
+    
+    sector_type sector = std::stoi(dense.at(1));
+    uint16_t density = std::stoi(dense.at(3));
+    uint16_t warps = std::stoi(dense.at(5));
+    uint16_t navhaz = std::stoi(dense.at(7));
     bool anom = in(dense.at(9), "Yes");
+
+    struct density d = {sector, density, warps, navhaz, anom};
+    galaxy.dscan.add_scan(d);
+
     // Commit data
     BUGZ_LOG(warning) << "densityline: {sector=" << sector
                       << " density=" << density << " warps=" << warps

+ 20 - 8
dispatchers.cpp

@@ -629,8 +629,10 @@ void MoveDispatch::server_line(const std::string &line,
   }
 }
 
-bool MoveDispatch::density_clear(int sector, int density) {
-  switch (density) {
+bool MoveDispatch::density_clear(density d) {  // int sector, int density) {
+  if (d.sector == 0) return false;
+
+  switch (d.density) {
     case 0:
     case 1:
     case 100:
@@ -638,8 +640,7 @@ bool MoveDispatch::density_clear(int sector, int density) {
       return true;
   }
   // special case for sector 1:
-  if ((sector == 1) && (density == 601))
-    return true;
+  if ((d.sector == 1) && (d.density == 601)) return true;
   return false;
 }
 
@@ -662,16 +663,24 @@ void MoveDispatch::server_prompt(const std::string &prompt) {
       deactivate();
       return;
     }
-  
+
   } else if (state == 4) {
     if (prompt == "Engage the Autopilot? (Y/N/Single step/Express) [Y] ") {
       int to_check = warp_lane[warp_pos + 1];
       // check density scan
+      density d = director.galaxy.dscan.find(to_check);
+      /*
       int density =
           director.galaxy.meta["density"][to_check]["density"].as<int>();
-      if (density_clear(to_check, density)) {
+      */
+      if (density_clear(d)) {  // to_check, density)) {
         to_server("S");
         ++warp_pos;
+      } else {
+        to_server("N");
+        BUGZ_LOG(fatal) << "density_clear(" << to_check << ") : false";
+        success = false;
+        deactivate();
       }
     }
     if (prompt == "Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N] ? ") {
@@ -682,15 +691,18 @@ void MoveDispatch::server_prompt(const std::string &prompt) {
     // finished scan
     if (prompt == "Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N] ? ") {
       int to_check = warp_lane[warp_pos + 1];
+      density d = director.galaxy.dscan.find(to_check);
+      /*
       int density =
           director.galaxy.meta["density"][to_check]["density"].as<int>();
-      if (density_clear(to_check, density)) {
+      */          
+      if (density_clear(d)) {
         to_server("N");
         ++warp_pos;
         state = 4;
       } else {
         to_server("Y");
-        BUGZ_LOG(fatal) << "Stopped by density: " << density;
+        BUGZ_LOG(fatal) << "Stopped by density: " << to_check;
         success = 0;
         deactivate();
       }

+ 1 - 1
dispatchers.h

@@ -144,7 +144,7 @@ class MoveDispatch : public Dispatch {
   void server_prompt(const std::string &prompt) override;
   void client_input(const std::string &input) override;
  private:
-  bool density_clear(int sector, int density);  
+  bool density_clear(density d); // int sector, int density);  
 };
 
 

+ 38 - 0
galaxy.cpp

@@ -33,6 +33,44 @@ bool port::unknown(void) {
   return true;
 }
 
+density_scan::density_scan() { reset(0); }
+
+void density_scan::reset(sector_type s) {
+  sector = s;
+  pos = 0;
+  for (int x = 0; x < (int)d.size(); ++x) {
+    d[x].sector = 0;
+  }
+}
+
+void density_scan::add_scan(density den) {
+  if (pos > (int)d.size()) {
+    std::string message("density_scan::add_scan() exceeded max size ");
+    message.append(std::to_string(d.size()));
+    throw std::out_of_range(message);
+  }
+
+  d[pos] = den;
+  ++pos;
+}
+
+density density_scan::find(sector_type sector) {
+  for (int x = 0; x < pos; ++x) {
+    if (d[x].sector == sector) return d[x];
+  }
+  BUGZ_LOG(fatal) << "density_scan::find failed: " << sector << " we have "
+                  << pos << " scans.";
+  density den;
+  den.sector = 0;
+  return den;
+}
+
+bool operator==(const density lhs, const density rhs) {
+  return ((lhs.sector == rhs.sector) && (lhs.density == rhs.density) &&
+          (lhs.navhaz == rhs.navhaz) && (lhs.anomaly == rhs.anomaly) &&
+          (lhs.warps == rhs.warps));
+}
+
 trade_type_result trade_type_info(port_type port1, port_type port2) {
   // NONE = 0
   // GOOD = 1 = OE PAIR

+ 28 - 2
galaxy.h

@@ -61,7 +61,6 @@ struct port_pair_type {
   sector_type s1, s2;
 };
 
-
 struct port {
   sector_type sector;
   uint8_t type;
@@ -74,6 +73,32 @@ struct port {
 
 struct port parse_portcim(const std::string line);
 
+// store density scan information in galaxy
+
+struct density {
+  sector_type sector;
+  uint16_t density;
+  uint16_t warps;
+  uint16_t navhaz;
+  bool anomaly;
+  friend bool operator==(const struct density lhs, const struct density rhs);
+};
+
+class density_scan {
+ public:
+  sector_type sector;
+  std::array<density, 6> d;
+  density_scan();
+
+  // resets the scan with a new sector
+  void reset(sector_type sector);
+  void add_scan(density d);
+  density find(sector_type sector);
+
+ private:
+  int pos;
+};
+
 // SPECIAL = 0
 // STARDOCK = 9
 
@@ -87,6 +112,7 @@ class Galaxy {
   YAML::Node meta;
 
   int burnt_percent;
+  density_scan dscan;
 
   // warps;
   // ports;
@@ -105,7 +131,7 @@ class Galaxy {
                                           bool highest = true);
   void sort_port_pair_type(std::vector<port_pair_type>& pptv);
   port_pair_type find_closest(int sector);
-  
+
   char game;
   std::string username;
 };

+ 24 - 6
test-director.cpp

@@ -63,7 +63,7 @@ TEST(director, director_galaxy_save) {
 
   dir.galaxy.username = "test";
   dir.galaxy.game = 'Z';
-  // This causes a YAML::Node Sequence 
+  // This causes a YAML::Node Sequence
   dir.galaxy.meta["trade"][10963][0] = 1;
   dir.galaxy.save();
 
@@ -119,8 +119,21 @@ TEST(director, director_parsing_density) {
     }
   }
 
+  std::array<density, 6> dense = {{{70, 0, 6, 0, false},
+                                   {441, 100, 2, 0, false},
+                                   {575, 1000, 2, 15, true},
+                                   {600, 40, 6, 0, false},
+                                   {629, 1, 4, 0, false},
+                                   {711, 101, 6, 0, false}}};
+
+  for (auto const& s : dense) {
+    auto d = dir.galaxy.dscan.find(s.sector);
+    EXPECT_EQ(d, s) << "Sector " << s.sector << " density";
+  }
+
   // Check that each sector was correctly processed
   std::vector<int> sectors = {70, 441, 575, 600, 629, 711};
+
   YAML::Node sector_data;
   sector_data[70]["density"] = 0;
   sector_data[70]["warps"] = 6;
@@ -156,15 +169,20 @@ TEST(director, director_parsing_density) {
   for (auto sector : sectors) {
     // GTEST_COUT << "Testing Sector " << sector << std::endl;
     EXPECT_EQ(dir.galaxy.meta["density"][sector]["density"].as<int>(),
-              sector_data[sector]["density"].as<int>()) << "Sector " << sector << " density";
+              sector_data[sector]["density"].as<int>())
+        << "Sector " << sector << " density";
     EXPECT_EQ(dir.galaxy.meta["density"][sector]["warps"].as<int>(),
-              sector_data[sector]["warps"].as<int>()) << "Sector " << sector << " warps";
+              sector_data[sector]["warps"].as<int>())
+        << "Sector " << sector << " warps";
     EXPECT_EQ(dir.galaxy.meta["density"][sector]["navhaz"].as<int>(),
-              sector_data[sector]["navhaz"].as<int>()) << "Sector " << sector << " navhaz";
+              sector_data[sector]["navhaz"].as<int>())
+        << "Sector " << sector << " navhaz";
     EXPECT_EQ(dir.galaxy.meta["density"][sector]["anom"].as<bool>(),
-              sector_data[sector]["anom"].as<bool>()) << "Sector " << sector << " anom";
+              sector_data[sector]["anom"].as<bool>())
+        << "Sector " << sector << " anom";
     EXPECT_EQ(dir.galaxy.meta["density"][sector]["known"].as<bool>(),
-              sector_data[sector]["known"].as<bool>()) << "Sector " << sector << " known";
+              sector_data[sector]["known"].as<bool>())
+        << "Sector " << sector << " known";
   }
 }