home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / HACKSRC.ZIP / O_INIT.C < prev    next >
C/C++ Source or Header  |  1985-10-16  |  6KB  |  231 lines

  1. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  2. /* o_init.c - version 1.0.3 */
  3.  
  4. #include    "config.h"        /* for typedefs */
  5. #include    "objects.h"
  6. #include    "onames.h"        /* for LAST_GEM */
  7. extern char *index();
  8.  
  9. int
  10. letindex(let) register char let; {
  11. register int i = 0;
  12. register char ch;
  13.     while((ch = obj_symbols[i++]) != 0)
  14.         if(ch == let) return(i);
  15.     return(0);
  16. }
  17.  
  18. init_objects(){
  19. register int i, j, first, last, sum, end;
  20. register char let, *tmp;
  21. #ifdef MSDOS
  22.     /* Indices into the objname[] were stored instead of the actual
  23.      * pointers.  Replace the indices with pointers here.
  24.      */
  25.     ObjectIndicesToPointers();
  26. #endif MSDOS
  27.     /* init base; if probs given check that they add up to 100, 
  28.        otherwise compute probs; shuffle descriptions */
  29.     end = SIZE(objects);
  30.     first = 0;
  31.     while( first < end ) {
  32.         let = objects[first].oc_olet;
  33.         last = first+1;
  34.         while(last < end && objects[last].oc_olet == let
  35.                 && objects[last].oc_name != NULL)
  36.             last++;
  37.         i = letindex(let);
  38.         if((!i && let != ILLOBJ_SYM) || bases[i] != 0)
  39.             error("initialization error");
  40.         bases[i] = first;
  41.  
  42.         if(let == GEM_SYM)
  43.             setgemprobs();
  44.     check:
  45.         sum = 0;
  46.         for(j = first; j < last; j++) sum += objects[j].oc_prob;
  47.         if(sum == 0) {
  48.             for(j = first; j < last; j++)
  49.                 objects[j].oc_prob = (100+j-first)/(last-first);
  50.             goto check;
  51.         }
  52.         if(sum != 100)
  53.             error("init-prob error for %c", let);
  54.  
  55.         if(objects[first].oc_descr != NULL && let != TOOL_SYM){
  56.             /* shuffle, also some additional descriptions */
  57.             while(last < end && objects[last].oc_olet == let)
  58.                 last++;
  59.             j = last;
  60.             while(--j > first) {
  61.                 i = first + rn2(j+1-first);
  62.                 tmp = objects[j].oc_descr;
  63.                 objects[j].oc_descr = objects[i].oc_descr;
  64.                 objects[i].oc_descr = tmp;
  65.             }
  66.         }
  67.         first = last;
  68.     }
  69. }
  70.  
  71. probtype(let) register char let; {
  72. register int i = bases[letindex(let)];
  73. register int prob = rn2(100);
  74.     while((prob -= objects[i].oc_prob) >= 0) i++;
  75.     if(objects[i].oc_olet != let || !objects[i].oc_name)
  76.         panic("probtype(%c) error, i=%d", let, i);
  77.     return(i);
  78. }
  79.  
  80. setgemprobs()
  81. {
  82.     register int j,first;
  83.     extern xchar dlevel;
  84.  
  85.     first = bases[letindex(GEM_SYM)];
  86.  
  87.     for(j = 0; j < 9-dlevel/3; j++)
  88.         objects[first+j].oc_prob = 0;
  89.     first += j;
  90.     if(first >= LAST_GEM || first >= SIZE(objects) ||
  91.         objects[first].oc_olet != GEM_SYM ||
  92.         objects[first].oc_name == NULL)
  93.         printf("Not enough gems? - first=%d j=%d LAST_GEM=%d\n",
  94.             first, j, LAST_GEM);
  95.     for(j = first; j < LAST_GEM; j++)
  96.         objects[j].oc_prob = (20+j-first)/(LAST_GEM-first);
  97. }
  98.  
  99. oinit()            /* level dependent initialization */
  100. {
  101.     setgemprobs();
  102. }
  103.  
  104. extern long *alloc();
  105.  
  106. savenames(fd) register fd; {
  107. register int i;
  108. unsigned len;
  109. #ifdef MSDOS
  110.     /* Before writing the objects, replace pointers into objname[] with
  111.      * indices.
  112.      */
  113.     ObjectPointersToIndices();
  114. #endif MSDOS
  115.     bwrite(fd, (char *) bases, sizeof bases);
  116.     bwrite(fd, (char *) objects, sizeof objects);
  117.     /* as long as we use only one version of Hack/Quest we
  118.        need not save oc_name and oc_descr, but we must save
  119.        oc_uname for all objects */
  120.     for(i=0; i < SIZE(objects); i++) {
  121.         if(objects[i].oc_uname) {
  122.             len = strlen(objects[i].oc_uname)+1;
  123.             bwrite(fd, (char *) &len, sizeof len);
  124.             bwrite(fd, objects[i].oc_uname, len);
  125.         }
  126.     }
  127. }
  128.  
  129. restnames(fd) register fd; {
  130. register int i;
  131. unsigned len;
  132.     mread(fd, (char *) bases, sizeof bases);
  133.     mread(fd, (char *) objects, sizeof objects);
  134. #ifdef MSDOS
  135.     /* After reading the objects, replace indices into objname[] with
  136.      * pointers.
  137.      */
  138.     ObjectIndicesToPointers();
  139. #endif MSDOS
  140.     for(i=0; i < SIZE(objects); i++) if(objects[i].oc_uname) {
  141.         mread(fd, (char *) &len, sizeof len);
  142.         objects[i].oc_uname = (char *) alloc(len);
  143.         mread(fd, objects[i].oc_uname, len);
  144.     }
  145. }
  146.  
  147. dodiscovered()                /* free after Robert Viduya */
  148. {
  149.     extern char *typename();
  150.     register int i, end;
  151.     int    ct = 0;
  152. #ifdef DGK
  153.     char class = -1;
  154.     extern char *let_to_name();
  155. #endif DGK
  156.  
  157.     cornline(0, "Discoveries");
  158.  
  159.     end = SIZE(objects);
  160.     for (i = 0; i < end; i++) {
  161.     if (interesting_to_discover (i)) {
  162.         ct++;
  163. #ifdef DGK
  164.             if (objects[i].oc_olet != class) {
  165.         class = objects[i].oc_olet;
  166.         cornline(1, let_to_name(class));
  167.             }
  168. #endif DGK
  169.         cornline(1, typename(i));
  170.     }
  171.     }
  172.     if (ct == 0) {
  173.     pline ("You haven't discovered anything yet...");
  174.     cornline(3, (char *) 0);
  175.     } else
  176.     cornline(2, (char *) 0);
  177.  
  178.     return(0);
  179. }
  180.  
  181. interesting_to_discover(i)
  182. register int i;
  183. {
  184.     return(
  185.     objects[i].oc_uname != NULL ||
  186.      (objects[i].oc_name_known && objects[i].oc_descr != NULL)
  187.     );
  188. }
  189.  
  190. #ifdef MSDOS
  191. static
  192. ObjectIndicesToPointers()
  193. {
  194.     int i;
  195.     /* the objects have indices into the object_name and object_descr
  196.      * arrays.  Replace the indices with the true pointer value
  197.      */
  198.     for (i = 0; i < SIZE(objects); i++) {
  199.         if (objects[i].oc_name != NULL)
  200.             objects[i].oc_name =
  201.                 objname[(int) (objects[i].oc_name)].name;
  202.         if (objects[i].oc_descr != NULL)
  203.             objects[i].oc_descr =
  204.                 objname[(int) (objects[i].oc_descr)].descr;
  205.     }
  206. }
  207.  
  208. static
  209. ObjectPointersToIndices()
  210. {
  211.     int i,j;
  212.     /* Before writing the objects, replace pointers to object_name and
  213.      * object_descr with indices.
  214.      */
  215.     for (i = 0; i < SIZE(objects); i++) {
  216.         if (objects[i].oc_name != NULL)
  217.             for (j = 0; j < SIZE(objname); j++)
  218.                 if (objects[i].oc_name == objname[j].name) {
  219.                     *((int *)(&objects[i].oc_name)) = j;
  220.                     break;
  221.                 }
  222.         if (objects[i].oc_descr != NULL)
  223.             for (j = 0; j < SIZE(objname); j++)
  224.                 if (objects[i].oc_descr == objname[j].descr) {
  225.                     *((int *)(&objects[i].oc_descr)) = j;
  226.                     break;
  227.                 }
  228.     }
  229. }
  230. #endif MSDOS
  231.