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