move to /usr/share, move pathnames to pathnames.h
[unix-history] / usr / src / games / monop / cards.c
CommitLineData
ec9c86ca 1/*
a2ded391 2 * Copyright (c) 1980 Regents of the University of California.
ec9c86ca
KB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
a2ded391
KB
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.
ec9c86ca
KB
16 */
17
18#ifndef lint
4da3fc48 19static char sccsid[] = "@(#)cards.c 5.3 (Berkeley) %G%";
ec9c86ca
KB
20#endif /* not lint */
21
22# include "monop.ext"
4da3fc48 23# include "pathnames.h"
ec9c86ca
KB
24
25/*
26 * These routine deal with the card decks
27 */
28
29# define GOJF 'F' /* char for get-out-of-jail-free cards */
30
31# ifndef DEV
4da3fc48 32static char *cardfile = _PATH_CARDS;
ec9c86ca
KB
33# else
34static char *cardfile = "cards.pck";
35# endif
36
37static FILE *deckf;
38
39/*
40 * This routine initializes the decks from the data file,
41 * which it opens.
42 */
43init_decks() {
44
45 if ((deckf=fopen(cardfile, "r")) == NULL) {
46file_err:
47 perror(cardfile);
48 exit(1);
49 }
50 if (fread(deck, sizeof (DECK), 2, deckf) != 2)
51 goto file_err;
52 set_up(&CC_D);
53 set_up(&CH_D);
54}
55/*
56 * This routine sets up the offset pointers for the given deck.
57 */
58set_up(dp)
59DECK *dp; {
60
61 reg int r1, r2;
62 int i;
63
64 dp->offsets = (long *) calloc(sizeof (long), dp->num_cards);
65 if (fread(dp->offsets, sizeof(long), dp->num_cards, deckf) != dp->num_cards) {
66 perror(cardfile);
67 exit(1);
68 }
69 dp->last_card = 0;
70 dp->gojf_used = FALSE;
71 for (i = 0; i < dp->num_cards; i++) {
72 reg long temp;
73
74 r1 = roll(1, dp->num_cards) - 1;
75 r2 = roll(1, dp->num_cards) - 1;
76 temp = dp->offsets[r2];
77 dp->offsets[r2] = dp->offsets[r1];
78 dp->offsets[r1] = temp;
79 }
80}
81/*
82 * This routine draws a card from the given deck
83 */
84get_card(dp)
85DECK *dp; {
86
87 reg char type_maj, type_min;
88 reg int num;
89 int i, per_h, per_H, num_h, num_H;
90 OWN *op;
91
92 do {
93 fseek(deckf, dp->offsets[dp->last_card], 0);
94 dp->last_card = ++(dp->last_card) % dp->num_cards;
95 type_maj = getc(deckf);
96 } while (dp->gojf_used && type_maj == GOJF);
97 type_min = getc(deckf);
98 num = getw(deckf);
99 printmes();
100 switch (type_maj) {
101 case '+': /* get money */
102 if (type_min == 'A') {
103 for (i = 0; i < num_play; i++)
104 if (i != player)
105 play[i].money -= num;
106 num = num * (num_play - 1);
107 }
108 cur_p->money += num;
109 break;
110 case '-': /* lose money */
111 if (type_min == 'A') {
112 for (i = 0; i < num_play; i++)
113 if (i != player)
114 play[i].money += num;
115 num = num * (num_play - 1);
116 }
117 cur_p->money -= num;
118 break;
119 case 'M': /* move somewhere */
120 switch (type_min) {
121 case 'F': /* move forward */
122 num -= cur_p->loc;
123 if (num < 0)
124 num += 40;
125 break;
126 case 'J': /* move to jail */
127 goto_jail();
128 return;
129 case 'R': /* move to railroad */
130 spec = TRUE;
131 num = (int)((cur_p->loc + 5)/10)*10 + 5 - cur_p->loc;
132 break;
133 case 'U': /* move to utility */
134 spec = TRUE;
135 if (cur_p->loc >= 12 && cur_p->loc < 28)
136 num = 28 - cur_p->loc;
137 else {
138 num = 12 - cur_p->loc;
139 if (num < 0)
140 num += 40;
141 }
142 break;
143 case 'B':
144 num = -num;
145 break;
146 }
147 move(num);
148 break;
149 case 'T': /* tax */
150 if (dp == &CC_D) {
151 per_h = 40;
152 per_H = 115;
153 }
154 else {
155 per_h = 25;
156 per_H = 100;
157 }
158 num_h = num_H = 0;
159 for (op = cur_p->own_list; op; op = op->next)
160 if (op->sqr->type == PRPTY)
161 if (op->sqr->desc->houses == 5)
162 ++num_H;
163 else
164 num_h += op->sqr->desc->houses;
165 num = per_h * num_h + per_H * num_H;
166 printf("You had %d Houses and %d Hotels, so that cost you $%d\n", num_h, num_H, num);
167 if (num == 0)
168 lucky("");
169 else
170 cur_p->money -= num;
171 break;
172 case GOJF: /* get-out-of-jail-free card */
173 cur_p->num_gojf++;
174 dp->gojf_used = TRUE;
175 break;
176 }
177 spec = FALSE;
178}
179/*
180 * This routine prints out the message on the card
181 */
182printmes() {
183
184 reg char c;
185
186 printline();
187 fflush(stdout);
188 while ((c = getc(deckf)) != '\0')
189 putchar(c);
190 printline();
191 fflush(stdout);
192}