123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- #include "scripts.h"
- #include "logging.h"
- ScriptTrader::ScriptTrader(Director &d) : Dispatch(d) {
- BUGZ_LOG(fatal) << "ScriptTrader()";
- };
- ScriptTrader::~ScriptTrader() { BUGZ_LOG(fatal) << "~ScriptTrader()"; }
- void ScriptTrader::activate(void) {
-
- 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_info = director.galaxy.ports.find(port[1]);
- int port1_type = port_info->second.type;
- BUGZ_LOG(fatal) << port0_type << " and " << port1_type;
- auto ttr = trade_type_info(port0_type, port1_type);
- trades = ttr.trades;
-
-
- state = 1;
- to_server("I");
- }
- void ScriptTrader::deactivate(void) { notify(); }
- void ScriptTrader::server_line(const std::string &line,
- const std::string &raw_line) {
-
- }
- void ScriptTrader::server_prompt(const std::string &prompt) {
-
- if (at_command_prompt(prompt)) {
- if (state == 1) {
-
- if (director.galaxy.meta["ship"]["holds"]["c"]) {
-
- to_client("ScriptTrader FAIL: holds contain colonists.");
- deactivate();
- return;
- }
-
- BUGZ_LOG(fatal) << "trades: " << trades;
- BUGZ_LOG(fatal) << "port0:" << text_from_buysell(port_buysell[0]);
- BUGZ_LOG(fatal) << "port1:" << text_from_buysell(port_buysell[1]);
-
- bool have_buy = false;
- char foe[4] = "foe";
- int active_buy = 0;
- int active_sell = 0;
- for (int x = 0; x < 3; ++x) {
- if (trades.foe[x]) {
-
- if (director.galaxy.meta["ship"]["holds"][foe[x]]) {
-
- have_buy = true;
-
- if (port_buysell[0].foe[x]) {
- active_buy = 0;
- } else {
- active_buy = 1;
- }
- }
- 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 {
-
-
-
-
- BUGZ_LOG(fatal) << "!have_buy: port " << active_sell;
- active_port = port[active_sell];
- }
- state = 2;
- if (director.current_sector == active_port) {
-
- state = 3;
- to_client("Trade...\n\r");
- to_server("PT");
- return;
- } else {
-
- std::string move = std::to_string(active_port);
- to_client("Moving...\n\r");
- move.append("\r");
- to_server(move);
- return;
- }
-
- deactivate();
- }
- if (state == 3) {
- if (director.current_sector == active_port) {
-
- state = 3;
- to_server("PT");
- return;
- } else {
-
- BUGZ_LOG(fatal) << "Expecting: " << active_port << " but got "
- << director.current_sector;
- deactivate();
- return;
- }
- }
- }
- if (state == 3) {
- if (in(prompt, " to sell ")) {
-
- to_server("\r");
- return;
- }
- }
- }
- void ScriptTrader::client_input(const std::string &cinput) { deactivate(); };
|