help.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* =============================================================================
  2. * PROGRAM: ularn
  3. * FILENAME: help.c
  4. *
  5. * DESCRIPTION:
  6. * This module contains functions to display the help file and the welcome
  7. * screen.
  8. *
  9. * format of the Ularn help file:
  10. *
  11. * The 1st character of file is the # of pages of help available (ascii digit).
  12. * The first page (23 lines) of the file is for the introductory message
  13. * (not counted in above).
  14. * The pages of help text are 23 lines per page.
  15. * The help file allows escape sequences for specifying formatting of the
  16. * text.
  17. * The currently supported sequences are:
  18. * ^[[7m - Set text format to standout (red)
  19. * ^[[8m - Set text format to standout2 (green)
  20. * ^[[9m - Set text format to standout3 (blue)
  21. * ^[[m - Set text format to normal.
  22. *
  23. * =============================================================================
  24. * EXPORTED VARIABLES
  25. *
  26. * None.
  27. *
  28. * =============================================================================
  29. * EXPORTED FUNCTIONS
  30. *
  31. * help - Display the help file.
  32. * welcome - Display the welcome message
  33. *
  34. * =============================================================================
  35. */
  36. #include <stdio.h>
  37. #include "header.h"
  38. #include "ularn_game.h"
  39. #include "ularn_win.h"
  40. #include "player.h"
  41. /* =============================================================================
  42. * Local variables
  43. */
  44. /*
  45. * Help file escape sequence states.
  46. */
  47. typedef enum { HELP_NORMAL, HELP_ESC, HELP_COUNT } HelpStateType;
  48. /*
  49. * A pointer to the help file handle.
  50. */
  51. static FILE *help_fp;
  52. /* =============================================================================
  53. * Local functions
  54. */
  55. /* =============================================================================
  56. * FUNCTION: retcont
  57. *
  58. * DESCRIPTION:
  59. * Function to say press return to continue.
  60. *
  61. * PARAMETERS:
  62. *
  63. * None.
  64. *
  65. * RETURN VALUE:
  66. *
  67. * None.
  68. */
  69. static void retcont(void) {
  70. MoveCursor(1, 24);
  71. Print("Press ");
  72. Standout("return");
  73. Print(" to continue: ");
  74. get_prompt_input("", "\015\033", 0);
  75. }
  76. /* =============================================================================
  77. * FUNCTION: openhelp
  78. *
  79. * DESCRIPTION:
  80. * Function to open the help file and return the number of pages in the help
  81. * file.
  82. *
  83. * PARAMETERS:
  84. *
  85. * None
  86. *
  87. * RETURN VALUE:
  88. *
  89. * The number of pages in the help file as specified by the first character.
  90. */
  91. static int openhelp(void) {
  92. help_fp = fopen(helpfile, "r");
  93. if (help_fp == NULL) {
  94. Printf("Can't open help file \"%s\" ", helpfile);
  95. nap(4000);
  96. return -1;
  97. }
  98. return fgetc(help_fp) - '0';
  99. }
  100. /* =============================================================================
  101. * FUNCTION: show_help_page
  102. *
  103. * DESCRIPTION:
  104. * This function prints the next page of help in the help file.
  105. *
  106. * PARAMETERS:
  107. *
  108. * None.
  109. *
  110. * RETURN VALUE:
  111. *
  112. * None.
  113. */
  114. static void show_help_page(void) {
  115. HelpStateType state;
  116. int line;
  117. int line_len;
  118. int line_pos;
  119. char tmbuf[128];
  120. ClearText();
  121. for (line = 0; line < 23; line++) {
  122. fgets(tmbuf, 128, help_fp);
  123. line_len = strlen(tmbuf);
  124. state = HELP_NORMAL;
  125. line_pos = 0;
  126. while (line_pos < line_len) {
  127. switch (state) {
  128. case HELP_NORMAL:
  129. if ((tmbuf[line_pos] == '^') && ((line_pos + 3) < line_len)) {
  130. if ((tmbuf[line_pos + 1] == '[') && (tmbuf[line_pos + 2] == '[')) {
  131. state = HELP_ESC;
  132. line_pos += 3;
  133. }
  134. }
  135. if (state == HELP_NORMAL) {
  136. Printc(tmbuf[line_pos]);
  137. line_pos++;
  138. }
  139. break;
  140. case HELP_ESC:
  141. if (tmbuf[line_pos] == '7') {
  142. SetFormat(FORMAT_STANDOUT);
  143. line_pos++;
  144. } else if (tmbuf[line_pos] == '8') {
  145. SetFormat(FORMAT_STANDOUT2);
  146. line_pos++;
  147. } else if (tmbuf[line_pos] == '9') {
  148. SetFormat(FORMAT_STANDOUT3);
  149. line_pos++;
  150. } else if (tmbuf[line_pos] == 'm')
  151. SetFormat(FORMAT_NORMAL);
  152. line_pos++;
  153. state = HELP_NORMAL;
  154. break;
  155. default:
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. /* =============================================================================
  162. * Exported functions
  163. */
  164. /* =============================================================================
  165. * FUNCTION: help
  166. */
  167. void help(void) {
  168. int num_pages;
  169. int page;
  170. int i;
  171. char tmbuf[128];
  172. set_display(DISPLAY_TEXT);
  173. /* open the help file and get # pages */
  174. num_pages = openhelp();
  175. if (num_pages < 0) {
  176. set_display(DISPLAY_MAP);
  177. return;
  178. }
  179. /* skip over intro message */
  180. for (i = 0; i < 23; i++)
  181. fgets(tmbuf, 128, help_fp);
  182. for (page = num_pages; page > 0; page--) {
  183. show_help_page();
  184. /* intercept ESC's */
  185. if (page > 1) {
  186. Print(" ---- Press ");
  187. Standout("return");
  188. Print(" to exit, ");
  189. Standout("space");
  190. Print(" for more help ---- ");
  191. i = get_prompt_input("", " \015\033", 0);
  192. if ((i == '\015') || (i == ESC)) {
  193. fclose(help_fp);
  194. set_display(DISPLAY_MAP);
  195. return;
  196. }
  197. }
  198. }
  199. fclose(help_fp);
  200. retcont();
  201. set_display(DISPLAY_MAP);
  202. }
  203. /* =============================================================================
  204. * FUNCTION: welcome
  205. */
  206. void welcome(void) {
  207. int num_pages;
  208. set_display(DISPLAY_TEXT);
  209. /* open the help file */
  210. num_pages = openhelp();
  211. if (num_pages < 0)
  212. return;
  213. show_help_page();
  214. fclose(help_fp);
  215. /* press return to continue */
  216. retcont();
  217. set_display(DISPLAY_MAP);
  218. }