houses.c revision 1.15 1 1.15 dholland /* $NetBSD: houses.c,v 1.15 2012/06/19 05:35:32 dholland Exp $ */
2 1.3 cgd
3 1.1 cgd /*
4 1.3 cgd * Copyright (c) 1980, 1993
5 1.3 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.7 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.4 christos #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.3 cgd #if 0
35 1.3 cgd static char sccsid[] = "@(#)houses.c 8.1 (Berkeley) 5/31/93";
36 1.3 cgd #else
37 1.15 dholland __RCSID("$NetBSD: houses.c,v 1.15 2012/06/19 05:35:32 dholland Exp $");
38 1.3 cgd #endif
39 1.1 cgd #endif /* not lint */
40 1.1 cgd
41 1.13 dholland #include "monop.h"
42 1.1 cgd
43 1.6 jsm static const char *names[N_MON+2];
44 1.6 jsm static char cur_prop[80];
45 1.1 cgd
46 1.1 cgd static MON *monops[N_MON];
47 1.1 cgd
48 1.8 jsm static void buy_h(MON *);
49 1.8 jsm static void sell_h(MON *);
50 1.8 jsm static void list_cur(MON *);
51 1.14 dholland static int get_avail_houses(void);
52 1.14 dholland static int get_avail_hotels(void);
53 1.14 dholland static bool ready_for_hotels(MON *);
54 1.12 dholland
55 1.1 cgd /*
56 1.1 cgd * These routines deal with buying and selling houses
57 1.1 cgd */
58 1.4 christos void
59 1.15 dholland buy_houses(void)
60 1.4 christos {
61 1.4 christos int num_mon;
62 1.5 simonb MON *mp;
63 1.5 simonb OWN *op;
64 1.11 dholland bool good, got_morg;
65 1.5 simonb int i,p;
66 1.1 cgd
67 1.1 cgd over:
68 1.1 cgd num_mon = 0;
69 1.1 cgd good = TRUE;
70 1.1 cgd got_morg = FALSE;
71 1.1 cgd for (op = cur_p->own_list; op && op->sqr->type != PRPTY; op = op->next)
72 1.1 cgd continue;
73 1.1 cgd while (op)
74 1.1 cgd if (op->sqr->desc->monop) {
75 1.1 cgd mp = op->sqr->desc->mon_desc;
76 1.1 cgd names[num_mon] = (monops[num_mon]=mp)->name;
77 1.1 cgd num_mon++;
78 1.1 cgd got_morg = good = FALSE;
79 1.1 cgd for (i = 0; i < mp->num_in; i++) {
80 1.1 cgd if (op->sqr->desc->morg)
81 1.11 dholland got_morg = TRUE;
82 1.1 cgd if (op->sqr->desc->houses != 5)
83 1.11 dholland good = TRUE;
84 1.1 cgd op = op->next;
85 1.1 cgd }
86 1.1 cgd if (!good || got_morg)
87 1.1 cgd --num_mon;
88 1.1 cgd }
89 1.1 cgd else
90 1.1 cgd op = op->next;
91 1.1 cgd if (num_mon == 0) {
92 1.1 cgd if (got_morg)
93 1.1 cgd printf("You can't build on mortgaged monopolies.\n");
94 1.1 cgd else if (!good)
95 1.1 cgd printf("You can't build any more.\n");
96 1.1 cgd else
97 1.1 cgd printf("But you don't have any monopolies!!\n");
98 1.1 cgd return;
99 1.1 cgd }
100 1.1 cgd if (num_mon == 1)
101 1.1 cgd buy_h(monops[0]);
102 1.1 cgd else {
103 1.1 cgd names[num_mon++] = "done";
104 1.1 cgd names[num_mon--] = 0;
105 1.5 simonb if ((p = getinp(
106 1.5 simonb "Which property do you wish to buy houses for? ",
107 1.5 simonb names)) == num_mon)
108 1.1 cgd return;
109 1.1 cgd buy_h(monops[p]);
110 1.1 cgd goto over;
111 1.1 cgd }
112 1.1 cgd }
113 1.1 cgd
114 1.4 christos static void
115 1.15 dholland buy_h(MON *mnp)
116 1.4 christos {
117 1.5 simonb int i;
118 1.5 simonb MON *mp;
119 1.5 simonb int price;
120 1.14 dholland short input[3], result[3];
121 1.14 dholland int wanted_houses, wanted_hotels;
122 1.14 dholland int total_purchase;
123 1.5 simonb PROP *pp;
124 1.14 dholland int avail_houses, avail_hotels;
125 1.14 dholland bool buying_hotels;
126 1.1 cgd
127 1.1 cgd mp = mnp;
128 1.1 cgd price = mp->h_cost * 50;
129 1.14 dholland
130 1.14 dholland avail_houses = get_avail_houses();
131 1.14 dholland avail_hotels = get_avail_hotels();
132 1.14 dholland buying_hotels = ready_for_hotels(mnp);
133 1.14 dholland
134 1.14 dholland if (avail_houses == 0 && !buying_hotels) {
135 1.12 dholland printf("Building shortage: no houses available.");
136 1.12 dholland return;
137 1.12 dholland }
138 1.14 dholland if (avail_hotels == 0 && buying_hotels) {
139 1.12 dholland printf("Building shortage: no hotels available.");
140 1.12 dholland return;
141 1.12 dholland }
142 1.14 dholland
143 1.1 cgd blew_it:
144 1.1 cgd list_cur(mp);
145 1.1 cgd printf("Houses will cost $%d\n", price);
146 1.1 cgd printf("How many houses do you wish to buy for\n");
147 1.1 cgd for (i = 0; i < mp->num_in; i++) {
148 1.1 cgd pp = mp->sq[i]->desc;
149 1.1 cgd over:
150 1.1 cgd if (pp->houses == 5) {
151 1.1 cgd printf("%s (H):\n", mp->sq[i]->name);
152 1.1 cgd input[i] = 0;
153 1.14 dholland result[i] = 5;
154 1.1 cgd continue;
155 1.1 cgd }
156 1.10 dholland (void)snprintf(cur_prop, sizeof(cur_prop), "%s (%d): ",
157 1.1 cgd mp->sq[i]->name, pp->houses);
158 1.1 cgd input[i] = get_int(cur_prop);
159 1.14 dholland result[i] = input[i] + pp->houses;
160 1.14 dholland if (result[i] > 5 || result[i] < 0) {
161 1.1 cgd printf("That's too many. The most you can buy is %d\n",
162 1.12 dholland 5 - pp->houses);
163 1.1 cgd goto over;
164 1.1 cgd }
165 1.1 cgd }
166 1.14 dholland if (mp->num_in == 3 &&
167 1.14 dholland (abs(result[0] - result[1]) > 1 ||
168 1.14 dholland abs(result[0] - result[2]) > 1 ||
169 1.14 dholland abs(result[1] - result[2]) > 1)) {
170 1.1 cgd err: printf("That makes the spread too wide. Try again\n");
171 1.1 cgd goto blew_it;
172 1.1 cgd }
173 1.14 dholland else if (mp->num_in == 2 && abs(result[0] - result[1]) > 1)
174 1.1 cgd goto err;
175 1.14 dholland
176 1.14 dholland wanted_houses = 0;
177 1.14 dholland wanted_hotels = 0;
178 1.14 dholland total_purchase = 0;
179 1.14 dholland
180 1.14 dholland for (i = 0; i < mp->num_in; i++) {
181 1.14 dholland wanted_houses += input[i];
182 1.14 dholland total_purchase += input[i];
183 1.14 dholland if (result[i] == 5 && input[i] > 0) {
184 1.14 dholland wanted_hotels++;
185 1.14 dholland wanted_houses--;
186 1.14 dholland }
187 1.12 dholland }
188 1.14 dholland if (wanted_houses > avail_houses) {
189 1.14 dholland printf("You have asked for %d %s but only %d are available. "
190 1.14 dholland "Try again\n",
191 1.14 dholland wanted_houses, wanted_houses == 1 ? "house" : "houses",
192 1.14 dholland avail_houses);
193 1.12 dholland goto blew_it;
194 1.14 dholland } else if (wanted_hotels > avail_hotels) {
195 1.14 dholland printf("You have asked for %d %s but only %d are available. "
196 1.14 dholland "Try again\n",
197 1.14 dholland wanted_hotels, wanted_hotels == 1 ? "hotel" : "hotels",
198 1.14 dholland avail_hotels);
199 1.12 dholland goto blew_it;
200 1.12 dholland }
201 1.12 dholland
202 1.14 dholland if (total_purchase) {
203 1.14 dholland printf("You asked for %d %s and %d %s for $%d\n",
204 1.14 dholland wanted_houses, wanted_houses == 1 ? "house" : "houses",
205 1.14 dholland wanted_hotels, wanted_hotels == 1 ? "hotel" : "hotels",
206 1.14 dholland total_purchase * price);
207 1.4 christos if (getyn("Is that ok? ") == 0) {
208 1.14 dholland cur_p->money -= total_purchase * price;
209 1.14 dholland for (i = 0; i < mp->num_in; i++)
210 1.14 dholland mp->sq[i]->desc->houses = result[i];
211 1.1 cgd }
212 1.1 cgd }
213 1.1 cgd }
214 1.1 cgd
215 1.1 cgd /*
216 1.1 cgd * This routine sells houses.
217 1.1 cgd */
218 1.4 christos void
219 1.15 dholland sell_houses(void)
220 1.4 christos {
221 1.5 simonb int num_mon;
222 1.5 simonb MON *mp;
223 1.5 simonb OWN *op;
224 1.5 simonb bool good;
225 1.5 simonb int p;
226 1.1 cgd
227 1.1 cgd over:
228 1.1 cgd num_mon = 0;
229 1.1 cgd good = TRUE;
230 1.9 christos for (op = cur_p->own_list; op;)
231 1.1 cgd if (op->sqr->type == PRPTY && op->sqr->desc->monop) {
232 1.1 cgd mp = op->sqr->desc->mon_desc;
233 1.1 cgd names[num_mon] = (monops[num_mon]=mp)->name;
234 1.1 cgd num_mon++;
235 1.1 cgd good = 0;
236 1.1 cgd do
237 1.1 cgd if (!good && op->sqr->desc->houses != 0)
238 1.11 dholland good = TRUE;
239 1.1 cgd while (op->next && op->sqr->desc->mon_desc == mp
240 1.9 christos && (op = op->next));
241 1.1 cgd if (!good)
242 1.1 cgd --num_mon;
243 1.9 christos } else
244 1.9 christos op = op->next;
245 1.1 cgd if (num_mon == 0) {
246 1.1 cgd printf("You don't have any houses to sell!!\n");
247 1.1 cgd return;
248 1.1 cgd }
249 1.1 cgd if (num_mon == 1)
250 1.1 cgd sell_h(monops[0]);
251 1.1 cgd else {
252 1.1 cgd names[num_mon++] = "done";
253 1.1 cgd names[num_mon--] = 0;
254 1.5 simonb if ((p = getinp(
255 1.5 simonb "Which property do you wish to sell houses from? ",
256 1.5 simonb names)) == num_mon)
257 1.1 cgd return;
258 1.1 cgd sell_h(monops[p]);
259 1.1 cgd notify();
260 1.1 cgd goto over;
261 1.1 cgd }
262 1.1 cgd }
263 1.1 cgd
264 1.4 christos static void
265 1.15 dholland sell_h(MON *mnp)
266 1.4 christos {
267 1.5 simonb int i;
268 1.5 simonb MON *mp;
269 1.5 simonb int price;
270 1.5 simonb short input[3],temp[3];
271 1.5 simonb int tot;
272 1.5 simonb PROP *pp;
273 1.1 cgd
274 1.1 cgd mp = mnp;
275 1.1 cgd price = mp->h_cost * 25;
276 1.1 cgd blew_it:
277 1.1 cgd printf("Houses will get you $%d apiece\n", price);
278 1.1 cgd list_cur(mp);
279 1.1 cgd printf("How many houses do you wish to sell from\n");
280 1.1 cgd for (i = 0; i < mp->num_in; i++) {
281 1.1 cgd pp = mp->sq[i]->desc;
282 1.1 cgd over:
283 1.1 cgd if (pp->houses == 0) {
284 1.1 cgd printf("%s (0):\n", mp->sq[i]->name);
285 1.1 cgd input[i] = temp[i] = 0;
286 1.1 cgd continue;
287 1.1 cgd }
288 1.1 cgd if (pp->houses < 5)
289 1.10 dholland (void)snprintf(cur_prop, sizeof(cur_prop), "%s (%d): ",
290 1.1 cgd mp->sq[i]->name,pp->houses);
291 1.1 cgd else
292 1.10 dholland (void)snprintf(cur_prop, sizeof(cur_prop), "%s (H): ",
293 1.10 dholland mp->sq[i]->name);
294 1.1 cgd input[i] = get_int(cur_prop);
295 1.1 cgd temp[i] = pp->houses - input[i];
296 1.1 cgd if (temp[i] < 0) {
297 1.5 simonb printf(
298 1.5 simonb "That's too many. The most you can sell is %d\n",
299 1.5 simonb pp->houses);
300 1.1 cgd goto over;
301 1.1 cgd }
302 1.1 cgd }
303 1.1 cgd if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
304 1.1 cgd abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
305 1.1 cgd err: printf("That makes the spread too wide. Try again\n");
306 1.1 cgd goto blew_it;
307 1.1 cgd }
308 1.1 cgd else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
309 1.1 cgd goto err;
310 1.1 cgd for (tot = i = 0; i < mp->num_in; i++)
311 1.1 cgd tot += input[i];
312 1.1 cgd if (tot) {
313 1.12 dholland printf("You asked to sell %d house%s for $%d\n", tot,
314 1.12 dholland tot == 1 ? "" : "s", tot * price);
315 1.4 christos if (getyn("Is that ok? ") == 0) {
316 1.1 cgd cur_p->money += tot * price;
317 1.1 cgd for (tot = i = 0; i < mp->num_in; i++)
318 1.1 cgd mp->sq[i]->desc->houses = temp[i];
319 1.1 cgd }
320 1.1 cgd }
321 1.1 cgd }
322 1.1 cgd
323 1.4 christos static void
324 1.15 dholland list_cur(MON *mp)
325 1.4 christos {
326 1.5 simonb int i;
327 1.5 simonb SQUARE *sqp;
328 1.1 cgd
329 1.1 cgd for (i = 0; i < mp->num_in; i++) {
330 1.1 cgd sqp = mp->sq[i];
331 1.1 cgd if (sqp->desc->houses == 5)
332 1.1 cgd printf("%s (H) ", sqp->name);
333 1.1 cgd else
334 1.1 cgd printf("%s (%d) ", sqp->name, sqp->desc->houses);
335 1.1 cgd }
336 1.1 cgd putchar('\n');
337 1.1 cgd }
338 1.12 dholland
339 1.12 dholland static int
340 1.14 dholland get_avail_houses(void)
341 1.12 dholland {
342 1.12 dholland int i, c;
343 1.12 dholland SQUARE *sqp;
344 1.12 dholland
345 1.12 dholland c = 0;
346 1.12 dholland for (i = 0; i < N_SQRS; i++) {
347 1.12 dholland sqp = &board[i];
348 1.12 dholland if (sqp->type == PRPTY && sqp->owner >= 0 && sqp->desc->monop) {
349 1.12 dholland if (sqp->desc->houses < 5 && sqp->desc->houses > 0)
350 1.12 dholland c += sqp->desc->houses;
351 1.12 dholland }
352 1.12 dholland }
353 1.12 dholland return(N_HOUSE - c);
354 1.12 dholland }
355 1.12 dholland
356 1.12 dholland static int
357 1.14 dholland get_avail_hotels(void)
358 1.12 dholland {
359 1.12 dholland int i, c;
360 1.12 dholland SQUARE *sqp;
361 1.12 dholland
362 1.12 dholland c = 0;
363 1.12 dholland for (i = 0; i < N_SQRS; i++) {
364 1.12 dholland sqp = &board[i];
365 1.12 dholland if (sqp->type == PRPTY && sqp->owner >= 0 && sqp->desc->monop) {
366 1.12 dholland if (sqp->desc->houses == 5)
367 1.12 dholland c++;
368 1.12 dholland }
369 1.12 dholland }
370 1.12 dholland return(N_HOTEL - c);
371 1.12 dholland }
372 1.12 dholland
373 1.14 dholland /*
374 1.14 dholland * If we can put a hotel on, we can't put any houses on, and if we can
375 1.14 dholland * put houses on, then we can't put a hotel on yet.
376 1.14 dholland */
377 1.12 dholland static bool
378 1.14 dholland ready_for_hotels(MON *mp)
379 1.12 dholland {
380 1.12 dholland int i;
381 1.12 dholland
382 1.12 dholland for (i = 0; i < mp->num_in; i++) {
383 1.12 dholland if (mp->sq[i]->desc->houses < 4)
384 1.12 dholland return(FALSE);
385 1.12 dholland }
386 1.12 dholland return(TRUE);
387 1.12 dholland }
388