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