Home | History | Annotate | Line # | Download | only in monop
prop.c revision 1.16
      1 /*	$NetBSD: prop.c,v 1.16 2008/02/23 22:17:02 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.16 2008/02/23 22:17:02 dholland Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <stdlib.h>
     42 #include "monop.ext"
     43 
     44 static int value(SQUARE *);
     45 
     46 /*
     47  *	This routine deals with buying property, setting all the
     48  * appropriate flags.
     49  */
     50 void
     51 buy(playernum, sqrp)
     52 	int playernum;
     53 	SQUARE *sqrp;
     54 {
     55 	trading = FALSE;
     56 	sqrp->owner = playernum;
     57 	add_list(playernum, &(play[playernum].own_list), cur_p->loc);
     58 }
     59 
     60 /*
     61  *	This routine adds an item to the list.
     62  */
     63 void
     64 add_list(plr, head, op_sqr)
     65 	int plr;
     66 	OWN **head;
     67 	int op_sqr;
     68 {
     69 	int val;
     70 	OWN *tp, *last_tp;
     71 	OWN *op;
     72 
     73 	op = (OWN *)calloc(1, sizeof (OWN));
     74 	if (op == NULL)
     75 		errx(1, "out of memory");
     76 	op->sqr = &board[op_sqr];
     77 	val = value(op->sqr);
     78 	last_tp = NULL;
     79 	for (tp = *head; tp && value(tp->sqr) < val; tp = tp->next)
     80 		if (val == value(tp->sqr)) {
     81 			free(op);
     82 			return;
     83 		}
     84 		else
     85 			last_tp = tp;
     86 	op->next = tp;
     87 	if (last_tp != NULL)
     88 		last_tp->next = op;
     89 	else
     90 		*head = op;
     91 	if (!trading)
     92 		set_ownlist(plr);
     93 }
     94 
     95 /*
     96  *	This routine deletes property from the list.
     97  */
     98 void
     99 del_list(plr, head, op_sqr)
    100 	int plr;
    101 	OWN **head;
    102 	short op_sqr;
    103 {
    104 	OWN *op, *last_op;
    105 
    106 	switch (board[op_sqr].type) {
    107 	  case PRPTY:
    108 		board[op_sqr].desc->mon_desc->num_own--;
    109 		break;
    110 	  case RR:
    111 		play[plr].num_rr--;
    112 		break;
    113 	  case UTIL:
    114 		play[plr].num_util--;
    115 		break;
    116 	}
    117 	last_op = NULL;
    118 	for (op = *head; op; op = op->next)
    119 		if (op->sqr == &board[op_sqr])
    120 			break;
    121 		else
    122 			last_op = op;
    123 	if (op == NULL)
    124 		return;
    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 piece of property.
    163  */
    164 void
    165 bid()
    166 {
    167 	static bool in[MAX_PL];
    168 	int i, num_in, cur_max;
    169 	char buf[257];
    170 	int cur_bid;
    171 
    172 	printf("\nSo it goes up for auction.  Type your bid after your name\n");
    173 	for (i = 0; i < num_play; i++)
    174 		in[i] = TRUE;
    175 	i = -1;
    176 	cur_max = 0;
    177 	num_in = num_play;
    178 	while (num_in > 1 || (cur_max == 0 && num_in > 0)) {
    179 		i = (i + 1) % num_play;
    180 		if (in[i]) {
    181 			do {
    182 				(void)snprintf(buf, sizeof(buf), "%s: ",
    183 				    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 				} else if (cur_bid <= cur_max) {
    190 					printf("You must bid higher than %d "
    191 					    "to stay in\n", cur_max);
    192 					printf("(bid of 0 drops you out)\n");
    193 				} else if (cur_bid > play[i].money) {
    194 					printf("You can't bid more than your cash ($%d)\n",
    195 					    play[i].money);
    196 					cur_bid = -1;
    197 				}
    198 			} while (cur_bid != 0 && cur_bid <= cur_max);
    199 			cur_max = (cur_bid ? cur_bid : cur_max);
    200 		}
    201 	}
    202 	if (cur_max != 0) {
    203 		while (!in[i])
    204 			i = (i + 1) % num_play;
    205 		printf("It goes to %s (%d) for $%d\n",play[i].name,i+1,cur_max);
    206 		buy(i, &board[cur_p->loc]);
    207 		play[i].money -= cur_max;
    208 	}
    209 	else
    210 		printf("Nobody seems to want it, so we'll leave it for "
    211 		    "later\n");
    212 }
    213 
    214 /*
    215  *	This routine calculates the value of the property
    216  * of given player.
    217  */
    218 int
    219 prop_worth(plp)
    220 	PLAY *plp;
    221 {
    222 	OWN *op;
    223 	int worth;
    224 
    225 	worth = 0;
    226 	for (op = plp->own_list; op; op = op->next) {
    227 		if (op->sqr->type == PRPTY && op->sqr->desc->monop)
    228 			worth += op->sqr->desc->mon_desc->h_cost * 50 *
    229 			    op->sqr->desc->houses;
    230 		worth += op->sqr->cost;
    231 	}
    232 	return worth;
    233 }
    234