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