scripts.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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 = startswith(json_str(director.galaxy.config["prefer_ports"]), "Y");
  301. }
  302. if (!director.galaxy.meta["help"].contains("prefer_ports")) {
  303. director.galaxy.meta["help"]["prefer_ports"] =
  304. "Explorer prefers to find ports";
  305. }
  306. if (!director.galaxy.config.contains("use_nearest_unknown")) {
  307. director.galaxy.config["use_nearest_unknown"] = "Y";
  308. use_nearest_unknown = true;
  309. } else {
  310. use_nearest_unknown = startswith(json_str(director.galaxy.config["use_nearest_unknown"]), "Y");
  311. }
  312. if (!director.galaxy.meta["help"].contains("use_nearest_unknown")) {
  313. director.galaxy.meta["help"]["use_nearest_unknown"] =
  314. "Explorer will find nearest unknown when out of known sectors to explore";
  315. }
  316. BUGZ_LOG(warning) << "Prefer Ports: " + prefer_ports;
  317. }
  318. void ScriptExplore::activate() {
  319. us = director.chain;
  320. state = 1;
  321. to_server("I");
  322. /*
  323. director.chain = input;
  324. input->activate();
  325. if(director.galaxy.meta["ship"]) {
  326. if(!director.galaxy.meta["ship"]["scanner"]) {
  327. to_client("\n\rIt appears your ship doesn't have a long range
  328. scanner.\n\r"); deactivate();
  329. }
  330. }
  331. state = 1;
  332. return;
  333. */
  334. }
  335. void ScriptExplore::deactivate() {
  336. BUGZ_LOG(warning) << "ScriptExplore::deactivate()";
  337. us.reset();
  338. notify();
  339. }
  340. void ScriptExplore::move_notify() {
  341. director.chain = us;
  342. if (md->aborted) {
  343. deactivate();
  344. return;
  345. }
  346. if (md->success) {
  347. to_server("SD");
  348. state = 3;
  349. return;
  350. } else {
  351. if (unknown_warps.size() != 0) {
  352. BUGZ_LOG(warning) << "Seeking previous unexplored (Unsafe Dest.)";
  353. state = 4;
  354. target = unknown_warps.top();
  355. unknown_warps.pop();
  356. std::string message = "UNSAFE DESTINATION";
  357. ANSIColor alert(COLOR::WHITE, COLOR::RED, ATTR::BOLD);
  358. to_client(" " + alert() + message + reset() + "\n\r");
  359. BUGZ_LOG(warning) << "Target = " << target;
  360. next();
  361. //deactivate();
  362. } else {
  363. std::string message = "Move failed: " + md->why_failed + "\n\r";
  364. // to_client("No safe moves.\n\r");
  365. BUGZ_LOG(warning) << message;
  366. to_client(message);
  367. deactivate();
  368. }
  369. }
  370. }
  371. void ScriptExplore::input_notify() {
  372. director.chain = us;
  373. if (id->input.empty() || id->aborted) {
  374. to_client("Maybe next time.\n\r");
  375. deactivate();
  376. return;
  377. }
  378. loops = sstoi(id->input, -1);
  379. if (loops == -1) {
  380. to_client("I'm sorry, WHAT?\n\r");
  381. deactivate();
  382. return;
  383. }
  384. if (loops == 0) {
  385. infinite = true;
  386. } else {
  387. infinite = false;
  388. }
  389. id->input.clear();
  390. if (!infinite) {
  391. BUGZ_LOG(warning) << "Explore loops: " << loops;
  392. } else {
  393. to_client("Infinite Mode!\n\r");
  394. to_client("[ PRESS A KEY TO STOP ]\n\r");
  395. BUGZ_LOG(warning) << "Explore loops: INFINITE";
  396. }
  397. to_server("SD");
  398. state = 3;
  399. }
  400. bool has_port(const density d) {
  401. int den = d.density;
  402. if(d.navhaz <= 5) {
  403. den -= 21 * d.navhaz;
  404. }
  405. return den == 100 || den == 101;
  406. }
  407. bool sort_ports(const density a, const density b) {
  408. if(has_port(a) && has_port(b)) {
  409. return a.warps > b.warps;
  410. } else {
  411. if((has_port(a) && !has_port(b)) || (!has_port(a) && has_port(b))) {
  412. return has_port(a);
  413. }
  414. return a.warps > b.warps;
  415. }
  416. }
  417. bool sort_warps(const density a, const density b) {
  418. if((has_port(a) && !has_port(b)) || (!has_port(a) && has_port(b))) {
  419. return has_port(a) && a.warps > b.warps;
  420. }
  421. return a.warps > b.warps;
  422. }
  423. void ScriptExplore::next() {
  424. if (loops <= 0 && !infinite) {
  425. to_client("The exploration ends, for now.\n\r");
  426. deactivate();
  427. return;
  428. }
  429. if (!infinite) --loops;
  430. // Calculate next best sector to goto
  431. density_scan &ds = director.galaxy.dscan;
  432. density best_sector;
  433. best_sector.sector = 0;
  434. std::array<density, 6> sectors = ds.d;
  435. for(int x = 0; x < 6; ++x) {
  436. if(sectors[x].known || !density_clear(sectors[x].sector, sectors[x].density, sectors[x].navhaz)) {
  437. sectors[x].sector = 0;
  438. sectors[x].density = 0;
  439. sectors[x].warps = 0;
  440. sectors[x].navhaz = 0;
  441. sectors[x].known = false;
  442. sectors[x].anomaly = false;
  443. }
  444. }
  445. if(prefer_ports) {
  446. std::sort(sectors.begin(), sectors.end(), sort_ports);
  447. } else {
  448. std::sort(sectors.begin(), sectors.end(), sort_warps);
  449. }
  450. for(int x = 0; x < 6; ++x) {
  451. BUGZ_LOG(info) << "["
  452. << sectors[x].sector << ", "
  453. << sectors[x].density << ", "
  454. << sectors[x].warps << ", "
  455. << sectors[x].anomaly << ", "
  456. << sectors[x].known << "]";
  457. }
  458. BUGZ_LOG(warning) << "Best Sector: " << sectors[0].sector;
  459. best_sector = sectors[0];
  460. for(int x = 1; x < 6; ++x) {
  461. if(sectors[x].sector != 0 && sectors[x].sector != best_sector.sector) {
  462. unknown_warps.push(sectors[x].sector);
  463. BUGZ_LOG(info) << "Added " << sectors[x].sector << " to unknown_warps (" << unknown_warps.size() << ")";
  464. }
  465. }
  466. if (target != 0) {
  467. BUGZ_LOG(info) << "Using: " << target;
  468. best_sector.sector = target;
  469. target = 0;
  470. }
  471. BUGZ_LOG(warning) << "Unknown Warps: " << unknown_warps.size();
  472. if (best_sector.sector == 0) {
  473. if (unknown_warps.size() == 0) {
  474. if (!use_nearest_unknown) {
  475. to_client("No unknown warps.");
  476. deactivate();
  477. return;
  478. } else {
  479. BUGZ_LOG(warning) << "Seeking nearest unknown";
  480. std::string message = "Finding nearest unexplored";
  481. ANSIColor alert(COLOR::WHITE, COLOR::RED, ATTR::BOLD);
  482. to_client(" " + alert() + message + reset() + "\n\r");
  483. best_sector.sector = director.galaxy.find_nearest_unexplored(director.current_sector);
  484. if(best_sector.sector == 0) {
  485. to_client("No unknown warps in the galaxy!");
  486. deactivate();
  487. return;
  488. }
  489. }
  490. } else {
  491. BUGZ_LOG(warning) << "Seeking previous unexplored";
  492. best_sector.sector = unknown_warps.top();
  493. unknown_warps.pop();
  494. std::string message = "DEAD END";
  495. ANSIColor alert(COLOR::WHITE, COLOR::RED, ATTR::BOLD);
  496. to_client(" " + alert() + message + reset() + "\n\r");
  497. }
  498. }
  499. BUGZ_LOG(warning) << "Targeting sector: " << best_sector.sector;
  500. if (director.current_sector != best_sector.sector) {
  501. md->move_to = best_sector.sector;
  502. director.chain = move;
  503. director.chain->activate();
  504. } else {
  505. BUGZ_LOG(warning) << "Targeting current sector!";
  506. state = 3;
  507. to_server("SD");
  508. }
  509. }
  510. void ScriptExplore::server_prompt(const std::string &prompt) {
  511. BUGZ_LOG(info) << "Explorer State: SP " << state;
  512. // next();
  513. if (state == 1) {
  514. if (at_command_prompt(prompt)) {
  515. if (director.galaxy.meta.contains("ship")) {
  516. if (!director.galaxy.meta["ship"].contains("scanner")) {
  517. to_client(
  518. "\n\rIt appears your ship doesn't have a long range "
  519. "scanner.\n\r");
  520. deactivate();
  521. return;
  522. }
  523. }
  524. state = 2;
  525. BUGZ_LOG(info) << "state = 1, prompting for user input";
  526. director.chain = input;
  527. input->activate();
  528. return;
  529. }
  530. }
  531. if (state == 3) {
  532. if (at_command_prompt(prompt)) {
  533. state = 4;
  534. BUGZ_LOG(info) << "state = 3, calculating next sector";
  535. next();
  536. }
  537. }
  538. }
  539. ScriptPlanet::ScriptPlanet(Director &d, const char *called)
  540. : Dispatch(d, called) {
  541. BUGZ_LOG(warning) << "ScriptPlanet()";
  542. if (called == nullptr) name = "Planet";
  543. init();
  544. }
  545. ScriptPlanet::~ScriptPlanet() { BUGZ_LOG(warning) << "~ScriptPlanet()"; }
  546. void ScriptPlanet::init() {
  547. move = std::make_shared<MoveDispatch>(director, "PlanetMove");
  548. md = static_cast<MoveDispatch *>(&(*move));
  549. md->setNotify([this]() { this->move_notify(); });
  550. md->use_express = true;
  551. trader = std::make_shared<TraderDispatch>(director, "PlanetTrader");
  552. td = static_cast<TraderDispatch *>(&(*trader));
  553. td->setNotify([this]() { this->trade_notify(); });
  554. input = std::make_shared<InputDispatch>(director, "PlanetInput");
  555. id = static_cast<InputDispatch *>(&(*input));
  556. id->prompt = "Which planet would you like to upgrade => ";
  557. id->max_length = 3;
  558. id->numeric = true;
  559. id->setNotify([this]() { this->input_notify(); });
  560. state = 0;
  561. }
  562. void ScriptPlanet::activate() {
  563. us = director.chain;
  564. aborted = false;
  565. // FUTURE: handle special case here, where we activate at planet/citadel
  566. // prompt
  567. /*
  568. States:
  569. 1 = Team List Planets
  570. 2 = List Planets
  571. 3 = Input Which planet to upgrade?
  572. 4 = Move to Planet
  573. 5 = Land, get planet information
  574. 6 = CU Citadel build/upgrade
  575. 7 = Parse Citadel needs
  576. 8 = Construct/upgrade ?
  577. 9 = to terra!
  578. 10 = back to the planet!
  579. 11 = unloading...
  580. 12 = resources
  581. 13 = moving to buyer
  582. 14 = moving to planet
  583. 15 = product unloaded
  584. */
  585. state = 1;
  586. // clear out the planets list -- we're refreshing.
  587. director.galaxy.planets.clear();
  588. // get planet lists
  589. to_server("TLQ");
  590. }
  591. void ScriptPlanet::deactivate() {
  592. BUGZ_LOG(warning) << "ScriptPlanet::deactivate()";
  593. us.reset();
  594. notify();
  595. }
  596. void ScriptPlanet::clear_amounts(void) {
  597. // clear amounts
  598. for (int x = 0; x < 3; x++) {
  599. population[x] = 0;
  600. amount[x] = 0;
  601. needs[x] = 0;
  602. ship[x] = 0;
  603. to_make_one[x] = 0;
  604. }
  605. total_population = 0;
  606. support_construction = 0;
  607. days = 0;
  608. }
  609. /**
  610. * @brief Best place to put colonists
  611. *
  612. * Where is the best place to put colonists?
  613. *
  614. * That would be in production where they produce the most product.
  615. * Where the number to make one is the least.
  616. *
  617. * @return int
  618. */
  619. int ScriptPlanet::place_colonists(void) {
  620. int best = 1000;
  621. int pos = 0;
  622. for (int x = 0; x < 3; x++) {
  623. if (to_make_one[x] != 0) {
  624. if (to_make_one[x] < best) {
  625. best = to_make_one[x];
  626. pos = x;
  627. }
  628. }
  629. }
  630. return pos;
  631. }
  632. void ScriptPlanet::input_notify() {
  633. // deactivate();
  634. director.chain = us;
  635. if (id->input.empty()) {
  636. deactivate();
  637. return;
  638. }
  639. int selected = sstoi(id->input);
  640. if (selected == 0) {
  641. deactivate();
  642. return;
  643. }
  644. // find the planet in our list
  645. auto pos = director.galaxy.planets.find(selected);
  646. if (pos == director.galaxy.planets.end()) {
  647. to_client("Sorry, I can't find that planet #.\n\r");
  648. deactivate();
  649. return;
  650. }
  651. // Check planet level, if already max, don't bother!
  652. // Ok, we're off to planet # selected!
  653. planet = selected;
  654. sector = pos->second.sector;
  655. current_level = pos->second.level;
  656. if (director.current_sector == sector) {
  657. // We're already there!
  658. state = 5;
  659. // clear out numbers
  660. clear_amounts();
  661. // Landing ...
  662. to_server("L");
  663. return;
  664. } else {
  665. // Let's get to the planet
  666. state = 4;
  667. md->move_to = sector;
  668. director.chain = move;
  669. move->activate();
  670. return;
  671. }
  672. }
  673. void ScriptPlanet::move_notify() {
  674. director.chain = us;
  675. if (md->success) {
  676. // Ok, we're here
  677. if (state == 4) {
  678. state = 5;
  679. // clear out numbers
  680. clear_amounts();
  681. to_server("L");
  682. return;
  683. } else if (state == 9) {
  684. // Ok, we're at terra
  685. to_server("LT\r");
  686. return;
  687. } else if (state == 10) {
  688. // Back at the planet - Land and unload
  689. to_server("L");
  690. return;
  691. } else if (state == 13) {
  692. // Ok, we're here!
  693. td->port[0] = current_buyfrom;
  694. td->port[1] = 0;
  695. for (int x = 0; x < 3; x++) {
  696. td->port_buysell[0].foe[x] = false;
  697. td->port_buysell[1].foe[x] = false;
  698. td->trades.foe[x] = false;
  699. }
  700. td->port_buysell[0].foe[current_product] = true;
  701. td->trades.foe[current_product] = true;
  702. director.chain = trader;
  703. td->activate();
  704. return;
  705. } else if (state == 14) {
  706. // We're at the planet! Time to unload!
  707. to_server("L");
  708. return;
  709. }
  710. return;
  711. } else {
  712. std::string message = "MOVE FAILED: ";
  713. message.append(md->why_failed);
  714. message.append("\n\r");
  715. to_client(message);
  716. deactivate();
  717. return;
  718. }
  719. }
  720. void ScriptPlanet::trade_notify() {
  721. director.chain = us;
  722. if (td->success) {
  723. if (state == 13) {
  724. // Ok, we have what we came from, return to the planet.
  725. state = 14;
  726. md->move_to = sector;
  727. director.chain = move;
  728. move->activate();
  729. return;
  730. }
  731. } else {
  732. to_client("Trade failed.\n\r");
  733. deactivate();
  734. return;
  735. }
  736. }
  737. void ScriptPlanet::server_prompt(const std::string &prompt) {
  738. if (state == 1) {
  739. if (at_command_prompt(prompt)) {
  740. state = 2;
  741. to_server("CYQ");
  742. return;
  743. }
  744. } else if (state == 2) {
  745. if (at_command_prompt(prompt)) {
  746. state = 3;
  747. if (director.galaxy.planets.empty()) {
  748. to_client("Sorry, I don't see that you have any planets!\n\r");
  749. deactivate();
  750. return;
  751. }
  752. for (auto const &planet : director.galaxy.planets) {
  753. std::string text = str(
  754. boost::format("%1$3d <%2$5d> Class %3% Level %4% Name %5%\n\r") %
  755. planet.first % planet.second.sector % planet.second.c %
  756. planet.second.level % planet.second.name);
  757. to_client(text);
  758. }
  759. director.chain = input;
  760. director.chain->activate();
  761. return;
  762. }
  763. } else if (state == 5) {
  764. // Are they prompting us for which planet to land on?
  765. // SP: [Land on which planet <Q to abort> ? ]
  766. if (prompt == "Land on which planet <Q to abort> ? ") {
  767. std::string text = std::to_string(planet) + "\r";
  768. to_server(text);
  769. return;
  770. }
  771. // SP: [Planet command (?=help) [D] ]
  772. if (prompt == "Planet command (?=help) [D] ") {
  773. // Ok, we're here on the planet. (Did we capture the planet info on
  774. // landing?)
  775. BUGZ_LOG(fatal) << "Total population: " << total_population;
  776. // citadel, upgrade
  777. // what is the message when you don't have a citadel yet?
  778. state = 6;
  779. to_server("CU");
  780. return;
  781. }
  782. } else if (state == 8) {
  783. // SP: [Do you wish to construct one? ]
  784. if (startswith(prompt, "Do you wish to construct ") &&
  785. endswith(prompt, "? ")) {
  786. // Do we have what we need?
  787. bool ready = true;
  788. if (total_population < support_construction) {
  789. ready = false;
  790. state = 9;
  791. BUGZ_LOG(fatal) << "Need people ["
  792. << support_construction - total_population << "]";
  793. }
  794. for (int x = 0; x < 3; ++x) {
  795. if (needs[x] > amount[x]) {
  796. if (ready) {
  797. ready = false;
  798. state = 12;
  799. }
  800. BUGZ_LOG(fatal) << "Need " << x << " :" << needs[x] - amount[x];
  801. }
  802. }
  803. if (ready) {
  804. to_server("Y");
  805. deactivate();
  806. return;
  807. }
  808. // NNY!
  809. if (current_level > 0) {
  810. to_server("NQQ");
  811. } else
  812. to_server("NQ");
  813. // Ok, time to start moving!
  814. if (state == 9) {
  815. md->move_to = 1;
  816. director.chain = move;
  817. md->activate();
  818. return;
  819. }
  820. if (state == 12) {
  821. // Need resources
  822. current_product = -1;
  823. for (int x = 0; x < 3; ++x) {
  824. if (needs[x] > amount[x]) {
  825. current_product = x;
  826. break;
  827. }
  828. }
  829. if (current_product == -1) {
  830. // I think we have everything.
  831. BUGZ_LOG(fatal) << "I think we've got it...";
  832. to_server("CUY");
  833. deactivate();
  834. return;
  835. } else {
  836. // Ok, let's find where we need to go
  837. current_buyfrom = director.galaxy.find_nearest_selling(
  838. director.current_sector, current_product);
  839. if (current_buyfrom == 0) {
  840. to_client("We weren't able to locate a port selling.\n\r");
  841. deactivate();
  842. return;
  843. }
  844. state = 13;
  845. md->move_to = current_buyfrom;
  846. director.chain = move;
  847. md->activate();
  848. return;
  849. }
  850. }
  851. }
  852. } else if (state == 9) {
  853. if (at_command_prompt(prompt)) {
  854. // Ok! We have colonists ... back to the planet!
  855. state = 10;
  856. md->move_to = sector;
  857. director.chain = move;
  858. md->activate();
  859. return;
  860. }
  861. } else if (state == 10) {
  862. if (prompt == "Land on which planet <Q to abort> ? ") {
  863. std::string text = std::to_string(planet) + "\r";
  864. to_server(text);
  865. return;
  866. }
  867. if (prompt == "Planet command (?=help) [D] ") {
  868. // Ok, on the planet.
  869. state = 11;
  870. int place = place_colonists();
  871. std::string unload = "SNL";
  872. unload.append(std::to_string(place + 1));
  873. unload.append("\r");
  874. to_server(unload);
  875. return;
  876. }
  877. } else if (state == 11) {
  878. if (prompt == "Planet command (?=help) [D] ") {
  879. if (total_population < support_construction) {
  880. // Need More - we're not in citadel.
  881. to_server("Q");
  882. state = 9;
  883. md->move_to = 1;
  884. director.chain = move;
  885. md->activate();
  886. return;
  887. }
  888. // Ok, we're ready for the next step:
  889. // checking the resources.
  890. // Need resources
  891. current_product = -1;
  892. for (int x = 0; x < 3; ++x) {
  893. if (needs[x] > amount[x]) {
  894. current_product = x;
  895. break;
  896. }
  897. }
  898. if (current_product == -1) {
  899. // I think we have everything.
  900. BUGZ_LOG(fatal) << "I think we've got it...";
  901. to_server("CUY");
  902. deactivate();
  903. return;
  904. } else {
  905. // Ok, let's find where we need to go
  906. current_buyfrom = director.galaxy.find_nearest_selling(
  907. director.current_sector, current_product);
  908. if (current_buyfrom == 0) {
  909. to_client("We weren't able to locate a port selling.\n\r");
  910. deactivate();
  911. return;
  912. }
  913. to_server("Q");
  914. state = 13;
  915. md->move_to = current_buyfrom;
  916. director.chain = move;
  917. md->activate();
  918. return;
  919. }
  920. // ending here for now. (not in citadel)
  921. // to_server("Q");
  922. // deactivate();
  923. // return;
  924. }
  925. } else if (state == 14) {
  926. if (prompt == "Land on which planet <Q to abort> ? ") {
  927. std::string text = std::to_string(planet) + "\r";
  928. to_server(text);
  929. return;
  930. }
  931. if (prompt == "Planet command (?=help) [D] ") {
  932. state = 15;
  933. std::string command = "TNL";
  934. command.append(std::to_string(current_product + 1));
  935. command.append("\r");
  936. to_server(command);
  937. return;
  938. }
  939. } else if (state == 15) {
  940. if (prompt == "Planet command (?=help) [D] ") {
  941. // Ok, we're done unloading ... what do we need next?
  942. // Need resources
  943. current_product = -1;
  944. for (int x = 0; x < 3; ++x) {
  945. if (needs[x] > amount[x]) {
  946. current_product = x;
  947. break;
  948. }
  949. }
  950. if (current_product == -1) {
  951. // I think we have everything.
  952. BUGZ_LOG(fatal) << "I think we've got it...";
  953. to_server("CUY");
  954. deactivate();
  955. return;
  956. } else {
  957. // Ok, let's find where we need to go
  958. current_buyfrom = director.galaxy.find_nearest_selling(
  959. director.current_sector, current_product);
  960. if (current_buyfrom == 0) {
  961. to_client("We weren't able to locate a port selling.\n\r");
  962. deactivate();
  963. return;
  964. }
  965. to_server("Q");
  966. state = 13;
  967. md->move_to = current_buyfrom;
  968. director.chain = move;
  969. md->activate();
  970. return;
  971. }
  972. }
  973. }
  974. }
  975. void ScriptPlanet::server_line(const std::string &line,
  976. const std::string &raw_line) {
  977. // because I'm not sending this to the client, this is all hidden from them.
  978. if (state == 5) {
  979. // Save the planet information
  980. /*
  981. SL: [ ------- --------- --------- --------- --------- ---------
  982. ---------] SL: [Fuel Ore 4,950 2 2,475 28,135 0
  983. 200,000] SL: [Organics 0 5 0 100 0
  984. 200,000] SL: [Equipment 128 20 6 120 0
  985. 100,000] SL: [Fighters N/A 24 206 2,395 200
  986. 1,000,000]
  987. */
  988. std::string work = line;
  989. replace(work, "Fuel Ore", "Fuel");
  990. replace(work, ",", "");
  991. auto parts = split(work);
  992. if (parts.size() == 7) {
  993. int pos = -1;
  994. if (parts[0] == "Fuel") {
  995. pos = 0;
  996. } else if (parts[0] == "Organics") {
  997. pos = 1;
  998. } else if (parts[0] == "Equipment") {
  999. pos = 2;
  1000. }
  1001. // To save:
  1002. if (pos >= 0) {
  1003. population[pos] = sstoi(parts[1]);
  1004. to_make_one[pos] = sstoi(parts[2]);
  1005. total_population += population[pos];
  1006. amount[pos] = sstoi(parts[4]);
  1007. // What do we have on-board the ship?
  1008. ship[pos] = sstoi(parts[5]);
  1009. BUGZ_LOG(fatal) << pos << " : pop " << population[pos] << " 2M1 "
  1010. << to_make_one[pos] << " amount " << amount[pos]
  1011. << " ship: " << ship[pos];
  1012. }
  1013. }
  1014. } else if (state == 6) {
  1015. // [Be patient, your Citadel is not yet finished.]
  1016. if ((line == "Be patient, your Citadel is not yet finished.") ||
  1017. (startswith(line, "You may not upgrade while"))) {
  1018. // We're already upgrading!
  1019. std::string work = raw_line;
  1020. work += "\n\r";
  1021. to_client(work);
  1022. if (current_level > 0) {
  1023. to_server("QQ"); // exit citadel, exit planet
  1024. } else {
  1025. to_server("Q"); // exit planet
  1026. };
  1027. deactivate();
  1028. return;
  1029. }
  1030. // Citadel upgrade information (possibly)
  1031. /*
  1032. SL: [Citadel construction on this type of planet requires the following:]
  1033. SL: [ 400,000 Colonists to support the construction,]
  1034. SL: [ 150 units of Fuel Ore,]
  1035. SL: [ 100 units of Organics,]
  1036. SL: [ 150 units of Equipment and]
  1037. SL: [ 2 days to construct.]
  1038. [Construction of a Combat Control Computer on this type of planet requires]
  1039. */
  1040. if ((line == "Citadel construction on this type of planet requires the "
  1041. "following:") ||
  1042. (endswith(line, " on this type of planet requires"))) {
  1043. state = 7;
  1044. }
  1045. } else if (state == 7) {
  1046. if (line.empty()) {
  1047. state = 8;
  1048. } else {
  1049. // Display the upgrade information to the client
  1050. std::string work = raw_line;
  1051. work += "\n\r";
  1052. to_client(work);
  1053. work = line;
  1054. replace(work, ",", "");
  1055. trim(work);
  1056. auto pos = work.find(" Colonists to support");
  1057. if (pos != std::string::npos) {
  1058. support_construction = sstoi(work) / 1000;
  1059. return;
  1060. }
  1061. pos = work.find(" units of Fuel");
  1062. if (pos != std::string::npos) {
  1063. needs[0] = sstoi(work);
  1064. return;
  1065. }
  1066. pos = work.find(" units of Organics");
  1067. if (pos != std::string::npos) {
  1068. needs[1] = sstoi(work);
  1069. return;
  1070. }
  1071. pos = work.find(" units of Equipment");
  1072. if (pos != std::string::npos) {
  1073. needs[2] = sstoi(work);
  1074. return;
  1075. }
  1076. pos = work.find(" days to construct");
  1077. if (pos != std::string::npos) {
  1078. days = sstoi(work);
  1079. return;
  1080. }
  1081. }
  1082. } else if (state == 11) {
  1083. // SL: [How many groups of Colonists do you want to leave ([125] on board)
  1084. // ? ]
  1085. if (startswith(line, "How many groups of Colonists do you want to leave")) {
  1086. std::string work = line.substr(line.find('[') + 1);
  1087. int amount = sstoi(work);
  1088. total_population += amount;
  1089. BUGZ_LOG(fatal) << "Population now: " << total_population;
  1090. }
  1091. } else if (state == 15) {
  1092. // SL: [How many holds of Organics do you want to leave ([250] on board) ? ]
  1093. if (startswith(line, "How many holds of ") &&
  1094. endswith(line, "] on board) ? ")) {
  1095. std::string work = line.substr(line.find('[') + 1);
  1096. int amount_moved = sstoi(work);
  1097. amount[current_product] += amount_moved;
  1098. BUGZ_LOG(fatal) << "Planet " << current_product << " +" << amount_moved
  1099. << " = " << amount[current_product];
  1100. }
  1101. }
  1102. }
  1103. void ScriptPlanet::client_input(const std::string &input) { deactivate(); }