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