Browse Source

Fixing issue with ship with no scanner.

Steve Thielemann 3 years ago
parent
commit
d781a94d84
5 changed files with 109 additions and 32 deletions
  1. 33 7
      director.cpp
  2. 60 24
      dispatchers.cpp
  3. 12 0
      galaxy.cpp
  4. 1 0
      galaxy.h
  5. 3 1
      session.cpp

+ 33 - 7
director.cpp

@@ -44,9 +44,9 @@ Director::Director() {
 
 Director::~Director() { BUGZ_LOG(warning) << "Director::~Director()"; }
 
-void Director::client_input(const std::string &input) { 
+void Director::client_input(const std::string &input) {
   std::string output = repr(input);
-  
+
   if (chain) {
     BUGZ_LOG(trace) << chain->name << " CI: [" << output << "]";
     chain->client_input(input);
@@ -282,7 +282,8 @@ void Director::server_prompt(const std::string &prompt,
     emit_server_prompt(prompt);
    */
   if (chain) {
-    BUGZ_LOG(info) << chain->name << " SP: [" << prompt << "]";
+    std::string output = repr(prompt);
+    BUGZ_LOG(info) << chain->name << " SP: [" << output << "]";
     chain->server_prompt(prompt);
   }
 }
@@ -361,7 +362,7 @@ void Director::proxy_activate(void) {
     int minutes_ago = seconds_ago / 60;
     BUGZ_LOG(warning) << "port_CIM was " << minutes_ago << " minutes ago.";
 
-    if (minutes_ago >= 60) {
+    if (minutes_ago >= 120) {
       float hours_ago = minutes_ago / 60.0;
       std::string message =
           str(boost::format(
@@ -620,13 +621,38 @@ void Director::scripts_done(void) {
         // }
         case 'C': {
           auto best = galaxy.find_closest(current_sector);
+          std::string text;
           if (best.type != 0) {
-            std::string text =
-                str(boost::format("Best/Closest: %1% with %2% & %3%\n\r") %
-                    best.type % best.s1 % best.s2);
+            text = str(boost::format("Best/Closest: %1% with %2% & %3%\n\r") %
+                       best.type % best.s1 % best.s2);
             to_client(text);
           } else {
             to_client("I don't see any best trades.\n\r");
+          } 
+          const char * foe[3] = { "Fuel", "Organics", "Equipment"};
+
+          for (int x = 0; x < 3; ++x) {
+            auto s = galaxy.find_nearest_selling(current_sector, x);
+            text = str(boost::format("Closest Selling %1% is %2%\n\r") % foe[x] % s);
+            to_client(text);
+          }
+          to_client("Good places to hide:\n\r");
+          auto sectors = galaxy.find_safe();
+          text = "";
+          int c = 0;
+          for (auto const & s: sectors) {
+            ++c;
+            text += str(boost::format("%1$5d ") % s);
+            if (c % 10 == 0) {
+              text.append("\n\r");
+              to_client(text);
+              text.clear();
+              c = 0;
+            }
+          }
+          if (c != 0) {
+            text.append("\n\r");
+            to_client(text);
           }
         } break;
         case 'N': {

+ 60 - 24
dispatchers.cpp

@@ -16,11 +16,11 @@ Dispatch::Dispatch(Director &d, const char *called) : director{d} {
 };
 Dispatch::~Dispatch(){};
 
-void Dispatch::deactivate(void) {
-  notify();
-}
+void Dispatch::deactivate(void) { notify(); }
 
-void Dispatch::to_server(const std::string &send) { director.to_server(send, name); }
+void Dispatch::to_server(const std::string &send) {
+  director.to_server(send, name);
+}
 void Dispatch::to_client(const std::string &send) { director.to_client(send); }
 const std::string &Dispatch::get_prompt(void) {
   return director.current_prompt;
@@ -433,11 +433,11 @@ void CIMDispatch::server_line(const std::string &line,
   }
 }
 
-MoveDispatch::MoveDispatch(Director &d, const char * called) : Dispatch(d, called) {
+MoveDispatch::MoveDispatch(Director &d, const char *called)
+    : Dispatch(d, called) {
   BUGZ_LOG(warning) << "MoveDispatch()";
   use_express = false;
-  if (called == nullptr)
-    name = "Move";
+  if (called == nullptr) name = "Move";
 }
 
 MoveDispatch::~MoveDispatch() { BUGZ_LOG(warning) << "~MoveDispatch()"; }
@@ -471,7 +471,7 @@ void MoveDispatch::activate(void) {
     to_server(command);
     state = 3;
   } else {
-    to_server("SD");
+    to_server("S");  // because we might not have a scanner
     state = 1;
   }
 
@@ -480,14 +480,16 @@ void MoveDispatch::activate(void) {
   at_destination.append(std::to_string(move_to));
 }
 
-void MoveDispatch::deactivate(void) { 
+void MoveDispatch::deactivate(void) {
   BUGZ_LOG(fatal) << "MoveDispatch::deactivate() " << success;
-  notify(); 
+  notify();
 }
 
 // optional here
 void MoveDispatch::server_line(const std::string &line,
                                const std::string &raw_line) {
+  if (!line.empty()) BUGZ_LOG(fatal) << state << " : " << line;
+
   if (state == 1) {
     if (endswith(line, "Relative Density Scan")) {
       state = 2;
@@ -537,7 +539,9 @@ void MoveDispatch::server_line(const std::string &line,
       deactivate();
       return;
     }
-
+    if (line == "You don't have a long range scanner.") {
+      use_express = true;
+    }
     if (line == "That Warp Lane is not adjacent.") {
       // ok!  Parse out the path that we need to take...
     }
@@ -616,12 +620,16 @@ bool MoveDispatch::density_clear(density d) {  // int sector, int density) {
 } */
 
 void MoveDispatch::server_prompt(const std::string &prompt) {
-  BUGZ_LOG(fatal) << "server_prompt: " << prompt;
+  BUGZ_LOG(fatal) << "server_prompt: " << state << " SP: [" << prompt << "]";
 
   // SL: [###### DANGER! You have marked sector 740 to be avoided!]
   // SP: [Do you really want to warp there? (Y/N) ]
 
-  if (state == 2) {
+  if (state == 1) {
+    if (prompt == "Select (H)olo Scan or (D)ensity Scan or (Q)uit? [D] ") {
+      to_server("D");
+    }
+  } else if (state == 2) {
     if (at_command_prompt(prompt)) {
       // Ok, density is done
       // BUG:  If the sector is adjacent, this doesn't check density!
@@ -644,12 +652,23 @@ void MoveDispatch::server_prompt(const std::string &prompt) {
   } else if (state == 3) {
     if (at_command_prompt(prompt)) {
       // this happens when the sector is adjcent to current_sector.
-      BUGZ_LOG(fatal) << "Are we there yet? / Adjacent sector.";
-      success = true;
-      deactivate();
+      // Or it happens when the ship is FUBAR.
+
+      // NO!!!!
+      if (director.current_sector == move_to) {
+        BUGZ_LOG(fatal) << "Are we there yet? / Adjacent sector.";
+        success = true;
+        deactivate();
+      } else {
+        // Nope!  Wrong!
+      }
+
+      return;
+    }
+    if (prompt == "Engage the Autopilot? (Y/N/Single step/Express) [Y] ") {
+      to_server("E");
       return;
     }
-
   } else if (state == 4) {
     if (prompt == "Engage the Autopilot? (Y/N/Single step/Express) [Y] ") {
       if (use_express) {
@@ -679,9 +698,18 @@ void MoveDispatch::server_prompt(const std::string &prompt) {
     }
     if (prompt == "Stop in this sector (Y,N,E,I,R,S,D,P,?) (?=Help) [N] ? ") {
       state = 5;
-      to_server("SD");
+      // BUT?  Do we have a scanner?
+      if (use_express) {
+        to_server("E");
+      } else
+        to_server("SD");
+      return;
     }
   } else if (state == 5) {
+    if (prompt == "Engage Express mode? (Y/N) [N] ") {
+      to_server("Y");
+      return;
+    }
     // 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];
@@ -699,6 +727,14 @@ void MoveDispatch::server_prompt(const std::string &prompt) {
         deactivate();
       }
     }
+    if (at_command_prompt(prompt)) {
+      if (director.current_sector == move_to) {
+        success = 1;
+      } else
+        success = 0;
+      deactivate();
+      return;
+    }
   } else if (state == 6) {
     if (at_command_prompt(prompt)) {
       // We're done!
@@ -716,12 +752,12 @@ void MoveDispatch::client_input(const std::string &input) {
   deactivate();
 }
 
-TraderDispatch::TraderDispatch(Director &d, const char * called) : Dispatch(d, called) {
+TraderDispatch::TraderDispatch(Director &d, const char *called)
+    : Dispatch(d, called) {
   BUGZ_LOG(fatal) << "TraderDispatch()";
   state = 0;
   success = false;
-  if (called == nullptr)
-    name = "Trader";
+  if (called == nullptr) name = "Trader";
   director.galaxy.meta["help"]["stop_percent"] =
       "ScriptTrader stop trading if below this percent.";
   director.galaxy.meta["help"]["trade_end_empty"] =
@@ -1276,10 +1312,10 @@ void TraderDispatch::client_input(const std::string &cinput) {
  * CoreDispatch:  This is an example class that does dispatch.
  * Copy this and make changes from there...
  */
-CoreDispatch::CoreDispatch(Director &d, const char * called) : Dispatch(d, called) {
+CoreDispatch::CoreDispatch(Director &d, const char *called)
+    : Dispatch(d, called) {
   BUGZ_LOG(warning) << "CoreDispatch()";
-  if (called == nullptr)
-    name = "Core";
+  if (called == nullptr) name = "Core";
 }
 
 void CoreDispatch::activate(void) {

+ 12 - 0
galaxy.cpp

@@ -1029,3 +1029,15 @@ sector_type Galaxy::find_nearest_selling(int sector, int product, int selling) {
 
   return 0;
 };
+
+std::vector<sector_type> Galaxy::find_safe(void) {
+  std::vector<sector_type> vst;
+
+  for( auto const & warp : warps ) {
+    if (warp.second.warps.size() == 1) {
+      vst.push_back(warp.first);
+    }
+  }
+  return vst;
+}
+

+ 1 - 0
galaxy.h

@@ -162,6 +162,7 @@ class Galaxy {
   port_pair_type find_closest_trade(int sector, int lowest_trade_type,
                                     int burnt_percent = 20);
   sector_type find_nearest_selling(int sector, int product, int selling=250);
+  std::vector<sector_type> find_safe(void);
   char game;
   std::string username;
 };

+ 3 - 1
session.cpp

@@ -516,11 +516,13 @@ void Session::to_client(const std::string &message, bool log) {
   // output the cleaned string (so I can see what we're sending in the
   // logs)
 
+  /*
   if (log) {
     std::string clean = clean_string(message);
     BUGZ_LOG(trace) << "2C: " << clean;
   }
-
+  */
+ 
   boost::asio::async_write(
       socket_, boost::asio::buffer(message),
       [this, self](boost::system::error_code ec, std::size_t /*length*/) {