Home | History | Annotate | Line # | Download | only in monop
prop.c revision 1.5
      1 /*	$NetBSD: prop.c,v 1.5 1999/08/21 10:40:04 simonb 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)prop.c	8.1 (Berkeley) 5/31/93";
     40 #else
     41 __RCSID("$NetBSD: prop.c,v 1.5 1999/08/21 10:40:04 simonb Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <stdlib.h>
     46 #include "monop.ext"
     47 
     48 static int value __P((SQUARE *));
     49 
     50 /*
     51  *	This routine deals with buying property, setting all the
     52  * appropriate flags.
     53  */
     54 void
     55 buy(player, sqrp)
     56 	int player;
     57 	SQUARE *sqrp;
     58 {
     59 	trading = FALSE;
     60 	sqrp->owner = player;
     61 	add_list(player, &(play[player].own_list), cur_p->loc);
     62 }
     63 
     64 /*
     65  *	This routine adds an item to the list.
     66  */
     67 void
     68 add_list(plr, head, op_sqr)
     69 	int plr;
     70 	OWN **head;
     71 	int op_sqr;
     72 {
     73 	int val;
     74 	OWN *tp, *last_tp;
     75 	OWN *op;
     76 
     77 	op = (OWN *)calloc(1, sizeof (OWN));
     78 	op->sqr = &board[op_sqr];
     79 	val = value(op->sqr);
     80 	last_tp = NULL;
     81 	for (tp = *head; tp && value(tp->sqr) < val; tp = tp->next)
     82 		if (val == value(tp->sqr)) {
     83 			free(op);
     84 			return;
     85 		}
     86 		else
     87 			last_tp = tp;
     88 	op->next = tp;
     89 	if (last_tp != NULL)
     90 		last_tp->next = op;
     91 	else
     92 		*head = op;
     93 	if (!trading)
     94 		set_ownlist(plr);
     95 }
     96 
     97 /*
     98  *	This routine deletes property from the list.
     99  */
    100 void
    101 del_list(plr, head, op_sqr)
    102 	int plr;
    103 	OWN **head;
    104 	short op_sqr;
    105 {
    106 	OWN *op, *last_op;
    107 
    108 	switch (board[op_sqr].type) {
    109 	  case PRPTY:
    110 		board[op_sqr].desc->mon_desc->num_own--;
    111 		break;
    112 	  case RR:
    113 		play[plr].num_rr--;
    114 		break;
    115 	  case UTIL:
    116 		play[plr].num_util--;
    117 		break;
    118 	}
    119 	last_op = NULL;
    120 	for (op = *head; op; op = op->next)
    121 		if (op->sqr == &board[op_sqr])
    122 			break;
    123 		else
    124 			last_op = op;
    125 	if (last_op == NULL)
    126 		*head = op->next;
    127 	else {
    128 		last_op->next = op->next;
    129 		free(op);
    130 	}
    131 }
    132 
    133 /*
    134  *	This routine calculates the value for sorting of the
    135  * given square.
    136  */
    137 static int
    138 value(sqp)
    139 	SQUARE *sqp;
    140 {
    141 	int sqr;
    142 
    143 	sqr = sqnum(sqp);
    144 	switch (sqp->type) {
    145 	  case SAFE:
    146 		return 0;
    147 	  default:		/* Specials, etc */
    148 		return 1;
    149 	  case UTIL:
    150 		if (sqr == 12)
    151 			return 2;
    152 		else
    153 			return 3;
    154 	  case RR:
    155 		return 4 + sqr/10;
    156 	  case PRPTY:
    157 		return 8 + (sqp->desc) - prop;
    158 	}
    159 }
    160 
    161 /*
    162  *	This routine accepts bids for the current peice
    163  * of property.
    164  */
    165 void
    166 bid()
    167 {
    168 	static bool in[MAX_PL];
    169 	int i, num_in, cur_max;
    170 	char buf[80];
    171 	int cur_bid;
    172 
    173 	printf("\nSo it goes up for auction.  Type your bid after your name\n");
    174 	for (i = 0; i < num_play; i++)
    175 		in[i] = TRUE;
    176 	i = -1;
    177 	cur_max = 0;
    178 	num_in = num_play;
    179 	while (num_in > 1 || (cur_max == 0 && num_in > 0)) {
    180 		i = ++i % num_play;
    181 		if (in[i]) {
    182 			do {
    183 				(void)sprintf(buf, "%s: ", name_list[i]);
    184 				cur_bid = get_int(buf);
    185 				if (cur_bid == 0) {
    186 					in[i] = FALSE;
    187 					if (--num_in == 0)
    188 						break;
    189 				}
    190 				else if (cur_bid <= cur_max) {
    191 					printf("You must bid higher than %d "
    192 					    "to stay in\n", cur_max);
    193 					printf("(bid of 0 drops you out)\n");
    194 				}
    195 			} while (cur_bid != 0 && cur_bid <= cur_max);
    196 			cur_max = (cur_bid ? cur_bid : cur_max);
    197 		}
    198 	}
    199 	if (cur_max != 0) {
    200 		while (!in[i])
    201 			i = ++i % num_play;
    202 		printf("It goes to %s (%d) for $%d\n",play[i].name,i+1,cur_max);
    203 		buy(i, &board[cur_p->loc]);
    204 		play[i].money -= cur_max;
    205 	}
    206 	else
    207 		printf("Nobody seems to want it, so we'll leave it for "
    208 		    "later\n");
    209 }
    210 
    211 /*
    212  *	This routine calculates the value of the property
    213  * of given player.
    214  */
    215 int
    216 prop_worth(plp)
    217 	PLAY *plp;
    218 {
    219 	OWN *op;
    220 	int worth;
    221 
    222 	worth = 0;
    223 	for (op = plp->own_list; op; op = op->next) {
    224 		if (op->sqr->type == PRPTY && op->sqr->desc->monop)
    225 			worth += op->sqr->desc->mon_desc->h_cost * 50 *
    226 			    op->sqr->desc->houses;
    227 		worth += op->sqr->cost;
    228 	}
    229 	return worth;
    230 }
    231