main.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. #include "door.h"
  2. #include "space.h"
  3. #include <chrono> // chrono::system_clock
  4. #include <ctime> // localtime
  5. #include <iomanip> // put_time
  6. #include <iostream>
  7. #include <random>
  8. #include <string>
  9. #include "db.h"
  10. #include "deck.h"
  11. /*
  12. Cards:
  13. 4 layers deep.
  14. https://en.wikipedia.org/wiki/Code_page_437
  15. using: \xb0, 0xb1, 0xb2, 0xdb
  16. OR: \u2591, \u2592, \u2593, \u2588
  17. Like so:
  18. ##### #####
  19. ##### #####
  20. ##### #####
  21. Cards: (Black on White, or Red on White)
  22. 8D### TH###
  23. ##D## ##H##
  24. ###D8 ###HT
  25. D, H = Red, Clubs, Spades = Black.
  26. ^ Where D = Diamonds, H = Hearts
  27. ♥, ♦, ♣, ♠
  28. \x03, \x04, \x05, \x06
  29. \u2665, \u2666, \u2663, \u2660
  30. Card layout. Actual cards are 3 lines thick.
  31. ░░░░░ ░░░░░ ░░░░░
  32. ░░░░░ ░░░░░ ░░░░░
  33. ▒▒▒▒▒░▒▒▒▒▒ #####░##### #####░#####
  34. ▒▒▒▒▒ ▒▒▒▒▒ ##### ##### ##### #####
  35. ▓▓▓▓▓▒▓▓▓▓▓▒▓▓▓▓▓ #####=#####=##### #####=#####=#####
  36. ▓▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓ ##### ##### ##### ##### ##### #####
  37. █████▓█████▓█████▓#####=#####=#####=#####=#####=#####=#####
  38. █████ █████ █████ ##### ##### ##### ##### ##### ##### #####
  39. █████ █████ █████ ##### ##### ##### ##### ##### ##### #####
  40. #####
  41. Player Information ##### Time in: xx Time out: xx
  42. Name: ##### Playing Day: November 3rd
  43. Hand Score : Current Streak: N
  44. Todays Score : XX Cards Remaining Longest Streak: NN
  45. Monthly Score: Playing Hand X of X Most Won: xxx Lost: xxx
  46. [4] Lf [6] Rt [Space] Play Card [Enter] Draw [D]one [H]elp [R]edraw
  47. ►, ◄, ▲, ▼
  48. \x10, \x11, \x1e, \x1f
  49. \u25ba, \u25c4, \u25b2, \u25bc
  50. The Name is <- 6 to the left.
  51. # is back of card. = is upper card showing between.
  52. There's no fancy anything here. Cards overlap the last
  53. line of the previous line/card.
  54. */
  55. // The idea is that this would be defined elsewhere (maybe)
  56. int user_score = 0;
  57. // NOTE: When run as a door, we always detect CP437 (because that's what Enigma
  58. // is defaulting to)
  59. void adjust_score(int by) { user_score += by; }
  60. door::Panel make_timeout(int mx, int my) {
  61. door::ANSIColor yellowred =
  62. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::RED, door::ATTR::BOLD);
  63. std::string line_text("Sorry, you've been inactive for too long.");
  64. int msgWidth = line_text.length() + (2 * 3); // + padding * 2
  65. door::Panel timeout((mx - (msgWidth)) / 2, my / 2 + 4, msgWidth);
  66. // place.setTitle(std::make_unique<door::Line>(title), 1);
  67. timeout.setStyle(door::BorderStyle::DOUBLE);
  68. timeout.setColor(yellowred);
  69. door::Line base(line_text);
  70. base.setColor(yellowred);
  71. std::string pad1(3, ' ');
  72. /*
  73. std::string pad1(3, '\xb0');
  74. if (door::unicode) {
  75. std::string unicode;
  76. door::cp437toUnicode(pad1.c_str(), unicode);
  77. pad1 = unicode;
  78. }
  79. */
  80. base.setPadding(pad1, yellowred);
  81. // base.setColor(door::ANSIColor(door::COLOR::GREEN, door::COLOR::BLACK));
  82. std::unique_ptr<door::Line> stuff = std::make_unique<door::Line>(base);
  83. timeout.addLine(std::make_unique<door::Line>(base));
  84. return timeout;
  85. }
  86. door::Panel make_notime(int mx, int my) {
  87. door::ANSIColor yellowred =
  88. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::RED, door::ATTR::BOLD);
  89. std::string line_text("Sorry, you've used up all your time for today.");
  90. int msgWidth = line_text.length() + (2 * 3); // + padding * 2
  91. door::Panel timeout((mx - (msgWidth)) / 2, my / 2 + 4, msgWidth);
  92. // place.setTitle(std::make_unique<door::Line>(title), 1);
  93. timeout.setStyle(door::BorderStyle::DOUBLE);
  94. timeout.setColor(yellowred);
  95. door::Line base(line_text);
  96. base.setColor(yellowred);
  97. std::string pad1(3, ' ');
  98. /*
  99. std::string pad1(3, '\xb0');
  100. if (door::unicode) {
  101. std::string unicode;
  102. door::cp437toUnicode(pad1.c_str(), unicode);
  103. pad1 = unicode;
  104. }
  105. */
  106. base.setPadding(pad1, yellowred);
  107. // base.setColor(door::ANSIColor(door::COLOR::GREEN, door::COLOR::BLACK));
  108. std::unique_ptr<door::Line> stuff = std::make_unique<door::Line>(base);
  109. timeout.addLine(std::make_unique<door::Line>(base));
  110. return timeout;
  111. }
  112. door::Menu make_main_menu(void) {
  113. door::Menu m(5, 5, 25);
  114. door::Line mtitle("Space-Ace Main Menu");
  115. door::ANSIColor border_color(door::COLOR::CYAN, door::COLOR::BLUE);
  116. door::ANSIColor title_color(door::COLOR::CYAN, door::COLOR::BLUE,
  117. door::ATTR::BOLD);
  118. m.setColor(border_color);
  119. mtitle.setColor(title_color);
  120. mtitle.setPadding(" ", title_color);
  121. m.setTitle(std::make_unique<door::Line>(mtitle), 1);
  122. // m.setColorizer(true,
  123. m.setRender(true, door::Menu::makeRender(
  124. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  125. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
  126. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  127. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD)));
  128. // m.setColorizer(false,
  129. m.setRender(false, door::Menu::makeRender(
  130. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  131. door::ATTR::BOLD),
  132. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  133. door::ATTR::BOLD),
  134. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  135. door::ATTR::BOLD),
  136. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE,
  137. door::ATTR::BOLD)));
  138. m.addSelection('1', "Play Cards");
  139. m.addSelection('2', "View Scores");
  140. m.addSelection('3', "Configure");
  141. m.addSelection('H', "Help");
  142. m.addSelection('A', "About this game");
  143. m.addSelection('Q', "Quit");
  144. return m;
  145. }
  146. door::renderFunction statusValue(door::ANSIColor status,
  147. door::ANSIColor value) {
  148. door::renderFunction rf = [status,
  149. value](const std::string &txt) -> door::Render {
  150. door::Render r(txt);
  151. door::ColorOutput co;
  152. co.pos = 0;
  153. co.len = 0;
  154. co.c = status;
  155. size_t pos = txt.find(':');
  156. if (pos == std::string::npos) {
  157. // failed to find - use entire string as status color.
  158. co.len = txt.length();
  159. r.outputs.push_back(co);
  160. } else {
  161. pos++; // Have : in status color
  162. co.len = pos;
  163. r.outputs.push_back(co);
  164. co.reset();
  165. co.pos = pos;
  166. co.c = value;
  167. co.len = txt.length() - pos;
  168. r.outputs.push_back(co);
  169. }
  170. return r;
  171. };
  172. return rf;
  173. }
  174. door::renderFunction rStatus = [](const std::string &txt) -> door::Render {
  175. door::Render r(txt);
  176. door::ColorOutput co;
  177. // default colors STATUS: value
  178. door::ANSIColor status(door::COLOR::BLUE, door::ATTR::BOLD);
  179. door::ANSIColor value(door::COLOR::YELLOW, door::ATTR::BOLD);
  180. co.pos = 0;
  181. co.len = 0;
  182. co.c = status;
  183. size_t pos = txt.find(':');
  184. if (pos == std::string::npos) {
  185. // failed to find - use entire string as status color.
  186. co.len = txt.length();
  187. r.outputs.push_back(co);
  188. } else {
  189. pos++; // Have : in status color
  190. co.len = pos;
  191. r.outputs.push_back(co);
  192. co.reset();
  193. co.pos = pos;
  194. co.c = value;
  195. co.len = txt.length() - pos;
  196. r.outputs.push_back(co);
  197. }
  198. return r;
  199. };
  200. std::string return_current_time_and_date() {
  201. auto now = std::chrono::system_clock::now();
  202. auto in_time_t = std::chrono::system_clock::to_time_t(now);
  203. std::stringstream ss;
  204. // ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %X");
  205. ss << std::put_time(std::localtime(&in_time_t), "%Y-%m-%d %r");
  206. return ss.str();
  207. }
  208. int press_a_key(door::Door &door) {
  209. door << door::reset << "Press a key to continue...";
  210. int r = door.sleep_key(door.inactivity);
  211. door << door::nl;
  212. return r;
  213. }
  214. door::Menu make_config_menu(void) {
  215. door::Menu m(5, 5, 31);
  216. door::Line mtitle("Space-Ace Configuration Menu");
  217. door::ANSIColor border_color(door::COLOR::CYAN, door::COLOR::BLUE);
  218. door::ANSIColor title_color(door::COLOR::CYAN, door::COLOR::BLUE,
  219. door::ATTR::BOLD);
  220. m.setColor(border_color);
  221. mtitle.setColor(title_color);
  222. mtitle.setPadding(" ", title_color);
  223. m.setTitle(std::make_unique<door::Line>(mtitle), 1);
  224. // m.setColorizer(true,
  225. m.setRender(true, door::Menu::makeRender(
  226. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  227. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
  228. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  229. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD)));
  230. // m.setColorizer(false,
  231. m.setRender(false, door::Menu::makeRender(
  232. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  233. door::ATTR::BOLD),
  234. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  235. door::ATTR::BOLD),
  236. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  237. door::ATTR::BOLD),
  238. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE,
  239. door::ATTR::BOLD)));
  240. m.addSelection('D', "Deck Colors");
  241. m.addSelection('Q', "Quit");
  242. return m;
  243. }
  244. door::Menu make_deck_menu(void) {
  245. door::Menu m(5, 5, 31);
  246. door::Line mtitle("Space-Ace Deck Menu");
  247. door::ANSIColor border_color(door::COLOR::CYAN, door::COLOR::BLUE);
  248. door::ANSIColor title_color(door::COLOR::CYAN, door::COLOR::BLUE,
  249. door::ATTR::BOLD);
  250. m.setColor(border_color);
  251. mtitle.setColor(title_color);
  252. mtitle.setPadding(" ", title_color);
  253. m.setTitle(std::make_unique<door::Line>(mtitle), 1);
  254. // m.setColorizer(true,
  255. m.setRender(true, door::Menu::makeRender(
  256. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  257. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
  258. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  259. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD)));
  260. // m.setColorizer(false,
  261. m.setRender(false, door::Menu::makeRender(
  262. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  263. door::ATTR::BOLD),
  264. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  265. door::ATTR::BOLD),
  266. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  267. door::ATTR::BOLD),
  268. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE,
  269. door::ATTR::BOLD)));
  270. m.addSelection('A', "All");
  271. m.addSelection('B', "Blue");
  272. m.addSelection('C', "Cyan");
  273. m.addSelection('G', "Green");
  274. m.addSelection('M', "Magenta");
  275. m.addSelection('R', "Red");
  276. return m;
  277. }
  278. int configure(door::Door &door, DBData &db) {
  279. auto menu = make_config_menu();
  280. int r = 0;
  281. while (r >= 0) {
  282. r = menu.choose(door);
  283. if (r > 0) {
  284. door << door::reset << door::cls;
  285. char c = menu.which(r - 1);
  286. if (c == 'D') {
  287. // Ok, deck colors
  288. door << door::reset << door::cls;
  289. auto deck = make_deck_menu();
  290. deck.choose(door);
  291. door << door::reset << door::cls;
  292. }
  293. if (c == 'Q') {
  294. return r;
  295. }
  296. }
  297. }
  298. return r;
  299. }
  300. int play_cards(door::Door &door, std::mt19937 &rng) {
  301. int mx = door.width;
  302. int my = door.height;
  303. // cards color --
  304. // configured by the player.
  305. door::ANSIColor deck_color;
  306. // RED, BLUE, GREEN, MAGENTA, CYAN
  307. std::uniform_int_distribution<int> rand_color(0, 4);
  308. switch (rand_color(rng)) {
  309. case 0:
  310. deck_color = door::ANSIColor(door::COLOR::RED);
  311. break;
  312. case 1:
  313. deck_color = door::ANSIColor(door::COLOR::BLUE);
  314. break;
  315. case 2:
  316. deck_color = door::ANSIColor(door::COLOR::GREEN);
  317. break;
  318. case 3:
  319. deck_color = door::ANSIColor(door::COLOR::MAGENTA);
  320. break;
  321. case 4:
  322. deck_color = door::ANSIColor(door::COLOR::CYAN);
  323. break;
  324. default:
  325. deck_color = door::ANSIColor(door::COLOR::BLUE, door::ATTR::BLINK);
  326. break;
  327. }
  328. int height = 3;
  329. Deck d(deck_color, height);
  330. door::Panel *c;
  331. door << door::reset << door::cls;
  332. // This displays the cards in the upper left corner.
  333. // We want them center, and down some.
  334. int space = 3;
  335. // int cards_dealt_width = 59; int cards_dealt_height = 9;
  336. int game_width;
  337. {
  338. int cx, cy, level;
  339. cardgo(27, space, height, cx, cy, level);
  340. game_width = cx + 5; // card width
  341. }
  342. int off_x = (mx - game_width) / 2;
  343. int off_y = (my - 9) / 2;
  344. // The idea is to see the cards with <<Something Unique to the card game>>,
  345. // Year, Month, Day, and game (like 1 of 3).
  346. // This will make the games the same/fair for everyone.
  347. std::seed_seq s1{2021, 2, 27, 1};
  348. cards deck1 = card_shuffle(s1, 1);
  349. cards state = card_states();
  350. // I tried setting the cursor before the delay and before displaying the card.
  351. // It is very hard to see / just about useless. Not worth the effort.
  352. for (int x = 0; x < 28; x++) {
  353. int cx, cy, level;
  354. cardgo(x, space, height, cx, cy, level);
  355. // This is hardly visible.
  356. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  357. std::this_thread::sleep_for(std::chrono::milliseconds(75));
  358. c = d.back(level);
  359. c->set(cx + off_x, cy + off_y);
  360. door << *c;
  361. }
  362. /*
  363. std::this_thread::sleep_for(
  364. std::chrono::seconds(1)); // 3 secs seemed too long!
  365. */
  366. for (int x = 18; x < 28; x++) {
  367. int cx, cy, level;
  368. // usleep(1000 * 20);
  369. state.at(x) = 1;
  370. cardgo(x, space, height, cx, cy, level);
  371. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  372. std::this_thread::sleep_for(std::chrono::milliseconds(200));
  373. c = d.card(deck1.at(x));
  374. c->set(cx + off_x, cy + off_y);
  375. door << *c;
  376. }
  377. door << door::reset;
  378. door << door::nl << door::nl;
  379. int r = door.sleep_key(door.inactivity);
  380. return r;
  381. }
  382. door::Panel make_about(void) {
  383. door::Panel about(2, 2, 60);
  384. about.setStyle(door::BorderStyle::DOUBLE_SINGLE);
  385. about.setColor(door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  386. door::ATTR::BOLD));
  387. about.addLine(std::make_unique<door::Line>("About This Door", 60));
  388. /*
  389. door::Line magic("---------------------------------", 60);
  390. magic.setColor(door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLACK,
  391. door::ATTR::BOLD));
  392. */
  393. about.addLine(std::make_unique<door::Line>(
  394. "---------------------------------", 60,
  395. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE, door::ATTR::BOLD)));
  396. /*
  397. 123456789012345678901234567890123456789012345678901234567890
  398. This door was written by Bugz.
  399. It is written in c++, only supports Linux, and replaces
  400. opendoors.
  401. It's written in c++, and replaces the outdated opendoors
  402. library.
  403. */
  404. about.addLine(
  405. std::make_unique<door::Line>("This door was written by Bugz.", 60));
  406. about.addLine(std::make_unique<door::Line>("", 60));
  407. about.addLine(std::make_unique<door::Line>(
  408. "It is written in c++, only support Linux, and replaces", 60));
  409. about.addLine(std::make_unique<door::Line>("opendoors.", 60));
  410. /*
  411. door::updateFunction updater = [](void) -> std::string {
  412. std::string text = "Currently: ";
  413. text.append(return_current_time_and_date());
  414. return text;
  415. };
  416. std::string current = updater();
  417. door::Line active(current, 60);
  418. active.setUpdater(updater);
  419. active.setRender(renderStatusValue(
  420. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  421. door::ATTR::BOLD), door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  422. door::ATTR::BOLD)));
  423. about.addLine(std::make_unique<door::Line>(active));
  424. */
  425. /*
  426. about.addLine(std::make_unique<door::Line>(
  427. "Status: blue", 60,
  428. statusValue(door::ANSIColor(door::COLOR::GREEN, door::ATTR::BOLD),
  429. door::ANSIColor(door::COLOR::MAGENTA, door::ATTR::BLINK))));
  430. about.addLine(std::make_unique<door::Line>("Name: BUGZ", 60, rStatus));
  431. about.addLine(std::make_unique<door::Line>(
  432. "Size: 10240", 60,
  433. statusValue(door::ANSIColor(door::COLOR::GREEN, door::COLOR::BLUE,
  434. door::ATTR::BOLD),
  435. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  436. door::ATTR::BOLD, door::ATTR::BLINK))));
  437. about.addLine(std::make_unique<door::Line>("Bugz is here.", 60, rStatus));
  438. */
  439. return about;
  440. }
  441. void display_starfield(door::Door &door, std::mt19937 &rng) {
  442. door << door::reset << door::cls;
  443. int mx = door.width;
  444. int my = door.height;
  445. // display starfield
  446. const char *stars[2];
  447. stars[0] = ".";
  448. if (door::unicode) {
  449. stars[1] = "\u2219"; // "\u00b7";
  450. } else {
  451. stars[1] = "\xf9"; // "\xfa";
  452. };
  453. {
  454. // Make uniform random distribution between 1 and MAX screen size X/Y
  455. std::uniform_int_distribution<int> uni_x(1, mx);
  456. std::uniform_int_distribution<int> uni_y(1, my);
  457. door::ANSIColor white(door::COLOR::WHITE);
  458. door::ANSIColor dark(door::COLOR::BLACK, door::ATTR::BRIGHT);
  459. // 10 is too many, 100 is too few. 40 looks ok.
  460. int MAX_STARS = ((mx * my) / 40);
  461. // door.log() << "Generating starmap using " << mx << "," << my << " : "
  462. // << MAX_STARS << " stars." << std::endl;
  463. for (int x = 0; x < MAX_STARS; x++) {
  464. door::Goto star_at(uni_x(rng), uni_y(rng));
  465. door << star_at;
  466. if (x % 5 < 2)
  467. door << dark;
  468. else
  469. door << white;
  470. if (x % 2 == 0)
  471. door << stars[0];
  472. else
  473. door << stars[1];
  474. }
  475. }
  476. }
  477. void display_space_ace(door::Door &door) {
  478. int mx = door.width;
  479. int my = door.height;
  480. // space_ace is 72 chars wide, 6 high
  481. int sa_x = (mx - 72) / 2;
  482. int sa_y = (my - 6) / 2;
  483. // output the SpaceAce logo -- centered!
  484. for (const auto s : space) {
  485. door::Goto sa_at(sa_x, sa_y);
  486. door << sa_at;
  487. if (door::unicode) {
  488. std::string unicode;
  489. door::cp437toUnicode(s, unicode);
  490. door << unicode; // << door::nl;
  491. } else
  492. door << s; // << door::nl;
  493. sa_y++;
  494. }
  495. // pause 5 seconds so they can enjoy our awesome logo
  496. door.sleep_key(5);
  497. }
  498. void display_starfield_space_ace(door::Door &door, std::mt19937 &rng) {
  499. // mx = door.width;
  500. // my = door.height;
  501. display_starfield(door, rng);
  502. display_space_ace(door);
  503. door << door::reset;
  504. }
  505. int main(int argc, char *argv[]) {
  506. door::Door door("space-ace", argc, argv);
  507. // door << door::reset << door::cls << door::nl;
  508. DBData spacedb;
  509. /*
  510. std::function<std::ofstream &(void)> get_logger;
  511. get_logger = [&door]() -> ofstream & { return door.log(); };
  512. if (get_logger) {
  513. get_logger() << "MEOW" << std::endl;
  514. get_logger() << "hey! It works!" << std::endl;
  515. }
  516. */
  517. // spacedb.init();
  518. /*
  519. // Example: How to read/set values in spacedb settings.
  520. std::string setting = "last_play";
  521. std::string user = door.username;
  522. std::string value;
  523. std::string blank = "<blank>";
  524. value = spacedb.getSetting(user, setting, blank);
  525. door << door::reset << "last_play: " << value << door::nl;
  526. std::this_thread::sleep_for(std::chrono::seconds(2));
  527. value = return_current_time_and_date();
  528. spacedb.setSetting(user, setting, value);
  529. */
  530. // https://stackoverflow.com/questions/5008804/generating-random-integer-from-a-range
  531. std::random_device rd; // only used once to initialise (seed) engine
  532. std::mt19937 rng(rd());
  533. // random-number engine used (Mersenne-Twister in this case)
  534. // std::uniform_int_distribution<int> uni(min, max); // guaranteed unbiased
  535. int mx, my; // Max screen width/height
  536. if (door.width == 0) {
  537. // screen detect failed, use sensible defaults
  538. door.width = mx = 80;
  539. door.height = my = 23;
  540. } else {
  541. mx = door.width;
  542. my = door.height;
  543. }
  544. // We assume here that the width and height are something crazy like 10x15. :P
  545. display_starfield_space_ace(door, rng);
  546. // for testing inactivity timeout
  547. // door.inactivity = 10;
  548. door::Panel timeout = make_timeout(mx, my);
  549. door::Menu m = make_main_menu();
  550. door::Panel about = make_about();
  551. // center the about box
  552. about.set((mx - 60) / 2, (my - 5) / 2);
  553. int r = 0;
  554. while ((r >= 0) and (r != 6)) {
  555. // starfield + menu ?
  556. display_starfield(door, rng);
  557. r = m.choose(door);
  558. // need to reset the colors. (whoops!)
  559. door << door::reset << door::cls; // door::nl;
  560. // OK! The screen is blank at this point!
  561. switch (r) {
  562. case 1: // play game
  563. r = play_cards(door, rng);
  564. break;
  565. case 2: // view scores
  566. door << "Show scores goes here!" << door::nl;
  567. r = press_a_key(door);
  568. break;
  569. case 3: // configure
  570. r = configure(door, spacedb);
  571. // r = press_a_key(door);
  572. break;
  573. case 4: // help
  574. door << "Help! Need some help here..." << door::nl;
  575. r = press_a_key(door);
  576. break;
  577. case 5: // about
  578. display_starfield(door, rng);
  579. door << about << door::nl;
  580. r = press_a_key(door);
  581. break;
  582. case 6: // quit
  583. break;
  584. }
  585. }
  586. if (r < 0) {
  587. TIMEOUT:
  588. if (r == -1) {
  589. door.log() << "TIMEOUT" << std::endl;
  590. door << timeout << door::reset << door::nl << door::nl;
  591. } else {
  592. if (r == -3) {
  593. door.log() << "OUTTA TIME" << std::endl;
  594. door::Panel notime = make_notime(mx, my);
  595. door << notime << door::reset << door::nl;
  596. }
  597. }
  598. return 0;
  599. }
  600. door << door::nl;
  601. /*
  602. // magic time!
  603. door << door::reset << door::nl << "Press another key...";
  604. int x;
  605. for (x = 0; x < 60; ++x) {
  606. r = door.sleep_key(1);
  607. if (r == -1) {
  608. // ok! Expected timeout!
  609. // PROBLEM: regular "local" terminal loses current attributes
  610. // when cursor is save / restored.
  611. door << door::SaveCursor;
  612. if (about.update(door)) {
  613. // ok I need to "fix" the cursor position.
  614. // it has moved.
  615. }
  616. door << door::RestoreCursor << door::reset;
  617. } else {
  618. if (r < 0)
  619. goto TIMEOUT;
  620. if (r >= 0)
  621. break;
  622. }
  623. }
  624. if (x == 60)
  625. goto TIMEOUT;
  626. */
  627. door << door::nl;
  628. #ifdef NNY
  629. // configured by the player.
  630. door::ANSIColor deck_color;
  631. // RED, BLUE, GREEN, MAGENTA, CYAN
  632. std::uniform_int_distribution<int> rand_color(0, 4);
  633. switch (rand_color(rng)) {
  634. case 0:
  635. deck_color = door::ANSIColor(door::COLOR::RED);
  636. break;
  637. case 1:
  638. deck_color = door::ANSIColor(door::COLOR::BLUE);
  639. break;
  640. case 2:
  641. deck_color = door::ANSIColor(door::COLOR::GREEN);
  642. break;
  643. case 3:
  644. deck_color = door::ANSIColor(door::COLOR::MAGENTA);
  645. break;
  646. case 4:
  647. deck_color = door::ANSIColor(door::COLOR::CYAN);
  648. break;
  649. default:
  650. deck_color = door::ANSIColor(door::COLOR::BLUE, door::ATTR::BLINK);
  651. break;
  652. }
  653. int height = 3;
  654. Deck d(deck_color, height);
  655. door::Panel *c;
  656. door << door::reset << door::cls;
  657. // This displays the cards in the upper left corner.
  658. // We want them center, and down some.
  659. int space = 3;
  660. // int cards_dealt_width = 59; int cards_dealt_height = 9;
  661. int game_width;
  662. {
  663. int cx, cy, level;
  664. cardgo(27, space, height, cx, cy, level);
  665. game_width = cx + 5; // card width
  666. }
  667. int off_x = (mx - game_width) / 2;
  668. int off_y = (my - 9) / 2;
  669. // The idea is to see the cards with <<Something Unique to the card game>>,
  670. // Year, Month, Day, and game (like 1 of 3).
  671. // This will make the games the same/fair for everyone.
  672. std::seed_seq s1{2021, 2, 27, 1};
  673. cards deck1 = card_shuffle(s1, 1);
  674. cards state = card_states();
  675. // I tried setting the cursor before the delay and before displaying the card.
  676. // It is very hard to see / just about useless. Not worth the effort.
  677. for (int x = 0; x < 28; x++) {
  678. int cx, cy, level;
  679. cardgo(x, space, height, cx, cy, level);
  680. // This is hardly visible.
  681. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  682. std::this_thread::sleep_for(std::chrono::milliseconds(75));
  683. c = d.back(level);
  684. c->set(cx + off_x, cy + off_y);
  685. door << *c;
  686. }
  687. /*
  688. std::this_thread::sleep_for(
  689. std::chrono::seconds(1)); // 3 secs seemed too long!
  690. */
  691. for (int x = 18; x < 28; x++) {
  692. int cx, cy, level;
  693. // usleep(1000 * 20);
  694. state.at(x) = 1;
  695. cardgo(x, space, height, cx, cy, level);
  696. // door << door::Goto(cx + off_x - 1, cy + off_y + 1);
  697. std::this_thread::sleep_for(std::chrono::milliseconds(200));
  698. c = d.card(deck1.at(x));
  699. c->set(cx + off_x, cy + off_y);
  700. door << *c;
  701. }
  702. door << door::reset;
  703. door << door::nl << door::nl;
  704. r = door.sleep_key(door.inactivity);
  705. if (r < 0)
  706. goto TIMEOUT;
  707. #endif
  708. /*
  709. door::Panel *p = d.back(2);
  710. p->set(10, 10);
  711. door << *p;
  712. door::Panel *d8 = d.card(8);
  713. d8->set(20, 8);
  714. door << *d8;
  715. r = door.sleep_key(door.inactivity);
  716. if (r < 0)
  717. goto TIMEOUT;
  718. */
  719. // door << door::reset << door::cls;
  720. display_starfield(door, rng);
  721. door << m << door::reset << door::nl;
  722. // Normal DOOR exit goes here...
  723. door << door::nl << "Returning you to the BBS, please wait..." << door::nl;
  724. return 0;
  725. }