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