Jelajahi Sumber

find_closest_trade now finds the best closest.

This has working terror.
Steve Thielemann 3 tahun lalu
induk
melakukan
f7d74059ca
5 mengubah file dengan 64 tambahan dan 4 penghapusan
  1. 4 0
      dispatchers.cpp
  2. 3 0
      dispatchers.h
  3. 15 3
      galaxy.cpp
  4. 40 1
      scripts.cpp
  5. 2 0
      scripts.h

+ 4 - 0
dispatchers.cpp

@@ -778,6 +778,7 @@ void MoveDispatch::client_input(const std::string &input) {
 TraderDispatch::TraderDispatch(Director &d) : Dispatch(d) {
   BUGZ_LOG(fatal) << "TraderDispatch()";
   state = 0;
+  success = false;
 };
 
 TraderDispatch::~TraderDispatch() { BUGZ_LOG(fatal) << "~TraderDispatch()"; }
@@ -889,6 +890,7 @@ void TraderDispatch::server_line(const std::string &line,
     BUGZ_LOG(fatal) << "Success " << buying << " " << initial_offer << " : "
                     << last_offer;
     // calculate % ?
+    success = true;
     BUGZ_LOG(fatal) << "% " << (float)initial_offer / (float)last_offer * 100.0;
     BUGZ_LOG(fatal) << "meta trade setting: " << percent << " for "
                     << active_port << " " << product;
@@ -978,6 +980,7 @@ void TraderDispatch::server_prompt(const std::string &prompt) {
       // Ok, decision time!
       if (director.galaxy.meta["ship"]["holds"]["c"]) {
         // holds contain colonists
+        success = false;
         to_client("ScriptTrader FAIL: holds contain colonists.");
         deactivate();
         return;
@@ -1018,6 +1021,7 @@ void TraderDispatch::server_prompt(const std::string &prompt) {
             }
           }
           if (active_port == 0) {
+            success = false;
             to_client(
                 "I don't see any ports that are buying what we have in our "
                 "holds.\n\r");

+ 3 - 0
dispatchers.h

@@ -157,6 +157,9 @@ class TraderDispatch : public Dispatch {
   char foe[4] = "foe";
   bool trade_end_empty;
 
+  // success / failure ?
+  bool success;
+
   /**
    * internal state
    *

+ 15 - 3
galaxy.cpp

@@ -654,10 +654,17 @@ port_pair_type Galaxy::find_closest(int sector) {
   return ppt;
 }
 
+/**
+ * Find closest and best trade
+ * 
+ * @param sector 
+ * @param lowest_trade_type 
+ * @return port_pair_type 
+ */
 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::vector<port_pair_type> vppt;
   std::set<sector_type> seen;
   std::set<sector_type> current;
   current.insert(sector);
@@ -704,12 +711,17 @@ port_pair_type Galaxy::find_closest_trade(int sector, int lowest_trade_type) {
         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};
+        vppt.push_back(port_pair_type{ttr.type, ttr.trades, c, s});
+        found = true;
       }
     }
     added_new = false;
 
-    if (!found) {
+    if (found) {
+      // ok, we've found some trades, sort and return the best
+      sort_port_pair_type(vppt);
+      return port_pair_type{vppt[0]};
+    } else {
       // update the seen
       seen.insert(current.begin(), current.end());
       auto look_in = current;

+ 40 - 1
scripts.cpp

@@ -3,6 +3,8 @@
 #include "logging.h"
 #include <boost/format.hpp>
 
+#ifdef DEPRECATED_SEE_TRADER_DISPATCH
+
 ScriptTrader::ScriptTrader(Director &d) : Dispatch(d) {
   BUGZ_LOG(fatal) << "ScriptTrader()";
   state = 0;
@@ -485,8 +487,11 @@ void ScriptTrader::server_prompt(const std::string &prompt) {
     }
   }
 }
+
 void ScriptTrader::client_input(const std::string &cinput) { deactivate(); };
 
+#endif
+
 ScriptTerror::ScriptTerror(Director &d) : Dispatch(d) {
   BUGZ_LOG(warning) << "ScriptTerror()";
   init();
@@ -546,7 +551,7 @@ void ScriptTerror::input_notify(void) {
   }
   id->input.clear();
   BUGZ_LOG(warning) << "Loops of terror: " << max_loops;
-  loops = 0;
+  loops = max_loops;
   // find nearest
   ppt = director.galaxy.find_closest_trade(director.current_sector, 3);
   if (ppt.type == 0) {
@@ -585,6 +590,40 @@ void ScriptTerror::move_notify(void) {
 
 void ScriptTerror::trade_notify(void) {
   // Done trading -- maybe! :P
+  if (td->success) {
+    // success!
+    ppt = director.galaxy.find_closest_trade(director.current_sector, 3);
+    if (ppt.type == 0) {
+      to_client("No trades found!  You've burnt the galaxy!\n\r");
+      deactivate();
+      return;
+    }
+    if ((director.current_sector == ppt.s1) || (director.current_sector == ppt.s2)) {
+      // We're still here...
+      BUGZ_LOG(fatal) << "Trade it again, Sam.";
+      to_client("Keep trading.\n\r");
+      td->port[0] = ppt.s1;
+      td->port[1] = ppt.s2;
+      td->trades = ppt.trades;
+      td->type = ppt.type;
+      director.chain = trader;
+      director.chain->activate();
+      return;
+    }
+    // Ok, this isn't a local trade.
+    if (loops == 0) {
+      to_client("We're done terrorizing, for now...\n\r");
+      deactivate();
+      return;
+    }
+    --loops;
+    // Move to our next target
+    BUGZ_LOG(fatal) << "Moving to: " << ppt.s1;
+    md->move_to = ppt.s1;
+    director.chain = move;
+    director.chain->activate();
+    return;
+  }
   to_client("Ok, trade is done.\n\r");
   deactivate();
 }

+ 2 - 0
scripts.h

@@ -5,6 +5,7 @@
 #include "dispatchers.h"
 #include "galaxy.h"
 
+#ifdef DEPRECATED_SEE_TRADER_DISPATCH
 class ScriptTrader : public Dispatch {
  private:
  public:
@@ -54,6 +55,7 @@ class ScriptTrader : public Dispatch {
   void server_prompt(const std::string &prompt) override;
   void client_input(const std::string &cinput) override;
 };
+#endif
 
 class ScriptTerror : public Dispatch {
  private: