| 
					
				 | 
			
			
				@@ -1,496 +1,8 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #include "scripts.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#include "logging.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #include <boost/format.hpp> 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#ifdef DEPRECATED_SEE_TRADER_DISPATCH 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-ScriptTrader::ScriptTrader(Director &d) : Dispatch(d) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  BUGZ_LOG(fatal) << "ScriptTrader()"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  state = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-ScriptTrader::~ScriptTrader() { BUGZ_LOG(fatal) << "~ScriptTrader()"; } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-void ScriptTrader::activate(void) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // ok, lookup port1 port2 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  BUGZ_LOG(fatal) << "ScriptTrader::activate " << port[0] << " & " << port[1]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  auto port_info = director.galaxy.ports.find(port[0]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  int port0_type = port_info->second.type; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  port_buysell[0] = get_buysell(port0_type); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // Special case - we just want to buy resources 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  if (port[1] != 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    port_info = director.galaxy.ports.find(port[1]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    int port1_type = port_info->second.type; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    port_buysell[1] = get_buysell(port1_type); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    BUGZ_LOG(fatal) << port0_type << " and " << port1_type; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    BUGZ_LOG(fatal) << "Just buy from " << port[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  /* 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  auto ttr = director.galaxy.trade_type_info(port0_type, port1_type); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  trades = ttr.trades; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  /* 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  if (trades.foe[0] && trades.foe[1] && trades.foe[2]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    // it has all three -- use the last 2. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    trades.foe[0] = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // Ok, what do we do first here? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // I - Info 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  state = 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  percent = 5.0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  to_server("I"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  director.galaxy.meta["help"]["stop_percent"] = 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "ScriptTrader stop trading if below this percent."; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  if (director.galaxy.config["stop_percent"]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    stop_percent = director.galaxy.config["stop_percent"].as<int>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    stop_percent = 20; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    director.galaxy.config["stop_percent"] = stop_percent; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  director.galaxy.meta["help"]["trade_end_empty"] = 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "ScriptTrader end trades with empty holds? Y/N"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  if (director.galaxy.config["trade_end_empty"]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    std::string tee = 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        director.galaxy.config["trade_end_empty"].as<std::string>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if ((toupper(tee[0]) == 'Y') || (toupper(tee[0]) == 'T')) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      trade_end_empty = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      trade_end_empty = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // director.galaxy.config["trade_end_empty"] = "N"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    trade_end_empty = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    director.galaxy.config["trade_end_empty"] = "N"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-void ScriptTrader::deactivate(void) { notify(); } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-void ScriptTrader::server_line(const std::string &line, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                               const std::string &raw_line) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // FUTURE:  powering up weapons check 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // Show what's going on... 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  if (state > 1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    std::string temp = raw_line; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    temp.append("\n\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    to_client(temp); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  if (line == "Docking...") { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    last_offer = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    final_offer = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    initial_offer = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  static std::set<std::string> success_lines = { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "If only more honest traders would port here, we'll take them though.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "You will put me out of business, I'll take your offer.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "FINE, we'll take them, just leave!", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "Agreed, and a pleasure doing business with you!", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "You are a rogue! We'll take them anyway.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "You insult my intelligence, but we'll buy them anyway.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "Very well, we'll take that offer.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "You drive a hard bargain, but we'll take them.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "Done, we'll take the lot.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "I hate haggling, they're all yours.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "You are robbing me, but we'll buy them anyway.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "SOLD!  Come back anytime!", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "Cheapskate.  Here, take them and leave me alone.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "Very well, we'll buy them.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "You are a shrewd trader, they're all yours.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "I could have twice that much in the Androcan Empire, but they're yours.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "Oh well, maybe I can sell these to some other fool, we'll take them.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "I PAID more than that!  But we'll sell them to you anyway.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "(Sigh) Very well, pay up and take them away.", 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      "Agreed! We'll purchase them!"}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  if (success_lines.find(line) != success_lines.end()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    BUGZ_LOG(fatal) << "Success " << buying << " " << initial_offer << " : " 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    << last_offer; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    // calculate % ? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    BUGZ_LOG(fatal) << "% " << (float)initial_offer / (float)last_offer * 100.0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    BUGZ_LOG(fatal) << "meta trade setting: " << percent << " for " 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                    << active_port << " " << product; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    director.galaxy.meta["trade"][active_port][product] = percent; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // <P-Probe estimates your offer was 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  if (startswith(line, "Agreed, ")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    last_offer = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    final_offer = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if (director.galaxy.meta["trade"][active_port][product]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      percent = director.galaxy.meta["trade"][active_port][product].as<float>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      percent += 1.0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      BUGZ_LOG(fatal) << "Percent for " << active_port << " now " << percent; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      BUGZ_LOG(fatal) << "using default for " << active_port; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      percent = 5.0;  // check meta for past trades information 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  if (startswith(line, "We'll buy them for ")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    // I need the initial offer! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    std::string offer = line.substr(19); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    replace(offer, ",", ""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    initial_offer = stoi(offer); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    BUGZ_LOG(fatal) << "Buying, initial: " << initial_offer; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    buying = true;  // Port is buying, we are selling. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  if (startswith(line, "We'll sell them for ")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    // I need the initial offer! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    std::string offer = line.substr(20); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    replace(offer, ",", ""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    initial_offer = stoi(offer); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    BUGZ_LOG(fatal) << "Selling, initial: " << initial_offer; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    buying = false;  // Port is selling, we are buying. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // SL: [Our final offer is 1,263 credits.] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  if (startswith(line, "Our final offer is ")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    // Well snap! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    std::string offer = line.substr(19); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    replace(offer, ",", ""); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    final_offer = stoi(offer); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    BUGZ_LOG(fatal) << "Final offer: " << final_offer; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // SL: [You have 16,767 credits and 0 empty cargo holds.] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // trade accepted.  if not 0 empty cargo holds -- we failed! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // SL: [<P-Probe estimates your offer was  91.83% of best price>] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // SL: [You have 4,046 credits and 0 empty cargo holds.] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // this shows up at the initial docking of the port. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  if (startswith(line, "You have ")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if (initial_offer != 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // Ok, the offer was possibly accepted. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      int success; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (buying) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        success = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        success = director.galaxy.meta["ship"]["holds"]["total"].as<int>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      std::string text = std::to_string(success); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      text.append(" empty cargo holds."); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (endswith(line, text)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        BUGZ_LOG(fatal) << "Trade SUCCESS!"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // record this action somewhere in meta. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-void ScriptTrader::server_prompt(const std::string &prompt) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // FUTURE:  check for "Surrender/Attack" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  if (at_command_prompt(prompt)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if (state == 1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // Ok, decision time! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (director.galaxy.meta["ship"]["holds"]["c"]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // holds contain colonists 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        to_client("ScriptTrader FAIL: holds contain colonists."); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        deactivate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // Which port to trade with first?  examine trades 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      BUGZ_LOG(fatal) << "trades: " << trades; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      BUGZ_LOG(fatal) << "port0:" << text_from_buysell(port_buysell[0]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (port[1] != 0) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        BUGZ_LOG(fatal) << "port1:" << text_from_buysell(port_buysell[1]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // Ok, I might still need this (so I know what port to start with) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // which is selling? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // must set active port! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      bool all_holds_empty = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      active_port = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // check the ship and holds here.  (MAYBE) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      int holds = director.galaxy.meta["ship"]["holds"]["total"].as<int>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (director.galaxy.meta["ship"]["holds"]["empty"]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if (holds == director.galaxy.meta["ship"]["holds"]["empty"].as<int>()) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          all_holds_empty = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (port[1] == 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        active_port = port[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if (!all_holds_empty) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          for (int x = 0; x < 3; ++x) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (director.galaxy.meta["ship"]["holds"][foe[x]]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              if (port_buysell[0].foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                active_port = port[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              if (port_buysell[1].foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                active_port = port[1]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          if (active_port == 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            to_client( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                "I don't see any ports that are buying what we have in our " 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                "holds.\n\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            deactivate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          // all holds empty, find selling port 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          for (int x = 0; x < 3; ++x) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (trades.foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              if (port_buysell[0].foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                active_port = port[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              if (port_buysell[1].foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                active_port = port[1]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                break; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#ifdef NO_JUST_TRADES 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // Do we have what they are buying? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      bool have_buy = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      int active_buy = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      int active_sell = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      for (int x = 0; x < 3; ++x) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if (trades.foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          // this is what they will be trading... 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          if (director.galaxy.meta["ship"]["holds"][foe[x]]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            // key exists ... 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            have_buy = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            // which port is buying? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (port_buysell[0].foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              active_buy = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              have_buy = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } else if (port_buysell[1].foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              active_buy = 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              have_buy = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          if (!port_buysell[0].foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            active_sell = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            active_sell = 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (have_buy) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        BUGZ_LOG(fatal) << "have_buy: port " << active_buy; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        active_port = port[active_buy]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // which port is selling? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // if they aren't buying what I have in my holds, should I check to see 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // if my holds are full?  This could be the "not buying" what I'm 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // setting bug! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        BUGZ_LOG(fatal) << "!have_buy: port " << active_sell; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        active_port = port[active_sell]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // yes, this is the bug alright. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // Ok, this shows up all the time.  I need to look at my holds! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if (!all_holds_empty) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          to_client( 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              "I don't see any ports that are buying what we have in our " 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              "holds!\n\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          deactivate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#endif 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      state = 2; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (director.current_sector == active_port) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // begin state 3 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        state = 3; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        to_client("Trading...\n\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        to_server("PT"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // initiate move 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        std::string move = std::to_string(active_port); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        to_client("Moving...\n\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        move.append("\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        to_server(move); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if (state == 2) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (director.current_sector == active_port) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // We're here 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        state = 3; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        to_client("Trading...\n\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        to_server("PT"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // we failed to move to where we wanted to go?! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        BUGZ_LOG(fatal) << "Expecting: " << active_port << " but got " 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                        << director.current_sector; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        deactivate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  if (state == 3) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if (startswith(prompt, "How many holds of ")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      char selling = tolower(prompt[18]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      for (int x = 0; x < 3; ++x) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if (foe[x] == selling) product = x; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (in(prompt, " to sell ")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // always sell everything 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        to_server("\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (in(prompt, " to buy ")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        bool buy_ok = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if (trade_end_empty) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          // Ok, we want to end with empty holds... 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          int other_port; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          if (active_port == port[0]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            other_port = port[1]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            other_port = port[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          // Is target port burnt? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          auto pos = director.galaxy.ports.find(other_port); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          bool burnt = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          if (pos != director.galaxy.ports.end()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            // We'll find the port.  Really. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (!pos->second.unknown()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              // port isn't unknown, so check to see if it's burnt 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              for (int x = 0; x < 3; ++x) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                if (trades.foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                  if (pos->second.percent[x] < stop_percent) burnt = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          if (burnt) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            buy_ok = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // Ok, what are they selling? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // char selling = tolower(prompt[18]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        BUGZ_LOG(fatal) << "Selling: " << selling; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if (!buy_ok) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          // no! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          to_server("0\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          for (int x = 0; x < 3; ++x) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            // if (foe[x] == selling) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            // We found the item ... is it something that we're trading? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (foe[x] == selling) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              if (trades.foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                // Yes! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                to_server("\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                product = x; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                // No! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                to_server("0\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if (startswith(prompt, "Your offer [") && endswith(prompt, " ? ")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // Ok, things get weird here.  We also need to look for final offer. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (last_offer != 0) percent -= 1.0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (buying) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        last_offer = (int)(initial_offer * (100 + percent) / 100.0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        last_offer = (int)(initial_offer * (100 - percent) / 100.0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      BUGZ_LOG(fatal) << "Offer: " << buying << " offer " << last_offer << " % " 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                      << percent; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      std::string text = std::to_string(last_offer); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      text.append("\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      to_server(text); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if (at_command_prompt(prompt)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // we're done trading... 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // do we carry on, or stop? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // 1.) CHECK TURNS // need turn tracking 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // 2.) PORTS BURNT? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (active_port == port[0]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if (port[0] == 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          deactivate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        active_port = port[1]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        active_port = port[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      // Is target port burnt? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      auto pos = director.galaxy.ports.find(active_port); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      bool burnt = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (pos != director.galaxy.ports.end()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        // We'll find the port.  Really. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        if (!pos->second.unknown()) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          // port isn't unknown, check to see if burnt 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          for (int x = 0; x < 3; ++x) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            if (trades.foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              BUGZ_LOG(fatal) << x << " % " << (int)pos->second.percent[x] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-                              << " " << stop_percent; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-              if (pos->second.percent[x] < stop_percent) burnt = true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      if (burnt) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        to_client("Ports burnt.\n\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        deactivate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      std::string move = std::to_string(active_port); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      to_client("Moving...\n\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      move.append("\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      to_server(move); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      state = 2; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-void ScriptTrader::client_input(const std::string &cinput) { deactivate(); }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-#endif 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+#include "logging.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 ScriptTerror::ScriptTerror(Director &d) : Dispatch(d) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   BUGZ_LOG(warning) << "ScriptTerror()"; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -524,18 +36,33 @@ void ScriptTerror::activate(void) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   BUGZ_LOG(warning) << "ScriptTerror::activate()"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   // Need: InputDispatch, MoveDispatch, ScriptTrader 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  // Save the trade_end_empty setting, and set to Y 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  if (director.galaxy.config["trade_end_empty"]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    old_trade_end_empty = 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        director.galaxy.config["trade_end_empty"].as<std::string>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    old_trade_end_empty = "Y"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  director.galaxy.config["trade_end_empty"] = "Y"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  // Step 0:  Get ship information / # of holds 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  max_loops = loops = -1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  to_server("I"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   // Step 1:  Get number of loops of terror 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  director.chain = input; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  input->activate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  // director.chain = input; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  // input->activate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // Step 2:  Look for closest trades, try ScriptTrade until none < some level. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  // Step 3:  Move on, unless out of loops (or low on turns) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  // Step 2:  Look for closest trades, try ScriptTrade until none < some 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  // level. Step 3:  Move on, unless out of loops (or low on turns) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   // deactivate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 void ScriptTerror::deactivate(void) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   BUGZ_LOG(warning) << "ScriptTerror::deactivate()"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  // restore the original value. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  director.galaxy.config["trade_end_empty"] = old_trade_end_empty; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   notify(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -544,6 +71,11 @@ void ScriptTerror::input_notify(void) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     deactivate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  if (id->aborted) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    deactivate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   max_loops = sstoi(id->input, -1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   if (max_loops == -1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     deactivate(); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -562,15 +94,34 @@ void ScriptTerror::input_notify(void) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   // ok, step 2:  move! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   // md->setNotify([this]() { this->proxy_deactivate(); }); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-  BUGZ_LOG(fatal) << "Moving to: " << ppt.s1;           
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  if (director.current_sector != ppt.s1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  BUGZ_LOG(fatal) << "Moving to: " << ppt.s1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   md->move_to = ppt.s1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   director.chain = move; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   director.chain->activate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // We're already there! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    to_client("Ok!  Get 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; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 void ScriptTerror::move_notify(void) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   BUGZ_LOG(fatal) << "move_notify()"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  if (md->aborted) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    to_client("Move cancel.\n\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    deactivate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   // Check for success, and start trading! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   if (md->success) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     to_client("We're here, get trading!\n\r"); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -588,8 +139,25 @@ void ScriptTerror::move_notify(void) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+void ScriptTerror::server_prompt(const std::string &prompt) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  if ((loops == -1) && (max_loops == -1)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if (at_command_prompt(prompt)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      // Step 1:  Get number of loops of terror 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      director.chain = input; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      input->activate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 void ScriptTerror::trade_notify(void) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   // Done trading -- maybe! :P 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  if (td->aborted) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    to_client("Trade cancel.\n\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    deactivate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   if (td->success) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     // success! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     ppt = director.galaxy.find_closest_trade(director.current_sector, 3); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -598,7 +166,8 @@ void ScriptTerror::trade_notify(void) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       deactivate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if ((director.current_sector == ppt.s1) || (director.current_sector == ppt.s2)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    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"); 
			 |