Home | History | Annotate | Line # | Download | only in monop
cards.c revision 1.5
      1 /*	$NetBSD: cards.c,v 1.5 1998/08/30 09:19:39 veego 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)cards.c	8.1 (Berkeley) 5/31/93";
     40 #else
     41 __RCSID("$NetBSD: cards.c,v 1.5 1998/08/30 09:19:39 veego Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 # include	"monop.ext"
     46 # include	"pathnames.h"
     47 
     48 /*
     49  *	These routine deal with the card decks
     50  */
     51 
     52 # define	GOJF	'F'	/* char for get-out-of-jail-free cards	*/
     53 
     54 # ifndef DEV
     55 static char	*cardfile	= _PATH_CARDS;
     56 # else
     57 static char	*cardfile	= "cards.pck";
     58 # endif
     59 
     60 static FILE	*deckf;
     61 
     62 static void set_up __P((DECK *));
     63 static void printmes __P((void));
     64 
     65 /*
     66  *	This routine initializes the decks from the data file,
     67  * which it opens.
     68  */
     69 void
     70 init_decks()
     71 {
     72 
     73 	if ((deckf=fopen(cardfile, "r")) == NULL) {
     74 file_err:
     75 		perror(cardfile);
     76 		exit(1);
     77 	}
     78 	if (fread(deck, sizeof (DECK), 2, deckf) != 2)
     79 		goto file_err;
     80 	set_up(&CC_D);
     81 	set_up(&CH_D);
     82 }
     83 /*
     84  *	This routine sets up the offset pointers for the given deck.
     85  */
     86 static void
     87 set_up(dp)
     88 DECK	*dp; {
     89 
     90 	int	r1, r2;
     91 	int	i;
     92 
     93 	dp->offsets = (long *) calloc(sizeof (long), dp->num_cards);
     94 	if (fread(dp->offsets, sizeof(long), dp->num_cards, deckf) != dp->num_cards) {
     95 		perror(cardfile);
     96 		exit(1);
     97 	}
     98 	dp->last_card = 0;
     99 	dp->gojf_used = FALSE;
    100 	for (i = 0; i < dp->num_cards; i++) {
    101 		long	temp;
    102 
    103 		r1 = roll(1, dp->num_cards) - 1;
    104 		r2 = roll(1, dp->num_cards) - 1;
    105 		temp = dp->offsets[r2];
    106 		dp->offsets[r2] = dp->offsets[r1];
    107 		dp->offsets[r1] = temp;
    108 	}
    109 }
    110 /*
    111  *	This routine draws a card from the given deck
    112  */
    113 void
    114 get_card(dp)
    115 DECK	*dp;
    116 {
    117 
    118 	char	type_maj, type_min;
    119 	int		num;
    120 	int		i, per_h, per_H, num_h, num_H;
    121 	OWN		*op;
    122 
    123 	do {
    124 		fseek(deckf, dp->offsets[dp->last_card], 0);
    125 		dp->last_card = ++(dp->last_card) % dp->num_cards;
    126 		type_maj = getc(deckf);
    127 	} while (dp->gojf_used && type_maj == GOJF);
    128 	type_min = getc(deckf);
    129 	num = getw(deckf);
    130 	printmes();
    131 	switch (type_maj) {
    132 	  case '+':		/* get 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 '-':		/* lose money		*/
    142 		if (type_min == 'A') {
    143 			for (i = 0; i < num_play; i++)
    144 				if (i != player)
    145 					play[i].money += num;
    146 			num = num * (num_play - 1);
    147 		}
    148 		cur_p->money -= num;
    149 		break;
    150 	  case 'M':		/* move somewhere	*/
    151 		switch (type_min) {
    152 		  case 'F':		/* move forward	*/
    153 			num -= cur_p->loc;
    154 			if (num < 0)
    155 				num += 40;
    156 			break;
    157 		  case 'J':		/* move to jail	*/
    158 			goto_jail();
    159 			return;
    160 		  case 'R':		/* move to railroad	*/
    161 			spec = TRUE;
    162 			num = (int)((cur_p->loc + 5)/10)*10 + 5 - cur_p->loc;
    163 			break;
    164 		  case 'U':		/* move to utility	*/
    165 			spec = TRUE;
    166 			if (cur_p->loc >= 12 && cur_p->loc < 28)
    167 				num = 28 - cur_p->loc;
    168 			else {
    169 				num = 12 - cur_p->loc;
    170 				if (num < 0)
    171 					num += 40;
    172 			}
    173 			break;
    174 		  case 'B':
    175 			num = -num;
    176 			break;
    177 		}
    178 		move(num);
    179 		break;
    180 	  case 'T':			/* tax			*/
    181 		if (dp == &CC_D) {
    182 			per_h = 40;
    183 			per_H = 115;
    184 		}
    185 		else {
    186 			per_h = 25;
    187 			per_H = 100;
    188 		}
    189 		num_h = num_H = 0;
    190 		for (op = cur_p->own_list; op; op = op->next)
    191 			if (op->sqr->type == PRPTY) {
    192 				if (op->sqr->desc->houses == 5)
    193 					++num_H;
    194 				else
    195 					num_h += op->sqr->desc->houses;
    196 			}
    197 		num = per_h * num_h + per_H * num_H;
    198 		printf("You had %d Houses and %d Hotels, so that cost you $%d\n", num_h, num_H, num);
    199 		if (num == 0)
    200 			lucky("");
    201 		else
    202 			cur_p->money -= num;
    203 		break;
    204 	  case GOJF:		/* get-out-of-jail-free card	*/
    205 		cur_p->num_gojf++;
    206 		dp->gojf_used = TRUE;
    207 		break;
    208 	}
    209 	spec = FALSE;
    210 }
    211 
    212 /*
    213  *	This routine prints out the message on the card
    214  */
    215 static void
    216 printmes() {
    217 
    218 	char	c;
    219 
    220 	printline();
    221 	fflush(stdout);
    222 	while ((c = getc(deckf)) != '\0')
    223 		putchar(c);
    224 	printline();
    225 	fflush(stdout);
    226 }
    227