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