xref: /netbsd/games/monop/trade.c (revision bf9ec67e)
1 /*	$NetBSD: trade.c,v 1.7 1999/09/30 18:01:32 jsm 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)trade.c	8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: trade.c,v 1.7 1999/09/30 18:01:32 jsm Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include "monop.ext"
46 
47 struct trd_st {			/* how much to give to other player	*/
48 	int	trader;			/* trader number		*/
49 	int	cash;			/* amount of cash 		*/
50 	int	gojf;			/* # get-out-of-jail-free cards	*/
51 	OWN	*prop_list;		/* property list		*/
52 };
53 
54 typedef	struct trd_st	TRADE;
55 
56 static const char	*plist[MAX_PRP+2];
57 
58 static int	used[MAX_PRP];
59 
60 static TRADE	trades[2];
61 
62 static void get_list __P((int, int ));
63 static int set_list __P((OWN *));
64 static void summate __P((void));
65 static void do_trade __P((void));
66 static void move_em __P((TRADE *, TRADE *));
67 
68 void
69 trade()
70 {
71 	int tradee, i;
72 
73 	trading = TRUE;
74 	for (i = 0; i < 2; i++) {
75 		trades[i].cash = 0;
76 		trades[i].gojf = FALSE;
77 		trades[i].prop_list = NULL;
78 	}
79 over:
80 	if (num_play == 1) {
81 		printf("There ain't no-one around to trade WITH!!\n");
82 		return;
83 	}
84 	if (num_play > 2) {
85 		tradee = getinp("Which player do you wish to trade with? ",
86 		    name_list);
87 		if (tradee == num_play)
88 			return;
89 		if (tradee == player) {
90 			printf("You can't trade with yourself!\n");
91 			goto over;
92 		}
93 	}
94 	else
95 		tradee = 1 - player;
96 	get_list(0, player);
97 	get_list(1, tradee);
98 	if (getyn("Do you wish a summary? ") == 0)
99 		summate();
100 	if (getyn("Is the trade ok? ") == 0)
101 		do_trade();
102 }
103 
104 /*
105  *	This routine gets the list of things to be trader for the
106  * player, and puts in the structure given.
107  */
108 static void
109 get_list(struct_no, play_no)
110 	int struct_no, play_no;
111 {
112 	int sn, pn;
113 	PLAY *pp;
114 	int numin, prop, num_prp;
115 	OWN *op;
116 	TRADE *tp;
117 
118 	for (numin = 0; numin < MAX_PRP; numin++)
119 		used[numin] = FALSE;
120 	sn = struct_no, pn = play_no;
121 	pp = &play[pn];
122 	tp = &trades[sn];
123 	tp->trader = pn;
124 	printf("player %s (%d):\n", pp->name, pn+1);
125 	if (pp->own_list) {
126 		numin = set_list(pp->own_list);
127 		for (num_prp = numin; num_prp; ) {
128 			prop = getinp("Which property do you wish to trade? ",
129 			    plist);
130 			if (prop == numin)
131 				break;
132 			else if (used[prop])
133 				printf("You've already allocated that.\n");
134 			else {
135 				num_prp--;
136 				used[prop] = TRUE;
137 				for (op = pp->own_list; prop--; op = op->next)
138 					continue;
139 				add_list(pn, &(tp->prop_list), sqnum(op->sqr));
140 			}
141 		}
142 	}
143 	if (pp->money > 0) {
144 		printf("You have $%d.  ", pp->money);
145 		tp->cash = get_int("How much are you trading? ");
146 	}
147 	if (pp->num_gojf > 0) {
148 once_more:
149 		printf("You have %d get-out-of-jail-free cards. ",pp->num_gojf);
150 		tp->gojf = get_int("How many are you trading? ");
151 		if (tp->gojf > pp->num_gojf) {
152 			printf("You don't have that many.  Try again.\n");
153 			goto once_more;
154 		}
155 	}
156 }
157 
158 /*
159  *	This routine sets up the list of tradable property.
160  */
161 static int
162 set_list(the_list)
163 	OWN *the_list;
164 {
165 	int i;
166 	OWN *op;
167 
168 	i = 0;
169 	for (op = the_list; op; op = op->next)
170 		if (!used[i])
171 			plist[i++] = op->sqr->name;
172 	plist[i++] = "done";
173 	plist[i--] = 0;
174 	return i;
175 }
176 
177 /*
178  *	This routine summates the trade.
179  */
180 static void
181 summate()
182 {
183 	bool some;
184 	int i;
185 	TRADE *tp;
186 	OWN *op;
187 
188 	for (i = 0; i < 2; i++) {
189 		tp = &trades[i];
190 		some = FALSE;
191 		printf("Player %s (%d) gives:\n", play[tp->trader].name,
192 			tp->trader+1);
193 		if (tp->cash > 0)
194 			printf("\t$%d\n", tp->cash), some++;
195 		if (tp->gojf > 0)
196 			printf("\t%d get-out-of-jail-free card(s)\n", tp->gojf),
197 			some++;
198 		if (tp->prop_list) {
199 			for (op = tp->prop_list; op; op = op->next)
200 				putchar('\t'), printsq(sqnum(op->sqr), TRUE);
201 			some++;
202 		}
203 		if (!some)
204 			printf("\t-- Nothing --\n");
205 	}
206 }
207 
208 /*
209  *	This routine actually executes the trade.
210  */
211 static void
212 do_trade()
213 {
214 	move_em(&trades[0], &trades[1]);
215 	move_em(&trades[1], &trades[0]);
216 }
217 
218 /*
219  *	This routine does a switch from one player to another
220  */
221 static void
222 move_em(from, to)
223 	TRADE *from, *to;
224 {
225 	PLAY *pl_fr, *pl_to;
226 	OWN *op;
227 
228 	pl_fr = &play[from->trader];
229 	pl_to = &play[to->trader];
230 
231 	pl_fr->money -= from->cash;
232 	pl_to->money += from->cash;
233 	pl_fr->num_gojf -= from->gojf;
234 	pl_to->num_gojf += from->gojf;
235 	for (op = from->prop_list; op; op = op->next) {
236 		add_list(to->trader, &(pl_to->own_list), sqnum(op->sqr));
237 		op->sqr->owner = to->trader;
238 		del_list(from->trader, &(pl_fr->own_list), sqnum(op->sqr));
239 	}
240 	set_ownlist(to->trader);
241 }
242 
243 /*
244  *	This routine lets a player resign
245  */
246 void
247 resign()
248 {
249 	int i, new_own;
250 	OWN *op;
251 	SQUARE *sqp;
252 
253 	if (cur_p->money <= 0) {
254 		switch (board[cur_p->loc].type) {
255 		  case UTIL:
256 		  case RR:
257 		  case PRPTY:
258 			new_own = board[cur_p->loc].owner;
259 			break;
260 		  default:		/* Chance, taxes, etc */
261 			new_own = num_play;
262 			break;
263 		}
264 		if (new_own == num_play)
265 			printf("You would resign to the bank\n");
266 		else
267 			printf("You would resign to %s\n", name_list[new_own]);
268 	}
269 	else if (num_play == 1) {
270 		new_own = num_play;
271 		printf("You would resign to the bank\n");
272 	}
273 	else {
274 		name_list[num_play] = "bank";
275 		do {
276 			new_own = getinp("Who do you wish to resign to? ",
277 			    name_list);
278 			if (new_own == player)
279 				printf("You can't resign to yourself!!\n");
280 		} while (new_own == player);
281 		name_list[num_play] = "done";
282 	}
283 	if (getyn("Do you really want to resign? ") != 0)
284 		return;
285 	if (num_play == 1) {
286 		printf("Then NOBODY wins (not even YOU!)\n");
287 		exit(0);
288 	}
289 	if (new_own < num_play) {	/* resign to player		*/
290 		printf("resigning to player\n");
291 		trades[0].trader = new_own;
292 		trades[0].cash = trades[0].gojf = 0;
293 		trades[0].prop_list = NULL;
294 		trades[1].trader = player;
295 		trades[1].cash = cur_p->money > 0 ? cur_p->money : 0;
296 		trades[1].gojf = cur_p->num_gojf;
297 		trades[1].prop_list = cur_p->own_list;
298 		do_trade();
299 	}
300 	else {				/* resign to bank		*/
301 		printf("resigning to bank\n");
302 		for (op = cur_p->own_list; op; op = op->next) {
303 			sqp = op->sqr;
304 			sqp->owner = -1;
305 			sqp->desc->morg = FALSE;
306 			if (sqp->type == PRPTY) {
307 				is_not_monop(sqp->desc->mon_desc);
308 				sqp->desc->houses = 0;
309 			}
310 		}
311 		if (cur_p->num_gojf)
312 			ret_card(cur_p);
313 	}
314 	for (i = player; i < num_play; i++) {
315 		name_list[i] = name_list[i+1];
316 		if (i + 1 < num_play)
317 			play[i] =  play[i+1];
318 	}
319 	name_list[num_play--] = 0;
320 	for (i = 0; i < N_SQRS; i++)
321 		if (board[i].owner > player)
322 			--board[i].owner;
323 	player = --player < 0 ? num_play - 1 : player;
324 	next_play();
325 	if (num_play < 2) {
326 		printf("\nThen %s WINS!!!!!\n", play[0].name);
327 		printhold(0);
328 		printf("That's a grand worth of $%d.\n",
329 			play[0].money+prop_worth(&play[0]));
330 		exit(0);
331 	}
332 }
333