misc.c revision 1.21 1 1.21 dholland /* $NetBSD: misc.c,v 1.21 2009/08/12 08:10:49 dholland 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.21 dholland __RCSID("$NetBSD: misc.c,v 1.21 2009/08/12 08:10:49 dholland Exp $");
38 1.4 cgd #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.7 simonb #include <ctype.h>
42 1.18 dholland #include <limits.h>
43 1.7 simonb #include <signal.h>
44 1.20 christos #include <errno.h>
45 1.1 cgd
46 1.19 dholland #include "monop.h"
47 1.19 dholland
48 1.21 dholland static void is_monop(MON *, int);
49 1.21 dholland
50 1.1 cgd /*
51 1.1 cgd * This routine executes a truncated set of commands until a
52 1.1 cgd * "yes or "no" answer is gotten.
53 1.1 cgd */
54 1.6 christos int
55 1.1 cgd getyn(prompt)
56 1.8 jsm const char *prompt;
57 1.6 christos {
58 1.7 simonb int com;
59 1.1 cgd
60 1.1 cgd for (;;)
61 1.12 jsm if ((com=getinp(prompt, yncoms)) < 2)
62 1.1 cgd return com;
63 1.1 cgd else
64 1.1 cgd (*func[com-2])();
65 1.1 cgd }
66 1.7 simonb
67 1.1 cgd /*
68 1.1 cgd * This routine tells the player if he's out of money.
69 1.1 cgd */
70 1.6 christos void
71 1.6 christos notify()
72 1.6 christos {
73 1.1 cgd if (cur_p->money < 0)
74 1.1 cgd printf("That leaves you $%d in debt\n", -cur_p->money);
75 1.1 cgd else if (cur_p->money == 0)
76 1.1 cgd printf("that leaves you broke\n");
77 1.1 cgd else if (fixing && !told_em && cur_p->money > 0) {
78 1.1 cgd printf("-- You are now Solvent ---\n");
79 1.1 cgd told_em = TRUE;
80 1.1 cgd }
81 1.1 cgd }
82 1.7 simonb
83 1.1 cgd /*
84 1.1 cgd * This routine switches to the next player
85 1.1 cgd */
86 1.6 christos void
87 1.6 christos next_play()
88 1.6 christos {
89 1.10 cgd player = (player + 1) % num_play;
90 1.1 cgd cur_p = &play[player];
91 1.1 cgd num_doub = 0;
92 1.1 cgd }
93 1.7 simonb
94 1.1 cgd /*
95 1.1 cgd * This routine gets an integer from the keyboard after the
96 1.1 cgd * given prompt.
97 1.1 cgd */
98 1.6 christos int
99 1.1 cgd get_int(prompt)
100 1.8 jsm const char *prompt;
101 1.6 christos {
102 1.18 dholland long num;
103 1.7 simonb char *sp;
104 1.7 simonb char buf[257];
105 1.1 cgd
106 1.1 cgd for (;;) {
107 1.17 dholland printf("%s", prompt);
108 1.18 dholland fgets(buf, sizeof(buf), stdin);
109 1.18 dholland if (feof(stdin))
110 1.18 dholland return 0;
111 1.18 dholland sp = strchr(buf, '\n');
112 1.18 dholland if (sp)
113 1.18 dholland *sp = '\0';
114 1.18 dholland errno = 0;
115 1.18 dholland num = strtol(buf, &sp, 10);
116 1.18 dholland if (errno || strlen(sp) > 0 || num < 0 || num >= INT_MAX) {
117 1.18 dholland printf("I can't understand that\n");
118 1.1 cgd continue;
119 1.18 dholland }
120 1.18 dholland return num;
121 1.1 cgd }
122 1.1 cgd }
123 1.7 simonb
124 1.1 cgd /*
125 1.1 cgd * This routine sets the monopoly flag from the list given.
126 1.1 cgd */
127 1.6 christos void
128 1.1 cgd set_ownlist(pl)
129 1.7 simonb int pl;
130 1.6 christos {
131 1.7 simonb int num; /* general counter */
132 1.7 simonb MON *orig; /* remember starting monop ptr */
133 1.7 simonb OWN *op; /* current owned prop */
134 1.15 dholland OWN *orig_op; /* original prop before loop */
135 1.1 cgd
136 1.1 cgd op = play[pl].own_list;
137 1.1 cgd #ifdef DEBUG
138 1.16 dholland printf("op [%p] = play[pl [%d] ].own_list;\n", op, pl);
139 1.1 cgd #endif
140 1.1 cgd while (op) {
141 1.1 cgd #ifdef DEBUG
142 1.1 cgd printf("op->sqr->type = %d\n", op->sqr->type);
143 1.1 cgd #endif
144 1.1 cgd switch (op->sqr->type) {
145 1.1 cgd case UTIL:
146 1.1 cgd #ifdef DEBUG
147 1.1 cgd printf(" case UTIL:\n");
148 1.1 cgd #endif
149 1.7 simonb for (num = 0; op && op->sqr->type == UTIL;
150 1.7 simonb op = op->next)
151 1.1 cgd num++;
152 1.1 cgd play[pl].num_util = num;
153 1.1 cgd #ifdef DEBUG
154 1.1 cgd printf("play[pl].num_util = num [%d];\n", num);
155 1.1 cgd #endif
156 1.1 cgd break;
157 1.1 cgd case RR:
158 1.1 cgd #ifdef DEBUG
159 1.1 cgd printf(" case RR:\n");
160 1.1 cgd #endif
161 1.7 simonb for (num = 0; op && op->sqr->type == RR;
162 1.7 simonb op = op->next) {
163 1.1 cgd #ifdef DEBUG
164 1.1 cgd printf("iter: %d\n", num);
165 1.16 dholland printf("op = %p, op->sqr = %p, "
166 1.7 simonb "op->sqr->type = %d\n", op, op->sqr,
167 1.7 simonb op->sqr->type);
168 1.1 cgd #endif
169 1.1 cgd num++;
170 1.1 cgd }
171 1.1 cgd play[pl].num_rr = num;
172 1.1 cgd #ifdef DEBUG
173 1.1 cgd printf("play[pl].num_rr = num [%d];\n", num);
174 1.1 cgd #endif
175 1.1 cgd break;
176 1.1 cgd case PRPTY:
177 1.1 cgd #ifdef DEBUG
178 1.1 cgd printf(" case PRPTY:\n");
179 1.1 cgd #endif
180 1.1 cgd orig = op->sqr->desc->mon_desc;
181 1.1 cgd orig_op = op;
182 1.1 cgd num = 0;
183 1.1 cgd while (op && op->sqr->desc->mon_desc == orig) {
184 1.1 cgd #ifdef DEBUG
185 1.1 cgd printf("iter: %d\n", num);
186 1.1 cgd #endif
187 1.1 cgd num++;
188 1.1 cgd #ifdef DEBUG
189 1.1 cgd printf("op = op->next ");
190 1.1 cgd #endif
191 1.1 cgd op = op->next;
192 1.1 cgd #ifdef DEBUG
193 1.16 dholland printf("[%p];\n", op);
194 1.1 cgd #endif
195 1.1 cgd }
196 1.1 cgd #ifdef DEBUG
197 1.16 dholland printf("num = %d\n", num);
198 1.1 cgd #endif
199 1.16 dholland if (orig == NULL) {
200 1.7 simonb printf("panic: bad monopoly descriptor: "
201 1.7 simonb "orig = %p\n", orig);
202 1.1 cgd printf("player # %d\n", pl+1);
203 1.1 cgd printhold(pl);
204 1.6 christos printf("orig_op = %p\n", orig_op);
205 1.14 christos if (orig_op) {
206 1.14 christos printf("orig_op->sqr->type = %d (PRPTY)\n",
207 1.14 christos orig_op->sqr->type);
208 1.16 dholland printf("orig_op->next = %p\n",
209 1.16 dholland orig_op->next);
210 1.14 christos printf("orig_op->sqr->desc = %p\n",
211 1.14 christos orig_op->sqr->desc);
212 1.14 christos }
213 1.6 christos printf("op = %p\n", op);
214 1.14 christos if (op) {
215 1.14 christos printf("op->sqr->type = %d (PRPTY)\n",
216 1.14 christos op->sqr->type);
217 1.14 christos printf("op->next = %p\n", op->next);
218 1.14 christos printf("op->sqr->desc = %p\n",
219 1.14 christos op->sqr->desc);
220 1.14 christos }
221 1.1 cgd printf("num = %d\n", num);
222 1.18 dholland exit(1);
223 1.1 cgd }
224 1.1 cgd #ifdef DEBUG
225 1.1 cgd printf("orig->num_in = %d\n", orig->num_in);
226 1.1 cgd #endif
227 1.1 cgd if (num == orig->num_in)
228 1.1 cgd is_monop(orig, pl);
229 1.1 cgd else
230 1.9 jsm is_not_monop(orig);
231 1.1 cgd break;
232 1.1 cgd }
233 1.1 cgd }
234 1.1 cgd }
235 1.7 simonb
236 1.1 cgd /*
237 1.1 cgd * This routine sets things up as if it is a new monopoly
238 1.1 cgd */
239 1.21 dholland static void
240 1.1 cgd is_monop(mp, pl)
241 1.7 simonb MON *mp;
242 1.7 simonb int pl;
243 1.6 christos {
244 1.7 simonb int i;
245 1.1 cgd
246 1.1 cgd mp->owner = pl;
247 1.1 cgd mp->num_own = mp->num_in;
248 1.1 cgd for (i = 0; i < mp->num_in; i++)
249 1.1 cgd mp->sq[i]->desc->monop = TRUE;
250 1.1 cgd mp->name = mp->mon_n;
251 1.1 cgd }
252 1.7 simonb
253 1.1 cgd /*
254 1.1 cgd * This routine sets things up as if it is no longer a monopoly
255 1.1 cgd */
256 1.6 christos void
257 1.9 jsm is_not_monop(mp)
258 1.7 simonb MON *mp;
259 1.6 christos {
260 1.7 simonb int i;
261 1.1 cgd
262 1.1 cgd mp->owner = -1;
263 1.1 cgd for (i = 0; i < mp->num_in; i++)
264 1.1 cgd mp->sq[i]->desc->monop = FALSE;
265 1.1 cgd mp->name = mp->not_m;
266 1.1 cgd }
267 1.7 simonb
268 1.1 cgd /*
269 1.1 cgd * This routine gives a list of the current player's routine
270 1.1 cgd */
271 1.6 christos void
272 1.7 simonb list()
273 1.6 christos {
274 1.1 cgd printhold(player);
275 1.1 cgd }
276 1.7 simonb
277 1.1 cgd /*
278 1.1 cgd * This routine gives a list of a given players holdings
279 1.1 cgd */
280 1.6 christos void
281 1.7 simonb list_all()
282 1.6 christos {
283 1.7 simonb int pl;
284 1.1 cgd
285 1.7 simonb while ((pl = getinp("Whose holdings do you want to see? ", name_list))
286 1.7 simonb < num_play)
287 1.1 cgd printhold(pl);
288 1.1 cgd }
289 1.7 simonb
290 1.1 cgd /*
291 1.1 cgd * This routine gives the players a chance before it exits.
292 1.1 cgd */
293 1.1 cgd void
294 1.6 christos quit()
295 1.6 christos {
296 1.1 cgd putchar('\n');
297 1.6 christos if (getyn("Do you all really want to quit? ") == 0)
298 1.1 cgd exit(0);
299 1.1 cgd }
300