ularn_game.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 "ularn_game.h"
  54. #include "header.h"
  55. #include "monster.h"
  56. #include "player.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[] =
  102. {
  103. "None",
  104. "South",
  105. "East",
  106. "North",
  107. "West",
  108. "Northeast",
  109. "Northwest",
  110. "Southeast",
  111. "Southwest"
  112. };
  113. /* =============================================================================
  114. * Local variables
  115. */
  116. #define LINE_LEN 256
  117. typedef enum
  118. {
  119. OPTION_NULL,
  120. OPTION_NAME,
  121. OPTION_CLASS,
  122. OPTION_GENDER,
  123. OPTION_NAP,
  124. OPTION_NONAP,
  125. OPTION_WELCOME,
  126. OPTION_NOWELCOME,
  127. OPTION_ENHANCE_INT,
  128. OPTION_NOENHANCE_INT,
  129. OPTION_BEEP,
  130. OPTION_NOBEEP,
  131. OPTION_COUNT
  132. } OptionType;
  133. static char *OptionString[OPTION_COUNT] =
  134. {
  135. "",
  136. "name",
  137. "class",
  138. "gender",
  139. "nap",
  140. "nonap",
  141. "welcome",
  142. "nowelcome",
  143. "enhanced_interface",
  144. "noenhanced_interface",
  145. "beep",
  146. "nobeep"
  147. };
  148. /* =============================================================================
  149. * Exported functions
  150. */
  151. /* =============================================================================
  152. * FUNCTION: newgame
  153. */
  154. void newgame(void)
  155. {
  156. time(&initialtime);
  157. srand((unsigned)initialtime);
  158. }
  159. /* =============================================================================
  160. * FUNCTION: sethard
  161. */
  162. void sethard (int hard)
  163. {
  164. int j, k, i;
  165. if (restorflag == 0)
  166. {
  167. /* don't set c[HARDGAME] if restoring game */
  168. if (hashewon() == 0)
  169. {
  170. if (hard >= 0)
  171. {
  172. c[HARDGAME] = hard;
  173. }
  174. }
  175. else if (hard > c[HARDGAME] || wizard)
  176. {
  177. c[HARDGAME] = hard;
  178. }
  179. }
  180. k = c[HARDGAME];
  181. if (k != 0)
  182. {
  183. for (j = 0 ; j <= MAXMONST + 8 ; j++)
  184. {
  185. i = ((6+k)*monster[j].hitpoints+1)/6;
  186. monster[j].hitpoints = (short) ((i > 32767) ? 32767 : i);
  187. i = ((6+k)*monster[j].damage+1)/5;
  188. monster[j].damage = (char) ((i > 127) ? 127 : i);
  189. i = (10*monster[j].gold)/(10+k);
  190. monster[j].gold = (short) ((i > 32767) ? 32767 : i);
  191. i = monster[j].armorclass - k;
  192. monster[j].armorclass = (char) ((i < -127) ? -127 : i);
  193. i = (int) ((7*monster[j].experience)/(7+k) + 1);
  194. monster[j].experience = (i <=0) ? 1 : i;
  195. }
  196. }
  197. }
  198. /* =============================================================================
  199. * FUNCTION: read_options
  200. */
  201. void read_options(void)
  202. {
  203. char Line[LINE_LEN + 1];
  204. char *Str;
  205. char *tok;
  206. FILE *fp;
  207. OptionType OptionId;
  208. int Found;
  209. fp = fopen(optsfile, "r");
  210. if (fp == NULL)
  211. {
  212. /*
  213. * Couldn't open the options file.
  214. */
  215. return;
  216. }
  217. while (!feof(fp))
  218. {
  219. Str = fgets(Line, LINE_LEN, fp);
  220. if (Str == NULL)
  221. {
  222. /* End of file - do nothng */
  223. }
  224. else if (Line[0] == '#')
  225. {
  226. /* Comment line - do nothing */
  227. }
  228. else
  229. {
  230. tok = strtok(Line, " \t\n=");
  231. if (tok == NULL)
  232. {
  233. /* A blank line */
  234. }
  235. else if (strcmp(tok, "OPTION") == 0)
  236. {
  237. /* Read all options specified on this line. */
  238. do
  239. {
  240. /* get the option string */
  241. tok = strtok(NULL, ",:\n");
  242. /* identify the option */
  243. if (tok == NULL)
  244. {
  245. OptionId = OPTION_NULL;
  246. }
  247. else
  248. {
  249. OptionId = OPTION_NAME;
  250. Found = 0;
  251. while ((OptionId < OPTION_COUNT) && (!Found))
  252. {
  253. if (strcmp(OptionString[OptionId], tok) == 0)
  254. {
  255. Found = 1;
  256. }
  257. else
  258. {
  259. OptionId++;
  260. }
  261. }
  262. }
  263. switch (OptionId)
  264. {
  265. case OPTION_NULL:
  266. break;
  267. case OPTION_NAME:
  268. tok = strtok(NULL, ":,\n");
  269. strncpy(logname, tok, LOGNAMESIZE);
  270. break;
  271. case OPTION_CLASS:
  272. tok = strtok(NULL, ":,\n");
  273. char_picked = identify_class(tok);
  274. break;
  275. case OPTION_GENDER:
  276. tok = strtok(NULL, ":,\n");
  277. if (strcmp(tok, "male") == 0)
  278. {
  279. sex = 1;
  280. }
  281. else if (strcmp(tok, "female") == 0)
  282. {
  283. sex = 0;
  284. }
  285. else
  286. {
  287. Printf("\nUnknown gender '%s'", tok);
  288. }
  289. break;
  290. case OPTION_NAP:
  291. nonap = 0;
  292. break;
  293. case OPTION_NONAP:
  294. nonap = 1;
  295. break;
  296. case OPTION_WELCOME:
  297. nowelcome = 0;
  298. break;
  299. case OPTION_NOWELCOME:
  300. nowelcome = 1;
  301. break;
  302. case OPTION_ENHANCE_INT:
  303. enhance_interface = 1;
  304. break;
  305. case OPTION_NOENHANCE_INT:
  306. enhance_interface = 0;
  307. break;
  308. case OPTION_BEEP:
  309. nobeep = 0;
  310. break;
  311. case OPTION_NOBEEP:
  312. nobeep = 1;
  313. break;
  314. default:
  315. Printf("\nUnrecognised option '%s'", tok);
  316. break;
  317. }
  318. } while ((OptionId != OPTION_NULL) && (OptionId < OPTION_COUNT));
  319. }
  320. else if (strcmp(tok, "LIBDIR") == 0)
  321. {
  322. tok = strtok(NULL, ":\n");
  323. strcpy(libdir, tok);
  324. }
  325. else if (strcmp(tok, "SAVEDIR") == 0)
  326. {
  327. tok = strtok(NULL, ":\n");
  328. strcpy(savedir, tok);
  329. }
  330. else
  331. {
  332. Printf("\nUnrecognised option '%s'", tok);
  333. }
  334. }
  335. }
  336. fclose(fp);
  337. }