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