xref: /openbsd/games/monop/morg.c (revision 133306f0)
1 /*	$OpenBSD: morg.c,v 1.2 1998/09/20 23:36:54 pjanzen Exp $	*/
2 /*	$NetBSD: morg.c,v 1.4 1995/03/23 08:35:02 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1980, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)morg.c	8.1 (Berkeley) 5/31/93";
40 #else
41 static char rcsid[] = "$OpenBSD: morg.c,v 1.2 1998/09/20 23:36:54 pjanzen Exp $";
42 #endif
43 #endif /* not lint */
44 
45 # include	"monop.ext"
46 
47 /*
48  *	These routines deal with mortgaging.
49  */
50 
51 static char	*names[MAX_PRP+2],
52 		*morg_coms[]	= {
53 			"quit",		/*  0 */
54 			"print",	/*  1 */
55 			"where",	/*  2 */
56 			"own holdings",	/*  3 */
57 			"holdings",	/*  4 */
58 			"mortgage",	/*  5 */
59 			"unmortgage",	/*  6 */
60 			"buy",		/*  7 */
61 			"sell",		/*  8 */
62 			"card",		/*  9 */
63 			"pay",		/* 10 */
64 			"trade",	/* 11 */
65 			"resign",	/* 12 */
66 			"save game",	/* 13 */
67 			"restore game",	/* 14 */
68 			0
69 		};
70 
71 static shrt	square[MAX_PRP+2];
72 
73 static int	num_good,got_houses;
74 
75 static int	set_mlist __P((void));
76 static void	m __P((int));
77 static int	set_umlist __P((void));
78 static void	unm __P((int));
79 
80 /*
81  *	This routine is the command level response the mortgage command.
82  * it gets the list of mortgageable property and asks which are to
83  * be mortgaged.
84  */
85 void
86 mortgage()
87 {
88 	int	prop;
89 
90 	for (;;) {
91 		if (set_mlist() == 0) {
92 			if (got_houses)
93 				printf("You can't mortgage property with houses on it.\n");
94 			else
95 				printf("You don't have any un-mortgaged property.\n");
96 			return;
97 		}
98 		if (num_good == 1) {
99 			printf("Your only mortageable property is %s\n", names[0]);
100 			if (getyn("Do you want to mortgage it? ") == 0)
101 				m(square[0]);
102 			return;
103 		}
104 		prop = getinp("Which property do you want to mortgage? ", names);
105 		if (prop == num_good)
106 			return;
107 		m(square[prop]);
108 		notify();
109 	}
110 }
111 /*
112  *	This routine sets up the list of mortgageable property
113  */
114 static int
115 set_mlist()
116 {
117 	OWN	*op;
118 
119 	num_good = 0;
120 	for (op = cur_p->own_list; op; op = op->next)
121 		if (!op->sqr->desc->morg) {
122 			if (op->sqr->type == PRPTY && op->sqr->desc->houses)
123 				got_houses++;
124 			else {
125 				names[num_good] = op->sqr->name;
126 				square[num_good++] = sqnum(op->sqr);
127 			}
128 		}
129 	names[num_good++] = "done";
130 	names[num_good--] = 0;
131 	return num_good;
132 }
133 /*
134  *	This routine actually mortgages the property.
135  */
136 static void
137 m(prop)
138 	int	prop;
139 {
140 	int	price;
141 
142 	price = board[prop].cost/2;
143 	board[prop].desc->morg = TRUE;
144 	printf("That got you $%d\n",price);
145 	cur_p->money += price;
146 }
147 /*
148  *	This routine is the command level repsponse to the unmortgage
149  * command.  It gets the list of mortgaged property and asks which are
150  * to be unmortgaged.
151  */
152 void
153 unmortgage()
154 {
155 	int	prop;
156 
157 	for (;;) {
158 		if (set_umlist() == 0) {
159 			printf("You don't have any mortgaged property.\n");
160 			return;
161 		}
162 		if (num_good == 1) {
163 			printf("Your only mortaged property is %s\n", names[0]);
164 			if (getyn("Do you want to unmortgage it? ") == 0)
165 				unm(square[0]);
166 			return;
167 		}
168 		prop = getinp("Which property do you want to unmortgage? ", names);
169 		if (prop == num_good)
170 			return;
171 		unm(square[prop]);
172 	}
173 }
174 /*
175  *	This routine sets up the list of mortgaged property
176  */
177 static int
178 set_umlist()
179 {
180 	OWN	*op;
181 
182 	num_good = 0;
183 	for (op = cur_p->own_list; op; op = op->next)
184 		if (op->sqr->desc->morg) {
185 			names[num_good] = op->sqr->name;
186 			square[num_good++] = sqnum(op->sqr);
187 		}
188 	names[num_good++] = "done";
189 	names[num_good--] = 0;
190 	return num_good;
191 }
192 /*
193  *	This routine actually unmortgages the property
194  */
195 static void
196 unm(prop)
197 	int	prop;
198 {
199 	int	price;
200 
201 	price = board[prop].cost/2;
202 	board[prop].desc->morg = FALSE;
203 	price += price/10;
204 	printf("That cost you $%d\n",price);
205 	cur_p->money -= price;
206 	set_umlist();
207 }
208 /*
209  *	This routine forces the indebted player to fix his
210  * financial woes.
211  */
212 void
213 force_morg()
214 {
215 	told_em = fixing = TRUE;
216 	while (cur_p->money <= 0) {
217 		told_em = FALSE;
218 		(*func[(getinp("How are you going to fix it up? ",morg_coms))])();
219 		notify();
220 	}
221 	fixing = FALSE;
222 }
223