Home | History | Annotate | Line # | Download | only in monop
houses.c revision 1.4
      1 /*	$NetBSD: houses.c,v 1.4 1997/10/12 17:45:11 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[] = "@(#)houses.c	8.1 (Berkeley) 5/31/93";
     40 #else
     41 __RCSID("$NetBSD: houses.c,v 1.4 1997/10/12 17:45:11 christos Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include "monop.ext"
     46 
     47 static char	*names[N_MON+2],
     48 		cur_prop[80];
     49 
     50 static MON	*monops[N_MON];
     51 
     52 static void buy_h __P((MON *));
     53 static void sell_h __P((MON *));
     54 static void list_cur __P((MON *));
     55 /*
     56  *	These routines deal with buying and selling houses
     57  */
     58 void
     59 buy_houses()
     60 {
     61 
     62 	int num_mon;
     63 	MON	*mp;
     64 	OWN	*op;
     65 	bool	good,got_morg;
     66 	int	i,p;
     67 
     68 over:
     69 	num_mon = 0;
     70 	good = TRUE;
     71 	got_morg = FALSE;
     72 	for (op = cur_p->own_list; op && op->sqr->type != PRPTY; op = op->next)
     73 		continue;
     74 	while (op)
     75 		if (op->sqr->desc->monop) {
     76 			mp = op->sqr->desc->mon_desc;
     77 			names[num_mon] = (monops[num_mon]=mp)->name;
     78 			num_mon++;
     79 			got_morg = good = FALSE;
     80 			for (i = 0; i < mp->num_in; i++) {
     81 				if (op->sqr->desc->morg)
     82 					got_morg++;
     83 				if (op->sqr->desc->houses != 5)
     84 					good++;
     85 				op = op->next;
     86 			}
     87 			if (!good || got_morg)
     88 				--num_mon;
     89 		}
     90 		else
     91 			op = op->next;
     92 	if (num_mon == 0) {
     93 		if (got_morg)
     94 			printf("You can't build on mortgaged monopolies.\n");
     95 		else if (!good)
     96 			printf("You can't build any more.\n");
     97 		else
     98 			printf("But you don't have any monopolies!!\n");
     99 		return;
    100 	}
    101 	if (num_mon == 1)
    102 		buy_h(monops[0]);
    103 	else {
    104 		names[num_mon++] = "done";
    105 		names[num_mon--] = 0;
    106 		if ((p=getinp("Which property do you wish to buy houses for? ", names)) == num_mon)
    107 			return;
    108 		buy_h(monops[p]);
    109 		goto over;
    110 	}
    111 }
    112 
    113 static void
    114 buy_h(mnp)
    115 MON	*mnp;
    116 {
    117 
    118 	int	i;
    119 	MON	*mp;
    120 	int	price;
    121 	short	input[3],temp[3];
    122 	int	tot;
    123 	PROP	*pp;
    124 
    125 	mp = mnp;
    126 	price = mp->h_cost * 50;
    127 blew_it:
    128 	list_cur(mp);
    129 	printf("Houses will cost $%d\n", price);
    130 	printf("How many houses do you wish to buy for\n");
    131 	for (i = 0; i < mp->num_in; i++) {
    132 		pp = mp->sq[i]->desc;
    133 over:
    134 		if (pp->houses == 5) {
    135 			printf("%s (H):\n", mp->sq[i]->name);
    136 			input[i] = 0;
    137 			temp[i] = 5;
    138 			continue;
    139 		}
    140 		(void)sprintf(cur_prop, "%s (%d): ",
    141 			mp->sq[i]->name, pp->houses);
    142 		input[i] = get_int(cur_prop);
    143 		temp[i] = input[i] + pp->houses;
    144 		if (temp[i] > 5) {
    145 			printf("That's too many.  The most you can buy is %d\n",
    146 				5 - pp->houses);
    147 				goto over;
    148 			}
    149 	}
    150 	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
    151 	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
    152 err:		printf("That makes the spread too wide.  Try again\n");
    153 		goto blew_it;
    154 	}
    155 	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
    156 		goto err;
    157 	for (tot = i = 0; i < mp->num_in; i++)
    158 		tot += input[i];
    159 	if (tot) {
    160 		printf("You asked for %d houses for $%d\n", tot, tot * price);
    161 		if (getyn("Is that ok? ") == 0) {
    162 			cur_p->money -= tot * price;
    163 			for (tot = i = 0; i < mp->num_in; i++)
    164 				mp->sq[i]->desc->houses = temp[i];
    165 		}
    166 	}
    167 }
    168 
    169 /*
    170  *	This routine sells houses.
    171  */
    172 void
    173 sell_houses()
    174 {
    175 
    176 	int	num_mon;
    177 	MON	*mp;
    178 	OWN	*op;
    179 	bool	good;
    180 	int	p;
    181 
    182 over:
    183 	num_mon = 0;
    184 	good = TRUE;
    185 	for (op = cur_p->own_list; op; op = op->next)
    186 		if (op->sqr->type == PRPTY && op->sqr->desc->monop) {
    187 			mp = op->sqr->desc->mon_desc;
    188 			names[num_mon] = (monops[num_mon]=mp)->name;
    189 			num_mon++;
    190 			good = 0;
    191 			do
    192 				if (!good && op->sqr->desc->houses != 0)
    193 					good++;
    194 			while (op->next && op->sqr->desc->mon_desc == mp
    195 			    && (op=op->next));
    196 			if (!good)
    197 				--num_mon;
    198 		}
    199 	if (num_mon == 0) {
    200 		printf("You don't have any houses to sell!!\n");
    201 		return;
    202 	}
    203 	if (num_mon == 1)
    204 		sell_h(monops[0]);
    205 	else {
    206 		names[num_mon++] = "done";
    207 		names[num_mon--] = 0;
    208 		if ((p=getinp("Which property do you wish to sell houses from? ", names)) == num_mon)
    209 			return;
    210 		sell_h(monops[p]);
    211 		notify();
    212 		goto over;
    213 	}
    214 }
    215 
    216 static void
    217 sell_h(mnp)
    218 MON	*mnp;
    219 {
    220 
    221 	int	i;
    222 	MON	*mp;
    223 	int	price;
    224 	short	input[3],temp[3];
    225 	int	tot;
    226 	PROP	*pp;
    227 
    228 	mp = mnp;
    229 	price = mp->h_cost * 25;
    230 blew_it:
    231 	printf("Houses will get you $%d apiece\n", price);
    232 	list_cur(mp);
    233 	printf("How many houses do you wish to sell from\n");
    234 	for (i = 0; i < mp->num_in; i++) {
    235 		pp = mp->sq[i]->desc;
    236 over:
    237 		if (pp->houses == 0) {
    238 			printf("%s (0):\n", mp->sq[i]->name);
    239 			input[i] = temp[i] = 0;
    240 			continue;
    241 		}
    242 		if (pp->houses < 5)
    243 			(void)sprintf(cur_prop,"%s (%d): ",
    244 				mp->sq[i]->name,pp->houses);
    245 		else
    246 			(void)sprintf(cur_prop,"%s (H): ",mp->sq[i]->name);
    247 		input[i] = get_int(cur_prop);
    248 		temp[i] = pp->houses - input[i];
    249 		if (temp[i] < 0) {
    250 			printf("That's too many.  The most you can sell is %d\n", pp->houses);
    251 				goto over;
    252 			}
    253 	}
    254 	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
    255 	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
    256 err:		printf("That makes the spread too wide.  Try again\n");
    257 		goto blew_it;
    258 	}
    259 	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
    260 		goto err;
    261 	for (tot = i = 0; i < mp->num_in; i++)
    262 		tot += input[i];
    263 	if (tot) {
    264 		printf("You asked to sell %d houses for $%d\n",tot,tot * price);
    265 		if (getyn("Is that ok? ") == 0) {
    266 			cur_p->money += tot * price;
    267 			for (tot = i = 0; i < mp->num_in; i++)
    268 				mp->sq[i]->desc->houses = temp[i];
    269 		}
    270 	}
    271 }
    272 
    273 static void
    274 list_cur(mp)
    275 MON	*mp;
    276 {
    277 
    278 	int		i;
    279 	SQUARE	*sqp;
    280 
    281 	for (i = 0; i < mp->num_in; i++) {
    282 		sqp = mp->sq[i];
    283 		if (sqp->desc->houses == 5)
    284 			printf("%s (H) ", sqp->name);
    285 		else
    286 			printf("%s (%d) ", sqp->name, sqp->desc->houses);
    287 	}
    288 	putchar('\n');
    289 }
    290