xref: /openbsd/games/monop/houses.c (revision cca36db2)
1 /*	$OpenBSD: houses.c,v 1.8 2009/10/27 23:59:26 deraadt 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	"monop.ext"
34 
35 static char	*names[N_MON+2],
36 		cur_prop[80];
37 
38 static MON	*monops[N_MON];
39 
40 static void	buy_h(MON *);
41 static void	sell_h(MON *);
42 static void	list_cur(MON *);
43 static int	avail_houses();
44 static int	avail_hotels();
45 static bool	can_only_buy_hotel(MON *);
46 
47 /*
48  *	These routines deal with buying and selling houses
49  */
50 void
51 buy_houses()
52 {
53 	int	num_mon;
54 	MON	*mp;
55 	OWN	*op;
56 	bool	good, got_morg;
57 	int	i,p;
58 
59 over:
60 	num_mon = 0;
61 	good = TRUE;
62 	got_morg = FALSE;
63 	for (op = cur_p->own_list; op && op->sqr->type != PRPTY; op = op->next)
64 		continue;
65 	while (op)
66 		if (op->sqr->desc->monop) {
67 			mp = op->sqr->desc->mon_desc;
68 			names[num_mon] = (monops[num_mon]=mp)->name;
69 			num_mon++;
70 			got_morg = good = FALSE;
71 			for (i = 0; i < mp->num_in; i++) {
72 				if (op->sqr->desc->morg)
73 					got_morg = TRUE;
74 				if (op->sqr->desc->houses != 5)
75 					good = TRUE;
76 				op = op->next;
77 			}
78 			if (!good || got_morg)
79 				--num_mon;
80 		}
81 		else
82 			op = op->next;
83 	if (num_mon == 0) {
84 		if (got_morg)
85 			printf("You can't build on mortgaged monopolies.\n");
86 		else if (!good)
87 			printf("You can't build any more.\n");
88 		else
89 			printf("But you don't have any monopolies!!\n");
90 		return;
91 	}
92 	if (num_mon == 1)
93 		buy_h(monops[0]);
94 	else {
95 		names[num_mon++] = "done";
96 		names[num_mon--] = 0;
97 		if ((p=getinp("Which property do you wish to buy houses for? ", names)) == num_mon)
98 			return;
99 		buy_h(monops[p]);
100 		goto over;
101 	}
102 }
103 
104 static void
105 buy_h(mnp)
106      MON	*mnp;
107 {
108 	int	i;
109 	MON	*mp;
110 	int	price;
111 	shrt	input[3],temp[3];
112 	int	tot, tot2;
113 	PROP	*pp;
114 	int	nhous, nhot;
115 	bool chot;
116 
117 	mp = mnp;
118 	price = mp->h_cost * 50;
119 	nhous = avail_houses();
120 	nhot = avail_hotels();
121 	chot = can_only_buy_hotel(mnp);
122 	if (nhous == 0 && !chot) {
123 		printf("Building shortage:  no houses available.");
124 		return;
125 	}
126 	if (nhot == 0 && chot) {
127 		printf("Building shortage:  no hotels available.");
128 		return;
129 	}
130 blew_it:
131 	list_cur(mp);
132 	printf("Houses will cost $%d\n", price);
133 	printf("How many houses do you wish to buy for\n");
134 	for (i = 0; i < mp->num_in; i++) {
135 		pp = mp->sq[i]->desc;
136 over:
137 		if (pp->houses == 5) {
138 			printf("%s (H):\n", mp->sq[i]->name);
139 			input[i] = 0;
140 			temp[i] = 5;
141 			continue;
142 		}
143 		(void)snprintf(cur_prop, sizeof(cur_prop), "%s (%d): ",
144 			mp->sq[i]->name, pp->houses);
145 		input[i] = get_int(cur_prop);
146 		temp[i] = input[i] + pp->houses;
147 		if (temp[i] > 5 || temp[i] < 0) {
148 			printf("That's too many.  The most you can buy is %d\n",
149 			    5 - pp->houses);
150 				goto over;
151 			}
152 	}
153 	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
154 	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
155 err:		printf("That makes the spread too wide.  Try again\n");
156 		goto blew_it;
157 	}
158 	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
159 		goto err;
160 	for (tot = tot2 = i = 0; i < mp->num_in; i++) {
161 		if (temp[i] == 5 && input[i])
162 			tot2++;
163 		else
164 			tot += input[i];
165 	}
166 	if (tot > nhous) {
167 		printf(
168 "You have asked for %d house%s but only %d are available.  Try again\n",
169 		    tot, tot == 1 ? "":"s", nhous);
170 		goto blew_it;
171 	} else if (tot2 > nhot) {
172 		printf(
173 "You have asked for %d hotel%s but only %d are available.  Try again\n",
174 		    tot2, tot2 == 1 ? "":"s", nhot);
175 		goto blew_it;
176 	}
177 	if (!chot && tot2) {
178 		printf(
179 "You must have 4 houses on all your property before building hotels\n");
180 		goto blew_it;
181 	}
182 	if (tot || tot2) {
183 		printf("You asked for %d %s%s for $%d\n", tot ? tot : tot2,
184 		    tot ? "house" : "hotel", (tot == 1 || tot2 == 1) ? "" : "s",
185 		    (tot ? tot : tot2) * price);
186 		if (getyn("Is that ok? ") == 0) {
187 			cur_p->money -= (tot ? tot : tot2) * price;
188 			for (tot = i = 0; i < mp->num_in; i++)
189 				mp->sq[i]->desc->houses = temp[i];
190 		}
191 	}
192 }
193 
194 /*
195  *	This routine sells houses.
196  */
197 void
198 sell_houses()
199 {
200 	int	num_mon;
201 	MON	*mp;
202 	OWN	*op;
203 	bool	good;
204 	int	p;
205 
206 over:
207 	num_mon = 0;
208 	good = TRUE;
209 	for (op = cur_p->own_list; op; op = op->next)
210 		if (op->sqr->type == PRPTY && op->sqr->desc->monop) {
211 			mp = op->sqr->desc->mon_desc;
212 			names[num_mon] = (monops[num_mon]=mp)->name;
213 			num_mon++;
214 			good = 0;
215 			do
216 				if (!good && op->sqr->desc->houses != 0)
217 					good++;
218 			while (op->next && op->sqr->desc->mon_desc == mp
219 			    && (op=op->next));
220 			if (!good)
221 				--num_mon;
222 		}
223 	if (num_mon == 0) {
224 		printf("You don't have any houses to sell!!\n");
225 		return;
226 	}
227 	if (num_mon == 1)
228 		sell_h(monops[0]);
229 	else {
230 		names[num_mon++] = "done";
231 		names[num_mon--] = 0;
232 		if ((p=getinp("Which property do you wish to sell houses from? ", names)) == num_mon)
233 			return;
234 		sell_h(monops[p]);
235 		notify();
236 		goto over;
237 	}
238 }
239 
240 static void
241 sell_h(mnp)
242 	MON	*mnp;
243 {
244 	int	i;
245 	MON	*mp;
246 	int	price;
247 	shrt	input[3],temp[3];
248 	int	tot;
249 	PROP	*pp;
250 
251 	mp = mnp;
252 	price = mp->h_cost * 25;
253 blew_it:
254 	printf("Houses will get you $%d apiece\n", price);
255 	list_cur(mp);
256 	printf("How many houses do you wish to sell from\n");
257 	for (i = 0; i < mp->num_in; i++) {
258 		pp = mp->sq[i]->desc;
259 over:
260 		if (pp->houses == 0) {
261 			printf("%s (0):\n", mp->sq[i]->name);
262 			input[i] = temp[i] = 0;
263 			continue;
264 		}
265 		if (pp->houses < 5)
266 			(void)snprintf(cur_prop, sizeof(cur_prop), "%s (%d): ",
267 				mp->sq[i]->name,pp->houses);
268 		else
269 			(void)snprintf(cur_prop, sizeof(cur_prop), "%s (H): ",
270 			    mp->sq[i]->name);
271 		input[i] = get_int(cur_prop);
272 		temp[i] = pp->houses - input[i];
273 		if (temp[i] < 0) {
274 			printf("That's too many.  The most you can sell is %d\n", pp->houses);
275 				goto over;
276 			}
277 	}
278 	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
279 	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
280 err:		printf("That makes the spread too wide.  Try again\n");
281 		goto blew_it;
282 	}
283 	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
284 		goto err;
285 	for (tot = i = 0; i < mp->num_in; i++)
286 		tot += input[i];
287 	if (tot) {
288 		printf("You asked to sell %d house%s for $%d\n", tot,
289 		    tot == 1 ? "" : "s", tot * price);
290 		if (getyn("Is that ok? ") == 0) {
291 			cur_p->money += tot * price;
292 			for (tot = i = 0; i < mp->num_in; i++)
293 				mp->sq[i]->desc->houses = temp[i];
294 		}
295 	}
296 }
297 
298 static void
299 list_cur(mp)
300 	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()
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()
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(mp)
351 	MON	*mp;
352 {
353 	int i;
354 
355 	for (i = 0; i < mp->num_in; i++) {
356 		if (mp->sq[i]->desc->houses < 4)
357 			return(FALSE);
358 	}
359 	return(TRUE);
360 }
361