home *** CD-ROM | disk | FTP | other *** search
/ Oldies but Goodies / OldiesButGoodiesROMMANTechnologies1993.disc1of1.iso / adventur / advent.c < prev    next >
Text File  |  1984-06-10  |  6KB  |  273 lines

  1.  
  2. /*    program ADVENT.C                    *\
  3. \*    WARNING: "advent.c" allocates GLOBAL storage space by    *\
  4. \*        including "advdef.h".                *\
  5. \*        All other modules use "advdec.h"        */
  6.  
  7.  
  8. #include    "stdio.h"    /* drv = 1.1st file 2.def 3.A    */
  9. #include    "advent.h"    /* #define preprocessor equates    */
  10. #include    "advword.h"    /* definition of "word" array    */
  11. #include    "advcave.h"    /* definition of "cave" array    */
  12. #include    "advtext.h"    /* definition of "text" arrays    */
  13. #include    "advdef.h"
  14.  
  15. #define    strchr    index
  16.  
  17. extern    int    fclose();
  18. extern    int    fgetc();
  19. extern    FILE    *fopen();
  20. extern    int    fputc();
  21. extern    long    ftell();
  22. extern    int    printf();
  23. extern    int    setmem();
  24. extern    int    scanf();
  25. extern    int    sscanf();
  26. extern    char    *strcat();
  27. extern    char    *strchr();
  28. extern    unsigned    strlen();
  29. extern    int    tolower();
  30.  
  31.  
  32. main(argc, argv)
  33. int    argc;
  34. char    **argv;
  35. {
  36.     int    rflag;        /* user restore request option    */
  37.  
  38.     rflag = 0;
  39.     dbugflg = 0;
  40.     while (--argc > 0) {
  41.         ++argv;
  42.         if (**argv !=  '-')
  43.             break;
  44.         switch(tolower(argv[0][1])) {
  45.         case 'r':
  46.             ++rflag;
  47.             continue;
  48.         case 'd':
  49.             ++dbugflg;
  50.             continue;
  51.         default:
  52.             printf("unknown flag: %c\n", argv[0][1]);
  53.             continue;
  54.         }                /* switch    */
  55.     }                    /* while    */
  56.     if (dbugflg < 2)
  57.         dbugflg = 0;    /* must request three times    */
  58.     opentxt();
  59.     initplay();
  60.     if (rflag)
  61.         restore();
  62.     else if (yes(65, 1, 0))
  63.         limit = 1000;
  64.     else
  65.         limit = 330;
  66.     saveflg = 0;
  67.     srand(511);                /* seed random    */
  68.     while(!saveflg)
  69.         turn();
  70.     if (saveflg)
  71.         saveadv();
  72.     fclose(fd1);
  73.     fclose(fd2);
  74.     fclose(fd3);
  75.     fclose(fd4);
  76.     exit(0);                /* exit = ok    */
  77. }                        /* main        */
  78.  
  79. /* ************************************************************    */
  80.  
  81. /*
  82.     Initialize integer arrays with sscanf
  83. */
  84. scanint(pi, str)
  85. int    *pi;
  86. char    *str;
  87. {
  88.  
  89.     while (*str) {
  90.         if  ((sscanf(str, "%d,", pi++)) < 1)
  91.             bug(41);    /* failed before EOS    */
  92.         while (*str++ != ',')    /* advance str pointer    */
  93.             ;
  94.     }
  95.     return;
  96. }
  97.  
  98. /*
  99.     Initialization of adventure play variables
  100. */
  101. initplay()
  102. {
  103.     turns = 0;
  104.  
  105.     /* initialize location status array */
  106.     setmem(cond, (sizeof(int))*MAXLOC, 0);
  107.     scanint(&cond[1], "5,1,5,5,1,1,5,17,1,1,");
  108.     scanint(&cond[13], "32,0,0,2,0,0,64,2,");
  109.     scanint(&cond[21], "2,2,0,6,0,2,");
  110.     scanint(&cond[31], "2,2,0,0,0,0,0,4,0,2,");
  111.     scanint(&cond[42], "128,128,128,128,136,136,136,128,128,");
  112.     scanint(&cond[51], "128,128,136,128,136,0,8,0,2,");
  113.     scanint(&cond[79], "2,128,128,136,0,0,8,136,128,0,2,2,");
  114.     scanint(&cond[95], "4,0,0,0,0,1,");
  115.     scanint(&cond[113], "4,0,1,1,");
  116.     scanint(&cond[122], "8,8,8,8,8,8,8,8,8,");
  117.  
  118.     /* initialize object locations */
  119.     setmem(place, (sizeof(int))*MAXOBJ, 0);
  120.     scanint(&place[1], "3,3,8,10,11,0,14,13,94,96,");
  121.     scanint(&place[11], "19,17,101,103,0,106,0,0,3,3,");
  122.     scanint(&place[23], "109,25,23,111,35,0,97,");
  123.     scanint(&place[31], "119,117,117,0,130,0,126,140,0,96,");
  124.     scanint(&place[50], "18,27,28,29,30,");
  125.     scanint(&place[56], "92,95,97,100,101,0,119,127,130,");
  126.  
  127.     /* initialize second (fixed) locations */
  128.     setmem(fixed, (sizeof(int))*MAXOBJ, 0);
  129.     scanint(&fixed[3], "9,0,0,0,15,0,-1,");
  130.     scanint(&fixed[11], "-1,27,-1,0,0,0,-1,");
  131.     scanint(&fixed[23], "-1,-1,67,-1,110,0,-1,-1,");
  132.     scanint(&fixed[31], "121,122,122,0,-1,-1,-1,-1,0,-1,");
  133.     scanint(&fixed[62], "121,-1,");
  134.  
  135.     /* initialize default verb messages */
  136.     scanint(actmsg, "0,24,29,0,33,0,33,38,38,42,14,");
  137.     scanint(&actmsg[11], "43,110,29,110,73,75,29,13,59,59,");
  138.     scanint(&actmsg[21], "174,109,67,13,147,155,195,146,110,13,13,");
  139.  
  140.     /* initialize various flags and other variables */
  141.     setmem(visited, (sizeof(int))*MAXLOC, 0);
  142.     setmem(prop, (sizeof(int))*MAXOBJ, 0);
  143.     setmem(&prop[50], (sizeof(int))*(MAXOBJ-50), 0xff);
  144.     wzdark = closed = closing = holding = detail = 0;
  145.     limit = 100;
  146.     tally = 15;
  147.     tally2 = 0;
  148.     newloc = 3;
  149.     loc = oldloc = oldloc2 = 1;
  150.     knfloc = 0;
  151.     chloc = 114;
  152.     chloc2 = 140;
  153. /*    dloc[DWARFMAX-1] = chloc                */
  154.     scanint(dloc, "0,19,27,33,44,64,114,");
  155.     scanint(odloc, "0,0,0,0,0,0,0,");
  156.     dkill = 0;
  157.     scanint(dseen, "0,0,0,0,0,0,0,");
  158.     clock = 30;
  159.     clock2 = 50;
  160.     panic = 0;
  161.     bonus = 0;
  162.     numdie = 0;
  163.     daltloc = 18;
  164.     lmwarn = 0;
  165.     foobar = 0;
  166.     dflag = 0;
  167.     gaveup = 0;
  168.     saveflg = 0;
  169.     return;
  170. }
  171.  
  172. /*
  173.     Open advent?.txt files
  174. */
  175. opentxt()
  176. {
  177.     fd1 = fopen("advent1.txt", "r");
  178.     if (!fd1) {
  179.         printf("Sorry, I can't open advent1.txt...\n");
  180.         exit();
  181.     }
  182.     fd2 = fopen("advent2.txt", "r");
  183.     if (!fd2) {
  184.         printf("Sorry, I can't open advent2.txt...\n");
  185.         exit();
  186.     }
  187.     fd3 = fopen("advent3.txt", "r");
  188.     if (!fd3) {
  189.         printf("Sorry, I can't open advent3.txt...\n");
  190.         exit();
  191.     }
  192.     fd4 = fopen("advent4.txt", "r");
  193.     if (!fd4) {
  194.         printf("Sorry, I can't open advent4.txt...\n");
  195.         exit();
  196.     }
  197. }
  198.  
  199. /*
  200.         save adventure game
  201. */
  202. saveadv()
  203. {
  204.     char    *sptr;
  205.     int    savefd;
  206.     char    username[13];
  207.  
  208.     printf("What do you want to name the saved game? ");
  209.     scanf("%s", username);
  210.     if (sptr = strchr(username, '.'))
  211.         *sptr = '\0';        /* kill extension    */
  212.     if (strlen(username) > 8)
  213.         username[8] = '\0';    /* max 8 char filename    */
  214.     strcat(username, ".adv");
  215.     savefd = fopen(username, "wb");
  216.     if (savefd == NULL) {
  217.         printf("Sorry, I can't create the file...%s\n", \
  218.                username);
  219.         exit();
  220.     }
  221.     for (sptr = &turns; sptr < &lastglob; sptr++) {
  222.         if (fputc(*sptr, savefd)  ==  EOF) {
  223.             printf("Write error on save file...%s\n", \
  224.                    username);
  225.             exit();
  226.         }
  227.     }
  228.     if (fclose(savefd)  ==  -1) {
  229.         printf("Sorry, I can't close the file...%s\n", \
  230.                username);
  231.         exit();
  232.     }
  233.     printf("OK -- \"C\" you later...\n");
  234. }
  235.  
  236. /*
  237.     restore saved game handler
  238. */
  239. restore()
  240. {
  241.     char    username[13];
  242.     int    restfd, c;
  243.     char    *sptr;
  244.  
  245.     printf("What is the name of the saved game? ");
  246.     scanf("%s", username);
  247.     if (sptr = strchr(username, '.'))
  248.         *sptr = '\0';        /* kill extension    */
  249.     if (strlen(username) > 8)
  250.         username[8] = '\0';    /* max 8 char filename    */
  251.     strcat(username, ".adv");
  252.     restfd = fopen(username, "rb");
  253.     if (restfd == NULL) {
  254.         printf("Sorry, I can't open the file...%s\n", \
  255.                username);
  256.         exit();
  257.     }
  258.     for (sptr = &turns; sptr < &lastglobal; sptr++) {
  259.         if ((c = fgetc(restfd))  ==  -1) {
  260.             printf("Read error on save file...%s\n", \
  261.                    username);
  262.             exit();
  263.         }
  264.         *sptr = c;
  265.     }
  266.     if (fclose(restfd)  ==  -1) {
  267.         printf("Warning -- can't close save file...%s\n", \
  268.                username);
  269.     }
  270. }
  271.  
  272.  
  273.