]> git.cameronkatri.com Git - bsdgames-darwin.git/blobdiff - monop/cards.c
printf() pedant - do not pass variable alone, use %s.
[bsdgames-darwin.git] / monop / cards.c
index 5041c089de9179c025b6f97f18779315188d284b..08551cc285179e154f2dd9a549adc6daf58151e9 100644 (file)
@@ -1,6 +1,8 @@
+/*     $NetBSD: cards.c,v 1.11 1999/12/30 01:40:08 simonb Exp $        */
+
 /*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1980, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * SUCH DAMAGE.
  */
 
+#include <sys/cdefs.h>
 #ifndef lint
-static char sccsid[] = "@(#)cards.c    5.4 (Berkeley) 6/1/90";
+#if 0
+static char sccsid[] = "@(#)cards.c    8.1 (Berkeley) 5/31/93";
+#else
+__RCSID("$NetBSD: cards.c,v 1.11 1999/12/30 01:40:08 simonb 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;
+       DECK *dp;
+{
+       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) {
+       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;
@@ -94,24 +124,26 @@ 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);
        type_min = getc(deckf);
-       num = getw(deckf);
+       num = ntohl(getw(deckf));
        printmes();
        switch (type_maj) {
          case '+':             /* get money            */
@@ -173,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
@@ -192,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);