home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / larn.t.Z / larn.t / diag.c < prev    next >
Text File  |  1988-11-13  |  10KB  |  332 lines

  1. /*    diag.c        Larn is copyrighted 1986 by Noah Morgan. */
  2. # ifndef OSK
  3. #include <sys/types.h>
  4. #include <sys/times.h>
  5. #include <sys/stat.h>
  6. # else OSK
  7. #include <types.h>
  8. #include <time.h>
  9. #include <stat.h>
  10. # endif OSK
  11.  
  12. #include "header.h"
  13. extern long int initialtime;
  14. extern int rmst,maxitm,lasttime;
  15. extern char nosignal;
  16. # ifndef OSK
  17. static struct tms cputime;
  18. # endif OSK
  19. /*
  20.     ***************************
  21.     DIAG -- dungeon diagnostics
  22.     ***************************
  23.  
  24.     subroutine to print out data for debugging
  25.  */
  26. #ifdef EXTRA
  27. static int rndcount[16];
  28. diag()
  29.     {
  30.     register int i,j;
  31.     int hit,dam;
  32.     cursors();  lwclose();
  33.     if (lcreat(diagfile) < 0)    /*    open the diagnostic file    */
  34.         {
  35.         lcreat((char*)0); lprcat("\ndiagnostic failure\n"); return(-1);
  36.         }
  37.  
  38. # ifndef OSK
  39.     Write(1,"\nDiagnosing . . .\n",18);
  40. # else OSK
  41.     write(1,"\nDiagnosing . . .\n",18);
  42. # endif OSK
  43.     lprcat("\n\nBeginning of DIAG diagnostics ----------\n");
  44.  
  45. /*    for the character attributes    */
  46.  
  47.     lprintf("\n\nPlayer attributes:\n\nHit points: %2d(%2d)",(long)c[HP],(long)c[HPMAX]);
  48.     lprintf("\ngold: %d  Experience: %d  Character level: %d  Level in caverns: %d",
  49.         (long)c[GOLD],(long)c[EXPERIENCE],(long)c[LEVEL],(long)level);
  50.     lprintf("\nTotal types of monsters: %d",(long)MAXMONST+8);
  51.  
  52.     lprcat("\f\nHere's the dungeon:\n\n");
  53.  
  54.     i=level;
  55.     for (j=0; j<MAXLEVEL+MAXVLEVEL; j++)
  56.         {
  57.         newcavelevel(j);
  58.         lprintf("\nMaze for level %s:\n",levelname[level]);
  59.         diagdrawscreen();
  60.         }
  61.     newcavelevel(i);
  62.  
  63.     lprcat("\f\nNow for the monster data:\n\n");
  64.     lprcat("   Monster Name      LEV  AC   DAM  ATT  DEF    GOLD   HP     EXP   \n");
  65.     lprcat("--------------------------------------------------------------------------\n");
  66.     for (i=0; i<=MAXMONST+8; i++)
  67.         {
  68.         lprintf("%19s  %2d  %3d ",monster[i].name,(long)monster[i].level,(long)monster[i].armorclass);
  69.         lprintf(" %3d  %3d  %3d  ",(long)monster[i].damage,(long)monster[i].attack,(long)monster[i].defense);
  70.         lprintf("%6d  %3d   %6d\n",(long)monster[i].gold,(long)monster[i].hitpoints,(long)monster[i].experience);
  71.         }
  72.  
  73.     lprcat("\n\nHere's a Table for the to hit percentages\n");
  74.     lprcat("\n     We will be assuming that players level = 2 * monster level");
  75.     lprcat("\n     and that the players dexterity and strength are 16.");
  76.     lprcat("\n    to hit: if (rnd(22) < (2[monst AC] + your level + dex + WC/8 -1)/2) then hit");
  77.     lprcat("\n    damage = rund(8) + WC/2 + STR - c[HARDGAME] - 4");
  78.     lprcat("\n    to hit:  if rnd(22) < to hit  then player hits\n");
  79.     lprcat("\n    Each entry is as follows:  to hit / damage / number hits to kill\n");
  80.     lprcat("\n          monster     WC = 4         WC = 20        WC = 40");
  81.     lprcat("\n---------------------------------------------------------------");
  82.     for (i=0; i<=MAXMONST+8; i++)
  83.         {
  84.         hit = 2*monster[i].armorclass+2*monster[i].level+16;
  85.         dam = 16 - c[HARDGAME];
  86.         lprintf("\n%20s   %2d/%2d/%2d       %2d/%2d/%2d       %2d/%2d/%2d",
  87.                     monster[i].name,
  88.                     (long)(hit/2),(long)max(0,dam+2),(long)(monster[i].hitpoints/(dam+2)+1),
  89.                     (long)((hit+2)/2),(long)max(0,dam+10),(long)(monster[i].hitpoints/(dam+10)+1),
  90.                     (long)((hit+5)/2),(long)max(0,dam+20),(long)(monster[i].hitpoints/(dam+20)+1));
  91.         }
  92.  
  93.     lprcat("\n\nHere's the list of available potions:\n\n");
  94.     for (i=0; i<MAXPOTION; i++)    lprintf("%20s\n",&potionname[i][1]);
  95.     lprcat("\n\nHere's the list of available scrolls:\n\n");
  96.     for (i=0; i<MAXSCROLL; i++)    lprintf("%20s\n",&scrollname[i][1]);
  97.     lprcat("\n\nHere's the spell list:\n\n");
  98.     lprcat("spell          name           description\n");
  99.     lprcat("-------------------------------------------------------------------------------------------\n\n");
  100.     for (j=0; j<SPNUM; j++)
  101.         {
  102.         lprc(' ');    lprcat(spelcode[j]);
  103.         lprintf(" %21s  %s\n",spelname[j],speldescript[j]); 
  104.         }
  105.  
  106.     lprcat("\n\nFor the c[] array:\n");
  107.     for (j=0; j<100; j+=10)
  108.         {
  109.         lprintf("\nc[%2d] = ",(long)j); for (i=0; i<9; i++) lprintf("%5d ",(long)c[i+j]);
  110.         }
  111.  
  112.     lprcat("\n\nTest of random number generator ----------------");
  113.     lprcat("\n    for 25,000 calls divided into 16 slots\n\n");
  114.  
  115.     for (i=0; i<16; i++)  rndcount[i]=0;
  116.     for (i=0; i<25000; i++)    rndcount[rund(16)]++;
  117.     for (i=0; i<16; i++)  { lprintf("  %5d",(long)rndcount[i]); if (i==7) lprc('\n'); }
  118.  
  119.     lprcat("\n\n");            lwclose();
  120.     lcreat((char*)0);        lprcat("Done Diagnosing . . .");
  121.     return(0);
  122.     }
  123. /*
  124.     subroutine to count the number of occurrences of an object
  125.  */
  126. dcount(l)
  127.     int l;
  128.     {
  129.     register int i,j,p;
  130.     int k;
  131.     k=0;
  132.     for (i=0; i<MAXX; i++)
  133.         for (j=0; j<MAXY; j++)
  134.             for (p=0; p<MAXLEVEL; p++)
  135.                 if (cell[p*MAXX*MAXY+i*MAXY+j].item == l) k++;
  136.     return(k);
  137.     }
  138.  
  139. /*
  140.     subroutine to draw the whole screen as the player knows it
  141.  */
  142. diagdrawscreen()
  143.     {
  144.     register int i,j,k;
  145.  
  146.     for (i=0; i<MAXY; i++)
  147.  
  148. /*    for the east west walls of this line    */
  149.         {
  150.         for (j=0; j<MAXX; j++)    if (k=mitem[j][i]) lprc(monstnamelist[k]); else
  151.                                 lprc(objnamelist[item[j][i]]);
  152.         lprc('\n');
  153.         }
  154.     }
  155. #endif
  156.  
  157. /*
  158.     to save the game in a file
  159.  */
  160. static long int zzz=0;
  161. savegame(fname)
  162.     char *fname;
  163.     {
  164.     register int i,k;
  165.     register struct sphere *sp;
  166.     struct stat statbuf;
  167.     nosignal=1;  lflush();    savelevel();
  168.     ointerest();
  169.     if (lcreat(fname) < 0)
  170.         {
  171.         lcreat((char*)0); lprintf("\nCan't open file <%s> to save game\n",fname);
  172.         nosignal=0;  return(-1);
  173.         }
  174.  
  175.     set_score_output();
  176.     lWrite((char*)beenhere,MAXLEVEL+MAXVLEVEL);
  177.     for (k=0; k<MAXLEVEL+MAXVLEVEL; k++)
  178.         if (beenhere[k])
  179.             lWrite((char*)&cell[k*MAXX*MAXY],sizeof(struct cel)*MAXY*MAXX);
  180. # ifndef OSK
  181.     times(&cputime);    /* get cpu time */
  182.     c[CPUTIME] += (cputime.tms_utime+cputime.tms_stime)/60;
  183. # else OSK
  184.     c[CPUTIME] = 0;
  185. # endif OSK
  186.     lWrite((char*)&c[0],100*sizeof(long));
  187.     lprint((long)gtime);        lprc(level);
  188.     lprc(playerx);        lprc(playery);
  189.     lWrite((char*)iven,26);    lWrite((char*)ivenarg,26*sizeof(short));
  190.     for (k=0; k<MAXSCROLL; k++)  lprc(scrollname[k][0]);
  191.     for (k=0; k<MAXPOTION; k++)  lprc(potionname[k][0]);
  192.     lWrite((char*)spelknow,SPNUM);         lprc(wizard);
  193.     lprc(rmst);        /*    random monster generation counter */
  194.     for (i=0; i<90; i++)    lprc(itm[i].qty);
  195.     lWrite((char*)course,25);            lprc(cheat);        lprc(VERSION);
  196.     for (i=0; i<MAXMONST; i++) lprc(monster[i].genocided); /* genocide info */
  197.     for (sp=spheres; sp; sp=sp->p)
  198.         lWrite((char*)sp,sizeof(struct sphere));    /* save spheres of annihilation */
  199.     time(&zzz);            lprint((long)(zzz-initialtime));
  200.     lWrite((char*)&zzz,sizeof(long));
  201.     if (fstat(lfd,&statbuf)< 0) lprint(0L);
  202.     else lprint((long)statbuf.st_ino); /* inode # */
  203.     lwclose();    lastmonst[0] = 0;
  204. #ifndef VT100
  205.     setscroll();
  206. #endif VT100
  207.     lcreat((char*)0);  nosignal=0;
  208.     return(0);
  209.     }
  210.  
  211. restoregame(fname)
  212.     char *fname;
  213.     {
  214.     register int i,k;
  215.     register struct sphere *sp,*sp2;
  216.     struct stat filetimes;
  217.     cursors(); lprcat("\nRestoring . . .");  lflush();
  218.     if (lopen(fname) <= 0)
  219.         {
  220.         lcreat((char*)0); lprintf("\nCan't open file <%s>to restore game\n",fname);
  221.         nap(2000); c[GOLD]=c[BANKACCOUNT]=0;  died(-265); return;
  222.         }
  223.  
  224.     lrfill((char*)beenhere,MAXLEVEL+MAXVLEVEL);
  225.     for (k=0; k<MAXLEVEL+MAXVLEVEL; k++)
  226.         if (beenhere[k])
  227.             lrfill((char*)&cell[k*MAXX*MAXY],sizeof(struct cel)*MAXY*MAXX);
  228.  
  229.     lrfill((char*)&c[0],100*sizeof(long));    gtime = lrint();
  230.     level = c[CAVELEVEL] = lgetc();
  231.     playerx = lgetc();        playery = lgetc();
  232.     lrfill((char*)iven,26);        lrfill((char*)ivenarg,26*sizeof(short));
  233.     for (k=0; k<MAXSCROLL; k++)  scrollname[k][0] = lgetc();
  234.     for (k=0; k<MAXPOTION; k++)  potionname[k][0] = lgetc();
  235.     lrfill((char*)spelknow,SPNUM);        wizard = lgetc();
  236.     rmst = lgetc();            /*    random monster creation flag */
  237.  
  238.     for (i=0; i<90; i++)    itm[i].qty = lgetc();
  239.     lrfill((char*)course,25);            cheat = lgetc();
  240.     if (VERSION != lgetc())        /*  version number  */
  241.         {
  242.         cheat=1;
  243.         lprcat("Sorry, But your save file is for an older version of larn\n");
  244.         nap(2000); c[GOLD]=c[BANKACCOUNT]=0;  died(-266); return;
  245.         }
  246.  
  247.     for (i=0; i<MAXMONST; i++) monster[i].genocided=lgetc(); /* genocide info */
  248.     for (sp=0,i=0; i<c[SPHCAST]; i++)
  249.         {
  250.         sp2 = sp;
  251.         sp = (struct sphere *)malloc(sizeof(struct sphere));
  252.         if (sp==0) { Write(2,"Can't malloc() for sphere space\n",32); break; }
  253.         lrfill((char*)sp,sizeof(struct sphere));    /* get spheres of annihilation */
  254.         sp->p=0;    /* null out pointer */
  255.         if (i==0) spheres=sp;    /* beginning of list */
  256.             else sp2->p = sp;
  257.         }
  258.  
  259.     time(&zzz);
  260.     initialtime = zzz-lrint();
  261.     fstat(fd,&filetimes);    /*    get the creation and modification time of file    */
  262.     lrfill((char*)&zzz,sizeof(long));    zzz += 6;
  263.     if (filetimes.st_ctime > zzz) fsorry();    /*    file create time    */
  264.     else if (filetimes.st_mtime > zzz) fsorry(); /*    file modify time    */
  265.     if (c[HP]<0) { died(284); return; }    /* died a post mortem death */
  266.  
  267.     oldx = oldy = 0;
  268.     i = lrint();  /* inode # */
  269.     if (i && (filetimes.st_ino!=i)) fsorry();
  270.     lrclose();
  271.     if (strcmp(fname,ckpfile) == 0)
  272.         {
  273.         if (lappend(fname) < 0) fcheat();  else { lprc(' '); lwclose(); }
  274.         lcreat((char*)0);
  275.         }
  276.     else if (unlink(fname) < 0) fcheat(); /* can't unlink save file */
  277. /*    for the greedy cheater checker    */
  278.     for (k=0; k<6; k++) if (c[k]>99) greedy();
  279.     if (c[HPMAX]>999 || c[SPELLMAX]>125) greedy();
  280.     if (c[LEVEL]==25 && c[EXPERIENCE]>skill[24]) /* if patch up lev 25 player */
  281.         {
  282.         long tmp;
  283.         tmp = c[EXPERIENCE]-skill[24]; /* amount to go up */
  284.         c[EXPERIENCE] = skill[24];
  285.         raiseexperience((long)tmp);
  286.         }
  287.     getlevel();  lasttime=gtime;
  288.     }
  289.  
  290. /*
  291.     subroutine to not allow greedy cheaters
  292.  */
  293. greedy()
  294.     {
  295. #if WIZID
  296.     if (wizard) return;
  297. #endif
  298.  
  299.     lprcat("\n\nI am so sorry, but your character is a little TOO good!  Since this\n");
  300.     lprcat("cannot normally happen from an honest game, I must assume that you cheated.\n");
  301.     lprcat("In that you are GREEDY as well as a CHEATER, I cannot allow this game\n");
  302.     lprcat("to continue.\n"); nap(5000);  c[GOLD]=c[BANKACCOUNT]=0;  died(-267); return;
  303.     }
  304.  
  305. /*
  306.     subroutine to not allow altered save files and terminate the attempted
  307.     restart
  308.  */
  309. fsorry()
  310.     {
  311.     lprcat("\nSorry, but your savefile has been altered.\n");
  312.     lprcat("However, seeing as I am a good sport, I will let you play.\n");
  313.     lprcat("Be advised though, you won't be placed on the normal scoreboard.");
  314.     cheat = 1;    nap(4000);
  315.     }
  316.  
  317. /*
  318.     subroutine to not allow game if save file can't be deleted
  319.  */
  320. fcheat()
  321.     {
  322. #if WIZID
  323.     if (wizard) return;
  324. #endif
  325.  
  326.     lprcat("\nSorry, but your savefile can't be deleted.  This can only mean\n");
  327.     lprcat("that you tried to CHEAT by protecting the directory the savefile\n");
  328.     lprcat("is in.  Since this is unfair to the rest of the larn community, I\n");
  329.     lprcat("cannot let you play this game.\n");
  330.     nap(5000);  c[GOLD]=c[BANKACCOUNT]=0;  died(-268); return;
  331.     }
  332.