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