home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / games / adv / database.c < prev    next >
Text File  |  1988-08-26  |  7KB  |  419 lines

  1.  
  2. /*    program DATABASE.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>
  9. #include    <string.h>
  10. #include    <stdlib.h>
  11. #include    "advent.h"
  12. #include    "advdec.h"
  13.  
  14. #define     rindex(a,b)    strrchr(a,b)
  15.  
  16. /*
  17.     Routine to fill travel array for a given location
  18. */
  19. gettrav(loc)
  20. int    loc;
  21. {
  22.     int    i;
  23.     long    t;
  24.     char    atrav[256], *aptr;
  25.  
  26.     strcpy(atrav, cave[loc - 1]);
  27.     while (aptr = rindex(atrav, ','))
  28.         *aptr = '\0';        /* terminate substring    */
  29.     aptr = &atrav[0];
  30.     for (i = 0; i < MAXTRAV; ++i) {
  31.         t = atol(aptr);     /* convert to long int    */
  32.         travel[i].tcond = (t % 1000);
  33.         t /= 1000;
  34.         travel[i].tverb = (t % 1000);
  35.         t /= 1000;
  36.         travel[i].tdest = (t % 1000);
  37.         while (*aptr++)
  38.             ;        /* to next substring    */
  39.         if (!(*aptr)) {
  40.             travel[++i].tdest = -1;/* end of array    */
  41.             if (dbugflg)
  42.             for (i = 0; i < MAXTRAV; ++i)
  43.                 printf("cave[%d] = %d %d %d\n", \
  44.                 loc, travel[i].tdest, \
  45.                 travel[i].tverb, travel[i].tcond);
  46.             return;        /* terminate for loop    */
  47.         }
  48.     }
  49.     bug(33);
  50. }
  51.  
  52. /*
  53.     Function to scan a file up to a specified
  54.     point and either print or return a string.
  55. */
  56. rdupto(fdi, uptoc, print, string)
  57. FILE    *fdi;
  58. char    uptoc, print, *string;
  59. {
  60.     int    c;
  61.  
  62.     while ((c = fgetc(fdi)) != uptoc) {
  63.         if (c == EOF)
  64.             return(0);
  65.         if (c == '\r')
  66.             continue;
  67.         if (print)
  68.             fputc(c, stdout);
  69.         else
  70.             *string++ = c;
  71.     }
  72.     if (!print)
  73.         *string = '\0';
  74.     return(1);
  75. }
  76.  
  77. /*
  78.     Function to read a file skipping
  79.     a given character a specified number
  80.     of times, with or without repositioning
  81.     the file.
  82. */
  83. rdskip(fdi, skipc, n, rewind)
  84. FILE    *fdi;
  85. char    skipc, rewind;
  86. int    n;
  87. {
  88.     int    c;
  89.  
  90.     if (rewind)
  91.         if (fseek(fdi, 0, 0) == -1)
  92.             bug(31);
  93.     while (n--)
  94.         while ((c = fgetc(fdi)) != skipc)
  95.             if (c == EOF)
  96.                 bug(32);
  97. }
  98.  
  99. /*
  100.     Routine to request a yes or no answer to a question.
  101. */
  102. yes(msg1, msg2, msg3)
  103. int    msg1, msg2, msg3;
  104. {
  105.     char    answer[80];
  106.  
  107.     if (msg1)
  108.         rspeak(msg1);
  109.     fputc('>', stdout);
  110.     fgets(answer, 80, stdin);
  111.     if (tolower(answer[0]) == 'n') {
  112.         if (msg3)
  113.             rspeak(msg3);
  114.         return(0);
  115.     }
  116.     if (msg2)
  117.         rspeak(msg2);
  118.     return(1);
  119. }
  120.  
  121. /*
  122.     Print a location description from "advent4.txt"
  123. */
  124. rspeak(msg)
  125. int    msg;
  126. {
  127.     if (msg == 54)
  128.         printf("ok.\n");
  129.     else {
  130.         if (dbugflg)
  131.             printf("Seek loc msg #%d @ %ld\n", msg, idx4[msg]);
  132.         fseek(fd4, idx4[msg - 1], 0);
  133.         rdupto(fd4, '#', 1, 0);
  134.     }
  135.     return;
  136. }
  137.  
  138. /*
  139.     Print an item message for a given state from "advent3.txt"
  140. */
  141. pspeak(item, state)
  142. int    item, state;
  143. {
  144.     fseek(fd3, idx3[item - 1], 0);
  145.     rdskip(fd3, '/', state+2, 0);
  146.     rdupto(fd3, '/', 1, 0);
  147. }
  148.  
  149. /*
  150.     Print a long location description from "advent1.txt"
  151. */
  152. desclg(loc)
  153. int    loc;
  154. {
  155.     fseek(fd1, idx1[loc - 1], 0);
  156.     rdupto(fd1, '#', 1, 0);
  157. }
  158.  
  159. /*
  160.     Print a short location description from "advent2.txt"
  161. */
  162. descsh(loc)
  163. int    loc;
  164. {
  165.     fseek(fd2, idx2[loc - 1], 0);
  166.     rdupto(fd2, '#', 1, 0);
  167. }
  168.  
  169. /*
  170.     look-up vocabulary word in lex-ordered table.  words may have
  171.     two entries with different codes. if minimum acceptable value
  172.     = 0, then return minimum of different codes.  last word CANNOT
  173.     have two entries(due to binary sort).
  174.     word is the word to look up.
  175.     val  is the minimum acceptable value,
  176.         if != 0 return %1000
  177. */
  178. vocab(word, val)
  179. char    *word;
  180. int    val;
  181. {
  182.     int    v1, v2;
  183.  
  184.     if ((v1 = binary(word, wc, MAXWC)) >= 0) {
  185.         v2 = binary(word, wc, MAXWC-1);
  186.         if (v2 < 0)
  187.             v2 = v1;
  188.         if (!val)
  189.             return(wc[v1].acode < wc[v2].acode\
  190.                    ? wc[v1].acode : wc[v2].acode);
  191.         if (val <= wc[v1].acode)
  192.             return(wc[v1].acode % 1000);
  193.         else if (val <= wc[v2].acode)
  194.             return(wc[v2].acode % 1000);
  195.         else
  196.             return(-1);
  197.     }
  198.     else
  199.         return(-1);
  200. }
  201.  
  202. binary(w, wctable, maxwc)
  203. char    *w;
  204. int    maxwc;
  205. struct    wac    wctable[];
  206. {
  207.     int    lo, mid, hi, check;
  208.  
  209.     lo = 0;
  210.     hi = maxwc - 1;
  211.     while (lo <= hi) {
  212.         mid = (lo + hi) / 2;
  213.         if ((check = strcmp(w, wctable[mid].aword)) < 0)
  214.             hi = mid - 1;
  215.         else if (check > 0)
  216.             lo = mid + 1;
  217.         else
  218.             return(mid);
  219.     }
  220.     return(-1);
  221. }
  222.  
  223.  
  224. /*
  225.     Utility Routines
  226. */
  227.  
  228. /*
  229.     Routine to test for darkness
  230. */
  231. dark()
  232. {
  233.     return(!(cond[loc] & LIGHT) &&
  234.         (!prop[LAMP] ||
  235.         !here(LAMP)));
  236. }
  237.  
  238. /*
  239.     Routine to tell if an item is present.
  240. */
  241. here(item)
  242. int    item;
  243. {
  244.     return(place[item] == loc || toting(item));
  245. }
  246.  
  247. /*
  248.     Routine to tell if an item is being carried.
  249. */
  250. toting(item)
  251. int    item;
  252. {
  253.     return(place[item] == -1);
  254. }
  255.  
  256. /*
  257.     Routine to tell if a location causes
  258.     a forced move.
  259. */
  260. forced(atloc)
  261. int    atloc;
  262. {
  263.     return(cond[atloc] == 2);
  264. }
  265.  
  266. /*
  267.     Routine true x% of the time.
  268. */
  269. pct(x)
  270. int    x;
  271. {
  272.     return(rand() % 100 < x);
  273. }
  274.  
  275. /*
  276.     Routine to tell if player is on
  277.     either side of a two sided object.
  278. */
  279. at(item)
  280. int    item;
  281. {
  282.     return(place[item] == loc || fixed[item] == loc);
  283. }
  284.  
  285. /*
  286.     Routine to destroy an object
  287. */
  288. dstroy(obj)
  289. int    obj;
  290. {
  291.     move(obj, 0);
  292. }
  293.  
  294. /*
  295.     Routine to move an object
  296. */
  297. move(obj, where)
  298. int    obj, where;
  299. {
  300.     int    from;
  301.  
  302.     from = (obj<MAXOBJ) ? place[obj] : fixed[obj];
  303.     if (from>0 && from<=300)
  304.         carry(obj, from);
  305.     drop(obj, where);
  306. }
  307.  
  308. /*
  309.     Juggle an object
  310.     currently a no-op
  311. */
  312. juggle(loc)
  313. int    loc;
  314. {
  315. }
  316.  
  317. /*
  318.     Routine to carry an object
  319. */
  320. carry(obj, where)
  321. int    obj, where;
  322. {
  323.     if (obj<MAXOBJ){
  324.         if (place[obj] == -1)
  325.             return;
  326.         place[obj]=-1;
  327.         ++holding;
  328.     }
  329. }
  330.  
  331. /*
  332.     Routine to drop an object
  333. */
  334. drop(obj, where)
  335. int    obj, where;
  336. {
  337.     if (obj<MAXOBJ) {
  338.         if (place[obj] == -1)
  339.             --holding;
  340.         place[obj]=where;
  341.     }
  342.     else
  343.         fixed[obj-MAXOBJ]=where;
  344. }
  345.  
  346. /*
  347.     routine to move an object and return a
  348.     value used to set the negated prop values
  349.     for the repository.
  350. */
  351. put(obj, where, pval)
  352. int    obj, where, pval;
  353. {
  354.     move(obj, where);
  355.     return((-1)-pval);
  356. }
  357. /*
  358.     Routine to check for presence
  359.     of dwarves..
  360. */
  361. dcheck()
  362. {
  363.     int    i;
  364.  
  365.     for (i =1; i < (DWARFMAX-1); ++i)
  366.         if (dloc[i] == loc)
  367.             return(i);
  368.     return(0);
  369. }
  370.  
  371. /*
  372.     Determine liquid in the bottle
  373. */
  374. liq()
  375. {
  376.     int    i, j;
  377.     i=prop[BOTTLE];
  378.     j=-1-i;
  379.     return(liq2(i>j ? i : j));
  380. }
  381.  
  382. /*
  383.     Determine liquid at a location
  384. */
  385. liqloc(loc)
  386. int    loc;
  387. {
  388.     if (cond[loc]&LIQUID)
  389.         return(liq2(cond[loc]&WATOIL));
  390.     else
  391.         return(liq2(1));
  392. }
  393.  
  394. /*
  395.     Convert  0 to WATER
  396.          1 to nothing
  397.          2 to OIL
  398. */
  399. liq2(pbottle)
  400. int    pbottle;
  401. {
  402.     return((1-pbottle)*WATER+(pbottle>>1)*(WATER+OIL));
  403. }
  404.  
  405. /*
  406.     Fatal error routine
  407. */
  408. bug(n)
  409. int    n;
  410. {
  411.     printf("Fatal error number %d\n", n);
  412.     exit(1);
  413. }
  414.  
  415.  
  416.     
  417.  
  418. 
  419.