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