]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - monop/cards.c
Use the symbolic names `SEEK_SET' and `O_RDONLY' where appropriate in
[bsdgames-darwin.git] / monop / cards.c
1 /* $NetBSD: cards.c,v 1.9 1999/09/08 21:57:18 jsm 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.9 1999/09/08 21:57:18 jsm 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 const char *cardfile = _PATH_CARDS;
58 #else
59 static const 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 /*
95 * This routine sets up the offset pointers for the given deck.
96 */
97 static void
98 set_up(dp)
99 DECK *dp;
100 {
101 int r1, r2;
102 int i;
103
104 dp->offsets = (off_t *) calloc(sizeof (off_t), dp->num_cards);
105 if (fread(dp->offsets, sizeof(off_t), dp->num_cards, deckf) !=
106 dp->num_cards) {
107 perror(cardfile);
108 exit(1);
109 }
110 /* convert offsets from big-endian byte order */
111 for (i = 0; i < dp->num_cards; i++)
112 BE64TOH(dp->offsets[i]);
113 dp->last_card = 0;
114 dp->gojf_used = FALSE;
115 for (i = 0; i < dp->num_cards; i++) {
116 off_t temp;
117
118 r1 = roll(1, dp->num_cards) - 1;
119 r2 = roll(1, dp->num_cards) - 1;
120 temp = dp->offsets[r2];
121 dp->offsets[r2] = dp->offsets[r1];
122 dp->offsets[r1] = temp;
123 }
124 }
125
126 /*
127 * This routine draws a card from the given deck
128 */
129 void
130 get_card(dp)
131 DECK *dp;
132 {
133 char type_maj, type_min;
134 int num;
135 int i, per_h, per_H, num_h, num_H;
136 OWN *op;
137
138 do {
139 fseek(deckf, dp->offsets[dp->last_card], SEEK_SET);
140 dp->last_card = ++(dp->last_card) % dp->num_cards;
141 type_maj = getc(deckf);
142 } while (dp->gojf_used && type_maj == GOJF);
143 type_min = getc(deckf);
144 num = getw(deckf);
145 printmes();
146 switch (type_maj) {
147 case '+': /* get money */
148 if (type_min == 'A') {
149 for (i = 0; i < num_play; i++)
150 if (i != player)
151 play[i].money -= num;
152 num = num * (num_play - 1);
153 }
154 cur_p->money += num;
155 break;
156 case '-': /* lose money */
157 if (type_min == 'A') {
158 for (i = 0; i < num_play; i++)
159 if (i != player)
160 play[i].money += num;
161 num = num * (num_play - 1);
162 }
163 cur_p->money -= num;
164 break;
165 case 'M': /* move somewhere */
166 switch (type_min) {
167 case 'F': /* move forward */
168 num -= cur_p->loc;
169 if (num < 0)
170 num += 40;
171 break;
172 case 'J': /* move to jail */
173 goto_jail();
174 return;
175 case 'R': /* move to railroad */
176 spec = TRUE;
177 num = (int)((cur_p->loc + 5)/10)*10 + 5 - cur_p->loc;
178 break;
179 case 'U': /* move to utility */
180 spec = TRUE;
181 if (cur_p->loc >= 12 && cur_p->loc < 28)
182 num = 28 - cur_p->loc;
183 else {
184 num = 12 - cur_p->loc;
185 if (num < 0)
186 num += 40;
187 }
188 break;
189 case 'B':
190 num = -num;
191 break;
192 }
193 move(num);
194 break;
195 case 'T': /* tax */
196 if (dp == &CC_D) {
197 per_h = 40;
198 per_H = 115;
199 }
200 else {
201 per_h = 25;
202 per_H = 100;
203 }
204 num_h = num_H = 0;
205 for (op = cur_p->own_list; op; op = op->next)
206 if (op->sqr->type == PRPTY) {
207 if (op->sqr->desc->houses == 5)
208 ++num_H;
209 else
210 num_h += op->sqr->desc->houses;
211 }
212 num = per_h * num_h + per_H * num_H;
213 printf(
214 "You had %d Houses and %d Hotels, so that cost you $%d\n",
215 num_h, num_H, num);
216 if (num == 0)
217 lucky("");
218 else
219 cur_p->money -= num;
220 break;
221 case GOJF: /* get-out-of-jail-free card */
222 cur_p->num_gojf++;
223 dp->gojf_used = TRUE;
224 break;
225 }
226 spec = FALSE;
227 }
228
229 /*
230 * This routine prints out the message on the card
231 */
232 static void
233 printmes()
234 {
235 char c;
236
237 printline();
238 fflush(stdout);
239 while ((c = getc(deckf)) != '\0')
240 putchar(c);
241 printline();
242 fflush(stdout);
243 }