cards.c revision 1.21 1 /* $NetBSD: cards.c,v 1.21 2008/02/24 01:57:34 dholland 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)cards.c 8.1 (Berkeley) 5/31/93";
36 #else
37 __RCSID("$NetBSD: cards.c,v 1.21 2008/02/24 01:57:34 dholland Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/types.h>
42 #include <sys/endian.h>
43 #include "monop.h"
44 #include "deck.h"
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 const char *cardfile = _PATH_CARDS;
55 #else
56 static const char *cardfile = "cards.pck";
57 #endif
58
59 static FILE *deckf;
60
61 static void set_up(DECK *);
62 static void printmes(void);
63
64 /*
65 * This routine initializes the decks from the data file,
66 * which it opens.
67 */
68 void
69 init_decks()
70 {
71 int32_t nc;
72
73 if ((deckf = fopen(cardfile, "r")) == NULL) {
74 file_err:
75 err(1, "%s", cardfile);
76 }
77
78 /* read number of community chest cards... */
79 if (fread(&nc, sizeof(nc), 1, deckf) != 1)
80 goto file_err;
81 CC_D.num_cards = be32toh(nc);
82 /* ... and number of community chest cards. */
83 if (fread(&nc, sizeof(nc), 1, deckf) != 1)
84 goto file_err;
85 CH_D.num_cards = be32toh(nc);
86 set_up(&CC_D);
87 set_up(&CH_D);
88 }
89
90 /*
91 * This routine sets up the offset pointers for the given deck.
92 */
93 static void
94 set_up(dp)
95 DECK *dp;
96 {
97 int r1, r2;
98 int i;
99
100 dp->offsets = (off_t *) calloc(dp->num_cards, sizeof (off_t));
101 if (dp->offsets == NULL)
102 errx(1, "out of memory");
103 if (fread(dp->offsets, sizeof(off_t), dp->num_cards, deckf) !=
104 (unsigned) dp->num_cards) {
105 err(1, "%s", cardfile);
106 }
107 /* convert offsets from big-endian byte order */
108 for (i = 0; i < dp->num_cards; i++)
109 BE64TOH(dp->offsets[i]);
110 dp->top_card = 0;
111 dp->gojf_used = FALSE;
112 for (i = 0; i < dp->num_cards; i++) {
113 off_t temp;
114
115 r1 = roll(1, dp->num_cards) - 1;
116 r2 = roll(1, dp->num_cards) - 1;
117 temp = dp->offsets[r2];
118 dp->offsets[r2] = dp->offsets[r1];
119 dp->offsets[r1] = temp;
120 }
121 }
122
123 /*
124 * This routine draws a card from the given deck
125 */
126 void
127 get_card(dp)
128 DECK *dp;
129 {
130 char type_maj, type_min;
131 int num;
132 int i, per_h, per_H, num_h, num_H;
133 OWN *op;
134
135 do {
136 fseek(deckf, dp->offsets[dp->top_card], SEEK_SET);
137 dp->top_card = ++(dp->top_card) % dp->num_cards;
138 type_maj = getc(deckf);
139 } while (dp->gojf_used && type_maj == GOJF);
140 type_min = getc(deckf);
141 num = ntohl(getw(deckf));
142 printmes();
143 switch (type_maj) {
144 case '+': /* get money */
145 if (type_min == 'A') {
146 for (i = 0; i < num_play; i++)
147 if (i != player)
148 play[i].money -= num;
149 num = num * (num_play - 1);
150 }
151 cur_p->money += num;
152 break;
153 case '-': /* lose money */
154 if (type_min == 'A') {
155 for (i = 0; i < num_play; i++)
156 if (i != player)
157 play[i].money += num;
158 num = num * (num_play - 1);
159 }
160 cur_p->money -= num;
161 break;
162 case 'M': /* move somewhere */
163 switch (type_min) {
164 case 'F': /* move forward */
165 num -= cur_p->loc;
166 if (num < 0)
167 num += 40;
168 break;
169 case 'J': /* move to jail */
170 goto_jail();
171 return;
172 case 'R': /* move to railroad */
173 spec = TRUE;
174 num = (int)((cur_p->loc + 5)/10)*10 + 5 - cur_p->loc;
175 break;
176 case 'U': /* move to utility */
177 spec = TRUE;
178 if (cur_p->loc >= 12 && cur_p->loc < 28)
179 num = 28 - cur_p->loc;
180 else {
181 num = 12 - cur_p->loc;
182 if (num < 0)
183 num += 40;
184 }
185 break;
186 case 'B':
187 num = -num;
188 break;
189 }
190 move(num);
191 break;
192 case 'T': /* tax */
193 if (dp == &CC_D) {
194 per_h = 40;
195 per_H = 115;
196 }
197 else {
198 per_h = 25;
199 per_H = 100;
200 }
201 num_h = num_H = 0;
202 for (op = cur_p->own_list; op; op = op->next)
203 if (op->sqr->type == PRPTY) {
204 if (op->sqr->desc->houses == 5)
205 ++num_H;
206 else
207 num_h += op->sqr->desc->houses;
208 }
209 num = per_h * num_h + per_H * num_H;
210 printf(
211 "You had %d Houses and %d Hotels, so that cost you $%d\n",
212 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
242 /*
243 * This routine returns the players get-out-of-jail-free card
244 * to the bottom of a deck. XXX currently does not return to the correct
245 * deck.
246 */
247 void
248 ret_card(plr)
249 PLAY *plr;
250 {
251 char type_maj;
252 int gojfpos, last_card;
253 int i;
254 DECK *dp;
255 off_t temp;
256
257 plr->num_gojf--;
258 if (CC_D.gojf_used)
259 dp = &CC_D;
260 else
261 dp = &CH_D;
262 dp->gojf_used = FALSE;
263
264 /* Put at bottom of deck (top_card - 1) and remove it from wherever else
265 * it used to be.
266 */
267 last_card = dp->top_card - 1;
268 if (last_card < 0)
269 last_card += dp->num_cards;
270 gojfpos = dp->top_card;
271 do {
272 gojfpos = (gojfpos + 1) % dp->num_cards;
273 fseek(deckf, dp->offsets[gojfpos], SEEK_SET);
274 type_maj = getc(deckf);
275 } while (type_maj != GOJF);
276 temp = dp->offsets[gojfpos];
277 /* Only one of the next two loops does anything */
278 for (i = gojfpos - 1; i > last_card; i--)
279 dp->offsets[i + 1] = dp->offsets[i];
280 for (i = gojfpos; i < last_card; i++)
281 dp->offsets[i] = dp->offsets[i + 1];
282 if (gojfpos > last_card) {
283 dp->offsets[dp->top_card] = temp;
284 dp->top_card++;
285 dp->top_card %= dp->num_cards;
286 } else
287 dp->offsets[last_card] = temp;
288 }
289