main.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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 "deck.h"
  10. /*
  11. Cards:
  12. 4 layers deep.
  13. https://en.wikipedia.org/wiki/Code_page_437
  14. using: \xb0, 0xb1, 0xb2, 0xdb
  15. OR: \u2591, \u2592, \u2593, \u2588
  16. Like so:
  17. ##### #####
  18. ##### #####
  19. ##### #####
  20. Cards: (Black on White, or Red on White)
  21. 8D### TH###
  22. ##D## ##H##
  23. ###D8 ###HT
  24. D, H = Red, Clubs, Spades = Black.
  25. ^ Where D = Diamonds, H = Hearts
  26. ♥, ♦, ♣, ♠
  27. \x03, \x04, \x05, \x06
  28. \u2665, \u2666, \u2663, \u2660
  29. Card layout. Actual cards are 3 lines thick.
  30. ░░░░░ ░░░░░ ░░░░░
  31. ░░░░░ ░░░░░ ░░░░░
  32. ▒▒▒▒▒░▒▒▒▒▒ #####░##### #####░#####
  33. ▒▒▒▒▒ ▒▒▒▒▒ ##### ##### ##### #####
  34. ▓▓▓▓▓▒▓▓▓▓▓▒▓▓▓▓▓ #####=#####=##### #####=#####=#####
  35. ▓▓▓▓▓ ▓▓▓▓▓ ▓▓▓▓▓ ##### ##### ##### ##### ##### #####
  36. █████▓█████▓█████▓#####=#####=#####=#####=#####=#####=#####
  37. █████ █████ █████ ##### ##### ##### ##### ##### ##### #####
  38. █████ █████ █████ ##### ##### ##### ##### ##### ##### #####
  39. #####
  40. Player Information ##### Time in: xx Time out: xx
  41. Name: ##### Playing Day: November 3rd
  42. Hand Score : Current Streak: N
  43. Todays Score : XX Cards Remaining Longest Streak: NN
  44. Monthly Score: Playing Hand X of X Most Won: xxx Lost: xxx
  45. [4] Lf [6] Rt [Space] Play Card [Enter] Draw [D]one [H]elp [R]edraw
  46. ►, ◄, ▲, ▼
  47. \x10, \x11, \x1e, \x1f
  48. \u25ba, \u25c4, \u25b2, \u25bc
  49. The Name is <- 6 to the left.
  50. # is back of card. = is upper card showing between.
  51. There's no fancy anything here. Cards overlap the last
  52. line of the previous line/card.
  53. */
  54. // The idea is that this would be defined elsewhere (maybe)
  55. int user_score = 0;
  56. // NOTE: When run as a door, we always detect CP437 (because that's what Enigma
  57. // is defaulting to)
  58. void adjust_score(int by) { user_score += by; }
  59. door::Panel make_timeout(int mx, int my) {
  60. door::ANSIColor yellowred =
  61. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::RED, door::ATTR::BOLD);
  62. std::string line_text("Sorry, you've been inactive for too long.");
  63. int msgWidth = line_text.length() + (2 * 3); // + padding * 2
  64. door::Panel timeout((mx - (msgWidth)) / 2, my / 2 + 4, msgWidth);
  65. // place.setTitle(std::make_unique<door::Line>(title), 1);
  66. timeout.setStyle(door::BorderStyle::DOUBLE);
  67. timeout.setColor(yellowred);
  68. door::Line base(line_text);
  69. base.setColor(yellowred);
  70. std::string pad1(3, ' ');
  71. /*
  72. std::string pad1(3, '\xb0');
  73. if (door::unicode) {
  74. std::string unicode;
  75. door::cp437toUnicode(pad1.c_str(), unicode);
  76. pad1 = unicode;
  77. }
  78. */
  79. base.setPadding(pad1, yellowred);
  80. // base.setColor(door::ANSIColor(door::COLOR::GREEN, door::COLOR::BLACK));
  81. std::unique_ptr<door::Line> stuff = std::make_unique<door::Line>(base);
  82. timeout.addLine(std::make_unique<door::Line>(base));
  83. return timeout;
  84. }
  85. door::Panel make_notime(int mx, int my) {
  86. door::ANSIColor yellowred =
  87. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::RED, door::ATTR::BOLD);
  88. std::string line_text("Sorry, you've used up all your time for today.");
  89. int msgWidth = line_text.length() + (2 * 3); // + padding * 2
  90. door::Panel timeout((mx - (msgWidth)) / 2, my / 2 + 4, msgWidth);
  91. // place.setTitle(std::make_unique<door::Line>(title), 1);
  92. timeout.setStyle(door::BorderStyle::DOUBLE);
  93. timeout.setColor(yellowred);
  94. door::Line base(line_text);
  95. base.setColor(yellowred);
  96. std::string pad1(3, ' ');
  97. /*
  98. std::string pad1(3, '\xb0');
  99. if (door::unicode) {
  100. std::string unicode;
  101. door::cp437toUnicode(pad1.c_str(), unicode);
  102. pad1 = unicode;
  103. }
  104. */
  105. base.setPadding(pad1, yellowred);
  106. // base.setColor(door::ANSIColor(door::COLOR::GREEN, door::COLOR::BLACK));
  107. std::unique_ptr<door::Line> stuff = std::make_unique<door::Line>(base);
  108. timeout.addLine(std::make_unique<door::Line>(base));
  109. return timeout;
  110. }
  111. door::Menu make_main_menu(void) {
  112. door::Menu m(5, 5, 25);
  113. door::Line mtitle("Space-Ace Main Menu");
  114. door::ANSIColor border_color(door::COLOR::CYAN, door::COLOR::BLUE);
  115. door::ANSIColor title_color(door::COLOR::CYAN, door::COLOR::BLUE,
  116. door::ATTR::BOLD);
  117. m.setColor(border_color);
  118. mtitle.setColor(title_color);
  119. mtitle.setPadding(" ", title_color);
  120. m.setTitle(std::make_unique<door::Line>(mtitle), 1);
  121. // m.setColorizer(true,
  122. m.setRender(true, door::Menu::makeRender(
  123. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  124. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD),
  125. door::ANSIColor(door::COLOR::CYAN, door::ATTR::BOLD),
  126. door::ANSIColor(door::COLOR::BLUE, door::ATTR::BOLD)));
  127. // m.setColorizer(false,
  128. m.setRender(false, door::Menu::makeRender(
  129. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  130. door::ATTR::BOLD),
  131. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE,
  132. door::ATTR::BOLD),
  133. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  134. door::ATTR::BOLD),
  135. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE,
  136. door::ATTR::BOLD)));
  137. m.addSelection('1', "Play Cards");
  138. m.addSelection('2', "View Scores");
  139. m.addSelection('3', "Drop to DOS");
  140. m.addSelection('4', "Chat with BUGZ");
  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. door::Panel make_about(void) {
  209. door::Panel about(2, 2, 60);
  210. about.setStyle(door::BorderStyle::DOUBLE_SINGLE);
  211. about.setColor(door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  212. door::ATTR::BOLD));
  213. about.addLine(std::make_unique<door::Line>("About This Door", 60));
  214. /*
  215. door::Line magic("---------------------------------", 60);
  216. magic.setColor(door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLACK,
  217. door::ATTR::BOLD));
  218. */
  219. about.addLine(std::make_unique<door::Line>(
  220. "---------------------------------", 60,
  221. door::ANSIColor(door::COLOR::CYAN, door::COLOR::BLUE, door::ATTR::BOLD)));
  222. /*
  223. 123456789012345678901234567890123456789012345678901234567890
  224. This door was written by Bugz.
  225. It is written in c++, only supports Linux, and replaces
  226. opendoors.
  227. It's written in c++, and replaces the outdated opendoors
  228. library.
  229. */
  230. about.addLine(
  231. std::make_unique<door::Line>("This door was written by Bugz.", 60));
  232. about.addLine(std::make_unique<door::Line>("", 60));
  233. about.addLine(std::make_unique<door::Line>(
  234. "It is written in c++, only support Linux, and replaces", 60));
  235. about.addLine(std::make_unique<door::Line>("opendoors.", 60));
  236. door::updateFunction updater = [](void) -> std::string {
  237. std::string text = "Currently: ";
  238. text.append(return_current_time_and_date());
  239. return text;
  240. };
  241. std::string current = updater();
  242. door::Line active(current, 60);
  243. active.setUpdater(updater);
  244. active.setRender(renderStatusValue(
  245. door::ANSIColor(door::COLOR::WHITE, door::COLOR::BLUE, door::ATTR::BOLD),
  246. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  247. door::ATTR::BOLD)));
  248. about.addLine(std::make_unique<door::Line>(active));
  249. /*
  250. about.addLine(std::make_unique<door::Line>(
  251. "Status: blue", 60,
  252. statusValue(door::ANSIColor(door::COLOR::GREEN, door::ATTR::BOLD),
  253. door::ANSIColor(door::COLOR::MAGENTA, door::ATTR::BLINK))));
  254. about.addLine(std::make_unique<door::Line>("Name: BUGZ", 60, rStatus));
  255. about.addLine(std::make_unique<door::Line>(
  256. "Size: 10240", 60,
  257. statusValue(door::ANSIColor(door::COLOR::GREEN, door::COLOR::BLUE,
  258. door::ATTR::BOLD),
  259. door::ANSIColor(door::COLOR::YELLOW, door::COLOR::BLUE,
  260. door::ATTR::BOLD, door::ATTR::BLINK))));
  261. about.addLine(std::make_unique<door::Line>("Bugz is here.", 60, rStatus));
  262. */
  263. return about;
  264. }
  265. void display_starfield(int mx, int my, door::Door &door, std::mt19937 &rng) {
  266. door << door::reset << door::cls;
  267. // display starfield
  268. const char *stars[2];
  269. stars[0] = ".";
  270. if (door::unicode) {
  271. stars[1] = "\u2219"; // "\u00b7";
  272. } else {
  273. stars[1] = "\xf9"; // "\xfa";
  274. };
  275. {
  276. // Make uniform random distribution between 1 and MAX screen size X/Y
  277. std::uniform_int_distribution<int> uni_x(1, mx);
  278. std::uniform_int_distribution<int> uni_y(1, my);
  279. door::ANSIColor white(door::COLOR::WHITE);
  280. door::ANSIColor dark(door::COLOR::BLACK, door::ATTR::BRIGHT);
  281. for (int x = 0; x < (mx * my / 100); x++) {
  282. door::Goto star_at(uni_x(rng), uni_y(rng));
  283. door << star_at;
  284. if (x % 5 < 2)
  285. door << dark;
  286. else
  287. door << white;
  288. if (x % 2 == 0)
  289. door << stars[0];
  290. else
  291. door << stars[1];
  292. }
  293. }
  294. }
  295. void display_space_ace(int mx, int my, door::Door &door) {
  296. // space_ace is 72 chars wide, 6 high
  297. int sa_x = (mx - 72) / 2;
  298. int sa_y = (my - 6) / 2;
  299. // output the SpaceAce logo -- centered!
  300. for (const auto s : space) {
  301. door::Goto sa_at(sa_x, sa_y);
  302. door << sa_at;
  303. if (door::unicode) {
  304. std::string unicode;
  305. door::cp437toUnicode(s, unicode);
  306. door << unicode; // << door::nl;
  307. } else
  308. door << s; // << door::nl;
  309. sa_y++;
  310. }
  311. // pause 5 seconds so they can enjoy our awesome logo
  312. door.sleep_key(5);
  313. }
  314. void display_starfield_space_ace(int mx, int my, door::Door &door,
  315. std::mt19937 &rng) {
  316. display_starfield(mx, my, door, rng);
  317. display_space_ace(mx, my, door);
  318. door << door::reset;
  319. }
  320. int main(int argc, char *argv[]) {
  321. /*
  322. google::InitGoogleLogging(argv[0]);
  323. LOG(INFO) << "Welcome!";
  324. */
  325. door::Door door("space-ace", argc, argv);
  326. // door << door::reset << door::cls << door::nl;
  327. door::ANSIColor ac(door::COLOR::YELLOW, door::ATTR::BOLD);
  328. door::ANSIColor mb(door::COLOR::MAGENTA, door::ATTR::BLINK);
  329. door << mb << "Does this work?" << door::reset << door::nl;
  330. // https://stackoverflow.com/questions/5008804/generating-random-integer-from-a-range
  331. std::random_device rd; // only used once to initialise (seed) engine
  332. std::mt19937 rng(
  333. rd()); // random-number engine used (Mersenne-Twister in this case)
  334. // std::uniform_int_distribution<int> uni(min, max); // guaranteed unbiased
  335. int mx, my; // Max screen width/height
  336. if (door.width == 0) {
  337. // screen detect failed, use sensible defaults
  338. mx = 80;
  339. my = 23;
  340. } else {
  341. mx = door.width;
  342. my = door.height;
  343. }
  344. // We assume here that the width and height are something crazy like 10x15. :P
  345. display_starfield_space_ace(mx, my, door, rng);
  346. // for testing inactivity timeout
  347. // door.inactivity = 10;
  348. door::Panel timeout = make_timeout(mx, my);
  349. door::Menu m = make_main_menu();
  350. int r = m.choose(door);
  351. // need to reset the colors. (whoops!)
  352. door << door::reset << door::nl;
  353. if (r < 0) {
  354. TIMEOUT:
  355. if (r == -1) {
  356. door.log("TIMEOUT");
  357. door << timeout << door::reset << door::nl << door::nl;
  358. } else {
  359. if (r == -3) {
  360. door.log("OUTTA TIME");
  361. door::Panel notime = make_notime(mx, my);
  362. door << notime << door::reset << door::nl;
  363. }
  364. }
  365. return 0;
  366. }
  367. /*
  368. display_starfield(mx, my, door, rng);
  369. // WARNING: After starfield, cursor position is random!
  370. door << door::Goto(1, 9); // door::nl << door::nl; // door::cls;
  371. door << "Your name: "
  372. << door::ANSIColor(door::COLOR::WHITE, door::COLOR::GREEN,
  373. door::ATTR::BOLD);
  374. std::string istring;
  375. istring = door.input_string(25);
  376. door << door::reset << door::nl << "You typed in [" << istring << "]"
  377. << door::nl;
  378. door << "Hello, " << door.username << ", you chose option " << r << "!"
  379. << door::nl;
  380. door << "Press a key...";
  381. r = door.sleep_key(door.inactivity);
  382. if (r < 0)
  383. goto TIMEOUT;
  384. */
  385. door << door::nl;
  386. door::Panel about = make_about();
  387. about.set((mx - 60) / 2, (my - 5) / 2);
  388. door << about;
  389. // magic time!
  390. door << door::reset << door::nl << "Press another key...";
  391. int x;
  392. for (x = 0; x < 60; ++x) {
  393. r = door.sleep_key(1);
  394. if (r == -1) {
  395. // ok! Expected timeout!
  396. // PROBLEM: regular "local" terminal loses current attributes
  397. // when cursor is save / restored.
  398. door << door::SaveCursor;
  399. if (about.update(door)) {
  400. // ok I need to "fix" the cursor position.
  401. // it has moved.
  402. }
  403. door << door::RestoreCursor;
  404. door.previous = door::reset;
  405. } else {
  406. if (r < 0)
  407. goto TIMEOUT;
  408. if (r >= 0)
  409. break;
  410. }
  411. }
  412. if (x == 60)
  413. goto TIMEOUT;
  414. door << door::nl;
  415. door::ANSIColor deck_color;
  416. // RED, BLUE, GREEN, MAGENTA, CYAN
  417. std::uniform_int_distribution<int> rand_color(0, 4);
  418. switch (rand_color(rng)) {
  419. case 0:
  420. deck_color = door::ANSIColor(door::COLOR::RED);
  421. break;
  422. case 1:
  423. deck_color = door::ANSIColor(door::COLOR::BLUE);
  424. break;
  425. case 2:
  426. deck_color = door::ANSIColor(door::COLOR::GREEN);
  427. break;
  428. case 3:
  429. deck_color = door::ANSIColor(door::COLOR::MAGENTA);
  430. break;
  431. case 4:
  432. deck_color = door::ANSIColor(door::COLOR::CYAN);
  433. break;
  434. default:
  435. deck_color = door::ANSIColor(door::COLOR::BLUE, door::ATTR::BLINK);
  436. break;
  437. }
  438. int height = 3;
  439. Deck d(deck_color, height);
  440. door::Panel *c;
  441. door << door::reset << door::cls;
  442. // This displays the cards in the upper left corner.
  443. // We want them center, and down some.
  444. int space = 3;
  445. // int cards_dealt_width = 59; int cards_dealt_height = 9;
  446. int game_width;
  447. {
  448. int cx, cy, level;
  449. cardgo(27, space, height, cx, cy, level);
  450. game_width = cx + 5; // card width
  451. }
  452. int off_x = (mx - game_width) / 2;
  453. int off_y = (my - 9) / 2;
  454. std::seed_seq s1{2021, 2, 27, 1};
  455. vector<int> deck1 = card_shuffle(s1, 1);
  456. std::uniform_int_distribution<int> rand_card(0, 51); // 0 - 51
  457. std::uniform_int_distribution<int> rand_action(0, 100);
  458. for (int x = 0; x < 28; x++) {
  459. int cx, cy, level;
  460. cardgo(x, space, height, cx, cy, level);
  461. if (rand_action(rng) > 80) {
  462. c = d.back(level);
  463. } else {
  464. c = d.card(deck1[x]);
  465. // c = d.card(rand_card(rng));
  466. }
  467. c->set(cx + off_x, cy + off_y);
  468. door << *c;
  469. }
  470. door << door::reset;
  471. door << door::nl << door::nl;
  472. r = door.sleep_key(door.inactivity);
  473. if (r < 0)
  474. goto TIMEOUT;
  475. /*
  476. door::Panel *p = d.back(2);
  477. p->set(10, 10);
  478. door << *p;
  479. door::Panel *d8 = d.card(8);
  480. d8->set(20, 8);
  481. door << *d8;
  482. r = door.sleep_key(door.inactivity);
  483. if (r < 0)
  484. goto TIMEOUT;
  485. */
  486. // door << door::reset << door::cls;
  487. display_starfield(mx, my, door, rng);
  488. door << m << door::reset << door::nl << "This is what the menu looked liked!"
  489. << door::nl;
  490. // Normal DOOR exit goes here...
  491. door << door::nl << "Returning you to the BBS, please wait..." << door::nl;
  492. return 0;
  493. }