misc.c revision 1.11 1 1.11 agc /* $NetBSD: misc.c,v 1.11 2003/08/07 09:37:28 agc Exp $ */
2 1.4 cgd
3 1.1 cgd /*
4 1.4 cgd * Copyright (c) 1980, 1993
5 1.4 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.11 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.6 christos #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.4 cgd #if 0
35 1.4 cgd static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 5/31/93";
36 1.4 cgd #else
37 1.11 agc __RCSID("$NetBSD: misc.c,v 1.11 2003/08/07 09:37:28 agc Exp $");
38 1.4 cgd #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.7 simonb #include "monop.ext"
42 1.7 simonb #include <ctype.h>
43 1.7 simonb #include <signal.h>
44 1.1 cgd
45 1.1 cgd /*
46 1.1 cgd * This routine executes a truncated set of commands until a
47 1.1 cgd * "yes or "no" answer is gotten.
48 1.1 cgd */
49 1.6 christos int
50 1.1 cgd getyn(prompt)
51 1.8 jsm const char *prompt;
52 1.6 christos {
53 1.7 simonb int com;
54 1.1 cgd
55 1.1 cgd for (;;)
56 1.1 cgd if ((com=getinp(prompt, yn)) < 2)
57 1.1 cgd return com;
58 1.1 cgd else
59 1.1 cgd (*func[com-2])();
60 1.1 cgd }
61 1.7 simonb
62 1.1 cgd /*
63 1.1 cgd * This routine tells the player if he's out of money.
64 1.1 cgd */
65 1.6 christos void
66 1.6 christos notify()
67 1.6 christos {
68 1.1 cgd if (cur_p->money < 0)
69 1.1 cgd printf("That leaves you $%d in debt\n", -cur_p->money);
70 1.1 cgd else if (cur_p->money == 0)
71 1.1 cgd printf("that leaves you broke\n");
72 1.1 cgd else if (fixing && !told_em && cur_p->money > 0) {
73 1.1 cgd printf("-- You are now Solvent ---\n");
74 1.1 cgd told_em = TRUE;
75 1.1 cgd }
76 1.1 cgd }
77 1.7 simonb
78 1.1 cgd /*
79 1.1 cgd * This routine switches to the next player
80 1.1 cgd */
81 1.6 christos void
82 1.6 christos next_play()
83 1.6 christos {
84 1.10 cgd player = (player + 1) % num_play;
85 1.1 cgd cur_p = &play[player];
86 1.1 cgd num_doub = 0;
87 1.1 cgd }
88 1.7 simonb
89 1.1 cgd /*
90 1.1 cgd * This routine gets an integer from the keyboard after the
91 1.1 cgd * given prompt.
92 1.1 cgd */
93 1.6 christos int
94 1.1 cgd get_int(prompt)
95 1.8 jsm const char *prompt;
96 1.6 christos {
97 1.7 simonb int num;
98 1.7 simonb char *sp;
99 1.7 simonb int c;
100 1.7 simonb char buf[257];
101 1.1 cgd
102 1.1 cgd for (;;) {
103 1.1 cgd inter:
104 1.1 cgd printf(prompt);
105 1.1 cgd num = 0;
106 1.5 thorpej for (sp = buf; (c=getchar()) != '\n'; *sp++ = c)
107 1.5 thorpej if (c == -1) /* check for interrupted system call */
108 1.1 cgd goto inter;
109 1.5 thorpej *sp = c;
110 1.1 cgd if (sp == buf)
111 1.1 cgd continue;
112 1.1 cgd for (sp = buf; isspace(*sp); sp++)
113 1.1 cgd continue;
114 1.1 cgd for (; isdigit(*sp); sp++)
115 1.1 cgd num = num * 10 + *sp - '0';
116 1.1 cgd if (*sp == '\n')
117 1.1 cgd return num;
118 1.1 cgd else
119 1.1 cgd printf("I can't understand that\n");
120 1.1 cgd }
121 1.1 cgd }
122 1.7 simonb
123 1.1 cgd /*
124 1.1 cgd * This routine sets the monopoly flag from the list given.
125 1.1 cgd */
126 1.6 christos void
127 1.1 cgd set_ownlist(pl)
128 1.7 simonb int pl;
129 1.6 christos {
130 1.7 simonb int num; /* general counter */
131 1.7 simonb MON *orig; /* remember starting monop ptr */
132 1.7 simonb OWN *op; /* current owned prop */
133 1.7 simonb OWN *orig_op; /* origianl prop before loop */
134 1.1 cgd
135 1.1 cgd op = play[pl].own_list;
136 1.1 cgd #ifdef DEBUG
137 1.1 cgd printf("op [%d] = play[pl [%d] ].own_list;\n", op, pl);
138 1.1 cgd #endif
139 1.1 cgd while (op) {
140 1.1 cgd #ifdef DEBUG
141 1.1 cgd printf("op->sqr->type = %d\n", op->sqr->type);
142 1.1 cgd #endif
143 1.1 cgd switch (op->sqr->type) {
144 1.1 cgd case UTIL:
145 1.1 cgd #ifdef DEBUG
146 1.1 cgd printf(" case UTIL:\n");
147 1.1 cgd #endif
148 1.7 simonb for (num = 0; op && op->sqr->type == UTIL;
149 1.7 simonb op = op->next)
150 1.1 cgd num++;
151 1.1 cgd play[pl].num_util = num;
152 1.1 cgd #ifdef DEBUG
153 1.1 cgd printf("play[pl].num_util = num [%d];\n", num);
154 1.1 cgd #endif
155 1.1 cgd break;
156 1.1 cgd case RR:
157 1.1 cgd #ifdef DEBUG
158 1.1 cgd printf(" case RR:\n");
159 1.1 cgd #endif
160 1.7 simonb for (num = 0; op && op->sqr->type == RR;
161 1.7 simonb op = op->next) {
162 1.1 cgd #ifdef DEBUG
163 1.1 cgd printf("iter: %d\n", num);
164 1.7 simonb printf("op = %d, op->sqr = %d, "
165 1.7 simonb "op->sqr->type = %d\n", op, op->sqr,
166 1.7 simonb op->sqr->type);
167 1.1 cgd #endif
168 1.1 cgd num++;
169 1.1 cgd }
170 1.1 cgd play[pl].num_rr = num;
171 1.1 cgd #ifdef DEBUG
172 1.1 cgd printf("play[pl].num_rr = num [%d];\n", num);
173 1.1 cgd #endif
174 1.1 cgd break;
175 1.1 cgd case PRPTY:
176 1.1 cgd #ifdef DEBUG
177 1.1 cgd printf(" case PRPTY:\n");
178 1.1 cgd #endif
179 1.1 cgd orig = op->sqr->desc->mon_desc;
180 1.1 cgd orig_op = op;
181 1.1 cgd num = 0;
182 1.1 cgd while (op && op->sqr->desc->mon_desc == orig) {
183 1.1 cgd #ifdef DEBUG
184 1.1 cgd printf("iter: %d\n", num);
185 1.1 cgd #endif
186 1.1 cgd num++;
187 1.1 cgd #ifdef DEBUG
188 1.1 cgd printf("op = op->next ");
189 1.1 cgd #endif
190 1.1 cgd op = op->next;
191 1.1 cgd #ifdef DEBUG
192 1.1 cgd printf("[%d];\n", op);
193 1.1 cgd #endif
194 1.1 cgd }
195 1.1 cgd #ifdef DEBUG
196 1.1 cgd printf("num = %d\n");
197 1.1 cgd #endif
198 1.1 cgd if (orig == 0) {
199 1.7 simonb printf("panic: bad monopoly descriptor: "
200 1.7 simonb "orig = %p\n", orig);
201 1.1 cgd printf("player # %d\n", pl+1);
202 1.1 cgd printhold(pl);
203 1.6 christos printf("orig_op = %p\n", orig_op);
204 1.7 simonb printf("orig_op->sqr->type = %d (PRPTY)\n",
205 1.7 simonb op->sqr->type);
206 1.6 christos printf("orig_op->next = %p\n", op->next);
207 1.7 simonb printf("orig_op->sqr->desc = %p\n",
208 1.7 simonb op->sqr->desc);
209 1.6 christos printf("op = %p\n", op);
210 1.7 simonb printf("op->sqr->type = %d (PRPTY)\n",
211 1.7 simonb op->sqr->type);
212 1.6 christos printf("op->next = %p\n", op->next);
213 1.6 christos printf("op->sqr->desc = %p\n", op->sqr->desc);
214 1.1 cgd printf("num = %d\n", num);
215 1.1 cgd }
216 1.1 cgd #ifdef DEBUG
217 1.1 cgd printf("orig->num_in = %d\n", orig->num_in);
218 1.1 cgd #endif
219 1.1 cgd if (num == orig->num_in)
220 1.1 cgd is_monop(orig, pl);
221 1.1 cgd else
222 1.9 jsm is_not_monop(orig);
223 1.1 cgd break;
224 1.1 cgd }
225 1.1 cgd }
226 1.1 cgd }
227 1.7 simonb
228 1.1 cgd /*
229 1.1 cgd * This routine sets things up as if it is a new monopoly
230 1.1 cgd */
231 1.6 christos void
232 1.1 cgd is_monop(mp, pl)
233 1.7 simonb MON *mp;
234 1.7 simonb int pl;
235 1.6 christos {
236 1.7 simonb int i;
237 1.1 cgd
238 1.1 cgd mp->owner = pl;
239 1.1 cgd mp->num_own = mp->num_in;
240 1.1 cgd for (i = 0; i < mp->num_in; i++)
241 1.1 cgd mp->sq[i]->desc->monop = TRUE;
242 1.1 cgd mp->name = mp->mon_n;
243 1.1 cgd }
244 1.7 simonb
245 1.1 cgd /*
246 1.1 cgd * This routine sets things up as if it is no longer a monopoly
247 1.1 cgd */
248 1.6 christos void
249 1.9 jsm is_not_monop(mp)
250 1.7 simonb MON *mp;
251 1.6 christos {
252 1.7 simonb int i;
253 1.1 cgd
254 1.1 cgd mp->owner = -1;
255 1.1 cgd for (i = 0; i < mp->num_in; i++)
256 1.1 cgd mp->sq[i]->desc->monop = FALSE;
257 1.1 cgd mp->name = mp->not_m;
258 1.1 cgd }
259 1.7 simonb
260 1.1 cgd /*
261 1.1 cgd * This routine gives a list of the current player's routine
262 1.1 cgd */
263 1.6 christos void
264 1.7 simonb list()
265 1.6 christos {
266 1.1 cgd printhold(player);
267 1.1 cgd }
268 1.7 simonb
269 1.1 cgd /*
270 1.1 cgd * This routine gives a list of a given players holdings
271 1.1 cgd */
272 1.6 christos void
273 1.7 simonb list_all()
274 1.6 christos {
275 1.7 simonb int pl;
276 1.1 cgd
277 1.7 simonb while ((pl = getinp("Whose holdings do you want to see? ", name_list))
278 1.7 simonb < num_play)
279 1.1 cgd printhold(pl);
280 1.1 cgd }
281 1.7 simonb
282 1.1 cgd /*
283 1.1 cgd * This routine gives the players a chance before it exits.
284 1.1 cgd */
285 1.1 cgd void
286 1.6 christos quit()
287 1.6 christos {
288 1.1 cgd putchar('\n');
289 1.6 christos if (getyn("Do you all really want to quit? ") == 0)
290 1.1 cgd exit(0);
291 1.1 cgd }
292