scripts.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. #include "scripts.h"
  2. #include <boost/format.hpp>
  3. #include "logging.h"
  4. ScriptTerror::ScriptTerror(Director &d, const char *called)
  5. : Dispatch(d, called) {
  6. BUGZ_LOG(warning) << "ScriptTerror()";
  7. if (called == nullptr) name = "Terror";
  8. init();
  9. }
  10. ScriptTerror::~ScriptTerror() { BUGZ_LOG(warning) << "~ScriptTerror()"; }
  11. void ScriptTerror::init(void) {
  12. BUGZ_LOG(fatal) << "ScriptTerror::init()";
  13. move = std::make_shared<MoveDispatch>(director, "TerrorMove");
  14. md = static_cast<MoveDispatch *>(&(*move));
  15. // setup notify functions for results/completion.
  16. md->setNotify([this]() { this->move_notify(); });
  17. input = std::make_shared<InputDispatch>(director, "TerrorInput");
  18. id = static_cast<InputDispatch *>(&(*input));
  19. id->prompt = "Number of loops: ";
  20. id->max_length = 4;
  21. id->numeric = true;
  22. id->setNotify([this]() { this->input_notify(); });
  23. trader = std::make_shared<TraderDispatch>(director, "TerrorTrader");
  24. td = static_cast<TraderDispatch *>(&(*trader));
  25. td->setNotify([this]() { this->trade_notify(); });
  26. BUGZ_LOG(fatal) << "ScriptTerror::init() completed.";
  27. }
  28. void ScriptTerror::activate(void) {
  29. BUGZ_LOG(warning) << "ScriptTerror::activate()";
  30. // Need: InputDispatch, MoveDispatch, ScriptTrader
  31. // Save the trade_end_empty setting, and set to Y
  32. if (director.galaxy.config.contains("trade_end_empty")) {
  33. old_trade_end_empty = director.galaxy.config["trade_end_empty"];
  34. } else {
  35. old_trade_end_empty = "Y";
  36. }
  37. director.galaxy.config["trade_end_empty"] = "Y";
  38. // Step 0: Get ship information / # of holds
  39. max_loops = loops = -1;
  40. to_server("I");
  41. // Step 1: Get number of loops of terror
  42. // director.chain = input;
  43. // input->activate();
  44. // Step 2: Look for closest trades, try ScriptTrade until none < some
  45. // level. Step 3: Move on, unless out of loops (or low on turns)
  46. // deactivate();
  47. }
  48. void ScriptTerror::deactivate(void) {
  49. BUGZ_LOG(warning) << "ScriptTerror::deactivate()";
  50. // restore the original value.
  51. director.galaxy.config["trade_end_empty"] = old_trade_end_empty;
  52. notify();
  53. }
  54. void ScriptTerror::input_notify(void) {
  55. if (id->input.empty()) {
  56. deactivate();
  57. return;
  58. }
  59. if (id->aborted) {
  60. deactivate();
  61. return;
  62. }
  63. max_loops = sstoi(id->input, -1);
  64. if (max_loops == -1) {
  65. deactivate();
  66. return;
  67. }
  68. id->input.clear();
  69. BUGZ_LOG(warning) << "Loops of terror: " << max_loops;
  70. loops = max_loops;
  71. // find nearest
  72. int stop_percent;
  73. if (director.galaxy.config.contains("stop_percent")) {
  74. stop_percent = director.galaxy.config["stop_percent"].get<int>();
  75. } else {
  76. stop_percent = 25;
  77. director.galaxy.config["stop_percent"] = stop_percent;
  78. }
  79. ppt = director.galaxy.find_closest_trade(director.current_sector, 3,
  80. stop_percent);
  81. if (ppt.type == 0) {
  82. to_client("No trades found! You've burnt the galaxy!\n\r");
  83. deactivate();
  84. return;
  85. }
  86. // ok, step 2: move!
  87. // md->setNotify([this]() { this->proxy_deactivate(); });
  88. if (director.current_sector != ppt.s1) {
  89. BUGZ_LOG(fatal) << "Moving to: " << ppt.s1;
  90. md->move_to = ppt.s1;
  91. director.chain = move;
  92. director.chain->activate();
  93. return;
  94. } else {
  95. // We're already there!
  96. to_client("Ok! Get trading!\n\r");
  97. td->port[0] = ppt.s1;
  98. td->port[1] = ppt.s2;
  99. td->trades = ppt.trades;
  100. td->type = ppt.type;
  101. director.chain = trader;
  102. director.chain->activate();
  103. return;
  104. }
  105. }
  106. void ScriptTerror::move_notify(void) {
  107. BUGZ_LOG(fatal) << "move_notify()";
  108. if (md->aborted) {
  109. to_client("Move cancel.\n\r");
  110. deactivate();
  111. return;
  112. }
  113. // Check for success, and start trading!
  114. if (md->success) {
  115. to_client("We're here, get trading!\n\r");
  116. td->port[0] = ppt.s1;
  117. td->port[1] = ppt.s2;
  118. td->trades = ppt.trades;
  119. td->type = ppt.type;
  120. director.chain = trader;
  121. director.chain->activate();
  122. return;
  123. } else {
  124. std::string message = "Move FAILED. " + md->why_failed + "\n\r";
  125. // to_client("Move FAILED.\n\r");
  126. to_client(message);
  127. deactivate();
  128. }
  129. }
  130. void ScriptTerror::server_prompt(const std::string &prompt) {
  131. if ((loops == -1) && (max_loops == -1)) {
  132. if (at_command_prompt(prompt)) {
  133. // Step 1: Get number of loops of terror
  134. director.chain = input;
  135. input->activate();
  136. return;
  137. }
  138. }
  139. }
  140. void ScriptTerror::trade_notify(void) {
  141. // Done trading -- maybe! :P
  142. if (td->aborted) {
  143. to_client("Trade cancel.\n\r");
  144. deactivate();
  145. return;
  146. }
  147. if (td->success) {
  148. // success!
  149. // find nearest
  150. int stop_percent;
  151. if (director.galaxy.config.contains("stop_percent")) {
  152. stop_percent = director.galaxy.config["stop_percent"].get<int>();
  153. } else {
  154. stop_percent = 25;
  155. director.galaxy.config["stop_percent"] = stop_percent;
  156. }
  157. ppt = director.galaxy.find_closest_trade(director.current_sector, 3,
  158. stop_percent);
  159. if (ppt.type == 0) {
  160. to_client("No trades found! You've burnt the galaxy!\n\r");
  161. deactivate();
  162. return;
  163. }
  164. if ((director.current_sector == ppt.s1) ||
  165. (director.current_sector == ppt.s2)) {
  166. // We're still here...
  167. BUGZ_LOG(fatal) << "Trade it again, Sam.";
  168. to_client("Keep trading.\n\r");
  169. td->port[0] = ppt.s1;
  170. td->port[1] = ppt.s2;
  171. td->trades = ppt.trades;
  172. td->type = ppt.type;
  173. director.chain = trader;
  174. director.chain->activate();
  175. return;
  176. }
  177. // Ok, this isn't a local trade.
  178. if (loops == 0) {
  179. to_client("We're done terrorizing, for now...\n\r");
  180. deactivate();
  181. return;
  182. }
  183. --loops;
  184. // Move to our next target
  185. BUGZ_LOG(fatal) << "Moving to: " << ppt.s1;
  186. md->move_to = ppt.s1;
  187. director.chain = move;
  188. director.chain->activate();
  189. return;
  190. } else {
  191. std::string message = "Trade done: " + td->why_failed + "\n\r";
  192. to_client(message);
  193. }
  194. // to_client("Ok, trade is done.\n\r");
  195. deactivate();
  196. }
  197. ScriptVoyager::ScriptVoyager(Director &d, const char *called)
  198. : Dispatch(d, called) {
  199. BUGZ_LOG(warning) << "ScriptVoyager()";
  200. if (called == nullptr) name = "Voyager";
  201. init();
  202. }
  203. ScriptVoyager::~ScriptVoyager() { BUGZ_LOG(warning) << "~ScriptVoyager()"; }
  204. void ScriptVoyager::init(void) {
  205. move = std::make_shared<MoveDispatch>(director, "VoyagerMove");
  206. md = static_cast<MoveDispatch *>(&(*move));
  207. md->setNotify([this]() { this->move_notify(); });
  208. input = std::make_shared<InputDispatch>(director, "VoyagerInput");
  209. id = static_cast<InputDispatch *>(&(*input));
  210. id->prompt = "Number of loops/tries: ";
  211. id->max_length = 5;
  212. id->numeric = true;
  213. id->setNotify([this]() { this->input_notify(); });
  214. }
  215. void ScriptVoyager::activate(void) {
  216. director.chain = input;
  217. input->activate();
  218. return;
  219. }
  220. void ScriptVoyager::deactivate(void) {
  221. BUGZ_LOG(warning) << "ScriptVoyager::deactivate()";
  222. notify();
  223. }
  224. void ScriptVoyager::move_notify(void) {
  225. if (md->aborted) {
  226. deactivate();
  227. return;
  228. }
  229. if (md->success) {
  230. // Great!
  231. next();
  232. return;
  233. } else {
  234. std::string message = "No safe moves. " + md->why_failed + "\n\r";
  235. to_client(message);
  236. deactivate();
  237. }
  238. }
  239. void ScriptVoyager::input_notify(void) {
  240. if (id->input.empty() || id->aborted) {
  241. to_client("Ok, maybe later then.\n\r");
  242. deactivate();
  243. return;
  244. }
  245. loops = sstoi(id->input, -1);
  246. if (loops == -1) {
  247. to_client("I'm sorry, WHAT?\n\r");
  248. deactivate();
  249. }
  250. id->input.clear();
  251. BUGZ_LOG(warning) << "Voyager loops: " << loops;
  252. next();
  253. }
  254. void ScriptVoyager::next(void) {
  255. if (loops == 0) {
  256. // ok, stop here.
  257. to_client("The voyage ends here, for now.\n\r");
  258. deactivate();
  259. return;
  260. }
  261. --loops;
  262. sector_type s =
  263. director.galaxy.find_nearest_unexplored(director.current_sector);
  264. if (s == 0) {
  265. to_client("I don't see anything else to explorer.\n\r");
  266. BUGZ_LOG(warning) << "find_nearest_unexplored returned 0";
  267. deactivate();
  268. }
  269. BUGZ_LOG(warning) << "Next stop: " << s;
  270. md->move_to = s;
  271. director.chain = move;
  272. director.chain->activate();
  273. }
  274. // SL: [###### DANGER! You have marked sector 740 to be avoided!]
  275. // SP: [Do you really want to warp there? (Y/N) ]
  276. void ScriptVoyager::server_prompt(const std::string &prompt) {}
  277. ScriptExplore::ScriptExplore(Director &d, const char *called)
  278. : Dispatch(d, called) {
  279. BUGZ_LOG(warning) << "ScriptExplore()";
  280. if (called == nullptr) name = "Explore";
  281. init();
  282. }
  283. ScriptExplore::~ScriptExplore() { BUGZ_LOG(warning) << "~ScriptExplore()"; }
  284. void ScriptExplore::init() {
  285. move = std::make_shared<MoveDispatch>(director, "ExploreMove");
  286. md = static_cast<MoveDispatch *>(&(*move));
  287. md->setNotify([this]() { this->move_notify(); });
  288. input = std::make_shared<InputDispatch>(director, "ExploreInput");
  289. id = static_cast<InputDispatch *>(&(*input));
  290. id->prompt = "Number of sectors to explore: ";
  291. id->max_length = 5;
  292. id->numeric = true;
  293. id->setNotify([this]() { this->input_notify(); });
  294. state = 0;
  295. target = 0;
  296. if (!director.galaxy.config.contains("prefer_ports")) {
  297. director.galaxy.config["prefer_ports"] = "Y";
  298. prefer_ports = true;
  299. } else {
  300. prefer_ports =
  301. startswith(json_str(director.galaxy.config["prefer_ports"]), "Y");
  302. }
  303. if (!director.galaxy.meta["help"].contains("prefer_ports")) {
  304. director.galaxy.meta["help"]["prefer_ports"] =
  305. "Explorer prefers to find ports";
  306. }
  307. if (!director.galaxy.config.contains("use_nearest_unknown")) {
  308. director.galaxy.config["use_nearest_unknown"] = "Y";
  309. use_nearest_unknown = true;
  310. } else {
  311. use_nearest_unknown = startswith(
  312. json_str(director.galaxy.config["use_nearest_unknown"]), "Y");
  313. }
  314. if (!director.galaxy.meta["help"].contains("use_nearest_unknown")) {
  315. director.galaxy.meta["help"]["use_nearest_unknown"] =
  316. "Explorer will find nearest unknown when out of known sectors to "
  317. "explore";
  318. }
  319. BUGZ_LOG(warning) << "Prefer Ports: " << prefer_ports;
  320. }
  321. void ScriptExplore::activate() {
  322. us = director.chain;
  323. state = 1;
  324. to_server("I");
  325. }
  326. void ScriptExplore::deactivate() {
  327. BUGZ_LOG(warning) << "ScriptExplore::deactivate()";
  328. us.reset();
  329. notify();
  330. }
  331. void ScriptExplore::move_notify() {
  332. director.chain = us;
  333. if (md->aborted) {
  334. deactivate();
  335. return;
  336. }
  337. if (md->success) {
  338. to_server("SD");
  339. state = 3;
  340. return;
  341. } else {
  342. if (unknown_warps.size() != 0) {
  343. BUGZ_LOG(warning) << "Seeking previous unexplored (Unsafe Dest.)";
  344. state = 4;
  345. target = unknown_warps.top();
  346. unknown_warps.pop();
  347. std::string message = "UNSAFE DESTINATION";
  348. ANSIColor alert(COLOR::WHITE, COLOR::RED, ATTR::BOLD);
  349. to_client(" " + alert() + message + reset() + "\n\r");
  350. BUGZ_LOG(warning) << "Target = " << target;
  351. next();
  352. } else {
  353. std::string message = "Move failed: " + md->why_failed + "\n\r";
  354. BUGZ_LOG(warning) << message;
  355. to_client(message);
  356. deactivate();
  357. }
  358. }
  359. }
  360. void ScriptExplore::input_notify() {
  361. director.chain = us;
  362. if (id->input.empty() || id->aborted) {
  363. to_client("Maybe next time.\n\r");
  364. deactivate();
  365. return;
  366. }
  367. loops = sstoi(id->input, -1);
  368. if (loops == -1) {
  369. to_client("I'm sorry, WHAT?\n\r");
  370. deactivate();
  371. return;
  372. }
  373. if (loops == 0) {
  374. infinite = true;
  375. } else {
  376. infinite = false;
  377. }
  378. id->input.clear();
  379. if (!infinite) {
  380. BUGZ_LOG(warning) << "Explore loops: " << loops;
  381. } else {
  382. to_client("Infinite Mode!\n\r");
  383. to_client("[ PRESS A KEY TO STOP ]\n\r");
  384. BUGZ_LOG(warning) << "Explore loops: INFINITE";
  385. }
  386. to_server("SD");
  387. state = 3;
  388. }
  389. bool has_port(const density d) {
  390. int den = d.density;
  391. if (d.navhaz <= 5) {
  392. den -= 21 * d.navhaz;
  393. }
  394. return den == 100 || den == 101;
  395. }
  396. bool sort_ports(const density a, const density b) {
  397. if (has_port(a) && has_port(b)) {
  398. return a.warps > b.warps;
  399. } else {
  400. if ((has_port(a) && !has_port(b)) || (!has_port(a) && has_port(b))) {
  401. return has_port(a);
  402. }
  403. return a.warps > b.warps;
  404. }
  405. }
  406. bool sort_warps(const density a, const density b) {
  407. if ((has_port(a) && !has_port(b)) || (!has_port(a) && has_port(b))) {
  408. return has_port(a) && a.warps > b.warps;
  409. }
  410. return a.warps > b.warps;
  411. }
  412. void ScriptExplore::next() {
  413. if (loops <= 0 && !infinite) {
  414. to_client("The exploration ends, for now.\n\r");
  415. deactivate();
  416. return;
  417. }
  418. if (!infinite) --loops;
  419. // Calculate next best sector to goto
  420. density_scan &ds = director.galaxy.dscan;
  421. density best_sector;
  422. best_sector.sector = 0;
  423. std::array<density, 6> sectors = ds.d;
  424. for (int x = 0; x < 6; ++x) {
  425. if (sectors[x].known ||
  426. !density_clear(sectors[x].sector, sectors[x].density,
  427. sectors[x].navhaz)) {
  428. sectors[x].sector = 0;
  429. sectors[x].density = 0;
  430. sectors[x].warps = 0;
  431. sectors[x].navhaz = 0;
  432. sectors[x].known = false;
  433. sectors[x].anomaly = false;
  434. }
  435. }
  436. if (prefer_ports) {
  437. std::sort(sectors.begin(), sectors.end(), sort_ports);
  438. } else {
  439. std::sort(sectors.begin(), sectors.end(), sort_warps);
  440. }
  441. for (int x = 0; x < 6; ++x) {
  442. BUGZ_LOG(trace) << "[" << sectors[x].sector << ", " << sectors[x].density
  443. << ", " << sectors[x].warps << ", " << sectors[x].anomaly
  444. << ", " << sectors[x].known << "]";
  445. }
  446. BUGZ_LOG(warning) << "Best Sector: " << sectors[0].sector;
  447. best_sector = sectors[0];
  448. for (int x = 1; x < 6; ++x) {
  449. if (sectors[x].sector != 0 && sectors[x].sector != best_sector.sector) {
  450. unknown_warps.push(sectors[x].sector);
  451. BUGZ_LOG(info) << "Added " << sectors[x].sector << " to unknown_warps ("
  452. << unknown_warps.size() << ")";
  453. }
  454. }
  455. if (target != 0) {
  456. BUGZ_LOG(info) << "Using: " << target;
  457. best_sector.sector = target;
  458. target = 0;
  459. }
  460. BUGZ_LOG(warning) << "Unknown Warps: " << unknown_warps.size();
  461. if (best_sector.sector == 0) {
  462. if (unknown_warps.size() == 0) {
  463. if (!use_nearest_unknown) {
  464. to_client("No unknown warps.");
  465. deactivate();
  466. return;
  467. } else {
  468. BUGZ_LOG(warning) << "Seeking nearest unknown";
  469. std::string message = "Finding nearest unexplored";
  470. ANSIColor alert(COLOR::WHITE, COLOR::RED, ATTR::BOLD);
  471. to_client(" " + alert() + message + reset() + "\n\r");
  472. best_sector.sector =
  473. director.galaxy.find_nearest_unexplored(director.current_sector);
  474. if (best_sector.sector == 0) {
  475. to_client("No unknown warps in the galaxy!");
  476. deactivate();
  477. return;
  478. }
  479. }
  480. } else {
  481. BUGZ_LOG(warning) << "Seeking previous unexplored";
  482. best_sector.sector = unknown_warps.top();
  483. unknown_warps.pop();
  484. std::string message = "DEAD END";
  485. ANSIColor alert(COLOR::WHITE, COLOR::RED, ATTR::BOLD);
  486. to_client(" " + alert() + message + reset() + "\n\r");
  487. }
  488. }
  489. BUGZ_LOG(warning) << "Targeting sector: " << best_sector.sector;
  490. if (director.current_sector != best_sector.sector) {
  491. md->move_to = best_sector.sector;
  492. director.chain = move;
  493. director.chain->activate();
  494. } else {
  495. BUGZ_LOG(warning) << "Targeting current sector!";
  496. state = 3;
  497. to_server("SD");
  498. }
  499. }
  500. void ScriptExplore::server_prompt(const std::string &prompt) {
  501. BUGZ_LOG(info) << "Explorer State: SP " << state;
  502. // next();
  503. if (state == 1) {
  504. if (at_command_prompt(prompt)) {
  505. if (director.galaxy.meta.contains("ship")) {
  506. if (!director.galaxy.meta["ship"].contains("scanner")) {
  507. to_client(
  508. "\n\rIt appears your ship doesn't have a long range "
  509. "scanner.\n\r");
  510. deactivate();
  511. return;
  512. }
  513. }
  514. state = 2;
  515. BUGZ_LOG(trace) << "state = 1, prompting for user input";
  516. director.chain = input;
  517. input->activate();
  518. return;
  519. }
  520. }
  521. if (state == 3) {
  522. if (at_command_prompt(prompt)) {
  523. state = 4;
  524. BUGZ_LOG(trace) << "state = 3, calculating next sector";
  525. next();
  526. }
  527. }
  528. }
  529. ScriptPlanet::ScriptPlanet(Director &d, const char *called)
  530. : Dispatch(d, called) {
  531. BUGZ_LOG(warning) << "ScriptPlanet()";
  532. if (called == nullptr) name = "Planet";
  533. init();
  534. }
  535. ScriptPlanet::~ScriptPlanet() { BUGZ_LOG(warning) << "~ScriptPlanet()"; }
  536. void ScriptPlanet::init() {
  537. move = std::make_shared<MoveDispatch>(director, "PlanetMove");
  538. md = static_cast<MoveDispatch *>(&(*move));
  539. md->setNotify([this]() { this->move_notify(); });
  540. md->use_express = true;
  541. trader = std::make_shared<TraderDispatch>(director, "PlanetTrader");
  542. td = static_cast<TraderDispatch *>(&(*trader));
  543. td->setNotify([this]() { this->trade_notify(); });
  544. input = std::make_shared<InputDispatch>(director, "PlanetInput");
  545. id = static_cast<InputDispatch *>(&(*input));
  546. id->prompt = "Which planet would you like to upgrade => ";
  547. id->max_length = 3;
  548. id->numeric = true;
  549. id->setNotify([this]() { this->input_notify(); });
  550. state = 0;
  551. }
  552. void ScriptPlanet::activate() {
  553. us = director.chain;
  554. aborted = false;
  555. // FUTURE: handle special case here, where we activate at planet/citadel
  556. // prompt
  557. /*
  558. States:
  559. 1 = Team List Planets
  560. 2 = List Planets
  561. 3 = Input Which planet to upgrade?
  562. 4 = Move to Planet
  563. 5 = Land, get planet information
  564. 6 = CU Citadel build/upgrade
  565. 7 = Parse Citadel needs
  566. 8 = Construct/upgrade ?
  567. 9 = to terra!
  568. 10 = back to the planet!
  569. 11 = unloading...
  570. 12 = resources
  571. 13 = moving to buyer
  572. 14 = moving to planet
  573. 15 = product unloaded
  574. */
  575. state = 1;
  576. // clear out the planets list -- we're refreshing.
  577. director.galaxy.planets.clear();
  578. // get planet lists
  579. to_server("TLQ");
  580. }
  581. void ScriptPlanet::deactivate() {
  582. BUGZ_LOG(warning) << "ScriptPlanet::deactivate()";
  583. us.reset();
  584. notify();
  585. }
  586. void ScriptPlanet::clear_amounts(void) {
  587. // clear amounts
  588. for (int x = 0; x < 3; x++) {
  589. population[x] = 0;
  590. amount[x] = 0;
  591. needs[x] = 0;
  592. ship[x] = 0;
  593. to_make_one[x] = 0;
  594. }
  595. total_population = 0;
  596. support_construction = 0;
  597. days = 0;
  598. }
  599. /**
  600. * @brief Best place to put colonists
  601. *
  602. * Where is the best place to put colonists?
  603. *
  604. * That would be in production where they produce the most product.
  605. * Where the number to make one is the least.
  606. *
  607. * @return int
  608. */
  609. int ScriptPlanet::place_colonists(void) {
  610. int best = 1000;
  611. int pos = 0;
  612. for (int x = 0; x < 3; x++) {
  613. if (to_make_one[x] != 0) {
  614. if (to_make_one[x] < best) {
  615. best = to_make_one[x];
  616. pos = x;
  617. }
  618. }
  619. }
  620. return pos;
  621. }
  622. void ScriptPlanet::input_notify() {
  623. // deactivate();
  624. director.chain = us;
  625. if (id->input.empty()) {
  626. deactivate();
  627. return;
  628. }
  629. int selected = sstoi(id->input);
  630. if (selected == 0) {
  631. deactivate();
  632. return;
  633. }
  634. // find the planet in our list
  635. auto pos = director.galaxy.planets.find(selected);
  636. if (pos == director.galaxy.planets.end()) {
  637. to_client("Sorry, I can't find that planet #.\n\r");
  638. deactivate();
  639. return;
  640. }
  641. // Check planet level, if already max, don't bother!
  642. // Ok, we're off to planet # selected!
  643. planet = selected;
  644. sector = pos->second.sector;
  645. current_level = pos->second.level;
  646. if (director.current_sector == sector) {
  647. // We're already there!
  648. state = 5;
  649. // clear out numbers
  650. clear_amounts();
  651. // Landing ...
  652. to_server("L");
  653. return;
  654. } else {
  655. // Let's get to the planet
  656. state = 4;
  657. md->move_to = sector;
  658. director.chain = move;
  659. move->activate();
  660. return;
  661. }
  662. }
  663. void ScriptPlanet::move_notify() {
  664. director.chain = us;
  665. if (md->success) {
  666. // Ok, we're here
  667. if (state == 4) {
  668. state = 5;
  669. // clear out numbers
  670. clear_amounts();
  671. to_server("L");
  672. return;
  673. } else if (state == 9) {
  674. // Ok, we're at terra
  675. to_server("LT\r");
  676. return;
  677. } else if (state == 10) {
  678. // Back at the planet - Land and unload
  679. to_server("L");
  680. return;
  681. } else if (state == 13) {
  682. // Ok, we're here!
  683. td->port[0] = current_buyfrom;
  684. td->port[1] = 0;
  685. for (int x = 0; x < 3; x++) {
  686. td->port_buysell[0].foe[x] = false;
  687. td->port_buysell[1].foe[x] = false;
  688. td->trades.foe[x] = false;
  689. }
  690. td->port_buysell[0].foe[current_product] = true;
  691. td->trades.foe[current_product] = true;
  692. director.chain = trader;
  693. td->activate();
  694. return;
  695. } else if (state == 14) {
  696. // We're at the planet! Time to unload!
  697. to_server("L");
  698. return;
  699. }
  700. return;
  701. } else {
  702. std::string message = "MOVE FAILED: ";
  703. message.append(md->why_failed);
  704. message.append("\n\r");
  705. to_client(message);
  706. deactivate();
  707. return;
  708. }
  709. }
  710. void ScriptPlanet::trade_notify() {
  711. director.chain = us;
  712. if (td->success) {
  713. if (state == 13) {
  714. // Ok, we have what we came from, return to the planet.
  715. state = 14;
  716. md->move_to = sector;
  717. director.chain = move;
  718. move->activate();
  719. return;
  720. }
  721. } else {
  722. to_client("Trade failed.\n\r");
  723. deactivate();
  724. return;
  725. }
  726. }
  727. void ScriptPlanet::server_prompt(const std::string &prompt) {
  728. if (state == 1) {
  729. if (at_command_prompt(prompt)) {
  730. state = 2;
  731. to_server("CYQ");
  732. return;
  733. }
  734. } else if (state == 2) {
  735. if (at_command_prompt(prompt)) {
  736. state = 3;
  737. if (director.galaxy.planets.empty()) {
  738. to_client("Sorry, I don't see that you have any planets!\n\r");
  739. deactivate();
  740. return;
  741. }
  742. for (auto const &planet : director.galaxy.planets) {
  743. std::string text = str(
  744. boost::format("%1$3d <%2$5d> Class %3% Level %4% Name %5%\n\r") %
  745. planet.first % planet.second.sector % planet.second.c %
  746. planet.second.level % planet.second.name);
  747. to_client(text);
  748. }
  749. director.chain = input;
  750. director.chain->activate();
  751. return;
  752. }
  753. } else if (state == 5) {
  754. // Are they prompting us for which planet to land on?
  755. // SP: [Land on which planet <Q to abort> ? ]
  756. if (prompt == "Land on which planet <Q to abort> ? ") {
  757. std::string text = std::to_string(planet) + "\r";
  758. to_server(text);
  759. return;
  760. }
  761. // SP: [Planet command (?=help) [D] ]
  762. if (prompt == "Planet command (?=help) [D] ") {
  763. // Ok, we're here on the planet. (Did we capture the planet info on
  764. // landing?)
  765. BUGZ_LOG(fatal) << "Total population: " << total_population;
  766. // FIXUP: Empty out the ship holds if they aren't empty.
  767. for (int x = 0; x < 3; ++x) {
  768. if (ship[x] != 0) {
  769. std::string message = str(
  770. boost::format("Transfer ship cargo %1% (%2%) to planet.\n\r") %
  771. foe[x] % ship[x]);
  772. to_client(message);
  773. std::string transfer = "TNL";
  774. transfer.append(std::to_string(x + 1));
  775. transfer.append("\r");
  776. to_server(transfer);
  777. ship[x] = 0;
  778. return;
  779. }
  780. }
  781. // citadel, upgrade
  782. // what is the message when you don't have a citadel yet?
  783. state = 6;
  784. to_server("CU");
  785. return;
  786. }
  787. } else if (state == 8) {
  788. // SP: [Do you wish to construct one? ]
  789. if (startswith(prompt, "Do you wish to construct ") &&
  790. endswith(prompt, "? ")) {
  791. // Do we have what we need?
  792. bool ready = true;
  793. if (total_population < support_construction) {
  794. ready = false;
  795. state = 9;
  796. std::string message =
  797. str(boost::format("Planet has %1% people, need %2%.\n\r") %
  798. total_population % support_construction);
  799. to_client(message);
  800. BUGZ_LOG(fatal) << "Need people ["
  801. << support_construction - total_population << "]";
  802. }
  803. for (int x = 0; x < 3; ++x) {
  804. if (needs[x] > amount[x]) {
  805. if (ready) {
  806. ready = false;
  807. state = 12;
  808. }
  809. std::string message =
  810. str(boost::format("Has %1% of %2%, needs %3%\n\r") % amount[x] %
  811. foe[x] % needs[x]);
  812. to_client(message);
  813. BUGZ_LOG(fatal) << "Need " << x << " :" << needs[x] - amount[x];
  814. }
  815. }
  816. if (ready) {
  817. to_server("Y");
  818. deactivate();
  819. return;
  820. }
  821. // NNY!
  822. if (current_level > 0) {
  823. to_server("NQQ");
  824. } else
  825. to_server("NQ");
  826. // Ok, time to start moving!
  827. if (state == 9) {
  828. md->move_to = 1;
  829. director.chain = move;
  830. md->activate();
  831. return;
  832. }
  833. if (state == 12) {
  834. // Need resources
  835. current_product = -1;
  836. for (int x = 0; x < 3; ++x) {
  837. if (needs[x] > amount[x]) {
  838. current_product = x;
  839. break;
  840. }
  841. }
  842. if (current_product == -1) {
  843. // I think we have everything.
  844. BUGZ_LOG(fatal) << "I think we've got it...";
  845. to_server("CUY");
  846. deactivate();
  847. return;
  848. } else {
  849. // Ok, let's find where we need to go
  850. current_buyfrom = director.galaxy.find_nearest_selling(
  851. director.current_sector, current_product);
  852. if (current_buyfrom == 0) {
  853. to_client("We weren't able to locate a port selling.\n\r");
  854. deactivate();
  855. return;
  856. }
  857. state = 13;
  858. md->move_to = current_buyfrom;
  859. director.chain = move;
  860. md->activate();
  861. return;
  862. }
  863. }
  864. }
  865. } else if (state == 9) {
  866. if (at_command_prompt(prompt)) {
  867. // Ok! We have colonists ... back to the planet!
  868. state = 10;
  869. md->move_to = sector;
  870. director.chain = move;
  871. md->activate();
  872. return;
  873. }
  874. } else if (state == 10) {
  875. if (prompt == "Land on which planet <Q to abort> ? ") {
  876. std::string text = std::to_string(planet) + "\r";
  877. to_server(text);
  878. return;
  879. }
  880. if (prompt == "Planet command (?=help) [D] ") {
  881. // Ok, on the planet.
  882. state = 11;
  883. int place = place_colonists();
  884. std::string unload = "SNL";
  885. unload.append(std::to_string(place + 1));
  886. unload.append("\r");
  887. to_server(unload);
  888. return;
  889. }
  890. } else if (state == 11) {
  891. if (prompt == "Planet command (?=help) [D] ") {
  892. if (total_population < support_construction) {
  893. // Need More - we're not in citadel.
  894. to_server("Q");
  895. state = 9;
  896. md->move_to = 1;
  897. director.chain = move;
  898. md->activate();
  899. return;
  900. }
  901. // Ok, we're ready for the next step:
  902. // checking the resources.
  903. // Need resources
  904. current_product = -1;
  905. for (int x = 0; x < 3; ++x) {
  906. if (needs[x] > amount[x]) {
  907. current_product = x;
  908. break;
  909. }
  910. }
  911. if (current_product == -1) {
  912. // I think we have everything.
  913. BUGZ_LOG(fatal) << "I think we've got it...";
  914. to_server("CUY");
  915. deactivate();
  916. return;
  917. } else {
  918. // Ok, let's find where we need to go
  919. current_buyfrom = director.galaxy.find_nearest_selling(
  920. director.current_sector, current_product);
  921. if (current_buyfrom == 0) {
  922. to_client("We weren't able to locate a port selling.\n\r");
  923. deactivate();
  924. return;
  925. }
  926. to_server("Q");
  927. state = 13;
  928. md->move_to = current_buyfrom;
  929. director.chain = move;
  930. md->activate();
  931. return;
  932. }
  933. // ending here for now. (not in citadel)
  934. // to_server("Q");
  935. // deactivate();
  936. // return;
  937. }
  938. } else if (state == 14) {
  939. if (prompt == "Land on which planet <Q to abort> ? ") {
  940. std::string text = std::to_string(planet) + "\r";
  941. to_server(text);
  942. return;
  943. }
  944. if (prompt == "Planet command (?=help) [D] ") {
  945. state = 15;
  946. std::string command = "TNL";
  947. command.append(std::to_string(current_product + 1));
  948. command.append("\r");
  949. to_server(command);
  950. return;
  951. }
  952. } else if (state == 15) {
  953. if (prompt == "Planet command (?=help) [D] ") {
  954. // Ok, we're done unloading ... what do we need next?
  955. // Need resources
  956. current_product = -1;
  957. for (int x = 0; x < 3; ++x) {
  958. if (needs[x] > amount[x]) {
  959. current_product = x;
  960. break;
  961. }
  962. }
  963. if (current_product == -1) {
  964. // I think we have everything.
  965. BUGZ_LOG(fatal) << "I think we've got it...";
  966. to_server("CUY");
  967. deactivate();
  968. return;
  969. } else {
  970. // Ok, let's find where we need to go
  971. current_buyfrom = director.galaxy.find_nearest_selling(
  972. director.current_sector, current_product);
  973. if (current_buyfrom == 0) {
  974. to_client("We weren't able to locate a port selling.\n\r");
  975. deactivate();
  976. return;
  977. }
  978. to_server("Q");
  979. state = 13;
  980. md->move_to = current_buyfrom;
  981. director.chain = move;
  982. md->activate();
  983. return;
  984. }
  985. }
  986. }
  987. }
  988. void ScriptPlanet::server_line(const std::string &line,
  989. const std::string &raw_line) {
  990. // because I'm not sending this to the client, this is all hidden from them.
  991. if (state == 5) {
  992. // Save the planet information
  993. /*
  994. SL: [ ------- --------- --------- --------- --------- ---------
  995. ---------] SL: [Fuel Ore 4,950 2 2,475 28,135 0
  996. 200,000] SL: [Organics 0 5 0 100 0
  997. 200,000] SL: [Equipment 128 20 6 120 0
  998. 100,000] SL: [Fighters N/A 24 206 2,395 200
  999. 1,000,000]
  1000. */
  1001. std::string work = line;
  1002. replace(work, "Fuel Ore", "Fuel");
  1003. replace(work, ",", "");
  1004. auto parts = split(work);
  1005. if (parts.size() == 7) {
  1006. int pos = -1;
  1007. if (parts[0] == "Fuel") {
  1008. pos = 0;
  1009. } else if (parts[0] == "Organics") {
  1010. pos = 1;
  1011. } else if (parts[0] == "Equipment") {
  1012. pos = 2;
  1013. }
  1014. // To save:
  1015. if (pos >= 0) {
  1016. population[pos] = sstoi(parts[1]);
  1017. to_make_one[pos] = sstoi(parts[2]);
  1018. total_population += population[pos];
  1019. amount[pos] = sstoi(parts[4]);
  1020. // What do we have on-board the ship?
  1021. ship[pos] = sstoi(parts[5]);
  1022. BUGZ_LOG(fatal) << pos << " : pop " << population[pos] << " 2M1 "
  1023. << to_make_one[pos] << " amount " << amount[pos]
  1024. << " ship: " << ship[pos];
  1025. }
  1026. }
  1027. } else if (state == 6) {
  1028. // [Be patient, your Citadel is not yet finished.]
  1029. if ((line == "Be patient, your Citadel is not yet finished.") ||
  1030. (startswith(line, "You may not upgrade while"))) {
  1031. // We're already upgrading!
  1032. std::string work = raw_line;
  1033. work += "\n\r";
  1034. to_client(work);
  1035. if (current_level > 0) {
  1036. to_server("QQ"); // exit citadel, exit planet
  1037. } else {
  1038. to_server("Q"); // exit planet
  1039. };
  1040. deactivate();
  1041. return;
  1042. }
  1043. // Citadel upgrade information (possibly)
  1044. /*
  1045. SL: [Citadel construction on this type of planet requires the following:]
  1046. SL: [ 400,000 Colonists to support the construction,]
  1047. SL: [ 150 units of Fuel Ore,]
  1048. SL: [ 100 units of Organics,]
  1049. SL: [ 150 units of Equipment and]
  1050. SL: [ 2 days to construct.]
  1051. [Construction of a Combat Control Computer on this type of planet requires]
  1052. */
  1053. if ((line == "Citadel construction on this type of planet requires the "
  1054. "following:") ||
  1055. (endswith(line, " on this type of planet requires"))) {
  1056. state = 7;
  1057. }
  1058. } else if (state == 7) {
  1059. if (line.empty()) {
  1060. state = 8;
  1061. } else {
  1062. // Display the upgrade information to the client
  1063. std::string work = raw_line;
  1064. work += "\n\r";
  1065. to_client(work);
  1066. work = line;
  1067. replace(work, ",", "");
  1068. trim(work);
  1069. auto pos = work.find(" Colonists to support");
  1070. if (pos != std::string::npos) {
  1071. support_construction = sstoi(work) / 1000;
  1072. return;
  1073. }
  1074. pos = work.find(" units of Fuel");
  1075. if (pos != std::string::npos) {
  1076. needs[0] = sstoi(work);
  1077. return;
  1078. }
  1079. pos = work.find(" units of Organics");
  1080. if (pos != std::string::npos) {
  1081. needs[1] = sstoi(work);
  1082. return;
  1083. }
  1084. pos = work.find(" units of Equipment");
  1085. if (pos != std::string::npos) {
  1086. needs[2] = sstoi(work);
  1087. return;
  1088. }
  1089. pos = work.find(" days to construct");
  1090. if (pos != std::string::npos) {
  1091. days = sstoi(work);
  1092. return;
  1093. }
  1094. }
  1095. } else if (state == 9) {
  1096. bool output = false;
  1097. if (startswith(line, "How many groups of Colonist")) output = true;
  1098. if (startswith(line, "The Colonists file aboard your ship")) output = true;
  1099. if (output) {
  1100. // output to client
  1101. std::string work = raw_line;
  1102. work += "\n\r";
  1103. to_client(work);
  1104. }
  1105. } else if (state == 11) {
  1106. // SL: [How many groups of Colonists do you want to leave ([125] on board)
  1107. // ? ]
  1108. bool output = false;
  1109. if (startswith(line, "How many groups of Colonists do you want to leave")) {
  1110. std::string work = line.substr(line.find('[') + 1);
  1111. int amount = sstoi(work);
  1112. total_population += amount;
  1113. output = true;
  1114. BUGZ_LOG(fatal) << "Population now: " << total_population;
  1115. }
  1116. if (startswith(line, "The Colonists disembark")) output = true;
  1117. if (output) {
  1118. // output to client
  1119. std::string work = raw_line;
  1120. work += "\n\r";
  1121. to_client(work);
  1122. }
  1123. } else if (state == 15) {
  1124. bool output = false;
  1125. // SL: [How many holds of Organics do you want to leave ([250] on board) ? ]
  1126. if (startswith(line, "How many holds of ") &&
  1127. endswith(line, "] on board) ? ")) {
  1128. std::string work = line.substr(line.find('[') + 1);
  1129. int amount_moved = sstoi(work);
  1130. amount[current_product] += amount_moved;
  1131. output = true;
  1132. BUGZ_LOG(fatal) << "Planet " << current_product << " +" << amount_moved
  1133. << " = " << amount[current_product];
  1134. }
  1135. if (startswith(line, "You unload the ")) output = true;
  1136. if (output) {
  1137. // output to client
  1138. std::string work = raw_line;
  1139. work += "\n\r";
  1140. to_client(work);
  1141. }
  1142. }
  1143. }
  1144. void ScriptPlanet::client_input(const std::string &input) { deactivate(); }