xref: /openbsd/games/monop/houses.c (revision 771fbea0)
1 /*	$OpenBSD: houses.c,v 1.10 2016/01/08 18:20:33 mestre Exp $	*/
2 /*	$NetBSD: houses.c,v 1.3 1995/03/23 08:34:40 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1980, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 
36 #include "monop.ext"
37 
38 static char	*names[N_MON+2],
39 		cur_prop[80];
40 
41 static MON	*monops[N_MON];
42 
43 static void	buy_h(MON *);
44 static void	sell_h(MON *);
45 static void	list_cur(MON *);
46 static int	avail_houses();
47 static int	avail_hotels();
48 static bool	can_only_buy_hotel(MON *);
49 
50 /*
51  *	These routines deal with buying and selling houses
52  */
53 void
54 buy_houses(void)
55 {
56 	int	num_mon;
57 	MON	*mp;
58 	OWN	*op;
59 	bool	good, got_morg;
60 	int	i,p;
61 
62 over:
63 	num_mon = 0;
64 	good = TRUE;
65 	got_morg = FALSE;
66 	for (op = cur_p->own_list; op && op->sqr->type != PRPTY; op = op->next)
67 		continue;
68 	while (op)
69 		if (op->sqr->desc->monop) {
70 			mp = op->sqr->desc->mon_desc;
71 			names[num_mon] = (monops[num_mon]=mp)->name;
72 			num_mon++;
73 			got_morg = good = FALSE;
74 			for (i = 0; i < mp->num_in; i++) {
75 				if (op->sqr->desc->morg)
76 					got_morg = TRUE;
77 				if (op->sqr->desc->houses != 5)
78 					good = TRUE;
79 				op = op->next;
80 			}
81 			if (!good || got_morg)
82 				--num_mon;
83 		}
84 		else
85 			op = op->next;
86 	if (num_mon == 0) {
87 		if (got_morg)
88 			printf("You can't build on mortgaged monopolies.\n");
89 		else if (!good)
90 			printf("You can't build any more.\n");
91 		else
92 			printf("But you don't have any monopolies!!\n");
93 		return;
94 	}
95 	if (num_mon == 1)
96 		buy_h(monops[0]);
97 	else {
98 		names[num_mon++] = "done";
99 		names[num_mon--] = 0;
100 		if ((p=getinp("Which property do you wish to buy houses for? ", names)) == num_mon)
101 			return;
102 		buy_h(monops[p]);
103 		goto over;
104 	}
105 }
106 
107 static void
108 buy_h(MON *mnp)
109 {
110 	int	i;
111 	MON	*mp;
112 	int	price;
113 	shrt	input[3],temp[3];
114 	int	tot, tot2;
115 	PROP	*pp;
116 	int	nhous, nhot;
117 	bool chot;
118 
119 	mp = mnp;
120 	price = mp->h_cost * 50;
121 	nhous = avail_houses();
122 	nhot = avail_hotels();
123 	chot = can_only_buy_hotel(mnp);
124 	if (nhous == 0 && !chot) {
125 		printf("Building shortage:  no houses available.");
126 		return;
127 	}
128 	if (nhot == 0 && chot) {
129 		printf("Building shortage:  no hotels available.");
130 		return;
131 	}
132 blew_it:
133 	list_cur(mp);
134 	printf("Houses will cost $%d\n", price);
135 	printf("How many houses do you wish to buy for\n");
136 	for (i = 0; i < mp->num_in; i++) {
137 		pp = mp->sq[i]->desc;
138 over:
139 		if (pp->houses == 5) {
140 			printf("%s (H):\n", mp->sq[i]->name);
141 			input[i] = 0;
142 			temp[i] = 5;
143 			continue;
144 		}
145 		(void)snprintf(cur_prop, sizeof(cur_prop), "%s (%d): ",
146 			mp->sq[i]->name, pp->houses);
147 		input[i] = get_int(cur_prop);
148 		temp[i] = input[i] + pp->houses;
149 		if (temp[i] > 5 || temp[i] < 0) {
150 			printf("That's too many.  The most you can buy is %d\n",
151 			    5 - pp->houses);
152 				goto over;
153 			}
154 	}
155 	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
156 	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
157 err:		printf("That makes the spread too wide.  Try again\n");
158 		goto blew_it;
159 	}
160 	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
161 		goto err;
162 	for (tot = tot2 = i = 0; i < mp->num_in; i++) {
163 		if (temp[i] == 5 && input[i])
164 			tot2++;
165 		else
166 			tot += input[i];
167 	}
168 	if (tot > nhous) {
169 		printf(
170 "You have asked for %d house%s but only %d are available.  Try again\n",
171 		    tot, tot == 1 ? "":"s", nhous);
172 		goto blew_it;
173 	} else if (tot2 > nhot) {
174 		printf(
175 "You have asked for %d hotel%s but only %d are available.  Try again\n",
176 		    tot2, tot2 == 1 ? "":"s", nhot);
177 		goto blew_it;
178 	}
179 	if (!chot && tot2) {
180 		printf(
181 "You must have 4 houses on all your property before building hotels\n");
182 		goto blew_it;
183 	}
184 	if (tot || tot2) {
185 		printf("You asked for %d %s%s for $%d\n", tot ? tot : tot2,
186 		    tot ? "house" : "hotel", (tot == 1 || tot2 == 1) ? "" : "s",
187 		    (tot ? tot : tot2) * price);
188 		if (getyn("Is that ok? ") == 0) {
189 			cur_p->money -= (tot ? tot : tot2) * price;
190 			for (tot = i = 0; i < mp->num_in; i++)
191 				mp->sq[i]->desc->houses = temp[i];
192 		}
193 	}
194 }
195 
196 /*
197  *	This routine sells houses.
198  */
199 void
200 sell_houses(void)
201 {
202 	int	num_mon;
203 	MON	*mp;
204 	OWN	*op;
205 	bool	good;
206 	int	p;
207 
208 over:
209 	num_mon = 0;
210 	good = TRUE;
211 	for (op = cur_p->own_list; op; op = op->next)
212 		if (op->sqr->type == PRPTY && op->sqr->desc->monop) {
213 			mp = op->sqr->desc->mon_desc;
214 			names[num_mon] = (monops[num_mon]=mp)->name;
215 			num_mon++;
216 			good = 0;
217 			do
218 				if (!good && op->sqr->desc->houses != 0)
219 					good++;
220 			while (op->next && op->sqr->desc->mon_desc == mp
221 			    && (op=op->next));
222 			if (!good)
223 				--num_mon;
224 		}
225 	if (num_mon == 0) {
226 		printf("You don't have any houses to sell!!\n");
227 		return;
228 	}
229 	if (num_mon == 1)
230 		sell_h(monops[0]);
231 	else {
232 		names[num_mon++] = "done";
233 		names[num_mon--] = 0;
234 		if ((p=getinp("Which property do you wish to sell houses from? ", names)) == num_mon)
235 			return;
236 		sell_h(monops[p]);
237 		notify();
238 		goto over;
239 	}
240 }
241 
242 static void
243 sell_h(MON *mnp)
244 {
245 	int	i;
246 	MON	*mp;
247 	int	price;
248 	shrt	input[3],temp[3];
249 	int	tot;
250 	PROP	*pp;
251 
252 	mp = mnp;
253 	price = mp->h_cost * 25;
254 blew_it:
255 	printf("Houses will get you $%d apiece\n", price);
256 	list_cur(mp);
257 	printf("How many houses do you wish to sell from\n");
258 	for (i = 0; i < mp->num_in; i++) {
259 		pp = mp->sq[i]->desc;
260 over:
261 		if (pp->houses == 0) {
262 			printf("%s (0):\n", mp->sq[i]->name);
263 			input[i] = temp[i] = 0;
264 			continue;
265 		}
266 		if (pp->houses < 5)
267 			(void)snprintf(cur_prop, sizeof(cur_prop), "%s (%d): ",
268 				mp->sq[i]->name,pp->houses);
269 		else
270 			(void)snprintf(cur_prop, sizeof(cur_prop), "%s (H): ",
271 			    mp->sq[i]->name);
272 		input[i] = get_int(cur_prop);
273 		temp[i] = pp->houses - input[i];
274 		if (temp[i] < 0) {
275 			printf("That's too many.  The most you can sell is %d\n", pp->houses);
276 				goto over;
277 			}
278 	}
279 	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
280 	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
281 err:		printf("That makes the spread too wide.  Try again\n");
282 		goto blew_it;
283 	}
284 	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
285 		goto err;
286 	for (tot = i = 0; i < mp->num_in; i++)
287 		tot += input[i];
288 	if (tot) {
289 		printf("You asked to sell %d house%s for $%d\n", tot,
290 		    tot == 1 ? "" : "s", tot * price);
291 		if (getyn("Is that ok? ") == 0) {
292 			cur_p->money += tot * price;
293 			for (tot = i = 0; i < mp->num_in; i++)
294 				mp->sq[i]->desc->houses = temp[i];
295 		}
296 	}
297 }
298 
299 static void
300 list_cur(MON *mp)
301 {
302 	int	i;
303 	SQUARE	*sqp;
304 
305 	for (i = 0; i < mp->num_in; i++) {
306 		sqp = mp->sq[i];
307 		if (sqp->desc->houses == 5)
308 			printf("%s (H) ", sqp->name);
309 		else
310 			printf("%s (%d) ", sqp->name, sqp->desc->houses);
311 	}
312 	putchar('\n');
313 }
314 
315 static int
316 avail_houses(void)
317 {
318 	int i, c;
319 	SQUARE *sqp;
320 
321 	c = 0;
322 	for (i = 0; i < N_SQRS; i++) {
323 		sqp = &board[i];
324 		if (sqp->type == PRPTY && sqp->owner >= 0 && sqp->desc->monop) {
325 			if (sqp->desc->houses < 5 && sqp->desc->houses > 0)
326 				c += sqp->desc->houses;
327 		}
328 	}
329 	return(N_HOUSE - c);
330 }
331 
332 static int
333 avail_hotels(void)
334 {
335 	int i, c;
336 	SQUARE *sqp;
337 
338 	c = 0;
339 	for (i = 0; i < N_SQRS; i++) {
340 		sqp = &board[i];
341 		if (sqp->type == PRPTY && sqp->owner >= 0 && sqp->desc->monop) {
342 			if (sqp->desc->houses == 5)
343 				c++;
344 		}
345 	}
346 	return(N_HOTEL - c);
347 }
348 
349 static bool
350 can_only_buy_hotel(MON *mp)
351 {
352 	int i;
353 
354 	for (i = 0; i < mp->num_in; i++) {
355 		if (mp->sq[i]->desc->houses < 4)
356 			return(FALSE);
357 	}
358 	return(TRUE);
359 }
360