| 
					
				 | 
			
			
				@@ -23,6 +23,7 @@ void ScriptTrader::activate(void) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   // Ok, what do we do first here? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   // I - Info 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   state = 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  percent = 5.0;  // check meta for past trades information 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   to_server("I"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -31,6 +32,66 @@ void ScriptTrader::deactivate(void) { notify(); } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 void ScriptTrader::server_line(const std::string &line, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                                const std::string &raw_line) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				   // FUTURE:  powering up weapons check 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  if (line == "Docking...") { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    last_offer = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    final_offer = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    initial_offer = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  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; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    last_offer = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    final_offer = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  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; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    last_offer = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    final_offer = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  // SL: [Our final offer is 1,263 credits.] 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+  if (startswith(line, "Our final offer is ")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // Well snap! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    std::string offer = line.substr(20); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    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) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -53,7 +114,6 @@ void ScriptTrader::server_prompt(const std::string &prompt) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       // Do we have what they are buying? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       bool have_buy = false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-      char foe[4] = "foe"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       int active_buy = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       int active_sell = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -72,7 +132,7 @@ void ScriptTrader::server_prompt(const std::string &prompt) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				           } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-          if (port_buysell[0].foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+          if (!port_buysell[0].foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             active_sell = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				           } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             active_sell = 1; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -113,7 +173,8 @@ void ScriptTrader::server_prompt(const std::string &prompt) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       // for now... 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       deactivate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    if (state == 3) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if (state == 2) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       if (director.current_sector == active_port) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         // We're here 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         state = 3; 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -135,7 +196,56 @@ void ScriptTrader::server_prompt(const std::string &prompt) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       to_server("\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				       return; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    if (in(prompt, " to buy ") && startswith(prompt, "How many holds of ")) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      // Ok, what are they selling? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      char selling = tolower(prompt[18]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      BUGZ_LOG(fatal) << "Selling: " << selling; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      for (int x = 0; x < 3; ++x) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (foe[x] == selling) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+          // We found the item ... is it something that we're trading? 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+          if (trades.foe[x]) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            // Yes! 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            to_server("\r"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+          } 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: " << 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]) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        active_port = port[1]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      else 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        active_port = port[0]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+      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(); }; 
			 |