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