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