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