Home | History | Annotate | Line # | Download | only in monop
cards.c revision 1.8
      1  1.8       jsm /*	$NetBSD: cards.c,v 1.8 1999/09/08 21:17:51 jsm Exp $	*/
      2  1.3       cgd 
      3  1.1       cgd /*
      4  1.3       cgd  * Copyright (c) 1980, 1993
      5  1.3       cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1       cgd  *
      7  1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1       cgd  * modification, are permitted provided that the following conditions
      9  1.1       cgd  * are met:
     10  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1       cgd  *    must display the following acknowledgement:
     17  1.1       cgd  *	This product includes software developed by the University of
     18  1.1       cgd  *	California, Berkeley and its contributors.
     19  1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1       cgd  *    may be used to endorse or promote products derived from this software
     21  1.1       cgd  *    without specific prior written permission.
     22  1.1       cgd  *
     23  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1       cgd  * SUCH DAMAGE.
     34  1.1       cgd  */
     35  1.1       cgd 
     36  1.4  christos #include <sys/cdefs.h>
     37  1.1       cgd #ifndef lint
     38  1.3       cgd #if 0
     39  1.3       cgd static char sccsid[] = "@(#)cards.c	8.1 (Berkeley) 5/31/93";
     40  1.3       cgd #else
     41  1.8       jsm __RCSID("$NetBSD: cards.c,v 1.8 1999/09/08 21:17:51 jsm Exp $");
     42  1.3       cgd #endif
     43  1.1       cgd #endif /* not lint */
     44  1.1       cgd 
     45  1.7    simonb #include <sys/types.h>
     46  1.7    simonb #include <sys/endian.h>
     47  1.7    simonb #include "monop.ext"
     48  1.7    simonb #include "pathnames.h"
     49  1.1       cgd 
     50  1.1       cgd /*
     51  1.1       cgd  *	These routine deal with the card decks
     52  1.1       cgd  */
     53  1.1       cgd 
     54  1.7    simonb #define	GOJF	'F'	/* char for get-out-of-jail-free cards	*/
     55  1.1       cgd 
     56  1.7    simonb #ifndef DEV
     57  1.8       jsm static const char	*cardfile	= _PATH_CARDS;
     58  1.7    simonb #else
     59  1.8       jsm static const char	*cardfile	= "cards.pck";
     60  1.7    simonb #endif
     61  1.1       cgd 
     62  1.1       cgd static FILE	*deckf;
     63  1.1       cgd 
     64  1.4  christos static void set_up __P((DECK *));
     65  1.4  christos static void printmes __P((void));
     66  1.4  christos 
     67  1.1       cgd /*
     68  1.1       cgd  *	This routine initializes the decks from the data file,
     69  1.1       cgd  * which it opens.
     70  1.1       cgd  */
     71  1.4  christos void
     72  1.4  christos init_decks()
     73  1.4  christos {
     74  1.6    simonb 	int32_t nc;
     75  1.1       cgd 
     76  1.1       cgd 	if ((deckf=fopen(cardfile, "r")) == NULL) {
     77  1.1       cgd file_err:
     78  1.1       cgd 		perror(cardfile);
     79  1.1       cgd 		exit(1);
     80  1.1       cgd 	}
     81  1.6    simonb 
     82  1.6    simonb 	/* read number of community chest cards... */
     83  1.6    simonb 	if (fread(&nc, sizeof(nc), 1, deckf) != 1)
     84  1.6    simonb 		goto file_err;
     85  1.6    simonb 	CC_D.num_cards = be32toh(nc);
     86  1.6    simonb 	/* ... and number of community chest cards. */
     87  1.6    simonb 	if (fread(&nc, sizeof(nc), 1, deckf) != 1)
     88  1.1       cgd 		goto file_err;
     89  1.6    simonb 	CH_D.num_cards = be32toh(nc);
     90  1.1       cgd 	set_up(&CC_D);
     91  1.1       cgd 	set_up(&CH_D);
     92  1.1       cgd }
     93  1.7    simonb 
     94  1.1       cgd /*
     95  1.1       cgd  *	This routine sets up the offset pointers for the given deck.
     96  1.1       cgd  */
     97  1.4  christos static void
     98  1.1       cgd set_up(dp)
     99  1.7    simonb 	DECK *dp;
    100  1.7    simonb {
    101  1.7    simonb 	int r1, r2;
    102  1.7    simonb 	int i;
    103  1.1       cgd 
    104  1.6    simonb 	dp->offsets = (off_t *) calloc(sizeof (off_t), dp->num_cards);
    105  1.6    simonb 	if (fread(dp->offsets, sizeof(off_t), dp->num_cards, deckf) !=
    106  1.6    simonb 	    dp->num_cards) {
    107  1.1       cgd 		perror(cardfile);
    108  1.1       cgd 		exit(1);
    109  1.1       cgd 	}
    110  1.6    simonb 	/* convert offsets from big-endian byte order */
    111  1.6    simonb 	for (i = 0; i < dp->num_cards; i++)
    112  1.6    simonb 		BE64TOH(dp->offsets[i]);
    113  1.1       cgd 	dp->last_card = 0;
    114  1.1       cgd 	dp->gojf_used = FALSE;
    115  1.1       cgd 	for (i = 0; i < dp->num_cards; i++) {
    116  1.6    simonb 		off_t	temp;
    117  1.1       cgd 
    118  1.1       cgd 		r1 = roll(1, dp->num_cards) - 1;
    119  1.1       cgd 		r2 = roll(1, dp->num_cards) - 1;
    120  1.1       cgd 		temp = dp->offsets[r2];
    121  1.1       cgd 		dp->offsets[r2] = dp->offsets[r1];
    122  1.1       cgd 		dp->offsets[r1] = temp;
    123  1.1       cgd 	}
    124  1.1       cgd }
    125  1.7    simonb 
    126  1.1       cgd /*
    127  1.1       cgd  *	This routine draws a card from the given deck
    128  1.1       cgd  */
    129  1.4  christos void
    130  1.1       cgd get_card(dp)
    131  1.7    simonb 	DECK *dp;
    132  1.4  christos {
    133  1.7    simonb 	char type_maj, type_min;
    134  1.7    simonb 	int num;
    135  1.7    simonb 	int i, per_h, per_H, num_h, num_H;
    136  1.7    simonb 	OWN *op;
    137  1.1       cgd 
    138  1.1       cgd 	do {
    139  1.1       cgd 		fseek(deckf, dp->offsets[dp->last_card], 0);
    140  1.1       cgd 		dp->last_card = ++(dp->last_card) % dp->num_cards;
    141  1.1       cgd 		type_maj = getc(deckf);
    142  1.1       cgd 	} while (dp->gojf_used && type_maj == GOJF);
    143  1.1       cgd 	type_min = getc(deckf);
    144  1.1       cgd 	num = getw(deckf);
    145  1.1       cgd 	printmes();
    146  1.1       cgd 	switch (type_maj) {
    147  1.1       cgd 	  case '+':		/* get money		*/
    148  1.1       cgd 		if (type_min == 'A') {
    149  1.1       cgd 			for (i = 0; i < num_play; i++)
    150  1.1       cgd 				if (i != player)
    151  1.1       cgd 					play[i].money -= num;
    152  1.1       cgd 			num = num * (num_play - 1);
    153  1.1       cgd 		}
    154  1.1       cgd 		cur_p->money += num;
    155  1.1       cgd 		break;
    156  1.1       cgd 	  case '-':		/* lose money		*/
    157  1.1       cgd 		if (type_min == 'A') {
    158  1.1       cgd 			for (i = 0; i < num_play; i++)
    159  1.1       cgd 				if (i != player)
    160  1.1       cgd 					play[i].money += num;
    161  1.1       cgd 			num = num * (num_play - 1);
    162  1.1       cgd 		}
    163  1.1       cgd 		cur_p->money -= num;
    164  1.1       cgd 		break;
    165  1.1       cgd 	  case 'M':		/* move somewhere	*/
    166  1.1       cgd 		switch (type_min) {
    167  1.1       cgd 		  case 'F':		/* move forward	*/
    168  1.1       cgd 			num -= cur_p->loc;
    169  1.1       cgd 			if (num < 0)
    170  1.1       cgd 				num += 40;
    171  1.1       cgd 			break;
    172  1.1       cgd 		  case 'J':		/* move to jail	*/
    173  1.1       cgd 			goto_jail();
    174  1.1       cgd 			return;
    175  1.1       cgd 		  case 'R':		/* move to railroad	*/
    176  1.1       cgd 			spec = TRUE;
    177  1.1       cgd 			num = (int)((cur_p->loc + 5)/10)*10 + 5 - cur_p->loc;
    178  1.1       cgd 			break;
    179  1.1       cgd 		  case 'U':		/* move to utility	*/
    180  1.1       cgd 			spec = TRUE;
    181  1.1       cgd 			if (cur_p->loc >= 12 && cur_p->loc < 28)
    182  1.1       cgd 				num = 28 - cur_p->loc;
    183  1.1       cgd 			else {
    184  1.1       cgd 				num = 12 - cur_p->loc;
    185  1.1       cgd 				if (num < 0)
    186  1.1       cgd 					num += 40;
    187  1.1       cgd 			}
    188  1.1       cgd 			break;
    189  1.1       cgd 		  case 'B':
    190  1.1       cgd 			num = -num;
    191  1.1       cgd 			break;
    192  1.1       cgd 		}
    193  1.1       cgd 		move(num);
    194  1.1       cgd 		break;
    195  1.1       cgd 	  case 'T':			/* tax			*/
    196  1.1       cgd 		if (dp == &CC_D) {
    197  1.1       cgd 			per_h = 40;
    198  1.1       cgd 			per_H = 115;
    199  1.1       cgd 		}
    200  1.1       cgd 		else {
    201  1.1       cgd 			per_h = 25;
    202  1.1       cgd 			per_H = 100;
    203  1.1       cgd 		}
    204  1.1       cgd 		num_h = num_H = 0;
    205  1.1       cgd 		for (op = cur_p->own_list; op; op = op->next)
    206  1.5     veego 			if (op->sqr->type == PRPTY) {
    207  1.1       cgd 				if (op->sqr->desc->houses == 5)
    208  1.1       cgd 					++num_H;
    209  1.1       cgd 				else
    210  1.1       cgd 					num_h += op->sqr->desc->houses;
    211  1.5     veego 			}
    212  1.1       cgd 		num = per_h * num_h + per_H * num_H;
    213  1.7    simonb 		printf(
    214  1.7    simonb 		    "You had %d Houses and %d Hotels, so that cost you $%d\n",
    215  1.7    simonb 		    num_h, num_H, num);
    216  1.1       cgd 		if (num == 0)
    217  1.1       cgd 			lucky("");
    218  1.1       cgd 		else
    219  1.1       cgd 			cur_p->money -= num;
    220  1.1       cgd 		break;
    221  1.1       cgd 	  case GOJF:		/* get-out-of-jail-free card	*/
    222  1.1       cgd 		cur_p->num_gojf++;
    223  1.1       cgd 		dp->gojf_used = TRUE;
    224  1.1       cgd 		break;
    225  1.1       cgd 	}
    226  1.1       cgd 	spec = FALSE;
    227  1.1       cgd }
    228  1.4  christos 
    229  1.1       cgd /*
    230  1.1       cgd  *	This routine prints out the message on the card
    231  1.1       cgd  */
    232  1.4  christos static void
    233  1.7    simonb printmes()
    234  1.7    simonb {
    235  1.7    simonb 	char c;
    236  1.1       cgd 
    237  1.1       cgd 	printline();
    238  1.1       cgd 	fflush(stdout);
    239  1.1       cgd 	while ((c = getc(deckf)) != '\0')
    240  1.1       cgd 		putchar(c);
    241  1.1       cgd 	printline();
    242  1.1       cgd 	fflush(stdout);
    243  1.1       cgd }
    244