xref: /original-bsd/games/monop/houses.c (revision 2ce9ec30)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)houses.c	5.3 (Berkeley) 01/02/88";
15 #endif /* not lint */
16 
17 # include	"monop.ext"
18 
19 static char	*names[N_MON+2],
20 		cur_prop[80];
21 
22 static MON	*monops[N_MON];
23 
24 /*
25  *	These routines deal with buying and selling houses
26  */
27 buy_houses() {
28 
29 	reg int num_mon;
30 	reg MON	*mp;
31 	reg OWN	*op;
32 	bool	good,got_morg;
33 	int	i,p;
34 
35 over:
36 	num_mon = 0;
37 	good = TRUE;
38 	got_morg = FALSE;
39 	for (op = cur_p->own_list; op && op->sqr->type != PRPTY; op = op->next)
40 		continue;
41 	while (op)
42 		if (op->sqr->desc->monop) {
43 			mp = op->sqr->desc->mon_desc;
44 			names[num_mon] = (monops[num_mon]=mp)->name;
45 			num_mon++;
46 			got_morg = good = FALSE;
47 			for (i = 0; i < mp->num_in; i++) {
48 				if (op->sqr->desc->morg)
49 					got_morg++;
50 				if (op->sqr->desc->houses != 5)
51 					good++;
52 				op = op->next;
53 			}
54 			if (!good || got_morg)
55 				--num_mon;
56 		}
57 		else
58 			op = op->next;
59 	if (num_mon == 0) {
60 		if (got_morg)
61 			printf("You can't build on mortgaged monopolies.\n");
62 		else if (!good)
63 			printf("You can't build any more.\n");
64 		else
65 			printf("But you don't have any monopolies!!\n");
66 		return;
67 	}
68 	if (num_mon == 1)
69 		buy_h(monops[0]);
70 	else {
71 		names[num_mon++] = "done";
72 		names[num_mon--] = 0;
73 		if ((p=getinp("Which property do you wish to buy houses for? ", names)) == num_mon)
74 			return;
75 		buy_h(monops[p]);
76 		goto over;
77 	}
78 }
79 
80 buy_h(mnp)
81 MON	*mnp; {
82 
83 	reg int	i;
84 	reg MON	*mp;
85 	reg int	price;
86 	shrt	input[3],temp[3];
87 	int	tot;
88 	PROP	*pp;
89 
90 	mp = mnp;
91 	price = mp->h_cost * 50;
92 blew_it:
93 	list_cur(mp);
94 	printf("Houses will cost $%d\n", price);
95 	printf("How many houses do you wish to buy for\n");
96 	for (i = 0; i < mp->num_in; i++) {
97 		pp = mp->sq[i]->desc;
98 over:
99 		if (pp->houses == 5) {
100 			printf("%s (H):\n", mp->sq[i]->name);
101 			input[i] = 0;
102 			temp[i] = 5;
103 			continue;
104 		}
105 		(void)sprintf(cur_prop, "%s (%d): ",
106 			mp->sq[i]->name, pp->houses);
107 		input[i] = get_int(cur_prop);
108 		temp[i] = input[i] + pp->houses;
109 		if (temp[i] > 5) {
110 			printf("That's too many.  The most you can buy is %d\n",
111 				5 - pp->houses);
112 				goto over;
113 			}
114 	}
115 	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
116 	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
117 err:		printf("That makes the spread too wide.  Try again\n");
118 		goto blew_it;
119 	}
120 	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
121 		goto err;
122 	for (tot = i = 0; i < mp->num_in; i++)
123 		tot += input[i];
124 	if (tot) {
125 		printf("You asked for %d houses for $%d\n", tot, tot * price);
126 		if (getyn("Is that ok? ", yn) == 0) {
127 			cur_p->money -= tot * price;
128 			for (tot = i = 0; i < mp->num_in; i++)
129 				mp->sq[i]->desc->houses = temp[i];
130 		}
131 	}
132 }
133 
134 /*
135  *	This routine sells houses.
136  */
137 sell_houses() {
138 
139 	reg int	num_mon;
140 	reg MON	*mp;
141 	reg OWN	*op;
142 	bool	good;
143 	int	p;
144 
145 over:
146 	num_mon = 0;
147 	good = TRUE;
148 	for (op = cur_p->own_list; op; op = op->next)
149 		if (op->sqr->type == PRPTY && op->sqr->desc->monop) {
150 			mp = op->sqr->desc->mon_desc;
151 			names[num_mon] = (monops[num_mon]=mp)->name;
152 			num_mon++;
153 			good = 0;
154 			do
155 				if (!good && op->sqr->desc->houses != 0)
156 					good++;
157 			while (op->next && op->sqr->desc->mon_desc == mp
158 			    && (op=op->next));
159 			if (!good)
160 				--num_mon;
161 		}
162 	if (num_mon == 0) {
163 		printf("You don't have any houses to sell!!\n");
164 		return;
165 	}
166 	if (num_mon == 1)
167 		sell_h(monops[0]);
168 	else {
169 		names[num_mon++] = "done";
170 		names[num_mon--] = 0;
171 		if ((p=getinp("Which property do you wish to sell houses from? ", names)) == num_mon)
172 			return;
173 		sell_h(monops[p]);
174 		notify();
175 		goto over;
176 	}
177 }
178 
179 sell_h(mnp)
180 MON	*mnp; {
181 
182 	reg int	i;
183 	reg MON	*mp;
184 	reg int	price;
185 	shrt	input[3],temp[3];
186 	int	tot;
187 	PROP	*pp;
188 
189 	mp = mnp;
190 	price = mp->h_cost * 25;
191 blew_it:
192 	printf("Houses will get you $%d apiece\n", price);
193 	list_cur(mp);
194 	printf("How many houses do you wish to sell from\n");
195 	for (i = 0; i < mp->num_in; i++) {
196 		pp = mp->sq[i]->desc;
197 over:
198 		if (pp->houses == 0) {
199 			printf("%s (0):\n", mp->sq[i]->name);
200 			input[i] = temp[i] = 0;
201 			continue;
202 		}
203 		if (pp->houses < 5)
204 			(void)sprintf(cur_prop,"%s (%d): ",
205 				mp->sq[i]->name,pp->houses);
206 		else
207 			(void)sprintf(cur_prop,"%s (H): ",mp->sq[i]->name);
208 		input[i] = get_int(cur_prop);
209 		temp[i] = pp->houses - input[i];
210 		if (temp[i] < 0) {
211 			printf("That's too many.  The most you can sell is %d\n", pp->houses);
212 				goto over;
213 			}
214 	}
215 	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
216 	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
217 err:		printf("That makes the spread too wide.  Try again\n");
218 		goto blew_it;
219 	}
220 	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
221 		goto err;
222 	for (tot = i = 0; i < mp->num_in; i++)
223 		tot += input[i];
224 	if (tot) {
225 		printf("You asked to sell %d houses for $%d\n",tot,tot * price);
226 		if (getyn("Is that ok? ", yn) == 0) {
227 			cur_p->money += tot * price;
228 			for (tot = i = 0; i < mp->num_in; i++)
229 				mp->sq[i]->desc->houses = temp[i];
230 		}
231 	}
232 }
233 
234 list_cur(mp)
235 reg MON	*mp; {
236 
237 	reg int		i;
238 	reg SQUARE	*sqp;
239 
240 	for (i = 0; i < mp->num_in; i++) {
241 		sqp = mp->sq[i];
242 		if (sqp->desc->houses == 5)
243 			printf("%s (H) ", sqp->name);
244 		else
245 			printf("%s (%d) ", sqp->name, sqp->desc->houses);
246 	}
247 	putchar('\n');
248 }
249