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