xref: /netbsd/games/monop/trade.c (revision 6550d01e)
1 /*	$NetBSD: trade.c,v 1.15 2008/02/24 01:57:34 dholland 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)trade.c	8.1 (Berkeley) 5/31/93";
36 #else
37 __RCSID("$NetBSD: trade.c,v 1.15 2008/02/24 01:57:34 dholland Exp $");
38 #endif
39 #endif /* not lint */
40 
41 #include "monop.h"
42 
43 struct trd_st {			/* how much to give to other player	*/
44 	int	trader;			/* trader number		*/
45 	int	cash;			/* amount of cash 		*/
46 	int	gojf;			/* # get-out-of-jail-free cards	*/
47 	OWN	*prop_list;		/* property list		*/
48 };
49 
50 typedef	struct trd_st	TRADE;
51 
52 static const char	*plist[MAX_PRP+2];
53 
54 static int	used[MAX_PRP];
55 
56 static TRADE	trades[2];
57 
58 static void get_list(int, int );
59 static int set_list(OWN *);
60 static void summate(void);
61 static void do_trade(void);
62 static void move_em(TRADE *, TRADE *);
63 
64 void
65 trade()
66 {
67 	int tradee, i;
68 
69 	trading = TRUE;
70 	for (i = 0; i < 2; i++) {
71 		trades[i].cash = 0;
72 		trades[i].gojf = FALSE;
73 		trades[i].prop_list = NULL;
74 	}
75 over:
76 	if (num_play == 1) {
77 		printf("There ain't no-one around to trade WITH!!\n");
78 		return;
79 	}
80 	if (num_play > 2) {
81 		tradee = getinp("Which player do you wish to trade with? ",
82 		    name_list);
83 		if (tradee == num_play)
84 			return;
85 		if (tradee == player) {
86 			printf("You can't trade with yourself!\n");
87 			goto over;
88 		}
89 	}
90 	else
91 		tradee = 1 - player;
92 	get_list(0, player);
93 	get_list(1, tradee);
94 	if (getyn("Do you wish a summary? ") == 0)
95 		summate();
96 	if (getyn("Is the trade ok? ") == 0)
97 		do_trade();
98 }
99 
100 /*
101  *	This routine gets the list of things to be trader for the
102  * player, and puts in the structure given.
103  */
104 static void
105 get_list(struct_no, play_no)
106 	int struct_no, play_no;
107 {
108 	int sn, pn;
109 	PLAY *pp;
110 	int numin, propnum, num_prp;
111 	OWN *op;
112 	TRADE *tp;
113 
114 	for (numin = 0; numin < MAX_PRP; numin++)
115 		used[numin] = FALSE;
116 	sn = struct_no, pn = play_no;
117 	pp = &play[pn];
118 	tp = &trades[sn];
119 	tp->trader = pn;
120 	printf("player %s (%d):\n", pp->name, pn+1);
121 	if (pp->own_list) {
122 		numin = set_list(pp->own_list);
123 		for (num_prp = numin; num_prp; ) {
124 			propnum=getinp("Which property do you wish to trade? ",
125 			    plist);
126 			if (propnum == numin)
127 				break;
128 			else if (used[propnum])
129 				printf("You've already allocated that.\n");
130 			else {
131 				num_prp--;
132 				used[propnum] = TRUE;
133 				for (op = pp->own_list; propnum--; op = op->next)
134 					continue;
135 				add_list(pn, &(tp->prop_list), sqnum(op->sqr));
136 			}
137 		}
138 	}
139 	if (pp->money > 0) {
140 		printf("You have $%d.  ", pp->money);
141 		tp->cash = get_int("How much are you trading? ");
142 	}
143 	if (pp->num_gojf > 0) {
144 once_more:
145 		printf("You have %d get-out-of-jail-free cards. ",pp->num_gojf);
146 		tp->gojf = get_int("How many are you trading? ");
147 		if (tp->gojf > pp->num_gojf) {
148 			printf("You don't have that many.  Try again.\n");
149 			goto once_more;
150 		}
151 	}
152 }
153 
154 /*
155  *	This routine sets up the list of tradable property.
156  */
157 static int
158 set_list(the_list)
159 	OWN *the_list;
160 {
161 	int i;
162 	OWN *op;
163 
164 	i = 0;
165 	for (op = the_list; op; op = op->next)
166 		if (!used[i])
167 			plist[i++] = op->sqr->name;
168 	plist[i++] = "done";
169 	plist[i--] = 0;
170 	return i;
171 }
172 
173 /*
174  *	This routine summates the trade.
175  */
176 static void
177 summate()
178 {
179 	bool some;
180 	int i;
181 	TRADE *tp;
182 	OWN *op;
183 
184 	for (i = 0; i < 2; i++) {
185 		tp = &trades[i];
186 		some = FALSE;
187 		printf("Player %s (%d) gives:\n", play[tp->trader].name,
188 			tp->trader+1);
189 		if (tp->cash > 0)
190 			printf("\t$%d\n", tp->cash), some++;
191 		if (tp->gojf > 0)
192 			printf("\t%d get-out-of-jail-free card(s)\n", tp->gojf),
193 			some++;
194 		if (tp->prop_list) {
195 			for (op = tp->prop_list; op; op = op->next)
196 				putchar('\t'), printsq(sqnum(op->sqr), TRUE);
197 			some++;
198 		}
199 		if (!some)
200 			printf("\t-- Nothing --\n");
201 	}
202 }
203 
204 /*
205  *	This routine actually executes the trade.
206  */
207 static void
208 do_trade()
209 {
210 	move_em(&trades[0], &trades[1]);
211 	move_em(&trades[1], &trades[0]);
212 }
213 
214 /*
215  *	This routine does a switch from one player to another
216  */
217 static void
218 move_em(from, to)
219 	TRADE *from, *to;
220 {
221 	PLAY *pl_fr, *pl_to;
222 	OWN *op;
223 
224 	pl_fr = &play[from->trader];
225 	pl_to = &play[to->trader];
226 
227 	pl_fr->money -= from->cash;
228 	pl_to->money += from->cash;
229 	pl_fr->num_gojf -= from->gojf;
230 	pl_to->num_gojf += from->gojf;
231 	for (op = from->prop_list; op; op = op->next) {
232 		add_list(to->trader, &(pl_to->own_list), sqnum(op->sqr));
233 		op->sqr->owner = to->trader;
234 		del_list(from->trader, &(pl_fr->own_list), sqnum(op->sqr));
235 	}
236 	set_ownlist(to->trader);
237 }
238 
239 /*
240  *	This routine lets a player resign
241  */
242 void
243 resign()
244 {
245 	int i, new_own;
246 	OWN *op;
247 	SQUARE *sqp;
248 
249 	if (cur_p->money <= 0) {
250 		switch (board[cur_p->loc].type) {
251 		  case UTIL:
252 		  case RR:
253 		  case PRPTY:
254 			new_own = board[cur_p->loc].owner;
255 			/* If you ran out of money by buying current location */
256 			if (new_own == player)
257 				new_own = num_play;
258 			break;
259 		  default:		/* Chance, taxes, etc */
260 			new_own = num_play;
261 			break;
262 		}
263 		if (new_own == num_play)
264 			printf("You would resign to the bank\n");
265 		else
266 			printf("You would resign to %s\n", name_list[new_own]);
267 	}
268 	else if (num_play == 1) {
269 		new_own = num_play;
270 		printf("You would resign to the bank\n");
271 	}
272 	else {
273 		name_list[num_play] = "bank";
274 		do {
275 			new_own = getinp("Who do you wish to resign to? ",
276 			    name_list);
277 			if (new_own == player)
278 				printf("You can't resign to yourself!!\n");
279 		} while (new_own == player);
280 		name_list[num_play] = "done";
281 	}
282 	if (getyn("Do you really want to resign? ") != 0)
283 		return;
284 	if (num_play == 1) {
285 		printf("Then NOBODY wins (not even YOU!)\n");
286 		exit(0);
287 	}
288 	if (new_own < num_play) {	/* resign to player		*/
289 		printf("resigning to player\n");
290 		trades[0].trader = new_own;
291 		trades[0].cash = trades[0].gojf = 0;
292 		trades[0].prop_list = NULL;
293 		trades[1].trader = player;
294 		trades[1].cash = cur_p->money > 0 ? cur_p->money : 0;
295 		trades[1].gojf = cur_p->num_gojf;
296 		trades[1].prop_list = cur_p->own_list;
297 		do_trade();
298 	}
299 	else {				/* resign to bank		*/
300 		printf("resigning to bank\n");
301 		for (op = cur_p->own_list; op; op = op->next) {
302 			sqp = op->sqr;
303 			sqp->owner = -1;
304 			sqp->desc->morg = FALSE;
305 			if (sqp->type == PRPTY) {
306 				is_not_monop(sqp->desc->mon_desc);
307 				sqp->desc->houses = 0;
308 			}
309 		}
310 		if (cur_p->num_gojf)
311 			ret_card(cur_p);
312 	}
313 	free(play[player].name);
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--] = NULL;
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 - 1;
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