瀏覽代碼

Working MoveDispatch, tied to script "S".

This moves you to sector 1, using the save move
strategy.
Steve Thielemann 3 年之前
父節點
當前提交
984b7fe280
共有 3 個文件被更改,包括 154 次插入37 次删除
  1. 15 2
      director.cpp
  2. 119 0
      dispatchers.cpp
  3. 20 35
      dispatchers.h

+ 15 - 2
director.cpp

@@ -473,7 +473,10 @@ MenuDispatch *Director::init_scripts_menu(void) {
     md->menu_options_color = "\x1b[1;32;40m";
     md->lazy = false;
     md->menu_prompt = " SCRIPT : ";
-    md->menu = {{"!", "Terror"}, {"T", "Trade"}, {"U", "Upgrade Planet Pants"}};
+    md->menu = {{"!", "Terror"},
+                {"T", "Trade"},
+                {"S", "Safe Move"},
+                {"U", "Upgrade Planet Pants"}};
     md->setNotify([this]() { this->scripts_done(); });
     return md;
   }
@@ -519,6 +522,15 @@ void Director::scripts_done(void) {
           chain->activate();
           return;
         } break;
+        case 'S': {
+          script = std::make_shared<MoveDispatch>(*this);
+          MoveDispatch *md = static_cast<MoveDispatch *>(&((*script)));
+          md->setNotify([this]() { this->proxy_deactivate(); });
+          md->move_to = 1;
+          chain = script;
+          chain->activate();
+          return;
+        } break;
         case '!': {
           auto best = galaxy.find_closest(current_sector);
           if (best.type != 0) {
@@ -823,6 +835,7 @@ void Director::SL_sectorline(const std::string &line) {
       sector_warps sw;
       auto warps = split(temp);
       sw.sector = current_sector;
+      // what if there is only one warp?
       for (auto const &w : warps) {
         sw.add(stoi(w));
       }
@@ -868,7 +881,7 @@ void Director::SL_densityline(const std::string &line) {
     replace(work, "(", "");
     replace(work, ")", "");
     replace(work, "%", "");
-    std::vector<std::string> dense = split(work);
+    auto dense = split(work);
     // Parse our data
     int sector = std::stoi(dense.at(1));
     int density = std::stoi(dense.at(3));

+ 119 - 0
dispatchers.cpp

@@ -548,6 +548,125 @@ void CIMDispatch::server_line(const std::string &line,
   }
 }
 
+MoveDispatch::MoveDispatch(Director &d) : Dispatch(d) {
+  BUGZ_LOG(warning) << "MoveDispatch()";
+}
+
+// sector_type move_to;
+
+void MoveDispatch::activate(void) {
+  starting = director.current_sector;
+  BUGZ_LOG(warning) << "Moving from " << starting << " to " << move_to;
+  // Start with density scan
+  to_server("SD");
+  state = 1;
+  warp_lane.clear();
+  warp_pos = 0;
+  
+  // build final string to match against
+  at_destination = "Auto Warping to sector ";
+  at_destination.append(std::to_string(move_to));
+}
+
+void MoveDispatch::deactivate(void) { notify(); }
+
+// optional here
+void MoveDispatch::server_line(const std::string &line,
+                               const std::string &raw_line) {
+  BUGZ_LOG(fatal) << "server_line: " << line;
+
+  if (state == 1) {
+    if (endswith(line, "Relative Density Scan")) {
+      state = 2;
+    }
+  }
+  if (state == 3) {
+    if (line == "That Warp Lane is not adjacent.") {
+      //ok!  Parse out the path that we need to take...
+    }
+    // [611 > 612 > 577 > 543 > 162 > 947 > 185 > 720 > 894 > 3 > 1]
+    // multiple lines possible here?
+    // watch for <Move> it contains >
+    if ((line != "<Move>") && in(line, ">")) {
+      std::string work = line;
+      replace(work, " > ", " ");
+      auto warps = split(work);
+      for( auto const & w : warps) {
+        BUGZ_LOG(fatal) << "lane: " << w;
+        warp_lane.push_back(stoi(w));
+      }
+      state = 4;
+    }
+  }
+  if (state == 4) {
+    if (line == at_destination) {
+      // [Auto Warping to sector 1]
+      state = 6;
+    }
+  }
+}
+
+bool MoveDispatch::density_clear(int density) {
+  switch (density) {
+    case 0:
+    case 1:
+    case 100:
+    case 101:
+      return true;
+  }
+  return false;
+}
+
+void MoveDispatch::server_prompt(const std::string &prompt) {
+  BUGZ_LOG(fatal) << "server_prompt: " << prompt;
+
+  if (state == 2) {
+    if (at_command_prompt(prompt)) {
+      // Ok, density is done
+      std::string command = str(boost::format("M%1%\r") % move_to);
+      to_server(command);
+      state = 3;
+    }
+  } 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
+      int density = director.galaxy.meta["density"][to_check]["density"].as<int>();
+      if (density_clear(density)) {
+        to_server("S");
+        ++warp_pos;
+      }
+    }
+    if (prompt == "Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N] ? ") {
+      state = 5;
+      to_server("SD");
+    }
+  } else if (state == 5) {
+    // 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];
+      int density = director.galaxy.meta["density"][to_check]["density"].as<int>();
+      if (density_clear(density)) {
+        to_server("N");
+        ++warp_pos;
+        state = 4;
+      } else {
+        to_server("Y");
+        BUGZ_LOG(fatal) << "Stopped by density: " << density;
+        success = 0;
+        deactivate();
+      }  
+    }
+  } else if (state == 6) {
+    if (at_command_prompt(prompt)) {
+      // We're done!
+      success = 1;
+      deactivate();
+    }
+  }
+}
+void MoveDispatch::client_input(const std::string &input) {}
+
 /*
  * CoreDispatch:  This is an example class that does dispatch.
  * Copy this and make changes from there...

+ 20 - 35
dispatchers.h

@@ -113,56 +113,41 @@ class MenuDispatch : public Dispatch {
   void client_input(const std::string &cinput) override;
 };
 
-#ifdef NOMORE
-// This was the original idea, but we've lost our "Main Dispatch" at this point
-// I might bring this back, as a way to test the Input and Menu parts.
-
-/**
- * The main/first proxy Dispatcher.
- *
- * Don't follow this as an example.  On disable,
- * it resets everything back to nothing active.
- * (Which is likely not what you want.)
- *
- */
-
-class MainDispatch : public Dispatch {
- private:
-  InputDispatch id;
-  MenuDispatch md;
-
+class CIMDispatch : public Dispatch {
  public:
-  MainDispatch(Director &);
-  ~MainDispatch();
-
+  CIMDispatch(Director &);
+  int count;
+  
   void activate(void) override;
   void deactivate(void) override;
 
-  void have_input(void);
-  void menu_choice(void);
-
+  // optional here
   void server_line(const std::string &line, const std::string &raw_line) override;
-  void server_prompt(const std::string &prompt) override;
-  // void client_input(const std::string &input);
-
- private:
-  int count;
-  std::string old_prompt;
 };
-#endif
 
-class CIMDispatch : public Dispatch {
+class MoveDispatch : public Dispatch {
  public:
-  CIMDispatch(Director &);
-  int count;
-  
+  MoveDispatch(Director &);
+  sector_type move_to;
+  sector_type starting;
+  int state;
+  int success;
+  int warp_pos;
+  std::string at_destination;
+  std::vector<int> warp_lane;
+
   void activate(void) override;
   void deactivate(void) override;
 
   // optional here
   void server_line(const std::string &line, const std::string &raw_line) override;
+  void server_prompt(const std::string &prompt) override;
+  void client_input(const std::string &input) override;
+ private:
+  bool density_clear(int density);  
 };
 
+
 class CoreDispatch : public Dispatch {
  public:
   CoreDispatch(Director &);