xref: /netbsd-src/games/monop/prop.c (revision c34afa686bf074a6dcc7f17157faae72111dde9a)
1*c34afa68Sdholland /*	$NetBSD: prop.c,v 1.20 2012/06/19 05:35:32 dholland Exp $	*/
242fb1b9dScgd 
361f28255Scgd /*
442fb1b9dScgd  * Copyright (c) 1980, 1993
542fb1b9dScgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32b118ad22Schristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
3442fb1b9dScgd #if 0
3542fb1b9dScgd static char sccsid[] = "@(#)prop.c	8.1 (Berkeley) 5/31/93";
3642fb1b9dScgd #else
37*c34afa68Sdholland __RCSID("$NetBSD: prop.c,v 1.20 2012/06/19 05:35:32 dholland Exp $");
3842fb1b9dScgd #endif
3961f28255Scgd #endif /* not lint */
4061f28255Scgd 
41b118ad22Schristos #include <stdlib.h>
42b51259daSdholland 
43b51259daSdholland #include "monop.h"
4461f28255Scgd 
45cb5fd834Sjsm static int value(SQUARE *);
4661f28255Scgd 
4761f28255Scgd /*
4861f28255Scgd  *	This routine deals with buying property, setting all the
4961f28255Scgd  * appropriate flags.
5061f28255Scgd  */
51b118ad22Schristos void
buy(int playernum,SQUARE * sqrp)52*c34afa68Sdholland buy(int playernum, SQUARE *sqrp)
53b118ad22Schristos {
5461f28255Scgd 	trading = FALSE;
554e5cdff5Sdholland 	sqrp->owner = playernum;
564e5cdff5Sdholland 	add_list(playernum, &(play[playernum].own_list), cur_p->loc);
5761f28255Scgd }
58e42b2048Ssimonb 
5961f28255Scgd /*
6061f28255Scgd  *	This routine adds an item to the list.
6161f28255Scgd  */
62b118ad22Schristos void
add_list(int plr,OWN ** head,int op_sqr)63*c34afa68Sdholland add_list(int plr, OWN **head, int op_sqr)
64b118ad22Schristos {
65b118ad22Schristos 	int val;
66b118ad22Schristos 	OWN *tp, *last_tp;
6761f28255Scgd 	OWN *op;
6861f28255Scgd 
69a537df81Sdholland 	op = calloc(1, sizeof (OWN));
70a9c7f9b0Sjsm 	if (op == NULL)
71a9c7f9b0Sjsm 		errx(1, "out of memory");
7261f28255Scgd 	op->sqr = &board[op_sqr];
7361f28255Scgd 	val = value(op->sqr);
7461f28255Scgd 	last_tp = NULL;
7561f28255Scgd 	for (tp = *head; tp && value(tp->sqr) < val; tp = tp->next)
7661f28255Scgd 		if (val == value(tp->sqr)) {
77b118ad22Schristos 			free(op);
7861f28255Scgd 			return;
7961f28255Scgd 		}
8061f28255Scgd 		else
8161f28255Scgd 			last_tp = tp;
8261f28255Scgd 	op->next = tp;
8361f28255Scgd 	if (last_tp != NULL)
8461f28255Scgd 		last_tp->next = op;
8561f28255Scgd 	else
8661f28255Scgd 		*head = op;
8761f28255Scgd 	if (!trading)
8861f28255Scgd 		set_ownlist(plr);
8961f28255Scgd }
90e42b2048Ssimonb 
9161f28255Scgd /*
9261f28255Scgd  *	This routine deletes property from the list.
9361f28255Scgd  */
94b118ad22Schristos void
del_list(int plr,OWN ** head,short op_sqr)95dc11f84dSchristos del_list(int plr, OWN **head, short op_sqr)
96b118ad22Schristos {
97b118ad22Schristos 	OWN *op, *last_op;
9861f28255Scgd 
9961f28255Scgd 	switch (board[op_sqr].type) {
10061f28255Scgd 	  case PRPTY:
10161f28255Scgd 		board[op_sqr].desc->mon_desc->num_own--;
10261f28255Scgd 		break;
10361f28255Scgd 	  case RR:
10461f28255Scgd 		play[plr].num_rr--;
10561f28255Scgd 		break;
10661f28255Scgd 	  case UTIL:
10761f28255Scgd 		play[plr].num_util--;
10861f28255Scgd 		break;
10961f28255Scgd 	}
11061f28255Scgd 	last_op = NULL;
11161f28255Scgd 	for (op = *head; op; op = op->next)
11261f28255Scgd 		if (op->sqr == &board[op_sqr])
11361f28255Scgd 			break;
11461f28255Scgd 		else
11561f28255Scgd 			last_op = op;
116ad05902bSchristos 	if (op == NULL)
117a1952513Schristos 		return;
11861f28255Scgd 	if (last_op == NULL)
11961f28255Scgd 		*head = op->next;
12061f28255Scgd 	else {
12161f28255Scgd 		last_op->next = op->next;
122b118ad22Schristos 		free(op);
12361f28255Scgd 	}
12461f28255Scgd }
125e42b2048Ssimonb 
12661f28255Scgd /*
12761f28255Scgd  *	This routine calculates the value for sorting of the
12861f28255Scgd  * given square.
12961f28255Scgd  */
130b118ad22Schristos static int
value(SQUARE * sqp)131*c34afa68Sdholland value(SQUARE *sqp)
132b118ad22Schristos {
133b118ad22Schristos 	int sqr;
13461f28255Scgd 
13561f28255Scgd 	sqr = sqnum(sqp);
13661f28255Scgd 	switch (sqp->type) {
13761f28255Scgd 	  case SAFE:
13861f28255Scgd 		return 0;
13961f28255Scgd 	  default:		/* Specials, etc */
14061f28255Scgd 		return 1;
14161f28255Scgd 	  case UTIL:
14261f28255Scgd 		if (sqr == 12)
14361f28255Scgd 			return 2;
14461f28255Scgd 		else
14561f28255Scgd 			return 3;
14661f28255Scgd 	  case RR:
14761f28255Scgd 		return 4 + sqr/10;
14861f28255Scgd 	  case PRPTY:
14961f28255Scgd 		return 8 + (sqp->desc) - prop;
15061f28255Scgd 	}
15161f28255Scgd }
152e42b2048Ssimonb 
15361f28255Scgd /*
15432e86dd4Sdholland  * This routine accepts bids for the current piece of property.
15561f28255Scgd  */
156b118ad22Schristos void
bid(void)157*c34afa68Sdholland bid(void)
158b118ad22Schristos {
15961f28255Scgd 	static bool in[MAX_PL];
160b118ad22Schristos 	int i, num_in, cur_max;
1614e54ceffSdholland 	char buf[257];
16261f28255Scgd 	int cur_bid;
16361f28255Scgd 
16461f28255Scgd 	printf("\nSo it goes up for auction.  Type your bid after your name\n");
16561f28255Scgd 	for (i = 0; i < num_play; i++)
16661f28255Scgd 		in[i] = TRUE;
16761f28255Scgd 	i = -1;
16861f28255Scgd 	cur_max = 0;
16961f28255Scgd 	num_in = num_play;
17061f28255Scgd 	while (num_in > 1 || (cur_max == 0 && num_in > 0)) {
1719cfe468cScgd 		i = (i + 1) % num_play;
17261f28255Scgd 		if (in[i]) {
17361f28255Scgd 			do {
174c4dc2f9eSdholland 				(void)snprintf(buf, sizeof(buf), "%s: ",
175c4dc2f9eSdholland 				    name_list[i]);
17661f28255Scgd 				cur_bid = get_int(buf);
17761f28255Scgd 				if (cur_bid == 0) {
17861f28255Scgd 					in[i] = FALSE;
17961f28255Scgd 					if (--num_in == 0)
18061f28255Scgd 						break;
18132e86dd4Sdholland 				} else if (cur_bid <= cur_max) {
182e42b2048Ssimonb 					printf("You must bid higher than %d "
183e42b2048Ssimonb 					    "to stay in\n", cur_max);
18461f28255Scgd 					printf("(bid of 0 drops you out)\n");
1854e54ceffSdholland 				} else if (cur_bid > play[i].money) {
1864e54ceffSdholland 					printf("You can't bid more than your cash ($%d)\n",
1874e54ceffSdholland 					    play[i].money);
1884e54ceffSdholland 					cur_bid = -1;
18961f28255Scgd 				}
19061f28255Scgd 			} while (cur_bid != 0 && cur_bid <= cur_max);
19161f28255Scgd 			cur_max = (cur_bid ? cur_bid : cur_max);
19261f28255Scgd 		}
19361f28255Scgd 	}
19461f28255Scgd 	if (cur_max != 0) {
19561f28255Scgd 		while (!in[i])
1969cfe468cScgd 			i = (i + 1) % num_play;
19761f28255Scgd 		printf("It goes to %s (%d) for $%d\n",play[i].name,i+1,cur_max);
19861f28255Scgd 		buy(i, &board[cur_p->loc]);
19961f28255Scgd 		play[i].money -= cur_max;
20061f28255Scgd 	}
20161f28255Scgd 	else
202e42b2048Ssimonb 		printf("Nobody seems to want it, so we'll leave it for "
203e42b2048Ssimonb 		    "later\n");
20461f28255Scgd }
205e42b2048Ssimonb 
20661f28255Scgd /*
20761f28255Scgd  *	This routine calculates the value of the property
20861f28255Scgd  * of given player.
20961f28255Scgd  */
210b118ad22Schristos int
prop_worth(PLAY * plp)211*c34afa68Sdholland prop_worth(PLAY *plp)
212b118ad22Schristos {
213b118ad22Schristos 	OWN *op;
214b118ad22Schristos 	int worth;
21561f28255Scgd 
21661f28255Scgd 	worth = 0;
21761f28255Scgd 	for (op = plp->own_list; op; op = op->next) {
21861f28255Scgd 		if (op->sqr->type == PRPTY && op->sqr->desc->monop)
21961f28255Scgd 			worth += op->sqr->desc->mon_desc->h_cost * 50 *
22061f28255Scgd 			    op->sqr->desc->houses;
22161f28255Scgd 		worth += op->sqr->cost;
22261f28255Scgd 	}
22361f28255Scgd 	return worth;
22461f28255Scgd }
225