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