Home | History | Annotate | Line # | Download | only in monop
houses.c revision 1.13
      1  1.13  dholland /*	$NetBSD: houses.c,v 1.13 2008/02/24 01:57:34 dholland 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.7       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.4  christos #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.3       cgd #if 0
     35   1.3       cgd static char sccsid[] = "@(#)houses.c	8.1 (Berkeley) 5/31/93";
     36   1.3       cgd #else
     37  1.13  dholland __RCSID("$NetBSD: houses.c,v 1.13 2008/02/24 01:57:34 dholland Exp $");
     38   1.3       cgd #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41  1.13  dholland #include "monop.h"
     42   1.1       cgd 
     43   1.6       jsm static const char	*names[N_MON+2];
     44   1.6       jsm static char	cur_prop[80];
     45   1.1       cgd 
     46   1.1       cgd static MON	*monops[N_MON];
     47   1.1       cgd 
     48   1.8       jsm static void buy_h(MON *);
     49   1.8       jsm static void sell_h(MON *);
     50   1.8       jsm static void list_cur(MON *);
     51  1.12  dholland static int avail_houses(void);
     52  1.12  dholland static int avail_hotels(void);
     53  1.12  dholland static bool can_only_buy_hotel(MON *);
     54  1.12  dholland 
     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.11  dholland 	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.11  dholland 					got_morg = TRUE;
     82   1.1       cgd 				if (op->sqr->desc->houses != 5)
     83  1.11  dholland 					good = TRUE;
     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.12  dholland 	int tot, tot2;
    123   1.5    simonb 	PROP *pp;
    124  1.12  dholland 	int nhous, nhot;
    125  1.12  dholland 	bool chot;
    126   1.1       cgd 
    127   1.1       cgd 	mp = mnp;
    128   1.1       cgd 	price = mp->h_cost * 50;
    129  1.12  dholland 	nhous = avail_houses();
    130  1.12  dholland 	nhot = avail_hotels();
    131  1.12  dholland 	chot = can_only_buy_hotel(mnp);
    132  1.12  dholland 	if (nhous == 0 && !chot) {
    133  1.12  dholland 		printf("Building shortage:  no houses available.");
    134  1.12  dholland 		return;
    135  1.12  dholland 	}
    136  1.12  dholland 	if (nhot == 0 && chot) {
    137  1.12  dholland 		printf("Building shortage:  no hotels available.");
    138  1.12  dholland 		return;
    139  1.12  dholland 	}
    140   1.1       cgd blew_it:
    141   1.1       cgd 	list_cur(mp);
    142   1.1       cgd 	printf("Houses will cost $%d\n", price);
    143   1.1       cgd 	printf("How many houses do you wish to buy for\n");
    144   1.1       cgd 	for (i = 0; i < mp->num_in; i++) {
    145   1.1       cgd 		pp = mp->sq[i]->desc;
    146   1.1       cgd over:
    147   1.1       cgd 		if (pp->houses == 5) {
    148   1.1       cgd 			printf("%s (H):\n", mp->sq[i]->name);
    149   1.1       cgd 			input[i] = 0;
    150   1.1       cgd 			temp[i] = 5;
    151   1.1       cgd 			continue;
    152   1.1       cgd 		}
    153  1.10  dholland 		(void)snprintf(cur_prop, sizeof(cur_prop), "%s (%d): ",
    154   1.1       cgd 			mp->sq[i]->name, pp->houses);
    155   1.1       cgd 		input[i] = get_int(cur_prop);
    156   1.1       cgd 		temp[i] = input[i] + pp->houses;
    157  1.12  dholland 		if (temp[i] > 5 || temp[i] < 0) {
    158   1.1       cgd 			printf("That's too many.  The most you can buy is %d\n",
    159  1.12  dholland 			    5 - pp->houses);
    160   1.1       cgd 				goto over;
    161   1.1       cgd 			}
    162   1.1       cgd 	}
    163   1.1       cgd 	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
    164   1.1       cgd 	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
    165   1.1       cgd err:		printf("That makes the spread too wide.  Try again\n");
    166   1.1       cgd 		goto blew_it;
    167   1.1       cgd 	}
    168   1.1       cgd 	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
    169   1.1       cgd 		goto err;
    170  1.12  dholland 	for (tot = tot2 = i = 0; i < mp->num_in; i++) {
    171  1.12  dholland 		if (temp[i] == 5)
    172  1.12  dholland 			tot2++;
    173  1.12  dholland 		else
    174  1.12  dholland 			tot += input[i];
    175  1.12  dholland 	}
    176  1.12  dholland 	if (tot > nhous) {
    177  1.12  dholland 		printf(
    178  1.12  dholland "You have asked for %d house%s but only %d are available.  Try again\n",
    179  1.12  dholland 		    tot, tot == 1 ? "":"s", nhous);
    180  1.12  dholland 		goto blew_it;
    181  1.12  dholland 	} else if (tot2 > nhot) {
    182  1.12  dholland 		printf(
    183  1.12  dholland "You have asked for %d hotel%s but only %d are available.  Try again\n",
    184  1.12  dholland 		    tot2, tot2 == 1 ? "":"s", nhot);
    185  1.12  dholland 		goto blew_it;
    186  1.12  dholland 	}
    187  1.12  dholland 
    188   1.1       cgd 	if (tot) {
    189  1.12  dholland 		printf("You asked for %d house%s and %d hotel%s for $%d\n", tot,
    190  1.12  dholland 		    tot == 1 ? "" : "s", tot2, tot2 == 1 ? "" : "s", tot * price);
    191   1.4  christos 		if (getyn("Is that ok? ") == 0) {
    192   1.1       cgd 			cur_p->money -= tot * price;
    193   1.1       cgd 			for (tot = i = 0; i < mp->num_in; i++)
    194   1.1       cgd 				mp->sq[i]->desc->houses = temp[i];
    195   1.1       cgd 		}
    196   1.1       cgd 	}
    197   1.1       cgd }
    198   1.1       cgd 
    199   1.1       cgd /*
    200   1.1       cgd  *	This routine sells houses.
    201   1.1       cgd  */
    202   1.4  christos void
    203   1.4  christos sell_houses()
    204   1.4  christos {
    205   1.5    simonb 	int num_mon;
    206   1.5    simonb 	MON *mp;
    207   1.5    simonb 	OWN *op;
    208   1.5    simonb 	bool good;
    209   1.5    simonb 	int p;
    210   1.1       cgd 
    211   1.1       cgd over:
    212   1.1       cgd 	num_mon = 0;
    213   1.1       cgd 	good = TRUE;
    214   1.9  christos 	for (op = cur_p->own_list; op;)
    215   1.1       cgd 		if (op->sqr->type == PRPTY && op->sqr->desc->monop) {
    216   1.1       cgd 			mp = op->sqr->desc->mon_desc;
    217   1.1       cgd 			names[num_mon] = (monops[num_mon]=mp)->name;
    218   1.1       cgd 			num_mon++;
    219   1.1       cgd 			good = 0;
    220   1.1       cgd 			do
    221   1.1       cgd 				if (!good && op->sqr->desc->houses != 0)
    222  1.11  dholland 					good = TRUE;
    223   1.1       cgd 			while (op->next && op->sqr->desc->mon_desc == mp
    224   1.9  christos 			    && (op = op->next));
    225   1.1       cgd 			if (!good)
    226   1.1       cgd 				--num_mon;
    227   1.9  christos 		} else
    228   1.9  christos 			op = op->next;
    229   1.1       cgd 	if (num_mon == 0) {
    230   1.1       cgd 		printf("You don't have any houses to sell!!\n");
    231   1.1       cgd 		return;
    232   1.1       cgd 	}
    233   1.1       cgd 	if (num_mon == 1)
    234   1.1       cgd 		sell_h(monops[0]);
    235   1.1       cgd 	else {
    236   1.1       cgd 		names[num_mon++] = "done";
    237   1.1       cgd 		names[num_mon--] = 0;
    238   1.5    simonb 		if ((p = getinp(
    239   1.5    simonb 		    "Which property do you wish to sell houses from? ",
    240   1.5    simonb 		    names)) == num_mon)
    241   1.1       cgd 			return;
    242   1.1       cgd 		sell_h(monops[p]);
    243   1.1       cgd 		notify();
    244   1.1       cgd 		goto over;
    245   1.1       cgd 	}
    246   1.1       cgd }
    247   1.1       cgd 
    248   1.4  christos static void
    249   1.1       cgd sell_h(mnp)
    250   1.5    simonb 	MON *mnp;
    251   1.4  christos {
    252   1.5    simonb 	int i;
    253   1.5    simonb 	MON *mp;
    254   1.5    simonb 	int price;
    255   1.5    simonb 	short input[3],temp[3];
    256   1.5    simonb 	int tot;
    257   1.5    simonb 	PROP *pp;
    258   1.1       cgd 
    259   1.1       cgd 	mp = mnp;
    260   1.1       cgd 	price = mp->h_cost * 25;
    261   1.1       cgd blew_it:
    262   1.1       cgd 	printf("Houses will get you $%d apiece\n", price);
    263   1.1       cgd 	list_cur(mp);
    264   1.1       cgd 	printf("How many houses do you wish to sell from\n");
    265   1.1       cgd 	for (i = 0; i < mp->num_in; i++) {
    266   1.1       cgd 		pp = mp->sq[i]->desc;
    267   1.1       cgd over:
    268   1.1       cgd 		if (pp->houses == 0) {
    269   1.1       cgd 			printf("%s (0):\n", mp->sq[i]->name);
    270   1.1       cgd 			input[i] = temp[i] = 0;
    271   1.1       cgd 			continue;
    272   1.1       cgd 		}
    273   1.1       cgd 		if (pp->houses < 5)
    274  1.10  dholland 			(void)snprintf(cur_prop, sizeof(cur_prop), "%s (%d): ",
    275   1.1       cgd 				mp->sq[i]->name,pp->houses);
    276   1.1       cgd 		else
    277  1.10  dholland 			(void)snprintf(cur_prop, sizeof(cur_prop), "%s (H): ",
    278  1.10  dholland 				mp->sq[i]->name);
    279   1.1       cgd 		input[i] = get_int(cur_prop);
    280   1.1       cgd 		temp[i] = pp->houses - input[i];
    281   1.1       cgd 		if (temp[i] < 0) {
    282   1.5    simonb 			printf(
    283   1.5    simonb 			    "That's too many.  The most you can sell is %d\n",
    284   1.5    simonb 			    pp->houses);
    285   1.1       cgd 				goto over;
    286   1.1       cgd 			}
    287   1.1       cgd 	}
    288   1.1       cgd 	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
    289   1.1       cgd 	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
    290   1.1       cgd err:		printf("That makes the spread too wide.  Try again\n");
    291   1.1       cgd 		goto blew_it;
    292   1.1       cgd 	}
    293   1.1       cgd 	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
    294   1.1       cgd 		goto err;
    295   1.1       cgd 	for (tot = i = 0; i < mp->num_in; i++)
    296   1.1       cgd 		tot += input[i];
    297   1.1       cgd 	if (tot) {
    298  1.12  dholland 		printf("You asked to sell %d house%s for $%d\n", tot,
    299  1.12  dholland 		    tot == 1 ? "" : "s", tot * price);
    300   1.4  christos 		if (getyn("Is that ok? ") == 0) {
    301   1.1       cgd 			cur_p->money += tot * price;
    302   1.1       cgd 			for (tot = i = 0; i < mp->num_in; i++)
    303   1.1       cgd 				mp->sq[i]->desc->houses = temp[i];
    304   1.1       cgd 		}
    305   1.1       cgd 	}
    306   1.1       cgd }
    307   1.1       cgd 
    308   1.4  christos static void
    309   1.1       cgd list_cur(mp)
    310   1.5    simonb 	MON *mp;
    311   1.4  christos {
    312   1.5    simonb 	int i;
    313   1.5    simonb 	SQUARE *sqp;
    314   1.1       cgd 
    315   1.1       cgd 	for (i = 0; i < mp->num_in; i++) {
    316   1.1       cgd 		sqp = mp->sq[i];
    317   1.1       cgd 		if (sqp->desc->houses == 5)
    318   1.1       cgd 			printf("%s (H) ", sqp->name);
    319   1.1       cgd 		else
    320   1.1       cgd 			printf("%s (%d) ", sqp->name, sqp->desc->houses);
    321   1.1       cgd 	}
    322   1.1       cgd 	putchar('\n');
    323   1.1       cgd }
    324  1.12  dholland 
    325  1.12  dholland static int
    326  1.12  dholland avail_houses()
    327  1.12  dholland {
    328  1.12  dholland 	int i, c;
    329  1.12  dholland 	SQUARE *sqp;
    330  1.12  dholland 
    331  1.12  dholland 	c = 0;
    332  1.12  dholland 	for (i = 0; i < N_SQRS; i++) {
    333  1.12  dholland 		sqp = &board[i];
    334  1.12  dholland 		if (sqp->type == PRPTY && sqp->owner >= 0 && sqp->desc->monop) {
    335  1.12  dholland 			if (sqp->desc->houses < 5 && sqp->desc->houses > 0)
    336  1.12  dholland 				c += sqp->desc->houses;
    337  1.12  dholland 		}
    338  1.12  dholland 	}
    339  1.12  dholland 	return(N_HOUSE - c);
    340  1.12  dholland }
    341  1.12  dholland 
    342  1.12  dholland static int
    343  1.12  dholland avail_hotels()
    344  1.12  dholland {
    345  1.12  dholland 	int i, c;
    346  1.12  dholland 	SQUARE *sqp;
    347  1.12  dholland 
    348  1.12  dholland 	c = 0;
    349  1.12  dholland 	for (i = 0; i < N_SQRS; i++) {
    350  1.12  dholland 		sqp = &board[i];
    351  1.12  dholland 		if (sqp->type == PRPTY && sqp->owner >= 0 && sqp->desc->monop) {
    352  1.12  dholland 			if (sqp->desc->houses == 5)
    353  1.12  dholland 				c++;
    354  1.12  dholland 		}
    355  1.12  dholland 	}
    356  1.12  dholland 	return(N_HOTEL - c);
    357  1.12  dholland }
    358  1.12  dholland 
    359  1.12  dholland static bool
    360  1.12  dholland can_only_buy_hotel(mp)
    361  1.12  dholland 	MON	*mp;
    362  1.12  dholland {
    363  1.12  dholland 	int i;
    364  1.12  dholland 
    365  1.12  dholland 	for (i = 0; i < mp->num_in; i++) {
    366  1.12  dholland 		if (mp->sq[i]->desc->houses < 4)
    367  1.12  dholland 			return(FALSE);
    368  1.12  dholland 	}
    369  1.12  dholland 	return(TRUE);
    370  1.12  dholland }
    371