]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - monop/cards.c
Use the function usage().
[bsdgames-darwin.git] / monop / cards.c
index b65f0f268caf28e5ba657df0a628ccaf191c30d5..1b330db7fbdd6f81b0a5e051991b5f8752ea3cf1 100644 (file)
@@ -1,4 +1,4 @@
-/*     $NetBSD: cards.c,v 1.3 1995/03/23 08:34:35 cgd Exp $    */
+/*     $NetBSD: cards.c,v 1.10 1999/09/09 17:27:58 jsm Exp $   */
 
 /*
  * Copyright (c) 1980, 1993
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)cards.c    8.1 (Berkeley) 5/31/93";
 #else
-static char rcsid[] = "$NetBSD: cards.c,v 1.3 1995/03/23 08:34:35 cgd Exp $";
+__RCSID("$NetBSD: cards.c,v 1.10 1999/09/09 17:27:58 jsm Exp $");
 #endif
 #endif /* not lint */
 
-# include      "monop.ext"
-# include      "pathnames.h"
+#include <sys/types.h>
+#include <sys/endian.h>
+#include "monop.ext"
+#include "pathnames.h"
 
 /*
  *     These routine deal with the card decks
  */
 
-# define       GOJF    'F'     /* char for get-out-of-jail-free cards  */
+#define        GOJF    'F'     /* char for get-out-of-jail-free cards  */
 
-# ifndef DEV
-static char    *cardfile       = _PATH_CARDS;
-# else
-static char    *cardfile       = "cards.pck";
-# endif
+#ifndef DEV
+static const char      *cardfile       = _PATH_CARDS;
+#else
+static const char      *cardfile       = "cards.pck";
+#endif
 
 static FILE    *deckf;
 
+static void set_up __P((DECK *));
+static void printmes __P((void));
+
 /*
  *     This routine initializes the decks from the data file,
  * which it opens.
  */
-init_decks() {
+void
+init_decks()
+{
+       int32_t nc;
 
        if ((deckf=fopen(cardfile, "r")) == NULL) {
 file_err:
                perror(cardfile);
                exit(1);
        }
-       if (fread(deck, sizeof (DECK), 2, deckf) != 2)
+
+       /* read number of community chest cards... */
+       if (fread(&nc, sizeof(nc), 1, deckf) != 1)
                goto file_err;
+       CC_D.num_cards = be32toh(nc);
+       /* ... and number of community chest cards. */
+       if (fread(&nc, sizeof(nc), 1, deckf) != 1)
+               goto file_err;
+       CH_D.num_cards = be32toh(nc);
        set_up(&CC_D);
        set_up(&CH_D);
 }
+
 /*
  *     This routine sets up the offset pointers for the given deck.
  */
+static void
 set_up(dp)
-DECK   *dp; {
-
-       reg int r1, r2;
-       int     i;
-
-       dp->offsets = (long *) calloc(sizeof (long), dp->num_cards);
-       if (fread(dp->offsets, sizeof(long), dp->num_cards, deckf) != dp->num_cards) {
+       DECK *dp;
+{
+       int r1, r2;
+       int i;
+
+       dp->offsets = (off_t *) calloc(sizeof (off_t), dp->num_cards);
+       if (dp->offsets == NULL)
+               errx(1, "out of memory");
+       if (fread(dp->offsets, sizeof(off_t), dp->num_cards, deckf) !=
+           dp->num_cards) {
                perror(cardfile);
                exit(1);
        }
+       /* convert offsets from big-endian byte order */
+       for (i = 0; i < dp->num_cards; i++)
+               BE64TOH(dp->offsets[i]);
        dp->last_card = 0;
        dp->gojf_used = FALSE;
        for (i = 0; i < dp->num_cards; i++) {
-               reg long        temp;
+               off_t   temp;
 
                r1 = roll(1, dp->num_cards) - 1;
                r2 = roll(1, dp->num_cards) - 1;
@@ -100,19 +124,21 @@ DECK      *dp; {
                dp->offsets[r1] = temp;
        }
 }
+
 /*
  *     This routine draws a card from the given deck
  */
+void
 get_card(dp)
-DECK   *dp; {
-
-       reg char        type_maj, type_min;
-       reg int         num;
-       int             i, per_h, per_H, num_h, num_H;
-       OWN             *op;
+       DECK *dp;
+{
+       char type_maj, type_min;
+       int num;
+       int i, per_h, per_H, num_h, num_H;
+       OWN *op;
 
        do {
-               fseek(deckf, dp->offsets[dp->last_card], 0);
+               fseek(deckf, dp->offsets[dp->last_card], SEEK_SET);
                dp->last_card = ++(dp->last_card) % dp->num_cards;
                type_maj = getc(deckf);
        } while (dp->gojf_used && type_maj == GOJF);
@@ -179,13 +205,16 @@ DECK      *dp; {
                }
                num_h = num_H = 0;
                for (op = cur_p->own_list; op; op = op->next)
-                       if (op->sqr->type == PRPTY)
+                       if (op->sqr->type == PRPTY) {
                                if (op->sqr->desc->houses == 5)
                                        ++num_H;
                                else
                                        num_h += op->sqr->desc->houses;
+                       }
                num = per_h * num_h + per_H * num_H;
-               printf("You had %d Houses and %d Hotels, so that cost you $%d\n", num_h, num_H, num);
+               printf(
+                   "You had %d Houses and %d Hotels, so that cost you $%d\n",
+                   num_h, num_H, num);
                if (num == 0)
                        lucky("");
                else
@@ -198,12 +227,14 @@ DECK      *dp; {
        }
        spec = FALSE;
 }
+
 /*
  *     This routine prints out the message on the card
  */
-printmes() {
-
-       reg char        c;
+static void
+printmes()
+{
+       char c;
 
        printline();
        fflush(stdout);