xref: /minix/games/monop/prop.c (revision e39e890e)
1 /*	$NetBSD: prop.c,v 1.20 2012/06/19 05:35:32 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[] = "@(#)prop.c	8.1 (Berkeley) 5/31/93";
36 #else
37 __RCSID("$NetBSD: prop.c,v 1.20 2012/06/19 05:35:32 dholland Exp $");
38 #endif
39 #endif /* not lint */
40 
41 #include <stdlib.h>
42 
43 #include "monop.h"
44 
45 static int value(SQUARE *);
46 
47 /*
48  *	This routine deals with buying property, setting all the
49  * appropriate flags.
50  */
51 void
52 buy(int playernum, SQUARE *sqrp)
53 {
54 	trading = FALSE;
55 	sqrp->owner = playernum;
56 	add_list(playernum, &(play[playernum].own_list), cur_p->loc);
57 }
58 
59 /*
60  *	This routine adds an item to the list.
61  */
62 void
63 add_list(int plr, OWN **head, int op_sqr)
64 {
65 	int val;
66 	OWN *tp, *last_tp;
67 	OWN *op;
68 
69 	op = calloc(1, sizeof (OWN));
70 	if (op == NULL)
71 		errx(1, "out of memory");
72 	op->sqr = &board[op_sqr];
73 	val = value(op->sqr);
74 	last_tp = NULL;
75 	for (tp = *head; tp && value(tp->sqr) < val; tp = tp->next)
76 		if (val == value(tp->sqr)) {
77 			free(op);
78 			return;
79 		}
80 		else
81 			last_tp = tp;
82 	op->next = tp;
83 	if (last_tp != NULL)
84 		last_tp->next = op;
85 	else
86 		*head = op;
87 	if (!trading)
88 		set_ownlist(plr);
89 }
90 
91 /*
92  *	This routine deletes property from the list.
93  */
94 void
95 del_list(int plr, OWN **head, short op_sqr)
96 {
97 	OWN *op, *last_op;
98 
99 	switch (board[op_sqr].type) {
100 	  case PRPTY:
101 		board[op_sqr].desc->mon_desc->num_own--;
102 		break;
103 	  case RR:
104 		play[plr].num_rr--;
105 		break;
106 	  case UTIL:
107 		play[plr].num_util--;
108 		break;
109 	}
110 	last_op = NULL;
111 	for (op = *head; op; op = op->next)
112 		if (op->sqr == &board[op_sqr])
113 			break;
114 		else
115 			last_op = op;
116 	if (op == NULL)
117 		return;
118 	if (last_op == NULL)
119 		*head = op->next;
120 	else {
121 		last_op->next = op->next;
122 		free(op);
123 	}
124 }
125 
126 /*
127  *	This routine calculates the value for sorting of the
128  * given square.
129  */
130 static int
131 value(SQUARE *sqp)
132 {
133 	int sqr;
134 
135 	sqr = sqnum(sqp);
136 	switch (sqp->type) {
137 	  case SAFE:
138 		return 0;
139 	  default:		/* Specials, etc */
140 		return 1;
141 	  case UTIL:
142 		if (sqr == 12)
143 			return 2;
144 		else
145 			return 3;
146 	  case RR:
147 		return 4 + sqr/10;
148 	  case PRPTY:
149 		return 8 + (sqp->desc) - prop;
150 	}
151 }
152 
153 /*
154  * This routine accepts bids for the current piece of property.
155  */
156 void
157 bid(void)
158 {
159 	static bool in[MAX_PL];
160 	int i, num_in, cur_max;
161 	char buf[257];
162 	int cur_bid;
163 
164 	printf("\nSo it goes up for auction.  Type your bid after your name\n");
165 	for (i = 0; i < num_play; i++)
166 		in[i] = TRUE;
167 	i = -1;
168 	cur_max = 0;
169 	num_in = num_play;
170 	while (num_in > 1 || (cur_max == 0 && num_in > 0)) {
171 		i = (i + 1) % num_play;
172 		if (in[i]) {
173 			do {
174 				(void)snprintf(buf, sizeof(buf), "%s: ",
175 				    name_list[i]);
176 				cur_bid = get_int(buf);
177 				if (cur_bid == 0) {
178 					in[i] = FALSE;
179 					if (--num_in == 0)
180 						break;
181 				} else if (cur_bid <= cur_max) {
182 					printf("You must bid higher than %d "
183 					    "to stay in\n", cur_max);
184 					printf("(bid of 0 drops you out)\n");
185 				} else if (cur_bid > play[i].money) {
186 					printf("You can't bid more than your cash ($%d)\n",
187 					    play[i].money);
188 					cur_bid = -1;
189 				}
190 			} while (cur_bid != 0 && cur_bid <= cur_max);
191 			cur_max = (cur_bid ? cur_bid : cur_max);
192 		}
193 	}
194 	if (cur_max != 0) {
195 		while (!in[i])
196 			i = (i + 1) % num_play;
197 		printf("It goes to %s (%d) for $%d\n",play[i].name,i+1,cur_max);
198 		buy(i, &board[cur_p->loc]);
199 		play[i].money -= cur_max;
200 	}
201 	else
202 		printf("Nobody seems to want it, so we'll leave it for "
203 		    "later\n");
204 }
205 
206 /*
207  *	This routine calculates the value of the property
208  * of given player.
209  */
210 int
211 prop_worth(PLAY *plp)
212 {
213 	OWN *op;
214 	int worth;
215 
216 	worth = 0;
217 	for (op = plp->own_list; op; op = op->next) {
218 		if (op->sqr->type == PRPTY && op->sqr->desc->monop)
219 			worth += op->sqr->desc->mon_desc->h_cost * 50 *
220 			    op->sqr->desc->houses;
221 		worth += op->sqr->cost;
222 	}
223 	return worth;
224 }
225