home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 159_01 / database.c < prev    next >
C/C++ Source or Header  |  1990-05-17  |  6KB  |  379 lines

  1.  
  2. /*  programs in DATABASE.C    no changes for V 1.43            */
  3.  
  4. #include "advent.h"
  5.  
  6. static VOID    NEAR PASCAL DisplayText(char *pTbl[], SHORT sMsg);
  7.  
  8.  
  9. /*  Routine to fill travel array for a given location.    */
  10.  
  11. VOID PASCAL gettrav(SHORT loc)
  12. {
  13.     extern  TRAVTAB    TravTab[];
  14.  
  15.     --loc;
  16.     pTravel  = TravTab[loc].pTrav;
  17.     sTravCnt = TravTab[loc].sTrav;
  18.     return;
  19. }
  20.  
  21. /*  Routine to request a yes or no answer to a question.            */
  22.  
  23. SHORT PASCAL yes(SHORT msg1, SHORT msg2, SHORT msg3)
  24. {
  25.     auto char       answer[80];
  26.  
  27.     if (msg1)
  28.     rspeak(msg1);
  29.     putchar('>');
  30.     gets(answer);
  31.  
  32.     if (tolower(answer[0]) == 'n')
  33.     {
  34.     if (msg3)
  35.         rspeak(msg3);
  36.     return(0);
  37.     }
  38.  
  39.     if (msg2)
  40.     rspeak(msg2);
  41.  
  42.     return(1);
  43. }
  44.  
  45.  
  46. /*  Print a random message from database 6                    */
  47.  
  48. VOID PASCAL rspeak(SHORT msg)
  49. {
  50.     extern  char      *pTextMsg[];
  51.  
  52.     if (dbgflg)
  53.     printf("** rspeak(%d) ** ", msg);
  54.  
  55.     DisplayText(pTextMsg, msg);
  56.  
  57.     return;
  58. }
  59.  
  60.  
  61. /*  Routine to print the message for an item in a given state.            */
  62.  
  63. VOID PASCAL pspeak(SHORT item, SHORT state)
  64. {
  65.     register char     *p;
  66.     extern   char     *pObjDesc[];
  67.  
  68.     if (dbgflg)
  69.     printf("** pspeak(%d,%d) ** ", item, state);
  70.  
  71.     p = pObjDesc[item - 1];
  72.     while (state > -1)
  73.     {
  74.     while (*p && '/' != *p)
  75.         ++p;
  76.  
  77.     if (NUL == *p)
  78.         return;
  79.  
  80.     ++p;
  81.     --state;
  82.     }
  83.  
  84.     while (TRUE)
  85.     {
  86.     if (NUL == *p || '/' == *p)
  87.         break;
  88.  
  89.     putchar(*p);
  90.     ++p;
  91.     }
  92.  
  93.     putchar('\n');
  94.  
  95.     return;
  96. }
  97.  
  98.  
  99. /*  Print the long description of a location                    */
  100.  
  101. VOID PASCAL desclg(SHORT loc)
  102. {
  103.     extern   char     *pLongRmDesc[];
  104.  
  105.     if (dbgflg)
  106.     printf("** desclg(%d) ** ", loc);
  107.  
  108.     DisplayText(pLongRmDesc, loc);
  109.     putchar('\n');
  110.     return;
  111. }
  112.  
  113.  
  114. /*  Print the short description of a location                    */
  115.  
  116. VOID PASCAL descsh(SHORT loc)
  117. {
  118.     extern   char      *pShortRmDesc[];
  119.  
  120.     if (dbgflg)
  121.     printf("** descsh(%d) ** ", loc);
  122.  
  123.     DisplayText(pShortRmDesc, loc);
  124.     putchar('\n');
  125.     return;
  126. }
  127.  
  128.  
  129. static VOID NEAR PASCAL DisplayText(char *pTbl[], SHORT sMsg)
  130. {
  131.     auto     char      *p;
  132.  
  133.     --sMsg;
  134.  
  135.     if ('@' == *pTbl[sMsg])
  136.     {
  137.     p = pTbl[sMsg] + 1;
  138.     while (*p)
  139.     {
  140.         DisplayText(pTbl, atoi(p));
  141.         while (*p && ',' != *p)
  142.         ++p;
  143.  
  144.         if (',' == *p)
  145.         ++p;
  146.     }
  147.     }
  148.     else
  149.     puts(pTbl[sMsg]);
  150.  
  151.     return;
  152. }
  153.  
  154.  
  155. /*  routine to look up a vocabulary word.
  156.         word is the word to look up.
  157.         val  is the minimum acceptable value,
  158.                 if != 0 return %1000
  159. */
  160. SHORT PASCAL vocab(char *word, SHORT val)
  161. {
  162.     register SHORT    i;
  163.     register SHORT    left, right;
  164.     auto     SHORT    sCmp;
  165.     extern   SHORT    sVocabCount;
  166.     extern   VOCABTAB    VocabTab[];
  167.  
  168.     left  = 0;
  169.     right = sVocabCount - 1;
  170.  
  171.     while (right >= left)
  172.     {
  173.     i = (left + right) / 2;
  174.  
  175.     sCmp = *word - *VocabTab[i].pWord;
  176.     if (0 == sCmp)
  177.         sCmp = strcmp(word, VocabTab[i].pWord);
  178.  
  179.     if (0 == sCmp)
  180.         return(val ? VocabTab[i].sWord % 1000 : VocabTab[i].sWord);
  181.  
  182.     if (sCmp < 0)
  183.         right = i - 1;
  184.     else
  185.         left  = i + 1;
  186.     }
  187.  
  188.     return(-1);
  189. }
  190.  
  191. /*
  192.         Utility Routines
  193. */
  194.  
  195. /*
  196.         Routine to test for darkness
  197. */
  198. BOOL PASCAL dark(VOID)
  199. {
  200.     return(!(cond[loc] & LIGHT) && (!prop[LAMP] || !here(LAMP)));
  201. }
  202.  
  203. /*
  204.         Routine to tell if an item is present.
  205. */
  206. BOOL PASCAL here(SHORT item)
  207. {
  208.      return (place[item] == loc || toting(item));
  209. }
  210.  
  211. /*
  212.         Routine to tell if an item is being carried.
  213. */
  214. BOOL PASCAL toting(SHORT item)
  215. {
  216.     return(place[item] == -1);
  217. }
  218.  
  219. /*
  220.         Routine to tell if a location causes
  221.         a forced move.
  222. */
  223. BOOL PASCAL forced(SHORT atloc)
  224. {
  225.     return(cond[atloc] == 2);
  226. }
  227.  
  228. /*
  229.         Routine true x% of the time.
  230. */
  231. BOOL PASCAL pct(SHORT x)
  232. {
  233.     return(rand() % 100 < x);
  234. }
  235.  
  236. /*
  237.         Routine to tell if player is on
  238.         either side of a two sided object.
  239. */
  240. BOOL PASCAL at(SHORT item)
  241. {
  242.     return(place[item] == loc || fixed[item] == loc);
  243. }
  244.  
  245. /*
  246.         Routine to destroy an object
  247. */
  248. VOID PASCAL dstroy(SHORT obj)
  249. {
  250.                     move(obj, 0);
  251.     return;
  252. }
  253.  
  254. /*
  255.         Routine to move an object
  256. */
  257. VOID PASCAL move(SHORT obj, SHORT where)
  258. {
  259.     auto SHORT      from;
  260.  
  261.     from = (obj < MAXOBJ) ? place[obj] : fixed[obj];
  262.     if (from > 0 && from <= 300)
  263.     carry(obj, from);
  264.     drop(obj, where);
  265.     return;
  266. }
  267.  
  268. /*
  269.         Juggle an object
  270.         currently a no-op
  271. */
  272. VOID PASCAL juggle(SHORT loc)
  273. {
  274.     loc = loc;                    /* eliminate compiler warning */
  275.     return;
  276. }
  277.  
  278. /*
  279.         Routine to carry an object
  280. */
  281. VOID PASCAL carry(SHORT obj, SHORT where)
  282. {
  283.     where = where;               /* eliminate compiler warning */
  284.     if (obj < MAXOBJ)
  285.     {
  286.     if (place[obj] == -1)
  287.         return;
  288.     place[obj] = -1;
  289.     ++holding;
  290.     }
  291.     return;
  292. }
  293.  
  294. /*
  295.         Routine to drop an object
  296. */
  297. VOID PASCAL drop(SHORT obj, SHORT where)
  298. {
  299.     if (obj < MAXOBJ)
  300.     {
  301.     if (place[obj] == -1)
  302.     --holding;
  303.     place[obj] = where;
  304.     }
  305.     else
  306.     fixed[obj - MAXOBJ] = where;
  307.  
  308.     return;
  309. }
  310.  
  311. /*
  312.         routine to move an object and return a
  313.         value used to set the negated prop values
  314.         for the repository.
  315. */
  316. SHORT PASCAL put(SHORT obj, SHORT where, SHORT pval)
  317. {
  318.     move(obj, where);
  319.     return((-1) - pval);
  320. }
  321.  
  322. /*
  323.         Routine to check for presence
  324.         of dwarves..
  325. */
  326. SHORT PASCAL dcheck(VOID)
  327. {
  328.     register SHORT  i;
  329.  
  330.     for (i = 1; i < (DWARFMAX - 1); ++i)
  331.     {
  332.     if (dloc[i] == loc)
  333.         return(i);
  334.     }
  335.     return(0);
  336. }
  337.  
  338. /*
  339.         Determine liquid in the bottle
  340. */
  341. SHORT PASCAL liq(VOID)
  342. {
  343.     auto     SHORT    i, j;
  344.  
  345.     i = prop[BOTTLE];
  346.     j = (-1) - i;
  347.     return(liq2(i > j ? i : j));
  348. }
  349.  
  350. /*
  351.         Determine liquid at a location
  352. */
  353. SHORT PASCAL liqloc(SHORT loc)
  354. {
  355.     if (cond[loc] & LIQUID)
  356.     return(liq2(cond[loc] & WATOIL));
  357.  
  358.     return(liq2(1));
  359. }
  360.  
  361. /*
  362.         Convert  0 to WATER
  363.                  1 to nothing
  364.                  2 to OIL
  365. */
  366. SHORT PASCAL liq2(SHORT pbottle)
  367. {
  368.     return((1 - pbottle) * WATER + (pbottle >> 1) * (WATER + OIL));
  369. }
  370.  
  371. /*
  372.         Fatal error routine
  373. */
  374. VOID PASCAL bug(SHORT n)
  375. {
  376.     printf("Fatal error number %d\n", n);
  377.     exit(1);
  378. }
  379.