xref: /original-bsd/games/monop/cards.c (revision 2301fdfb)
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 are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)cards.c	5.2 (Berkeley) 06/18/88";
20 #endif /* not lint */
21 
22 # include	"monop.ext"
23 
24 /*
25  *	These routine deal with the card decks
26  */
27 
28 # define	GOJF	'F'	/* char for get-out-of-jail-free cards	*/
29 
30 # ifndef DEV
31 static char	*cardfile	= "/usr/games/lib/cards.pck";
32 # else
33 static char	*cardfile	= "cards.pck";
34 # endif
35 
36 static FILE	*deckf;
37 
38 /*
39  *	This routine initializes the decks from the data file,
40  * which it opens.
41  */
42 init_decks() {
43 
44 	if ((deckf=fopen(cardfile, "r")) == NULL) {
45 file_err:
46 		perror(cardfile);
47 		exit(1);
48 	}
49 	if (fread(deck, sizeof (DECK), 2, deckf) != 2)
50 		goto file_err;
51 	set_up(&CC_D);
52 	set_up(&CH_D);
53 }
54 /*
55  *	This routine sets up the offset pointers for the given deck.
56  */
57 set_up(dp)
58 DECK	*dp; {
59 
60 	reg int	r1, r2;
61 	int	i;
62 
63 	dp->offsets = (long *) calloc(sizeof (long), dp->num_cards);
64 	if (fread(dp->offsets, sizeof(long), dp->num_cards, deckf) != dp->num_cards) {
65 		perror(cardfile);
66 		exit(1);
67 	}
68 	dp->last_card = 0;
69 	dp->gojf_used = FALSE;
70 	for (i = 0; i < dp->num_cards; i++) {
71 		reg long	temp;
72 
73 		r1 = roll(1, dp->num_cards) - 1;
74 		r2 = roll(1, dp->num_cards) - 1;
75 		temp = dp->offsets[r2];
76 		dp->offsets[r2] = dp->offsets[r1];
77 		dp->offsets[r1] = temp;
78 	}
79 }
80 /*
81  *	This routine draws a card from the given deck
82  */
83 get_card(dp)
84 DECK	*dp; {
85 
86 	reg char	type_maj, type_min;
87 	reg int		num;
88 	int		i, per_h, per_H, num_h, num_H;
89 	OWN		*op;
90 
91 	do {
92 		fseek(deckf, dp->offsets[dp->last_card], 0);
93 		dp->last_card = ++(dp->last_card) % dp->num_cards;
94 		type_maj = getc(deckf);
95 	} while (dp->gojf_used && type_maj == GOJF);
96 	type_min = getc(deckf);
97 	num = getw(deckf);
98 	printmes();
99 	switch (type_maj) {
100 	  case '+':		/* get money		*/
101 		if (type_min == 'A') {
102 			for (i = 0; i < num_play; i++)
103 				if (i != player)
104 					play[i].money -= num;
105 			num = num * (num_play - 1);
106 		}
107 		cur_p->money += num;
108 		break;
109 	  case '-':		/* lose money		*/
110 		if (type_min == 'A') {
111 			for (i = 0; i < num_play; i++)
112 				if (i != player)
113 					play[i].money += num;
114 			num = num * (num_play - 1);
115 		}
116 		cur_p->money -= num;
117 		break;
118 	  case 'M':		/* move somewhere	*/
119 		switch (type_min) {
120 		  case 'F':		/* move forward	*/
121 			num -= cur_p->loc;
122 			if (num < 0)
123 				num += 40;
124 			break;
125 		  case 'J':		/* move to jail	*/
126 			goto_jail();
127 			return;
128 		  case 'R':		/* move to railroad	*/
129 			spec = TRUE;
130 			num = (int)((cur_p->loc + 5)/10)*10 + 5 - cur_p->loc;
131 			break;
132 		  case 'U':		/* move to utility	*/
133 			spec = TRUE;
134 			if (cur_p->loc >= 12 && cur_p->loc < 28)
135 				num = 28 - cur_p->loc;
136 			else {
137 				num = 12 - cur_p->loc;
138 				if (num < 0)
139 					num += 40;
140 			}
141 			break;
142 		  case 'B':
143 			num = -num;
144 			break;
145 		}
146 		move(num);
147 		break;
148 	  case 'T':			/* tax			*/
149 		if (dp == &CC_D) {
150 			per_h = 40;
151 			per_H = 115;
152 		}
153 		else {
154 			per_h = 25;
155 			per_H = 100;
156 		}
157 		num_h = num_H = 0;
158 		for (op = cur_p->own_list; op; op = op->next)
159 			if (op->sqr->type == PRPTY)
160 				if (op->sqr->desc->houses == 5)
161 					++num_H;
162 				else
163 					num_h += op->sqr->desc->houses;
164 		num = per_h * num_h + per_H * num_H;
165 		printf("You had %d Houses and %d Hotels, so that cost you $%d\n", num_h, num_H, num);
166 		if (num == 0)
167 			lucky("");
168 		else
169 			cur_p->money -= num;
170 		break;
171 	  case GOJF:		/* get-out-of-jail-free card	*/
172 		cur_p->num_gojf++;
173 		dp->gojf_used = TRUE;
174 		break;
175 	}
176 	spec = FALSE;
177 }
178 /*
179  *	This routine prints out the message on the card
180  */
181 printmes() {
182 
183 	reg char	c;
184 
185 	printline();
186 	fflush(stdout);
187 	while ((c = getc(deckf)) != '\0')
188 		putchar(c);
189 	printline();
190 	fflush(stdout);
191 }
192