|
@@ -4,6 +4,7 @@
|
|
|
|
|
|
ScriptTrader::ScriptTrader(Director &d) : Dispatch(d) {
|
|
ScriptTrader::ScriptTrader(Director &d) : Dispatch(d) {
|
|
BUGZ_LOG(fatal) << "ScriptTrader()";
|
|
BUGZ_LOG(fatal) << "ScriptTrader()";
|
|
|
|
+ state = 0;
|
|
};
|
|
};
|
|
|
|
|
|
ScriptTrader::~ScriptTrader() { BUGZ_LOG(fatal) << "~ScriptTrader()"; }
|
|
ScriptTrader::~ScriptTrader() { BUGZ_LOG(fatal) << "~ScriptTrader()"; }
|
|
@@ -20,11 +21,27 @@ void ScriptTrader::activate(void) {
|
|
BUGZ_LOG(fatal) << port0_type << " and " << port1_type;
|
|
BUGZ_LOG(fatal) << port0_type << " and " << port1_type;
|
|
auto ttr = trade_type_info(port0_type, port1_type);
|
|
auto ttr = trade_type_info(port0_type, port1_type);
|
|
trades = ttr.trades;
|
|
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?
|
|
// Ok, what do we do first here?
|
|
// I - Info
|
|
// I - Info
|
|
state = 1;
|
|
state = 1;
|
|
- percent = 5.0; // check meta for past trades information
|
|
|
|
|
|
+ percent = 5.0;
|
|
to_server("I");
|
|
to_server("I");
|
|
|
|
+ if (director.galaxy.config["stop_percent"]) {
|
|
|
|
+ stop_percent = director.galaxy.config["stop_percent"].as<int>();
|
|
|
|
+ director.galaxy.meta["help"]["stop_percent"] =
|
|
|
|
+ "ScriptTrader stop trading if below this percent.";
|
|
|
|
+ } else {
|
|
|
|
+ stop_percent = 20;
|
|
|
|
+ director.galaxy.config["stop_percent"] = stop_percent;
|
|
|
|
+ director.galaxy.meta["help"]["stop_percent"] =
|
|
|
|
+ "ScriptTrader stop trading if below this percent.";
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
void ScriptTrader::deactivate(void) { notify(); }
|
|
void ScriptTrader::deactivate(void) { notify(); }
|
|
@@ -32,21 +49,74 @@ void ScriptTrader::deactivate(void) { notify(); }
|
|
void ScriptTrader::server_line(const std::string &line,
|
|
void ScriptTrader::server_line(const std::string &line,
|
|
const std::string &raw_line) {
|
|
const std::string &raw_line) {
|
|
// FUTURE: powering up weapons check
|
|
// 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...") {
|
|
if (line == "Docking...") {
|
|
last_offer = 0;
|
|
last_offer = 0;
|
|
final_offer = 0;
|
|
final_offer = 0;
|
|
initial_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 ")) {
|
|
if (startswith(line, "We'll buy them for ")) {
|
|
// I need the initial offer!
|
|
// I need the initial offer!
|
|
std::string offer = line.substr(19);
|
|
std::string offer = line.substr(19);
|
|
replace(offer, ",", "");
|
|
replace(offer, ",", "");
|
|
initial_offer = stoi(offer);
|
|
initial_offer = stoi(offer);
|
|
BUGZ_LOG(fatal) << "Buying, initial: " << initial_offer;
|
|
BUGZ_LOG(fatal) << "Buying, initial: " << initial_offer;
|
|
- buying = true;
|
|
|
|
- last_offer = 0;
|
|
|
|
- final_offer = 0;
|
|
|
|
|
|
+ buying = true; // Port is buying, we are selling.
|
|
}
|
|
}
|
|
|
|
|
|
if (startswith(line, "We'll sell them for ")) {
|
|
if (startswith(line, "We'll sell them for ")) {
|
|
@@ -55,14 +125,13 @@ void ScriptTrader::server_line(const std::string &line,
|
|
replace(offer, ",", "");
|
|
replace(offer, ",", "");
|
|
initial_offer = stoi(offer);
|
|
initial_offer = stoi(offer);
|
|
BUGZ_LOG(fatal) << "Selling, initial: " << initial_offer;
|
|
BUGZ_LOG(fatal) << "Selling, initial: " << initial_offer;
|
|
- buying = false;
|
|
|
|
- last_offer = 0;
|
|
|
|
- final_offer = 0;
|
|
|
|
|
|
+ buying = false; // Port is selling, we are buying.
|
|
}
|
|
}
|
|
|
|
+
|
|
// SL: [Our final offer is 1,263 credits.]
|
|
// SL: [Our final offer is 1,263 credits.]
|
|
if (startswith(line, "Our final offer is ")) {
|
|
if (startswith(line, "Our final offer is ")) {
|
|
// Well snap!
|
|
// Well snap!
|
|
- std::string offer = line.substr(20);
|
|
|
|
|
|
+ std::string offer = line.substr(19);
|
|
replace(offer, ",", "");
|
|
replace(offer, ",", "");
|
|
final_offer = stoi(offer);
|
|
final_offer = stoi(offer);
|
|
BUGZ_LOG(fatal) << "Final offer: " << final_offer;
|
|
BUGZ_LOG(fatal) << "Final offer: " << final_offer;
|
|
@@ -158,7 +227,7 @@ void ScriptTrader::server_prompt(const std::string &prompt) {
|
|
if (director.current_sector == active_port) {
|
|
if (director.current_sector == active_port) {
|
|
// begin state 3
|
|
// begin state 3
|
|
state = 3;
|
|
state = 3;
|
|
- to_client("Trade...\n\r");
|
|
|
|
|
|
+ to_client("Trading...\n\r");
|
|
to_server("PT");
|
|
to_server("PT");
|
|
return;
|
|
return;
|
|
} else {
|
|
} else {
|
|
@@ -178,6 +247,7 @@ void ScriptTrader::server_prompt(const std::string &prompt) {
|
|
if (director.current_sector == active_port) {
|
|
if (director.current_sector == active_port) {
|
|
// We're here
|
|
// We're here
|
|
state = 3;
|
|
state = 3;
|
|
|
|
+ to_client("Trading...\n\r");
|
|
to_server("PT");
|
|
to_server("PT");
|
|
return;
|
|
return;
|
|
} else {
|
|
} else {
|
|
@@ -191,24 +261,33 @@ void ScriptTrader::server_prompt(const std::string &prompt) {
|
|
}
|
|
}
|
|
|
|
|
|
if (state == 3) {
|
|
if (state == 3) {
|
|
- if (in(prompt, " to sell ")) {
|
|
|
|
- // always sell everything
|
|
|
|
- to_server("\r");
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- if (in(prompt, " to buy ") && startswith(prompt, "How many holds of ")) {
|
|
|
|
- // Ok, what are they selling?
|
|
|
|
|
|
+ if (startswith(prompt, "How many holds of ")) {
|
|
char selling = tolower(prompt[18]);
|
|
char selling = tolower(prompt[18]);
|
|
- BUGZ_LOG(fatal) << "Selling: " << selling;
|
|
|
|
for (int x = 0; x < 3; ++x) {
|
|
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 (foe[x] == selling) product = x;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (in(prompt, " to sell ")) {
|
|
|
|
+ // always sell everything
|
|
|
|
+ to_server("\r");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (in(prompt, " to buy ")) {
|
|
|
|
+ // 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");
|
|
|
|
+ product = x;
|
|
|
|
+ } else {
|
|
|
|
+ // No!
|
|
|
|
+ to_server("0\r");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -219,10 +298,12 @@ void ScriptTrader::server_prompt(const std::string &prompt) {
|
|
if (last_offer != 0) percent -= 1.0;
|
|
if (last_offer != 0) percent -= 1.0;
|
|
|
|
|
|
if (buying)
|
|
if (buying)
|
|
- last_offer = (int)(initial_offer * (100 - percent) / 100.0);
|
|
|
|
- else
|
|
|
|
last_offer = (int)(initial_offer * (100 + percent) / 100.0);
|
|
last_offer = (int)(initial_offer * (100 + percent) / 100.0);
|
|
- BUGZ_LOG(fatal) << "Offer: " << last_offer << " % " << percent;
|
|
|
|
|
|
+ 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);
|
|
std::string text = std::to_string(last_offer);
|
|
text.append("\r");
|
|
text.append("\r");
|
|
to_server(text);
|
|
to_server(text);
|
|
@@ -233,12 +314,34 @@ void ScriptTrader::server_prompt(const std::string &prompt) {
|
|
// do we carry on, or stop?
|
|
// do we carry on, or stop?
|
|
// 1.) CHECK TURNS // need turn tracking
|
|
// 1.) CHECK TURNS // need turn tracking
|
|
// 2.) PORTS BURNT?
|
|
// 2.) PORTS BURNT?
|
|
-
|
|
|
|
|
|
+
|
|
if (active_port == port[0])
|
|
if (active_port == port[0])
|
|
active_port = port[1];
|
|
active_port = port[1];
|
|
else
|
|
else
|
|
active_port = port[0];
|
|
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.
|
|
|
|
+
|
|
|
|
+ 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);
|
|
std::string move = std::to_string(active_port);
|
|
to_client("Moving...\n\r");
|
|
to_client("Moving...\n\r");
|
|
move.append("\r");
|
|
move.append("\r");
|