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