| [9] | 1 | /** Adventure translated from Fortran to "C"
|
|---|
| 2 | and ported to Minix by:
|
|---|
| 3 | Robert R. Hall
|
|---|
| 4 | San Diego, Calif 92115
|
|---|
| 5 | hall@crash.cts.com
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | /** program ADVENT.C *
|
|---|
| 9 | * "advent.c" allocates GLOBAL storage space by *
|
|---|
| 10 | * #defining EXTERN before #including "advdec.h". */
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 | #include <string.h>
|
|---|
| 14 | #include <ctype.h>
|
|---|
| 15 | #include <stdlib.h>
|
|---|
| 16 | #include <time.h>
|
|---|
| 17 | #include <stdio.h>
|
|---|
| 18 | #include <errno.h>
|
|---|
| 19 | #include "advent.h" /* #define preprocessor equates */
|
|---|
| 20 | #include "advdec.h"
|
|---|
| 21 |
|
|---|
| 22 | #ifndef TEXTDIR
|
|---|
| 23 | #define TEXTDIR ""
|
|---|
| 24 | #endif
|
|---|
| 25 |
|
|---|
| 26 | char textdir[] = TEXTDIR; /* directory where text files
|
|---|
| 27 | live. */
|
|---|
| 28 |
|
|---|
| 29 | _PROTOTYPE(int main, (int, char **));
|
|---|
| 30 | _PROTOTYPE(static void opentxt, (void));
|
|---|
| 31 | _PROTOTYPE(static void file_error, (char *));
|
|---|
| 32 |
|
|---|
| 33 | int main(argc, argv)
|
|---|
| 34 | int argc;
|
|---|
| 35 | char **argv;
|
|---|
| 36 | {
|
|---|
| 37 | opentxt();
|
|---|
| 38 | initialize();
|
|---|
| 39 | rspeak(325);
|
|---|
| 40 | if (argc == 2)
|
|---|
| 41 | restore(argv[1]);
|
|---|
| 42 | else {
|
|---|
| 43 | g.hinted[3] = yes(65, 1, 0);
|
|---|
| 44 | g.limit = (g.hinted[3] ? 800 : 550);
|
|---|
| 45 | }
|
|---|
| 46 | gaveup = FALSE;
|
|---|
| 47 | srand((unsigned) time(NULL)); /* seed random */
|
|---|
| 48 | while (!gaveup)
|
|---|
| 49 | turn();
|
|---|
| 50 | fclose(fd1);
|
|---|
| 51 | fclose(fd2);
|
|---|
| 52 | fclose(fd3);
|
|---|
| 53 | fclose(fd4);
|
|---|
| 54 | return (EXIT_SUCCESS); /* exit = ok */
|
|---|
| 55 | } /* main */
|
|---|
| 56 |
|
|---|
| 57 | /*
|
|---|
| 58 | Open advent?.txt files
|
|---|
| 59 | */
|
|---|
| 60 | static void opentxt()
|
|---|
| 61 | {
|
|---|
| 62 | static char filename[sizeof(textdir) + 16];
|
|---|
| 63 | static FILE **fp[] = {0, &fd1, &fd2, &fd3, &fd4};
|
|---|
| 64 | int i;
|
|---|
| 65 | for (i = 1; i <= 4; i++) {
|
|---|
| 66 | sprintf(filename, "%sadvent%d.dat", textdir, i);
|
|---|
| 67 | *fp[i] = fopen(filename, "r");
|
|---|
| 68 | if (!*fp[i])
|
|---|
| 69 | file_error(filename);
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /*
|
|---|
| 74 | save adventure game
|
|---|
| 75 | */
|
|---|
| 76 | void saveadv(username)
|
|---|
| 77 | char *username;
|
|---|
| 78 | {
|
|---|
| 79 | int cnt;
|
|---|
| 80 | FILE *savefd;
|
|---|
| 81 |
|
|---|
| 82 | savefd = fopen(username, "wb");
|
|---|
| 83 | if (savefd == NULL) {
|
|---|
| 84 | perror(username);
|
|---|
| 85 | return;
|
|---|
| 86 | }
|
|---|
| 87 | cnt = fwrite((void *) &g, 1, sizeof(struct playinfo), savefd);
|
|---|
| 88 | if (cnt != sizeof(struct playinfo)) {
|
|---|
| 89 | fprintf(stderr, "wrote %d of %u bytes\n",
|
|---|
| 90 | cnt, (unsigned) sizeof(struct playinfo));
|
|---|
| 91 | if (ferror(savefd)) {
|
|---|
| 92 | fprintf(stderr, "errno is: 0x%.4x\n", errno);
|
|---|
| 93 | perror(username);
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | if (fclose(savefd) == -1) {
|
|---|
| 97 | perror(username);
|
|---|
| 98 | }
|
|---|
| 99 | printf("Saved in %s.\n", username);
|
|---|
| 100 | return;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /*
|
|---|
| 104 | restore saved game handler
|
|---|
| 105 | */
|
|---|
| 106 | void restore(username)
|
|---|
| 107 | char *username;
|
|---|
| 108 | {
|
|---|
| 109 | int cnt;
|
|---|
| 110 | FILE *restfd;
|
|---|
| 111 |
|
|---|
| 112 | restfd = fopen(username, "rb");
|
|---|
| 113 | if (restfd == NULL)
|
|---|
| 114 | file_error(username);
|
|---|
| 115 | cnt = fread((void *) &g, 1, sizeof(struct playinfo), restfd);
|
|---|
| 116 | if (cnt != sizeof(struct playinfo)) {
|
|---|
| 117 | fprintf(stderr, "read %d bytes, expected %u\n",
|
|---|
| 118 | cnt, (unsigned) sizeof(struct playinfo));
|
|---|
| 119 | if (ferror(restfd)) {
|
|---|
| 120 | fprintf(stderr, "errno is: 0x%.4x\n", errno);
|
|---|
| 121 | perror(username);
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | if (fclose(restfd) == -1) {
|
|---|
| 125 | perror(username);
|
|---|
| 126 | }
|
|---|
| 127 | printf("Restored from %s.\n", username);
|
|---|
| 128 | return;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | static void file_error(filename)
|
|---|
| 132 | char *filename;
|
|---|
| 133 | {
|
|---|
| 134 | perror(filename);
|
|---|
| 135 | exit(EXIT_FAILURE);
|
|---|
| 136 | }
|
|---|