ularn_game.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* =============================================================================
  2. * PROGRAM: vlarn
  3. * FILENAME: vlarn_game.h
  4. *
  5. * DESCRIPTION:
  6. * Game data used by VLarn.
  7. * This Contains:
  8. * . The names of data files used by vlarn
  9. * . The player's name
  10. * . Current game options
  11. *
  12. * =============================================================================
  13. * EXPORTED VARIABLES
  14. *
  15. * do_fork : True if fork on save (now unsupported)
  16. * boldon : True if objects are to be dislayed in bold (tty only)
  17. * mail : True if mail bills when game is won
  18. * ckpflag : True if checkpoint files are to be used.
  19. * nobeep : True if beep is off.
  20. * libdir : VLarn library path
  21. * savedir : Directroy for save games
  22. * savefilename : Filename for saving the game
  23. * scorefile : Filename for the scores
  24. * helpfile : Filename for vlarn help
  25. * larnlevels : Filename for pregenerated levels
  26. * fortfile : Filename for fortunes
  27. * optsfile : VLarn options file
  28. * ckpfile : Checkpoint file name
  29. * diagfile : Diagnostic dump file name
  30. * userid : User Id of the player
  31. * password : Wizard password
  32. * loginname : The login name of the player
  33. * logname : The name to appear on the score board
  34. * nowelcome : True if no welcome message is to be displayed
  35. * nomove : True if player action resulted in no move.
  36. * dropflag : True if the player just dropped the item
  37. * restoreflag : True if the game is to be restored from a file
  38. * diroffx : Direction offsets for x coordinate
  39. * diroffy : Direction offsets for y coordinate
  40. * ReverseDir : Lookup for the index of the reverse direction
  41. * dirname : The name of each direction.
  42. *
  43. * =============================================================================
  44. * EXPORTED FUNCTIONS
  45. *
  46. * newgame : Funtion to initialise a new game.
  47. * sethard : Function to set the game difficulty
  48. * read_options : Function to read the vlarn options file
  49. *
  50. * =============================================================================
  51. */
  52. #include <time.h>
  53. #include "header.h"
  54. #include "monster.h"
  55. #include "player.h"
  56. #include "ularn_game.h"
  57. /* =============================================================================
  58. * Exported variables
  59. */
  60. /*
  61. * Game options
  62. */
  63. char do_fork = 0; /* 1=fork on save, 0=save from main process. NOT SUPPORTED */
  64. char boldon = 1; /* 1=bold objects, 0=inverse objects */
  65. char mail = 1; /* 1=mail letters after win game */
  66. char ckpflag = 1; /* 1 if want checkpointing of game, 0 otherwise */
  67. char nobeep = 0; /* true if program is not to beep*/
  68. char libdir[MAXPATHLEN] = LIBDIR;
  69. char savedir[MAXPATHLEN];
  70. /* the game save filename */
  71. char savefilename[MAXPATHLEN];
  72. /* the temporary save filename */
  73. char tempfilename[MAXPATHLEN];
  74. /* the score file */
  75. char scorefile[MAXPATHLEN];
  76. /* the help text file */
  77. char helpfile[MAXPATHLEN];
  78. /* the maze data file */
  79. char larnlevels[MAXPATHLEN];
  80. /* the fortune data file */
  81. char fortfile[MAXPATHLEN];
  82. /* the options file filename */
  83. char optsfile[MAXPATHLEN] = "vlarn.opt";
  84. /* the checkpoint file filename */
  85. char ckpfile[MAXPATHLEN] = "vlarn.ckp";
  86. /* the diagnostic filename */
  87. char diagfile[] = "diagfile.txt";
  88. /* the wizard's password */
  89. char *password = "rodney";
  90. int userid; /* the players login user id number */
  91. char loginname[USERNAME_LENGTH + 1]; /* players login name */
  92. char logname[LOGNAMESIZE + 1]; /* players name storage for scoring */
  93. char nowelcome = 0; /* if nowelcome, don't display welcome message */
  94. char nomove = 0; /* if nomove no count next iteration as move */
  95. char dropflag = 0; /* if 1 then don't lookforobject() next round */
  96. char restorflag = 0; /* 1 means restore has been done */
  97. char enhance_interface = 0; /* 1 means use the enhanced command interface */
  98. char diroffx[] = {0, 0, 1, 0, -1, 1, -1, 1, -1};
  99. char diroffy[] = {0, 1, 0, -1, 0, -1, -1, 1, 1};
  100. int ReverseDir[] = {0, 3, 4, 1, 2, 8, 7, 6, 5};
  101. char *dirname[] = {"None", "South", "East", "North", "West",
  102. "Northeast", "Northwest", "Southeast", "Southwest"};
  103. /* =============================================================================
  104. * Local variables
  105. */
  106. #define LINE_LEN 256
  107. typedef enum {
  108. OPTION_NULL,
  109. OPTION_NAME,
  110. OPTION_CLASS,
  111. OPTION_GENDER,
  112. OPTION_NAP,
  113. OPTION_NONAP,
  114. OPTION_WELCOME,
  115. OPTION_NOWELCOME,
  116. OPTION_ENHANCE_INT,
  117. OPTION_NOENHANCE_INT,
  118. OPTION_BEEP,
  119. OPTION_NOBEEP,
  120. OPTION_COUNT
  121. } OptionType;
  122. static char *OptionString[OPTION_COUNT] = {"",
  123. "name",
  124. "class",
  125. "gender",
  126. "nap",
  127. "nonap",
  128. "welcome",
  129. "nowelcome",
  130. "enhanced_interface",
  131. "noenhanced_interface",
  132. "beep",
  133. "nobeep"};
  134. /* =============================================================================
  135. * Exported functions
  136. */
  137. /* =============================================================================
  138. * FUNCTION: newgame
  139. */
  140. void newgame(void) {
  141. time(&initialtime);
  142. srand((unsigned)initialtime);
  143. }
  144. /* =============================================================================
  145. * FUNCTION: sethard
  146. */
  147. void sethard(int hard) {
  148. int j, k, i;
  149. if (restorflag == 0) {
  150. /* don't set c[HARDGAME] if restoring game */
  151. if (hashewon() == 0) {
  152. if (hard >= 0)
  153. c[HARDGAME] = hard;
  154. } else if (hard > c[HARDGAME] || wizard)
  155. c[HARDGAME] = hard;
  156. }
  157. k = c[HARDGAME];
  158. if (k != 0) {
  159. for (j = 0; j <= MAXMONST + 8; j++) {
  160. i = ((6 + k) * monster[j].hitpoints + 1) / 6;
  161. monster[j].hitpoints = (short)((i > 32767) ? 32767 : i);
  162. i = ((6 + k) * monster[j].damage + 1) / 5;
  163. monster[j].damage = (char)((i > 127) ? 127 : i);
  164. i = (10 * monster[j].gold) / (10 + k);
  165. monster[j].gold = (short)((i > 32767) ? 32767 : i);
  166. i = monster[j].armorclass - k;
  167. monster[j].armorclass = (char)((i < -127) ? -127 : i);
  168. i = (int)((7 * monster[j].experience) / (7 + k) + 1);
  169. monster[j].experience = (i <= 0) ? 1 : i;
  170. }
  171. }
  172. }
  173. /* =============================================================================
  174. * FUNCTION: read_options
  175. */
  176. void read_options(void) {
  177. char Line[LINE_LEN + 1];
  178. char *Str;
  179. char *tok;
  180. FILE *fp;
  181. OptionType OptionId;
  182. int Found;
  183. fp = fopen(optsfile, "r");
  184. if (fp == NULL)
  185. /*
  186. * Couldn't open the options file.
  187. */
  188. return;
  189. while (!feof(fp)) {
  190. Str = fgets(Line, LINE_LEN, fp);
  191. if (Str == NULL) {
  192. /* End of file - do nothng */
  193. } else if (Line[0] == '#') {
  194. /* Comment line - do nothing */
  195. } else {
  196. tok = strtok(Line, " \t\n=");
  197. if (tok == NULL) {
  198. /* A blank line */
  199. } else if (strcmp(tok, "OPTION") == 0) {
  200. /* Read all options specified on this line. */
  201. do {
  202. /* get the option string */
  203. tok = strtok(NULL, ",:\n");
  204. /* identify the option */
  205. if (tok == NULL)
  206. OptionId = OPTION_NULL;
  207. else {
  208. OptionId = OPTION_NAME;
  209. Found = 0;
  210. while ((OptionId < OPTION_COUNT) && (!Found)) {
  211. if (strcmp(OptionString[OptionId], tok) == 0)
  212. Found = 1;
  213. else
  214. OptionId++;
  215. }
  216. }
  217. switch (OptionId) {
  218. case OPTION_NULL:
  219. break;
  220. case OPTION_NAME:
  221. tok = strtok(NULL, ":,\n");
  222. strncpy(logname, tok, LOGNAMESIZE);
  223. break;
  224. case OPTION_CLASS:
  225. tok = strtok(NULL, ":,\n");
  226. char_picked = identify_class(tok);
  227. break;
  228. case OPTION_GENDER:
  229. tok = strtok(NULL, ":,\n");
  230. if (strcmp(tok, "male") == 0)
  231. sex = 1;
  232. else if (strcmp(tok, "female") == 0)
  233. sex = 0;
  234. else
  235. Printf("\nUnknown gender '%s'", tok);
  236. break;
  237. case OPTION_NAP:
  238. nonap = 0;
  239. break;
  240. case OPTION_NONAP:
  241. nonap = 1;
  242. break;
  243. case OPTION_WELCOME:
  244. nowelcome = 0;
  245. break;
  246. case OPTION_NOWELCOME:
  247. nowelcome = 1;
  248. break;
  249. case OPTION_ENHANCE_INT:
  250. enhance_interface = 1;
  251. break;
  252. case OPTION_NOENHANCE_INT:
  253. enhance_interface = 0;
  254. break;
  255. case OPTION_BEEP:
  256. nobeep = 0;
  257. break;
  258. case OPTION_NOBEEP:
  259. nobeep = 1;
  260. break;
  261. default:
  262. Printf("\nUnrecognised option '%s'", tok);
  263. break;
  264. }
  265. } while ((OptionId != OPTION_NULL) && (OptionId < OPTION_COUNT));
  266. } else if (strcmp(tok, "LIBDIR") == 0) {
  267. tok = strtok(NULL, ":\n");
  268. strcpy(libdir, tok);
  269. } else if (strcmp(tok, "SAVEDIR") == 0) {
  270. tok = strtok(NULL, ":\n");
  271. strcpy(savedir, tok);
  272. } else
  273. Printf("\nUnrecognised option '%s'", tok);
  274. }
  275. }
  276. fclose(fp);
  277. }