home *** CD-ROM | disk | FTP | other *** search
/ Assassins - Ultimate CD Games Collection 4 / Assassins 4 (1999)(Weird Science).iso / misc / omega / source / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-02  |  15.1 KB  |  658 lines

  1. /* omega (c) 1987,1988,1989 by Laurence Raphael Brothers */
  2. /* file.c */
  3. /* functions with file access in them. Also some direct calls to
  4.    curses functions */
  5.  
  6. #ifdef MSDOS
  7. # include "curses.h"
  8. #else
  9. # ifdef AMIGA
  10. # else
  11. #  include <curses.h>
  12. # endif
  13. # include <sys/types.h>
  14. # include <unistd.h>
  15. # include <sys/file.h>
  16. # include <fcntl.h>
  17. # include <errno.h>
  18. #endif
  19.  
  20. #include "glob.h"
  21.  
  22. FILE *checkfopen(filestring,optionstring)
  23. char *filestring,*optionstring;
  24. {
  25.   FILE *fd;
  26.   char response;
  27.  
  28.   change_to_game_perms();
  29.   fd = fopen(filestring,optionstring);
  30.   clearmsg();
  31.   while (fd == NULL) {
  32.     print3("Warning! Error opening file:");
  33.     nprint3(filestring);
  34.     print1(" Abort or Retry? [ar] ");
  35.     do response = (char) mcigetc();
  36.     while ((response != 'a') && (response != 'r'));
  37.     if (response == 'r') fd = fopen(filestring,optionstring);
  38.     else {
  39.       print2("Sorry 'bout that.... Saving character, then quitting.");
  40.       morewait();
  41.       save(optionp(COMPRESS_OPTION), TRUE);
  42.       endgraf();
  43.       exit(0);
  44.     }
  45.   }
  46.   return(fd);
  47. }
  48.  
  49. void commandlist()
  50. {
  51.   strcpy(Str1,Omegalib);
  52.   if (Current_Environment == E_COUNTRYSIDE)
  53.     strcat(Str1,"help13.txt");
  54.   else strcat(Str1,"help12.txt");
  55.   displayfile(Str1);
  56.   xredraw();
  57. }
  58.  
  59.  
  60. void user_intro()
  61. {
  62.   strcpy(Str1,Omegalib);
  63.   strcat(Str1,"intro.txt");
  64.   displaycryptfile(Str1);
  65.   xredraw();
  66. }
  67.  
  68. void show_license()
  69. {
  70.   strcpy(Str1,Omegalib);
  71.   strcat(Str1,"license.txt");
  72.   displayfile(Str1);
  73.   xredraw();
  74. }
  75.  
  76.  
  77.  
  78. void abyss_file()
  79. {
  80.   strcpy(Str1,Omegalib);
  81.   strcat(Str1,"abyss.txt");
  82.   displaycryptfile(Str1);
  83. }
  84.  
  85.  
  86.  
  87.  
  88. void inv_help()
  89. {
  90.   strcpy(Str1,Omegalib);
  91.   strcat(Str1,"help3.txt");
  92.   displayfile(Str1);
  93.   xredraw();
  94. }
  95.  
  96.  
  97.  
  98. void combat_help()
  99. {
  100.   strcpy(Str1,Omegalib);
  101.   strcat(Str1,"help5.txt");
  102.   displayfile(Str1);
  103.   menuclear();
  104. }
  105.  
  106.  
  107.  
  108.  
  109. void cityguidefile()
  110. {
  111.   strcpy(Str1,Omegalib);
  112.   strcat(Str1,"scroll2.txt");
  113.   displaycryptfile(Str1);
  114.   xredraw();
  115. }
  116.  
  117.  
  118. void wishfile()
  119. {
  120.   strcpy(Str1,Omegalib);
  121.   strcat(Str1,"scroll3.txt");
  122.   displaycryptfile(Str1);
  123.   xredraw();
  124. }
  125.  
  126. void adeptfile()
  127. {
  128.   strcpy(Str1,Omegalib);
  129.   strcat(Str1,"scroll4.txt");
  130.   displaycryptfile(Str1);
  131.   xredraw();
  132. }
  133.  
  134. void theologyfile()
  135. {
  136.   strcpy(Str1,Omegalib);
  137.   strcat(Str1,"scroll1.txt");
  138.   displaycryptfile(Str1);
  139.   xredraw();
  140. }
  141.  
  142.  
  143. void showmotd()
  144. {
  145.   strcpy(Str1,Omegalib);
  146.   strcat(Str1,"motd.txt");
  147.   displayfile(Str1);
  148. }
  149.  
  150.  
  151. void lock_score_file()
  152. {
  153. #ifndef MSDOS
  154.     int lock, attempts, thispid, lastpid = 0;
  155.     FILE *lockfile;
  156.  
  157.     strcpy(Str1,Omegalib);
  158.     strcat(Str1,"omega.hi.lock");
  159.     do
  160.     {
  161.     lock = open(Str1, O_CREAT|O_EXCL, 0600);    /* create lock file */
  162.     if (lock < 0 && errno == EEXIST)
  163.     {
  164.         lockfile = fopen(Str1, "rb");
  165.         if (lockfile)
  166.         {
  167.         fscanf(lockfile, "%d", &thispid);
  168.         fclose(lockfile);
  169.         if (thispid != lastpid)
  170.             attempts = 0;
  171.         lastpid = thispid;
  172.         }
  173.         attempts++;
  174.         if (attempts > 10)    /* assume that lock file has been abandoned */
  175.         unlink(Str1);    /* so we unlink it ourselves - ugly...    */
  176.         else
  177.         sleep(2);
  178.     }
  179.     else if (lock < 0)    /* oops - something very wrong */
  180.         return;
  181.     } while (lock < 0);
  182.     sprintf(Str1, "%d", getpid());
  183.     write(lock, Str1, strlen(Str1));
  184.     close(lock);
  185. #endif
  186. }
  187.  
  188. void unlock_score_file()
  189. {
  190. #ifndef MSDOS
  191.     strcpy(Str1,Omegalib);
  192.     strcat(Str1,"omega.hi.lock");
  193.     unlink(Str1);
  194. #endif
  195. }
  196.  
  197. void showscores()
  198. {
  199.   FILE *fd;
  200.   int i;
  201.  
  202.   lock_score_file();
  203.   strcpy(Str1,Omegalib);
  204.   strcat(Str1,"omega.hi");
  205.   fd = checkfopen(Str1,"rb");
  206.   filescanstring(fd,Hiscorer);
  207.   filescanstring(fd,Hidescrip);
  208.   fscanf(fd,"%ld %d %d\n",&Hiscore,&Hilevel,&Hibehavior);
  209.   for(i=1;i<7;i++) {
  210.     filescanstring(fd,Priest[i]);
  211.     fscanf(fd,"%d %d\n",&(Priestlevel[i]),&(Priestbehavior[i]));
  212.   }
  213.   filescanstring(fd,Shadowlord);
  214.   fscanf(fd,"%d %d\n",&Shadowlordlevel,&Shadowlordbehavior);
  215.   filescanstring(fd,Commandant);
  216.   fscanf(fd,"%d %d\n",&Commandantlevel,&Commandantbehavior);
  217.   filescanstring(fd,Archmage);
  218.   fscanf(fd,"%d %d\n",&Archmagelevel,&Archmagebehavior);
  219.   filescanstring(fd,Prime);
  220.   fscanf(fd,"%d %d\n",&Primelevel,&Primebehavior);
  221.   filescanstring(fd,Champion);
  222.   fscanf(fd,"%d %d\n",&Championlevel,&Championbehavior);
  223.   filescanstring(fd,Duke);
  224.   fscanf(fd,"%d %d\n",&Dukelevel,&Dukebehavior);
  225.   filescanstring(fd,Chaoslord);
  226.   fscanf(fd,"%d %d %d\n",&Chaoslordlevel,&Chaos,&Chaoslordbehavior);
  227.   filescanstring(fd,Lawlord);
  228.   fscanf(fd,"%d %d %d\n",&Lawlordlevel,&Law,&Lawlordbehavior);
  229.   filescanstring(fd,Justiciar);
  230.   fscanf(fd,"%d %d\n",&Justiciarlevel,&Justiciarbehavior);
  231.   fclose(fd);
  232.   unlock_score_file();
  233.   clear();
  234.   printw("High Score: %ld",Hiscore);
  235.   printw(", by %s (%s)",Hiscorer,levelname(Hilevel));
  236.   printw("\n%s\n",Hidescrip);
  237.   printw("\nLord of Chaos: %s (%s)",Chaoslord,levelname(Chaoslordlevel));
  238.   printw("\nLord of Law: %s (%s)",Lawlord,levelname(Lawlordlevel));
  239.   printw("\n\nDuke of Rampart:              ");
  240.   printw("%s (%s)",Duke,levelname(Dukelevel));
  241.   printw("\nJusticiar:                    ");
  242.   printw("%s (%s)",Justiciar,levelname(Justiciarlevel));
  243.   printw("\nCommandant:                   ");
  244.   printw("%s (%s)",Commandant,levelname(Commandantlevel));
  245.   printw("\nChampion:                     ");
  246.   printw("%s (%s)",Champion,levelname(Championlevel));
  247.   printw("\nArchmage:                     ");
  248.   printw("%s (%s)",Archmage,levelname(Archmagelevel));
  249.   printw("\nPrime Sorceror:               ");
  250.   printw("%s (%s)",Prime,levelname(Primelevel));
  251.   printw("\nShadowlord:                   ");
  252.   printw("%s (%s)",Shadowlord,levelname(Shadowlordlevel));
  253.   printw("\n\nHigh Priests:");
  254.   printw("\n of Odin:                     ");
  255.   printw("%s (%s)",Priest[ODIN],levelname(Priestlevel[ODIN]));
  256.   printw("\n of Set:                      ");
  257.   printw("%s (%s)",Priest[SET],levelname(Priestlevel[SET]));
  258.   printw("\n of Athena:                   ");
  259.   printw("%s (%s)",Priest[ATHENA],levelname(Priestlevel[ATHENA]));
  260.   printw("\n of Hecate:                   ");
  261.   printw("%s (%s)",Priest[HECATE],levelname(Priestlevel[HECATE]));
  262.   printw("\n of the Lords of Destiny:     ");
  263.   printw("%s (%s)",Priest[DESTINY],levelname(Priestlevel[DESTINY]));
  264.   printw("\nThe ArchDruid:                ");
  265.   printw("%s (%s)",Priest[DRUID],levelname(Priestlevel[DRUID]));
  266.   printw("\n\nHit any key to continue.");
  267.   refresh();
  268.   wgetch(stdscr);
  269. }
  270.  
  271. /* Writes a new high-score file, with the nominated npc as whatever it is */
  272. /* in this particular game, but the others as they appear in the file. */
  273. void save_hiscore_npc(npc)
  274. int npc;
  275. {
  276.   FILE *infile, *outfile;
  277.   char buffer[80];
  278.   int i;
  279.  
  280.   if (gamestatusp(CHEATED))
  281.       return;
  282.   lock_score_file();
  283.   strcpy(Str1,Omegalib);
  284.   strcat(Str1,"omega.hi");
  285.   infile = checkfopen(Str1,"rb");
  286.   strcpy(Str2,Omegalib);
  287. #ifdef MSDOS
  288.   strcat(Str2,"omegahi.new");    /* stupid 8.3 msdos filename limit */
  289. #else
  290.   strcat(Str2,"omega.hi.new");
  291. #endif
  292.   outfile = checkfopen(Str2,"wb");
  293.   for (i = 0; i < 16; i++) {
  294.     if (npc == i) {
  295.       switch (i) {
  296.     case 0:
  297.       fprintf(outfile,"%s\n%s\n%ld %d %d\n",Hiscorer, Hidescrip, Hiscore,
  298.         Hilevel, Hibehavior);
  299.       break;
  300.     case 1: case 2: case 3: case 4: case 5: case 6:
  301.       fprintf(outfile,"%s\n%d %d\n", Priest[i], Priestlevel[i],
  302.         Priestbehavior[i]);
  303.       break;
  304.     case 7:
  305.       fprintf(outfile,"%s\n%d %d\n", Shadowlord, Shadowlordlevel,
  306.         Shadowlordbehavior);
  307.       break;
  308.     case 8:
  309.       fprintf(outfile,"%s\n%d %d\n", Commandant, Commandantlevel,
  310.         Commandantbehavior);
  311.       break;
  312.     case 9:
  313.       fprintf(outfile,"%s\n%d %d\n", Archmage, Archmagelevel,
  314.         Archmagebehavior);
  315.       break;
  316.     case 10:
  317.       fprintf(outfile,"%s\n%d %d\n", Prime, Primelevel, Primebehavior);
  318.       break;
  319.     case 11:
  320.       fprintf(outfile,"%s\n%d %d\n", Champion, Championlevel,
  321.         Championbehavior);
  322.       break;
  323.     case 12:
  324.       fprintf(outfile,"%s\n%d %d\n", Duke, Dukelevel, Dukebehavior);
  325.       break;
  326.     case 13:
  327.       fprintf(outfile,"%s\n%d %d %d\n",Chaoslord, Chaoslordlevel, Chaos,
  328.         Chaoslordbehavior);
  329.       break;
  330.     case 14:
  331.       fprintf(outfile,"%s\n%d %d %d\n", Lawlord, Lawlordlevel, Law,
  332.         Lawlordbehavior);
  333.       break;
  334.     case 15:
  335.       fprintf(outfile,"%s\n%d %d\n", Justiciar, Justiciarlevel,
  336.         Justiciarbehavior);
  337.       break;
  338.       }
  339.     }
  340.     if (i == 0) {
  341.       fgets(buffer, 80, infile);
  342.       if (i != npc)
  343.     fputs(buffer, outfile);
  344.     }
  345.     fgets(buffer, 80, infile);
  346.     if (i != npc)
  347.       fputs(buffer, outfile);
  348.     fgets(buffer, 80, infile);
  349.     if (i != npc)
  350.       fputs(buffer, outfile);
  351.   }
  352.   fclose(infile);
  353.   fclose(outfile);
  354.   unlink(Str1);
  355. #if defined(MSDOS) || defined(AMIGA)
  356.   rename(Str2, Str1);
  357. #else
  358.   link(Str2, Str1);
  359.   unlink(Str2);    /* renames, but sys-V doesn't have rename()... */
  360. #endif
  361.   unlock_score_file();
  362. }
  363.  
  364. void checkhigh(descrip,behavior)
  365. char *descrip;
  366. int behavior;
  367. {
  368.   long points;
  369.  
  370.   if (FixedPoints > 0) points = FixedPoints;
  371.   else points = calc_points();
  372.   if (!gamestatusp(CHEATED)) {
  373.     if (Hiscore < points) {
  374.       strcpy(Hiscorer, Player.name);
  375.       strcpy(Hidescrip, descrip);
  376.       Hiscore = points;
  377.       Hilevel = Player.level;
  378.       Hibehavior = behavior;
  379.       save_hiscore_npc(0);
  380.       mprint("Yow! A new high score!");
  381.       morewait();
  382.     }
  383.     if (Player.alignment < Chaos) {
  384.       strcpy(Chaoslord, Player.name);
  385.       Chaoslordlevel = Player.level;
  386.       Chaos = Player.alignment;
  387.       Chaoslordbehavior = behavior;
  388.       save_hiscore_npc(13);
  389.       mprint("Criminy! A new Lord of Chaos!");
  390.       morewait();
  391.     }
  392.     if (Player.alignment > Law) {
  393.       strcpy(Lawlord, Player.name);
  394.       Lawlordlevel = Player.level;
  395.       Law = Player.alignment;
  396.       Lawlordbehavior = behavior;
  397.       save_hiscore_npc(14);
  398.       mprint("Gosh! A new Lord of Law!");
  399.       morewait();
  400.     }
  401.   }
  402. }
  403.  
  404. void extendlog(descrip,lifestatus)
  405. char *descrip;
  406. int lifestatus;
  407. {
  408.   FILE *fd;
  409.   int npcbehavior;
  410.  
  411.   if ((Player.level > 0) && (! gamestatusp(CHEATED))) {
  412.     change_to_game_perms();
  413.     npcbehavior=fixnpc(lifestatus);
  414.     checkhigh(descrip,npcbehavior);
  415.     strcpy(Str1,Omegalib);
  416.     strcat(Str1,"omega.log");
  417.     fd = checkfopen(Str1,"a");
  418.     fprintf(fd, " %d %d %d %s\n", lifestatus, Player.level, npcbehavior,
  419.       Player.name);
  420.     fclose(fd);
  421.   }
  422. }
  423.  
  424.  
  425.  
  426.  
  427.  
  428. #ifndef MSDOS
  429. /* reads a string from a file. If it is a line with more than 80 char's,
  430.    then remainder of line to \n is consumed */
  431. void filescanstring(fd,fstr)
  432. FILE *fd;
  433. char *fstr;
  434. {
  435.   int i= -1;
  436.   int byte='x';
  437.   while ((i<80) && (byte != '\n') && (byte != EOF)) {
  438.     i++;
  439.     byte=fgetc(fd);
  440.     fstr[i] = byte;
  441.   } 
  442.   if (byte != '\n')
  443.     while((byte!='\n') && (byte != EOF))
  444.       byte=fgetc(fd);
  445.   fstr[i]=0;
  446. }
  447. #endif
  448.  
  449. #ifdef MSDOS
  450. int test_file_access(file_name, mode)
  451. char *file_name;
  452. char mode;
  453. {
  454.     FILE *fd;
  455.  
  456.     if (mode == 'r')
  457.     fd = fopen(file_name, "r");
  458.     else
  459.     fd = fopen(file_name, "r+");
  460.     if (!fd)
  461.     return 0;
  462.     fclose(fd);
  463.     return 1;
  464. }
  465. #else
  466. int test_file_access(file_name, mode)
  467. char *file_name;
  468. char mode;
  469. {
  470.     int fd;
  471.  
  472.     if (mode == 'r')
  473.     fd = open(file_name, O_RDONLY, 0);
  474.     else
  475.     fd = open(file_name, O_RDWR, 0);
  476.     if (fd < 0)
  477.     return 0;
  478.     close(fd);
  479.     return 1;
  480. }
  481. #endif
  482.  
  483. char *required_file_list[] =
  484. {
  485.   "city.dat", "country.dat", "dlair.dat", "misle.dat", "court.dat",
  486.   "speak.dat", "temple.dat", "abyss.dat", "village1.dat", "village2.dat",
  487.   "village3.dat", "village4.dat", "village5.dat", "village6.dat",
  488.   "home1.dat", "home2.dat", "home3.dat", "arena.dat", "maze1.dat",
  489.   "maze2.dat", "maze3.dat", "maze4.dat", "omega.hi", "omega.log", "motd.txt",
  490.   "license.txt", "circle.dat", NULL
  491. };
  492.  
  493. char *optional_file_list[] =
  494. {
  495.   "help1.txt", "help2.txt", "help3.txt", "help4.txt", "help5.txt",
  496.   "help6.txt", "help7.txt", "help8.txt", "help9.txt", "help10.txt",
  497.   "help11.txt", "help12.txt", "help13.txt", "abyss.txt", "scroll1.txt",
  498.   "scroll2.txt", "scroll3.txt", "scroll4.txt", NULL
  499. };
  500.  
  501. /* Checks existence of omega data files */
  502. /* Returns 1 if OK, 0 if impossible to run, -1 if possible but not OK */
  503. int filecheck()
  504. {
  505.     int impossible=FALSE,badbutpossible=FALSE;
  506.     int endpos;
  507.     int file;
  508.  
  509.     strcpy(Str1, Omegalib);
  510.     endpos = strlen(Str1);
  511.     for (file = 0; required_file_list[file]; file++)
  512.     {
  513.     strcpy(&(Str1[endpos]), required_file_list[file]);
  514.     if ((strcmp(required_file_list[file], "omega.hi") == 0 ||
  515.         strcmp(required_file_list[file], "omega.log") == 0) &&
  516.         test_file_access(Str1, 'w') == 0)
  517.     {
  518.         impossible = TRUE;
  519.         printf("\nError! File not appendable or accessible: %s", Str1);
  520.     }
  521.     else if (test_file_access(Str1, 'r') == 0)
  522.     {
  523.         impossible = TRUE;
  524.         printf("\nError! File not accessible: %s", Str1);
  525.     }
  526.     }
  527.     for (file = 0; optional_file_list[file]; file++)
  528.     {
  529.     strcpy(&(Str1[endpos]), optional_file_list[file]);
  530.     if (test_file_access(Str1, 'r') == 0)
  531.     {
  532.         badbutpossible = TRUE;
  533.         printf("\nWarning! File not accessible: %s", Str1);
  534.     }
  535.     }
  536.     if (impossible) {
  537.     printf("\nFurther execution is impossible. Sorry.");
  538.     if (strcmp(Omegalib, OMEGALIB))
  539.       printf("\nEnvironment variable OMEGALIB badly set\n");
  540.     else {
  541.       printf("\nOMEGALIB may be badly #defined in odefs.h\n");
  542. #ifndef FIXED_OMEGALIB
  543.       printf("\nYou can set the environment variable OMEGALIB to\n");
  544.       printf("the location of the omegalib directory.\n");
  545. #endif
  546.     }
  547.     return(0);
  548.     }
  549.     else if (badbutpossible) {
  550.     printf("\nFurther execution may cause anomalous behavior.");
  551.     printf("\nContinue anyhow? [yn] ");
  552.     if (getchar()=='y') return(-1);
  553.     else return(0);
  554.     }
  555.     else return(1);
  556. }
  557.  
  558. /* display a file given a string name of file */
  559. void displayfile(filestr)
  560. char *filestr;
  561. {
  562.   FILE *fd = checkfopen(filestr,"r");
  563.   int c,d=' ';
  564.   int x,y;
  565.   clear();
  566.   refresh();
  567.   c = fgetc(fd);
  568.   while ((c != EOF)&&((char) d != 'q')&&((char) d!=ESCAPE)) {
  569.     getyx(stdscr,&y,&x);
  570.     if (y > ScreenLength) { 
  571.       printw("\n-More-");
  572.       refresh();
  573.       d = wgetch(stdscr);
  574.       clear();
  575.     }
  576.     printw("%c",(char) c);
  577.     c = fgetc(fd);
  578.   }
  579.   if (((char) d != 'q')&&((char) d!=ESCAPE)) {
  580.     printw("\n-Done-");
  581.     refresh();
  582.     getch();
  583.   }
  584.   clear();
  585.   refresh();
  586.   fclose(fd);
  587. }    
  588.  
  589. /* display a file given a string name of file */
  590. void displaycryptfile(filestr)
  591. char *filestr;
  592. {
  593.   FILE *fd = checkfopen(filestr,"rb");
  594.   int c,d=' ';
  595.   int x,y;
  596.   char key = 100;
  597.  
  598.   clear();
  599.   refresh();
  600.   c = fgetc(fd);
  601.   while ((c != EOF)&&((char) d != 'q')&&((char) d!=ESCAPE)) {
  602.     getyx(stdscr,&y,&x);
  603.     if (y > ScreenLength) { 
  604.       printw("\n-More-");
  605.       refresh();
  606.       d = wgetch(stdscr);
  607.       clear();
  608.     }
  609.     key = ((unsigned char) c)^key;
  610.     printw("%c", key);
  611.     c = fgetc(fd);
  612.   }
  613.   if (((char) d != 'q')&&((char) d!=ESCAPE)) {
  614.     printw("\n-Done-");
  615.     refresh();
  616.     getch();
  617.   }
  618.   clear();
  619.   refresh();
  620.   fclose(fd);
  621. }    
  622.  
  623. /* copy a file given a string name of file */
  624. void copyfile(srcstr)
  625. char *srcstr;
  626. {
  627.   char deststr[80];
  628.   char buffer[STRING_LEN];
  629.   FILE *in, *out;
  630.  
  631.   print1("Enter name of file to create: ");
  632.   strcpy(deststr,msgscanstring());
  633.   if (strlen(deststr) == 0) {
  634.     print2("Aborting...");
  635.     morewait();
  636.     return;
  637.   }
  638.   in = checkfopen(srcstr, "rb");
  639.   change_to_user_perms();
  640.   out = fopen(deststr, "wb");
  641.   if (!out) {
  642.     sprintf(buffer, "Unable to write to file %s - Aborting.", deststr);
  643.     print2(buffer);
  644.     change_to_game_perms();
  645.     morewait();
  646.     fclose(in);
  647.     return;
  648.   }
  649.   print2("Copying file....");
  650.   while (fgets(buffer, STRING_LEN, in))
  651.     fputs(buffer, out);
  652.   fclose(in);
  653.   fclose(out);
  654.   change_to_game_perms();
  655.   print3("Done.");
  656. }    
  657.  
  658.