Home | History | Annotate | Line # | Download | only in monop
prop.c revision 1.4
      1 /*	$NetBSD: prop.c,v 1.4 1997/10/12 17:45:23 christos 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.4 1997/10/12 17:45:23 christos 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 
     60 	trading = FALSE;
     61 	sqrp->owner = player;
     62 	add_list(player, &(play[player].own_list), cur_p->loc);
     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 
     74 	int	val;
     75 	OWN	*tp, *last_tp;
     76 	OWN	*op;
     77 
     78 	op = (OWN *)calloc(1, sizeof (OWN));
     79 	op->sqr = &board[op_sqr];
     80 	val = value(op->sqr);
     81 	last_tp = NULL;
     82 	for (tp = *head; tp && value(tp->sqr) < val; tp = tp->next)
     83 		if (val == value(tp->sqr)) {
     84 			free(op);
     85 			return;
     86 		}
     87 		else
     88 			last_tp = tp;
     89 	op->next = tp;
     90 	if (last_tp != NULL)
     91 		last_tp->next = op;
     92 	else
     93 		*head = op;
     94 	if (!trading)
     95 		set_ownlist(plr);
     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 
    107 	OWN	*op, *last_op;
    108 
    109 	switch (board[op_sqr].type) {
    110 	  case PRPTY:
    111 		board[op_sqr].desc->mon_desc->num_own--;
    112 		break;
    113 	  case RR:
    114 		play[plr].num_rr--;
    115 		break;
    116 	  case UTIL:
    117 		play[plr].num_util--;
    118 		break;
    119 	}
    120 	last_op = NULL;
    121 	for (op = *head; op; op = op->next)
    122 		if (op->sqr == &board[op_sqr])
    123 			break;
    124 		else
    125 			last_op = op;
    126 	if (last_op == NULL)
    127 		*head = op->next;
    128 	else {
    129 		last_op->next = op->next;
    130 		free(op);
    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 
    142 	int	sqr;
    143 
    144 	sqr = sqnum(sqp);
    145 	switch (sqp->type) {
    146 	  case SAFE:
    147 		return 0;
    148 	  default:		/* Specials, etc */
    149 		return 1;
    150 	  case UTIL:
    151 		if (sqr == 12)
    152 			return 2;
    153 		else
    154 			return 3;
    155 	  case RR:
    156 		return 4 + sqr/10;
    157 	  case PRPTY:
    158 		return 8 + (sqp->desc) - prop;
    159 	}
    160 }
    161 /*
    162  *	This routine accepts bids for the current peice
    163  * of property.
    164  */
    165 void
    166 bid()
    167 {
    168 
    169 	static bool	in[MAX_PL];
    170 	int		i, num_in, cur_max;
    171 	char		buf[80];
    172 	int		cur_bid;
    173 
    174 	printf("\nSo it goes up for auction.  Type your bid after your name\n");
    175 	for (i = 0; i < num_play; i++)
    176 		in[i] = TRUE;
    177 	i = -1;
    178 	cur_max = 0;
    179 	num_in = num_play;
    180 	while (num_in > 1 || (cur_max == 0 && num_in > 0)) {
    181 		i = ++i % num_play;
    182 		if (in[i]) {
    183 			do {
    184 				(void)sprintf(buf, "%s: ", name_list[i]);
    185 				cur_bid = get_int(buf);
    186 				if (cur_bid == 0) {
    187 					in[i] = FALSE;
    188 					if (--num_in == 0)
    189 						break;
    190 				}
    191 				else if (cur_bid <= cur_max) {
    192 					printf("You must bid higher than %d 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 later\n");
    208 }
    209 /*
    210  *	This routine calculates the value of the property
    211  * of given player.
    212  */
    213 int
    214 prop_worth(plp)
    215 PLAY	*plp;
    216 {
    217 
    218 	OWN	*op;
    219 	int	worth;
    220 
    221 	worth = 0;
    222 	for (op = plp->own_list; op; op = op->next) {
    223 		if (op->sqr->type == PRPTY && op->sqr->desc->monop)
    224 			worth += op->sqr->desc->mon_desc->h_cost * 50 *
    225 			    op->sqr->desc->houses;
    226 		worth += op->sqr->cost;
    227 	}
    228 	return worth;
    229 }
    230