Home | History | Annotate | Line # | Download | only in monop
houses.c revision 1.5
      1  1.5    simonb /*	$NetBSD: houses.c,v 1.5 1999/08/21 10:40:03 simonb Exp $	*/
      2  1.3       cgd 
      3  1.1       cgd /*
      4  1.3       cgd  * Copyright (c) 1980, 1993
      5  1.3       cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1       cgd  *
      7  1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1       cgd  * modification, are permitted provided that the following conditions
      9  1.1       cgd  * are met:
     10  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1       cgd  *    must display the following acknowledgement:
     17  1.1       cgd  *	This product includes software developed by the University of
     18  1.1       cgd  *	California, Berkeley and its contributors.
     19  1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1       cgd  *    may be used to endorse or promote products derived from this software
     21  1.1       cgd  *    without specific prior written permission.
     22  1.1       cgd  *
     23  1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1       cgd  * SUCH DAMAGE.
     34  1.1       cgd  */
     35  1.1       cgd 
     36  1.4  christos #include <sys/cdefs.h>
     37  1.1       cgd #ifndef lint
     38  1.3       cgd #if 0
     39  1.3       cgd static char sccsid[] = "@(#)houses.c	8.1 (Berkeley) 5/31/93";
     40  1.3       cgd #else
     41  1.5    simonb __RCSID("$NetBSD: houses.c,v 1.5 1999/08/21 10:40:03 simonb Exp $");
     42  1.3       cgd #endif
     43  1.1       cgd #endif /* not lint */
     44  1.1       cgd 
     45  1.4  christos #include "monop.ext"
     46  1.1       cgd 
     47  1.1       cgd static char	*names[N_MON+2],
     48  1.1       cgd 		cur_prop[80];
     49  1.1       cgd 
     50  1.1       cgd static MON	*monops[N_MON];
     51  1.1       cgd 
     52  1.4  christos static void buy_h __P((MON *));
     53  1.4  christos static void sell_h __P((MON *));
     54  1.4  christos static void list_cur __P((MON *));
     55  1.1       cgd /*
     56  1.1       cgd  *	These routines deal with buying and selling houses
     57  1.1       cgd  */
     58  1.4  christos void
     59  1.4  christos buy_houses()
     60  1.4  christos {
     61  1.4  christos 	int num_mon;
     62  1.5    simonb 	MON *mp;
     63  1.5    simonb 	OWN *op;
     64  1.5    simonb 	bool good,got_morg;
     65  1.5    simonb 	int i,p;
     66  1.1       cgd 
     67  1.1       cgd over:
     68  1.1       cgd 	num_mon = 0;
     69  1.1       cgd 	good = TRUE;
     70  1.1       cgd 	got_morg = FALSE;
     71  1.1       cgd 	for (op = cur_p->own_list; op && op->sqr->type != PRPTY; op = op->next)
     72  1.1       cgd 		continue;
     73  1.1       cgd 	while (op)
     74  1.1       cgd 		if (op->sqr->desc->monop) {
     75  1.1       cgd 			mp = op->sqr->desc->mon_desc;
     76  1.1       cgd 			names[num_mon] = (monops[num_mon]=mp)->name;
     77  1.1       cgd 			num_mon++;
     78  1.1       cgd 			got_morg = good = FALSE;
     79  1.1       cgd 			for (i = 0; i < mp->num_in; i++) {
     80  1.1       cgd 				if (op->sqr->desc->morg)
     81  1.1       cgd 					got_morg++;
     82  1.1       cgd 				if (op->sqr->desc->houses != 5)
     83  1.1       cgd 					good++;
     84  1.1       cgd 				op = op->next;
     85  1.1       cgd 			}
     86  1.1       cgd 			if (!good || got_morg)
     87  1.1       cgd 				--num_mon;
     88  1.1       cgd 		}
     89  1.1       cgd 		else
     90  1.1       cgd 			op = op->next;
     91  1.1       cgd 	if (num_mon == 0) {
     92  1.1       cgd 		if (got_morg)
     93  1.1       cgd 			printf("You can't build on mortgaged monopolies.\n");
     94  1.1       cgd 		else if (!good)
     95  1.1       cgd 			printf("You can't build any more.\n");
     96  1.1       cgd 		else
     97  1.1       cgd 			printf("But you don't have any monopolies!!\n");
     98  1.1       cgd 		return;
     99  1.1       cgd 	}
    100  1.1       cgd 	if (num_mon == 1)
    101  1.1       cgd 		buy_h(monops[0]);
    102  1.1       cgd 	else {
    103  1.1       cgd 		names[num_mon++] = "done";
    104  1.1       cgd 		names[num_mon--] = 0;
    105  1.5    simonb 		if ((p = getinp(
    106  1.5    simonb 		    "Which property do you wish to buy houses for? ",
    107  1.5    simonb 		    names)) == num_mon)
    108  1.1       cgd 			return;
    109  1.1       cgd 		buy_h(monops[p]);
    110  1.1       cgd 		goto over;
    111  1.1       cgd 	}
    112  1.1       cgd }
    113  1.1       cgd 
    114  1.4  christos static void
    115  1.1       cgd buy_h(mnp)
    116  1.5    simonb 	MON *mnp;
    117  1.4  christos {
    118  1.5    simonb 	int i;
    119  1.5    simonb 	MON *mp;
    120  1.5    simonb 	int price;
    121  1.5    simonb 	short input[3],temp[3];
    122  1.5    simonb 	int tot;
    123  1.5    simonb 	PROP *pp;
    124  1.1       cgd 
    125  1.1       cgd 	mp = mnp;
    126  1.1       cgd 	price = mp->h_cost * 50;
    127  1.1       cgd blew_it:
    128  1.1       cgd 	list_cur(mp);
    129  1.1       cgd 	printf("Houses will cost $%d\n", price);
    130  1.1       cgd 	printf("How many houses do you wish to buy for\n");
    131  1.1       cgd 	for (i = 0; i < mp->num_in; i++) {
    132  1.1       cgd 		pp = mp->sq[i]->desc;
    133  1.1       cgd over:
    134  1.1       cgd 		if (pp->houses == 5) {
    135  1.1       cgd 			printf("%s (H):\n", mp->sq[i]->name);
    136  1.1       cgd 			input[i] = 0;
    137  1.1       cgd 			temp[i] = 5;
    138  1.1       cgd 			continue;
    139  1.1       cgd 		}
    140  1.1       cgd 		(void)sprintf(cur_prop, "%s (%d): ",
    141  1.1       cgd 			mp->sq[i]->name, pp->houses);
    142  1.1       cgd 		input[i] = get_int(cur_prop);
    143  1.1       cgd 		temp[i] = input[i] + pp->houses;
    144  1.1       cgd 		if (temp[i] > 5) {
    145  1.1       cgd 			printf("That's too many.  The most you can buy is %d\n",
    146  1.1       cgd 				5 - pp->houses);
    147  1.1       cgd 				goto over;
    148  1.1       cgd 			}
    149  1.1       cgd 	}
    150  1.1       cgd 	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
    151  1.1       cgd 	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
    152  1.1       cgd err:		printf("That makes the spread too wide.  Try again\n");
    153  1.1       cgd 		goto blew_it;
    154  1.1       cgd 	}
    155  1.1       cgd 	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
    156  1.1       cgd 		goto err;
    157  1.1       cgd 	for (tot = i = 0; i < mp->num_in; i++)
    158  1.1       cgd 		tot += input[i];
    159  1.1       cgd 	if (tot) {
    160  1.1       cgd 		printf("You asked for %d houses for $%d\n", tot, tot * price);
    161  1.4  christos 		if (getyn("Is that ok? ") == 0) {
    162  1.1       cgd 			cur_p->money -= tot * price;
    163  1.1       cgd 			for (tot = i = 0; i < mp->num_in; i++)
    164  1.1       cgd 				mp->sq[i]->desc->houses = temp[i];
    165  1.1       cgd 		}
    166  1.1       cgd 	}
    167  1.1       cgd }
    168  1.1       cgd 
    169  1.1       cgd /*
    170  1.1       cgd  *	This routine sells houses.
    171  1.1       cgd  */
    172  1.4  christos void
    173  1.4  christos sell_houses()
    174  1.4  christos {
    175  1.5    simonb 	int num_mon;
    176  1.5    simonb 	MON *mp;
    177  1.5    simonb 	OWN *op;
    178  1.5    simonb 	bool good;
    179  1.5    simonb 	int p;
    180  1.1       cgd 
    181  1.1       cgd over:
    182  1.1       cgd 	num_mon = 0;
    183  1.1       cgd 	good = TRUE;
    184  1.1       cgd 	for (op = cur_p->own_list; op; op = op->next)
    185  1.1       cgd 		if (op->sqr->type == PRPTY && op->sqr->desc->monop) {
    186  1.1       cgd 			mp = op->sqr->desc->mon_desc;
    187  1.1       cgd 			names[num_mon] = (monops[num_mon]=mp)->name;
    188  1.1       cgd 			num_mon++;
    189  1.1       cgd 			good = 0;
    190  1.1       cgd 			do
    191  1.1       cgd 				if (!good && op->sqr->desc->houses != 0)
    192  1.1       cgd 					good++;
    193  1.1       cgd 			while (op->next && op->sqr->desc->mon_desc == mp
    194  1.1       cgd 			    && (op=op->next));
    195  1.1       cgd 			if (!good)
    196  1.1       cgd 				--num_mon;
    197  1.1       cgd 		}
    198  1.1       cgd 	if (num_mon == 0) {
    199  1.1       cgd 		printf("You don't have any houses to sell!!\n");
    200  1.1       cgd 		return;
    201  1.1       cgd 	}
    202  1.1       cgd 	if (num_mon == 1)
    203  1.1       cgd 		sell_h(monops[0]);
    204  1.1       cgd 	else {
    205  1.1       cgd 		names[num_mon++] = "done";
    206  1.1       cgd 		names[num_mon--] = 0;
    207  1.5    simonb 		if ((p = getinp(
    208  1.5    simonb 		    "Which property do you wish to sell houses from? ",
    209  1.5    simonb 		    names)) == num_mon)
    210  1.1       cgd 			return;
    211  1.1       cgd 		sell_h(monops[p]);
    212  1.1       cgd 		notify();
    213  1.1       cgd 		goto over;
    214  1.1       cgd 	}
    215  1.1       cgd }
    216  1.1       cgd 
    217  1.4  christos static void
    218  1.1       cgd sell_h(mnp)
    219  1.5    simonb 	MON *mnp;
    220  1.4  christos {
    221  1.5    simonb 	int i;
    222  1.5    simonb 	MON *mp;
    223  1.5    simonb 	int price;
    224  1.5    simonb 	short input[3],temp[3];
    225  1.5    simonb 	int tot;
    226  1.5    simonb 	PROP *pp;
    227  1.1       cgd 
    228  1.1       cgd 	mp = mnp;
    229  1.1       cgd 	price = mp->h_cost * 25;
    230  1.1       cgd blew_it:
    231  1.1       cgd 	printf("Houses will get you $%d apiece\n", price);
    232  1.1       cgd 	list_cur(mp);
    233  1.1       cgd 	printf("How many houses do you wish to sell from\n");
    234  1.1       cgd 	for (i = 0; i < mp->num_in; i++) {
    235  1.1       cgd 		pp = mp->sq[i]->desc;
    236  1.1       cgd over:
    237  1.1       cgd 		if (pp->houses == 0) {
    238  1.1       cgd 			printf("%s (0):\n", mp->sq[i]->name);
    239  1.1       cgd 			input[i] = temp[i] = 0;
    240  1.1       cgd 			continue;
    241  1.1       cgd 		}
    242  1.1       cgd 		if (pp->houses < 5)
    243  1.1       cgd 			(void)sprintf(cur_prop,"%s (%d): ",
    244  1.1       cgd 				mp->sq[i]->name,pp->houses);
    245  1.1       cgd 		else
    246  1.1       cgd 			(void)sprintf(cur_prop,"%s (H): ",mp->sq[i]->name);
    247  1.1       cgd 		input[i] = get_int(cur_prop);
    248  1.1       cgd 		temp[i] = pp->houses - input[i];
    249  1.1       cgd 		if (temp[i] < 0) {
    250  1.5    simonb 			printf(
    251  1.5    simonb 			    "That's too many.  The most you can sell is %d\n",
    252  1.5    simonb 			    pp->houses);
    253  1.1       cgd 				goto over;
    254  1.1       cgd 			}
    255  1.1       cgd 	}
    256  1.1       cgd 	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
    257  1.1       cgd 	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
    258  1.1       cgd err:		printf("That makes the spread too wide.  Try again\n");
    259  1.1       cgd 		goto blew_it;
    260  1.1       cgd 	}
    261  1.1       cgd 	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
    262  1.1       cgd 		goto err;
    263  1.1       cgd 	for (tot = i = 0; i < mp->num_in; i++)
    264  1.1       cgd 		tot += input[i];
    265  1.1       cgd 	if (tot) {
    266  1.1       cgd 		printf("You asked to sell %d houses for $%d\n",tot,tot * price);
    267  1.4  christos 		if (getyn("Is that ok? ") == 0) {
    268  1.1       cgd 			cur_p->money += tot * price;
    269  1.1       cgd 			for (tot = i = 0; i < mp->num_in; i++)
    270  1.1       cgd 				mp->sq[i]->desc->houses = temp[i];
    271  1.1       cgd 		}
    272  1.1       cgd 	}
    273  1.1       cgd }
    274  1.1       cgd 
    275  1.4  christos static void
    276  1.1       cgd list_cur(mp)
    277  1.5    simonb 	MON *mp;
    278  1.4  christos {
    279  1.5    simonb 	int i;
    280  1.5    simonb 	SQUARE *sqp;
    281  1.1       cgd 
    282  1.1       cgd 	for (i = 0; i < mp->num_in; i++) {
    283  1.1       cgd 		sqp = mp->sq[i];
    284  1.1       cgd 		if (sqp->desc->houses == 5)
    285  1.1       cgd 			printf("%s (H) ", sqp->name);
    286  1.1       cgd 		else
    287  1.1       cgd 			printf("%s (%d) ", sqp->name, sqp->desc->houses);
    288  1.1       cgd 	}
    289  1.1       cgd 	putchar('\n');
    290  1.1       cgd }
    291