xref: /openbsd/games/monop/houses.c (revision df930be7)
1 /*	$NetBSD: houses.c,v 1.3 1995/03/23 08:34:40 cgd 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)houses.c	8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$NetBSD: houses.c,v 1.3 1995/03/23 08:34:40 cgd Exp $";
41 #endif
42 #endif /* not lint */
43 
44 # include	"monop.ext"
45 
46 static char	*names[N_MON+2],
47 		cur_prop[80];
48 
49 static MON	*monops[N_MON];
50 
51 /*
52  *	These routines deal with buying and selling houses
53  */
54 buy_houses() {
55 
56 	reg int num_mon;
57 	reg MON	*mp;
58 	reg 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++;
77 				if (op->sqr->desc->houses != 5)
78 					good++;
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 buy_h(mnp)
108 MON	*mnp; {
109 
110 	reg int	i;
111 	reg MON	*mp;
112 	reg int	price;
113 	shrt	input[3],temp[3];
114 	int	tot;
115 	PROP	*pp;
116 
117 	mp = mnp;
118 	price = mp->h_cost * 50;
119 blew_it:
120 	list_cur(mp);
121 	printf("Houses will cost $%d\n", price);
122 	printf("How many houses do you wish to buy for\n");
123 	for (i = 0; i < mp->num_in; i++) {
124 		pp = mp->sq[i]->desc;
125 over:
126 		if (pp->houses == 5) {
127 			printf("%s (H):\n", mp->sq[i]->name);
128 			input[i] = 0;
129 			temp[i] = 5;
130 			continue;
131 		}
132 		(void)sprintf(cur_prop, "%s (%d): ",
133 			mp->sq[i]->name, pp->houses);
134 		input[i] = get_int(cur_prop);
135 		temp[i] = input[i] + pp->houses;
136 		if (temp[i] > 5) {
137 			printf("That's too many.  The most you can buy is %d\n",
138 				5 - pp->houses);
139 				goto over;
140 			}
141 	}
142 	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
143 	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
144 err:		printf("That makes the spread too wide.  Try again\n");
145 		goto blew_it;
146 	}
147 	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
148 		goto err;
149 	for (tot = i = 0; i < mp->num_in; i++)
150 		tot += input[i];
151 	if (tot) {
152 		printf("You asked for %d houses for $%d\n", tot, tot * price);
153 		if (getyn("Is that ok? ", yn) == 0) {
154 			cur_p->money -= tot * price;
155 			for (tot = i = 0; i < mp->num_in; i++)
156 				mp->sq[i]->desc->houses = temp[i];
157 		}
158 	}
159 }
160 
161 /*
162  *	This routine sells houses.
163  */
164 sell_houses() {
165 
166 	reg int	num_mon;
167 	reg MON	*mp;
168 	reg OWN	*op;
169 	bool	good;
170 	int	p;
171 
172 over:
173 	num_mon = 0;
174 	good = TRUE;
175 	for (op = cur_p->own_list; op; op = op->next)
176 		if (op->sqr->type == PRPTY && op->sqr->desc->monop) {
177 			mp = op->sqr->desc->mon_desc;
178 			names[num_mon] = (monops[num_mon]=mp)->name;
179 			num_mon++;
180 			good = 0;
181 			do
182 				if (!good && op->sqr->desc->houses != 0)
183 					good++;
184 			while (op->next && op->sqr->desc->mon_desc == mp
185 			    && (op=op->next));
186 			if (!good)
187 				--num_mon;
188 		}
189 	if (num_mon == 0) {
190 		printf("You don't have any houses to sell!!\n");
191 		return;
192 	}
193 	if (num_mon == 1)
194 		sell_h(monops[0]);
195 	else {
196 		names[num_mon++] = "done";
197 		names[num_mon--] = 0;
198 		if ((p=getinp("Which property do you wish to sell houses from? ", names)) == num_mon)
199 			return;
200 		sell_h(monops[p]);
201 		notify();
202 		goto over;
203 	}
204 }
205 
206 sell_h(mnp)
207 MON	*mnp; {
208 
209 	reg int	i;
210 	reg MON	*mp;
211 	reg int	price;
212 	shrt	input[3],temp[3];
213 	int	tot;
214 	PROP	*pp;
215 
216 	mp = mnp;
217 	price = mp->h_cost * 25;
218 blew_it:
219 	printf("Houses will get you $%d apiece\n", price);
220 	list_cur(mp);
221 	printf("How many houses do you wish to sell from\n");
222 	for (i = 0; i < mp->num_in; i++) {
223 		pp = mp->sq[i]->desc;
224 over:
225 		if (pp->houses == 0) {
226 			printf("%s (0):\n", mp->sq[i]->name);
227 			input[i] = temp[i] = 0;
228 			continue;
229 		}
230 		if (pp->houses < 5)
231 			(void)sprintf(cur_prop,"%s (%d): ",
232 				mp->sq[i]->name,pp->houses);
233 		else
234 			(void)sprintf(cur_prop,"%s (H): ",mp->sq[i]->name);
235 		input[i] = get_int(cur_prop);
236 		temp[i] = pp->houses - input[i];
237 		if (temp[i] < 0) {
238 			printf("That's too many.  The most you can sell is %d\n", pp->houses);
239 				goto over;
240 			}
241 	}
242 	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
243 	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
244 err:		printf("That makes the spread too wide.  Try again\n");
245 		goto blew_it;
246 	}
247 	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
248 		goto err;
249 	for (tot = i = 0; i < mp->num_in; i++)
250 		tot += input[i];
251 	if (tot) {
252 		printf("You asked to sell %d houses for $%d\n",tot,tot * price);
253 		if (getyn("Is that ok? ", yn) == 0) {
254 			cur_p->money += tot * price;
255 			for (tot = i = 0; i < mp->num_in; i++)
256 				mp->sq[i]->desc->houses = temp[i];
257 		}
258 	}
259 }
260 
261 list_cur(mp)
262 reg MON	*mp; {
263 
264 	reg int		i;
265 	reg SQUARE	*sqp;
266 
267 	for (i = 0; i < mp->num_in; i++) {
268 		sqp = mp->sq[i];
269 		if (sqp->desc->houses == 5)
270 			printf("%s (H) ", sqp->name);
271 		else
272 			printf("%s (%d) ", sqp->name, sqp->desc->houses);
273 	}
274 	putchar('\n');
275 }
276