houses.c revision 1.10
11590Srgrimes/*	$OpenBSD: houses.c,v 1.10 2016/01/08 18:20:33 mestre Exp $	*/
2140392Srwatson/*	$NetBSD: houses.c,v 1.3 1995/03/23 08:34:40 cgd Exp $	*/
397377Sdes
41590Srgrimes/*
597377Sdes * Copyright (c) 1980, 1993
697377Sdes *	The Regents of the University of California.  All rights reserved.
797377Sdes *
897377Sdes * Redistribution and use in source and binary forms, with or without
997377Sdes * modification, are permitted provided that the following conditions
101590Srgrimes * are met:
111590Srgrimes * 1. Redistributions of source code must retain the above copyright
121590Srgrimes *    notice, this list of conditions and the following disclaimer.
131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141590Srgrimes *    notice, this list of conditions and the following disclaimer in the
151590Srgrimes *    documentation and/or other materials provided with the distribution.
161590Srgrimes * 3. Neither the name of the University nor the names of its contributors
171590Srgrimes *    may be used to endorse or promote products derived from this software
18140392Srwatson *    without specific prior written permission.
19140392Srwatson *
20140392Srwatson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21140392Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22140392Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23140392Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24140392Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25140392Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26140392Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27140392Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28140392Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29140392Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30140392Srwatson * SUCH DAMAGE.
31140392Srwatson */
32140392Srwatson
33140392Srwatson#include <stdio.h>
34140392Srwatson#include <stdlib.h>
35140392Srwatson
36140392Srwatson#include "monop.ext"
37140392Srwatson
38140392Srwatsonstatic char	*names[N_MON+2],
39140392Srwatson		cur_prop[80];
40140392Srwatson
41140392Srwatsonstatic MON	*monops[N_MON];
42140392Srwatson
431590Srgrimesstatic void	buy_h(MON *);
441590Srgrimesstatic void	sell_h(MON *);
451590Srgrimesstatic void	list_cur(MON *);
461590Srgrimesstatic int	avail_houses();
471590Srgrimesstatic int	avail_hotels();
481590Srgrimesstatic bool	can_only_buy_hotel(MON *);
491590Srgrimes
501590Srgrimes/*
511590Srgrimes *	These routines deal with buying and selling houses
521590Srgrimes */
531590Srgrimesvoid
541590Srgrimesbuy_houses(void)
551590Srgrimes{
561590Srgrimes	int	num_mon;
571590Srgrimes	MON	*mp;
581590Srgrimes	OWN	*op;
591590Srgrimes	bool	good, got_morg;
601590Srgrimes	int	i,p;
611590Srgrimes
621590Srgrimesover:
631590Srgrimes	num_mon = 0;
641590Srgrimes	good = TRUE;
6514440Smarkm	got_morg = FALSE;
661590Srgrimes	for (op = cur_p->own_list; op && op->sqr->type != PRPTY; op = op->next)
671590Srgrimes		continue;
681590Srgrimes	while (op)
691590Srgrimes		if (op->sqr->desc->monop) {
70127848Scharnier			mp = op->sqr->desc->mon_desc;
711590Srgrimes			names[num_mon] = (monops[num_mon]=mp)->name;
721590Srgrimes			num_mon++;
73127848Scharnier			got_morg = good = FALSE;
7428099Scharnier			for (i = 0; i < mp->num_in; i++) {
751590Srgrimes				if (op->sqr->desc->morg)
76127848Scharnier					got_morg = TRUE;
77127848Scharnier				if (op->sqr->desc->houses != 5)
78127848Scharnier					good = TRUE;
791590Srgrimes				op = op->next;
801590Srgrimes			}
811590Srgrimes			if (!good || got_morg)
8277220Smarkm				--num_mon;
831590Srgrimes		}
84161815Scsjp		else
85161815Scsjp			op = op->next;
86161815Scsjp	if (num_mon == 0) {
87161815Scsjp		if (got_morg)
88161815Scsjp			printf("You can't build on mortgaged monopolies.\n");
891590Srgrimes		else if (!good)
901590Srgrimes			printf("You can't build any more.\n");
911590Srgrimes		else
9277220Smarkm			printf("But you don't have any monopolies!!\n");
931590Srgrimes		return;
941590Srgrimes	}
9577220Smarkm	if (num_mon == 1)
961590Srgrimes		buy_h(monops[0]);
971590Srgrimes	else {
981590Srgrimes		names[num_mon++] = "done";
991590Srgrimes		names[num_mon--] = 0;
1001590Srgrimes		if ((p=getinp("Which property do you wish to buy houses for? ", names)) == num_mon)
101161815Scsjp			return;
10221646Sdavidn		buy_h(monops[p]);
10374874Smarkm		goto over;
10491745Sdes	}
10574874Smarkm}
106113262Sdes
107113262Sdesstatic void
108113262Sdesbuy_h(MON *mnp)
109113262Sdes{
110113262Sdes	int	i;
111113262Sdes	MON	*mp;
112113262Sdes	int	price;
113113262Sdes	shrt	input[3],temp[3];
114113262Sdes	int	tot, tot2;
115113262Sdes	PROP	*pp;
116113262Sdes	int	nhous, nhot;
117113262Sdes	bool chot;
118113262Sdes
119113262Sdes	mp = mnp;
120113262Sdes	price = mp->h_cost * 50;
121113262Sdes	nhous = avail_houses();
122113262Sdes	nhot = avail_hotels();
123113262Sdes	chot = can_only_buy_hotel(mnp);
12477220Smarkm	if (nhous == 0 && !chot) {
12574874Smarkm		printf("Building shortage:  no houses available.");
12674874Smarkm		return;
127113262Sdes	}
128113262Sdes	if (nhot == 0 && chot) {
129113262Sdes		printf("Building shortage:  no hotels available.");
130113262Sdes		return;
131113262Sdes	}
132113262Sdesblew_it:
133113262Sdes	list_cur(mp);
134113262Sdes	printf("Houses will cost $%d\n", price);
135130409Smarkm	printf("How many houses do you wish to buy for\n");
136113262Sdes	for (i = 0; i < mp->num_in; i++) {
13777220Smarkm		pp = mp->sq[i]->desc;
1383702Spstover:
13977220Smarkm		if (pp->houses == 5) {
1401590Srgrimes			printf("%s (H):\n", mp->sq[i]->name);
14177220Smarkm			input[i] = 0;
14277220Smarkm			temp[i] = 5;
14377220Smarkm			continue;
14477220Smarkm		}
145130409Smarkm		(void)snprintf(cur_prop, sizeof(cur_prop), "%s (%d): ",
146130409Smarkm			mp->sq[i]->name, pp->houses);
147130409Smarkm		input[i] = get_int(cur_prop);
14877220Smarkm		temp[i] = input[i] + pp->houses;
14977220Smarkm		if (temp[i] > 5 || temp[i] < 0) {
15077220Smarkm			printf("That's too many.  The most you can buy is %d\n",
15177220Smarkm			    5 - pp->houses);
1521590Srgrimes				goto over;
15377220Smarkm			}
1541590Srgrimes	}
155130409Smarkm	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
15677220Smarkm	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
15791745Sdeserr:		printf("That makes the spread too wide.  Try again\n");
15877220Smarkm		goto blew_it;
15977220Smarkm	}
16083373Smarkm	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
16183373Smarkm		goto err;
16283373Smarkm	for (tot = tot2 = i = 0; i < mp->num_in; i++) {
16397377Sdes		if (temp[i] == 5 && input[i])
16477220Smarkm			tot2++;
165153985Sbrian		else
166130409Smarkm			tot += input[i];
167113262Sdes	}
168130409Smarkm	if (tot > nhous) {
169130409Smarkm		printf(
17083373Smarkm"You have asked for %d house%s but only %d are available.  Try again\n",
171112695Sdavidxu		    tot, tot == 1 ? "":"s", nhous);
172112695Sdavidxu		goto blew_it;
173161815Scsjp	} else if (tot2 > nhot) {
174161815Scsjp		printf(
175161815Scsjp"You have asked for %d hotel%s but only %d are available.  Try again\n",
176161815Scsjp		    tot2, tot2 == 1 ? "":"s", nhot);
17798837Sdillon		goto blew_it;
17877220Smarkm	}
17977220Smarkm	if (!chot && tot2) {
18010586Sjoerg		printf(
18177220Smarkm"You must have 4 houses on all your property before building hotels\n");
182105758Srwatson		goto blew_it;
18377220Smarkm	}
184105758Srwatson	if (tot || tot2) {
18577220Smarkm		printf("You asked for %d %s%s for $%d\n", tot ? tot : tot2,
1861590Srgrimes		    tot ? "house" : "hotel", (tot == 1 || tot2 == 1) ? "" : "s",
1871590Srgrimes		    (tot ? tot : tot2) * price);
1881590Srgrimes		if (getyn("Is that ok? ") == 0) {
1891590Srgrimes			cur_p->money -= (tot ? tot : tot2) * price;
1901590Srgrimes			for (tot = i = 0; i < mp->num_in; i++)
1911590Srgrimes				mp->sq[i]->desc->houses = temp[i];
1921590Srgrimes		}
1931590Srgrimes	}
1941590Srgrimes}
1951590Srgrimes
1961590Srgrimes/*
1971590Srgrimes *	This routine sells houses.
198105758Srwatson */
199105758Srwatsonvoid
200105758Srwatsonsell_houses(void)
20130793Sguido{
20230793Sguido	int	num_mon;
20330793Sguido	MON	*mp;
2041590Srgrimes	OWN	*op;
2051590Srgrimes	bool	good;
20628099Scharnier	int	p;
207130409Smarkm
20828099Scharnierover:
20939538Sroberto	num_mon = 0;
21039538Sroberto	good = TRUE;
21110586Sjoerg	for (op = cur_p->own_list; op; op = op->next)
21210586Sjoerg		if (op->sqr->type == PRPTY && op->sqr->desc->monop) {
21328612Sjoerg			mp = op->sqr->desc->mon_desc;
21428612Sjoerg			names[num_mon] = (monops[num_mon]=mp)->name;
215130409Smarkm			num_mon++;
21628612Sjoerg			good = 0;
217140392Srwatson			do
218140392Srwatson				if (!good && op->sqr->desc->houses != 0)
219140392Srwatson					good++;
220140392Srwatson			while (op->next && op->sqr->desc->mon_desc == mp
221140392Srwatson			    && (op=op->next));
222140392Srwatson			if (!good)
223140392Srwatson				--num_mon;
224161815Scsjp		}
225161815Scsjp	if (num_mon == 0) {
226161815Scsjp		printf("You don't have any houses to sell!!\n");
227161815Scsjp		return;
228161815Scsjp	}
229161815Scsjp	if (num_mon == 1)
230161815Scsjp		sell_h(monops[0]);
231161815Scsjp	else {
232161815Scsjp		names[num_mon++] = "done";
233161815Scsjp		names[num_mon--] = 0;
234161815Scsjp		if ((p=getinp("Which property do you wish to sell houses from? ", names)) == num_mon)
235161815Scsjp			return;
236161815Scsjp		sell_h(monops[p]);
237161815Scsjp		notify();
238161815Scsjp		goto over;
239130409Smarkm	}
24077220Smarkm}
24177220Smarkm
24277220Smarkmstatic void
24310586Sjoergsell_h(MON *mnp)
24410586Sjoerg{
24577220Smarkm	int	i;
24683373Smarkm	MON	*mp;
24710586Sjoerg	int	price;
2481590Srgrimes	shrt	input[3],temp[3];
2491590Srgrimes	int	tot;
2501590Srgrimes	PROP	*pp;
2511590Srgrimes
2521590Srgrimes	mp = mnp;
2531590Srgrimes	price = mp->h_cost * 25;
25477220Smarkmblew_it:
25577220Smarkm	printf("Houses will get you $%d apiece\n", price);
25674874Smarkm	list_cur(mp);
2571590Srgrimes	printf("How many houses do you wish to sell from\n");
25877220Smarkm	for (i = 0; i < mp->num_in; i++) {
2591590Srgrimes		pp = mp->sq[i]->desc;
2601590Srgrimesover:
26177220Smarkm		if (pp->houses == 0) {
26277220Smarkm			printf("%s (0):\n", mp->sq[i]->name);
2631590Srgrimes			input[i] = temp[i] = 0;
264161815Scsjp			continue;
265161815Scsjp		}
266161815Scsjp		if (pp->houses < 5)
267161815Scsjp			(void)snprintf(cur_prop, sizeof(cur_prop), "%s (%d): ",
268161815Scsjp				mp->sq[i]->name,pp->houses);
269161815Scsjp		else
2701590Srgrimes			(void)snprintf(cur_prop, sizeof(cur_prop), "%s (H): ",
271161815Scsjp			    mp->sq[i]->name);
27277220Smarkm		input[i] = get_int(cur_prop);
2731590Srgrimes		temp[i] = pp->houses - input[i];
2741590Srgrimes		if (temp[i] < 0) {
27577220Smarkm			printf("That's too many.  The most you can sell is %d\n", pp->houses);
27677220Smarkm				goto over;
27721646Sdavidn			}
27821646Sdavidn	}
27977220Smarkm	if (mp->num_in == 3 && (abs(temp[0] - temp[1]) > 1 ||
28077220Smarkm	    abs(temp[0] - temp[2]) > 1 || abs(temp[1] - temp[2]) > 1)) {
28177220Smarkmerr:		printf("That makes the spread too wide.  Try again\n");
28277220Smarkm		goto blew_it;
28377220Smarkm	}
28477220Smarkm	else if (mp->num_in == 2 && abs(temp[0] - temp[1]) > 1)
2851590Srgrimes		goto err;
2861590Srgrimes	for (tot = i = 0; i < mp->num_in; i++)
2871590Srgrimes		tot += input[i];
28821646Sdavidn	if (tot) {
2891590Srgrimes		printf("You asked to sell %d house%s for $%d\n", tot,
29077220Smarkm		    tot == 1 ? "" : "s", tot * price);
29174874Smarkm		if (getyn("Is that ok? ") == 0) {
29274874Smarkm			cur_p->money += tot * price;
29374874Smarkm			for (tot = i = 0; i < mp->num_in; i++)
29474874Smarkm				mp->sq[i]->desc->houses = temp[i];
29574874Smarkm		}
29674874Smarkm	}
297110456Sdes}
29881529Smarkm
29974874Smarkmstatic void
30074874Smarkmlist_cur(MON *mp)
30174874Smarkm{
30277220Smarkm	int	i;
30377220Smarkm	SQUARE	*sqp;
30477220Smarkm
30574874Smarkm	for (i = 0; i < mp->num_in; i++) {
306161815Scsjp		sqp = mp->sq[i];
307161815Scsjp		if (sqp->desc->houses == 5)
308161815Scsjp			printf("%s (H) ", sqp->name);
309161815Scsjp		else
310161815Scsjp			printf("%s (%d) ", sqp->name, sqp->desc->houses);
311105386Smarkm	}
312105386Smarkm	putchar('\n');
31377220Smarkm}
31474874Smarkm
315161815Scsjpstatic int
316161815Scsjpavail_houses(void)
317161815Scsjp{
318161815Scsjp	int i, c;
31977220Smarkm	SQUARE *sqp;
32077220Smarkm
32177220Smarkm	c = 0;
32277220Smarkm	for (i = 0; i < N_SQRS; i++) {
32377220Smarkm		sqp = &board[i];
32477220Smarkm		if (sqp->type == PRPTY && sqp->owner >= 0 && sqp->desc->monop) {
325124166Sdes			if (sqp->desc->houses < 5 && sqp->desc->houses > 0)
326161815Scsjp				c += sqp->desc->houses;
327161815Scsjp		}
328161815Scsjp	}
329161815Scsjp	return(N_HOUSE - c);
330161815Scsjp}
331161815Scsjp
332124166Sdesstatic int
333161815Scsjpavail_hotels(void)
33474874Smarkm{
33577220Smarkm	int i, c;
33677220Smarkm	SQUARE *sqp;
33777220Smarkm
33877220Smarkm	c = 0;
33974874Smarkm	for (i = 0; i < N_SQRS; i++) {
340161815Scsjp		sqp = &board[i];
341161815Scsjp		if (sqp->type == PRPTY && sqp->owner >= 0 && sqp->desc->monop) {
342161815Scsjp			if (sqp->desc->houses == 5)
343161815Scsjp				c++;
344161815Scsjp		}
345161815Scsjp	}
346161815Scsjp	return(N_HOTEL - c);
347161815Scsjp}
34877220Smarkm
34977220Smarkmstatic bool
35074874Smarkmcan_only_buy_hotel(MON *mp)
35174874Smarkm{
35274874Smarkm	int i;
35377220Smarkm
354161815Scsjp	for (i = 0; i < mp->num_in; i++) {
355161815Scsjp		if (mp->sq[i]->desc->houses < 4)
356161815Scsjp			return(FALSE);
357161815Scsjp	}
358161815Scsjp	return(TRUE);
35977220Smarkm}
36077220Smarkm