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