misc.c revision 1.10 1 1.10 wiz /* $NetBSD: misc.c,v 1.10 2002/05/26 00:12:13 wiz Exp $ */
2 1.4 cgd
3 1.1 cgd /*
4 1.3 jtc * Copyright (c) 1983, 1993
5 1.3 jtc * 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.6 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.4 cgd #if 0
39 1.4 cgd static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 5/31/93";
40 1.4 cgd #else
41 1.10 wiz __RCSID("$NetBSD: misc.c,v 1.10 2002/05/26 00:12:13 wiz Exp $");
42 1.4 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.3 jtc #include <sys/file.h>
46 1.3 jtc #include <termios.h>
47 1.3 jtc
48 1.10 wiz #include <stdarg.h>
49 1.6 lukem
50 1.1 cgd #include "mille.h"
51 1.1 cgd #ifndef unctrl
52 1.1 cgd #include "unctrl.h"
53 1.1 cgd #endif
54 1.1 cgd
55 1.1 cgd
56 1.1 cgd /*
57 1.1 cgd * @(#)misc.c 1.2 (Berkeley) 3/28/83
58 1.1 cgd */
59 1.1 cgd
60 1.1 cgd #define NUMSAFE 4
61 1.1 cgd
62 1.6 lukem bool
63 1.7 mycroft error(const char *str, ...)
64 1.1 cgd {
65 1.6 lukem va_list ap;
66 1.6 lukem
67 1.6 lukem va_start(ap, str);
68 1.6 lukem wmove(Score, ERR_Y, ERR_X);
69 1.6 lukem vwprintw(Score, str, ap);
70 1.9 jdc wclrtoeol(Score);
71 1.1 cgd putchar('\07');
72 1.1 cgd refresh();
73 1.6 lukem va_end(ap);
74 1.1 cgd return FALSE;
75 1.1 cgd }
76 1.1 cgd
77 1.1 cgd CARD
78 1.1 cgd getcard()
79 1.1 cgd {
80 1.6 lukem int c, c1;
81 1.1 cgd
82 1.1 cgd for (;;) {
83 1.1 cgd while ((c = readch()) == '\n' || c == '\r' || c == ' ')
84 1.1 cgd continue;
85 1.1 cgd if (islower(c))
86 1.1 cgd c = toupper(c);
87 1.1 cgd if (c == killchar() || c == erasechar())
88 1.1 cgd return -1;
89 1.1 cgd addstr(unctrl(c));
90 1.1 cgd clrtoeol();
91 1.1 cgd switch (c) {
92 1.1 cgd case '1': case '2': case '3':
93 1.1 cgd case '4': case '5': case '6':
94 1.1 cgd c -= '0';
95 1.1 cgd break;
96 1.1 cgd case '0': case 'P': case 'p':
97 1.1 cgd c = 0;
98 1.1 cgd break;
99 1.1 cgd default:
100 1.1 cgd putchar('\07');
101 1.1 cgd addch('\b');
102 1.1 cgd if (!isprint(c))
103 1.1 cgd addch('\b');
104 1.1 cgd c = -1;
105 1.1 cgd break;
106 1.1 cgd }
107 1.1 cgd refresh();
108 1.1 cgd if (c >= 0) {
109 1.1 cgd while ((c1=readch()) != '\r' && c1 != '\n' && c1 != ' ')
110 1.1 cgd if (c1 == killchar())
111 1.1 cgd return -1;
112 1.1 cgd else if (c1 == erasechar()) {
113 1.1 cgd addch('\b');
114 1.1 cgd clrtoeol();
115 1.1 cgd refresh();
116 1.1 cgd goto cont;
117 1.1 cgd }
118 1.1 cgd else
119 1.1 cgd write(0, "\07", 1);
120 1.1 cgd return c;
121 1.1 cgd }
122 1.1 cgd cont: ;
123 1.1 cgd }
124 1.1 cgd }
125 1.1 cgd
126 1.6 lukem int
127 1.1 cgd check_ext(forcomp)
128 1.6 lukem bool forcomp;
129 1.6 lukem {
130 1.1 cgd
131 1.1 cgd
132 1.1 cgd if (End == 700)
133 1.1 cgd if (Play == PLAYER) {
134 1.1 cgd if (getyn(EXTENSIONPROMPT)) {
135 1.1 cgd extend:
136 1.1 cgd if (!forcomp)
137 1.1 cgd End = 1000;
138 1.1 cgd return TRUE;
139 1.1 cgd }
140 1.1 cgd else {
141 1.1 cgd done:
142 1.1 cgd if (!forcomp)
143 1.1 cgd Finished = TRUE;
144 1.1 cgd return FALSE;
145 1.1 cgd }
146 1.1 cgd }
147 1.1 cgd else {
148 1.6 lukem PLAY *pp, *op;
149 1.6 lukem int i, safe, miles;
150 1.1 cgd
151 1.1 cgd pp = &Player[COMP];
152 1.1 cgd op = &Player[PLAYER];
153 1.1 cgd for (safe = 0, i = 0; i < NUMSAFE; i++)
154 1.1 cgd if (pp->safety[i] != S_UNKNOWN)
155 1.1 cgd safe++;
156 1.1 cgd if (safe < 2)
157 1.1 cgd goto done;
158 1.1 cgd if (op->mileage == 0 || onecard(op)
159 1.1 cgd || (op->can_go && op->mileage >= 500))
160 1.1 cgd goto done;
161 1.1 cgd for (miles = 0, i = 0; i < NUMSAFE; i++)
162 1.1 cgd if (op->safety[i] != S_PLAYED
163 1.1 cgd && pp->safety[i] == S_UNKNOWN)
164 1.1 cgd miles++;
165 1.1 cgd if (miles + safe == NUMSAFE)
166 1.1 cgd goto extend;
167 1.1 cgd for (miles = 0, i = 0; i < HAND_SZ; i++)
168 1.1 cgd if ((safe = pp->hand[i]) <= C_200)
169 1.1 cgd miles += Value[safe];
170 1.1 cgd if (miles + (Topcard - Deck) * 3 > 1000)
171 1.1 cgd goto extend;
172 1.1 cgd goto done;
173 1.1 cgd }
174 1.1 cgd else
175 1.1 cgd goto done;
176 1.1 cgd }
177 1.1 cgd
178 1.1 cgd /*
179 1.1 cgd * Get a yes or no answer to the given question. Saves are
180 1.1 cgd * also allowed. Return TRUE if the answer was yes, FALSE if no.
181 1.1 cgd */
182 1.6 lukem int
183 1.1 cgd getyn(promptno)
184 1.6 lukem int promptno;
185 1.5 jtc {
186 1.6 lukem char c;
187 1.1 cgd
188 1.1 cgd Saved = FALSE;
189 1.1 cgd for (;;) {
190 1.1 cgd leaveok(Board, FALSE);
191 1.1 cgd prompt(promptno);
192 1.1 cgd clrtoeol();
193 1.1 cgd refresh();
194 1.1 cgd switch (c = readch()) {
195 1.1 cgd case 'n': case 'N':
196 1.1 cgd addch('N');
197 1.1 cgd refresh();
198 1.1 cgd leaveok(Board, TRUE);
199 1.1 cgd return FALSE;
200 1.1 cgd case 'y': case 'Y':
201 1.1 cgd addch('Y');
202 1.1 cgd refresh();
203 1.1 cgd leaveok(Board, TRUE);
204 1.1 cgd return TRUE;
205 1.1 cgd case 's': case 'S':
206 1.1 cgd addch('S');
207 1.1 cgd refresh();
208 1.1 cgd Saved = save();
209 1.1 cgd continue;
210 1.3 jtc case CTRL('L'):
211 1.3 jtc wrefresh(curscr);
212 1.3 jtc break;
213 1.1 cgd default:
214 1.1 cgd addstr(unctrl(c));
215 1.1 cgd refresh();
216 1.1 cgd putchar('\07');
217 1.1 cgd break;
218 1.1 cgd }
219 1.1 cgd }
220 1.1 cgd }
221 1.1 cgd
222 1.1 cgd /*
223 1.1 cgd * Check to see if more games are desired. If not, and game
224 1.1 cgd * came from a saved file, make sure that they don't want to restore
225 1.1 cgd * it. Exit appropriately.
226 1.1 cgd */
227 1.6 lukem void
228 1.6 lukem check_more()
229 1.6 lukem {
230 1.1 cgd On_exit = TRUE;
231 1.1 cgd if (Player[PLAYER].total >= 5000 || Player[COMP].total >= 5000)
232 1.1 cgd if (getyn(ANOTHERGAMEPROMPT))
233 1.1 cgd return;
234 1.1 cgd else {
235 1.1 cgd /*
236 1.1 cgd * must do accounting normally done in main()
237 1.1 cgd */
238 1.1 cgd if (Player[PLAYER].total > Player[COMP].total)
239 1.1 cgd Player[PLAYER].games++;
240 1.1 cgd else if (Player[PLAYER].total < Player[COMP].total)
241 1.1 cgd Player[COMP].games++;
242 1.1 cgd Player[COMP].total = 0;
243 1.1 cgd Player[PLAYER].total = 0;
244 1.1 cgd }
245 1.1 cgd else
246 1.1 cgd if (getyn(ANOTHERHANDPROMPT))
247 1.1 cgd return;
248 1.1 cgd if (!Saved && getyn(SAVEGAMEPROMPT))
249 1.1 cgd if (!save())
250 1.1 cgd return;
251 1.3 jtc die(0);
252 1.1 cgd }
253 1.1 cgd
254 1.6 lukem int
255 1.1 cgd readch()
256 1.1 cgd {
257 1.6 lukem int cnt;
258 1.1 cgd static char c;
259 1.1 cgd
260 1.1 cgd for (cnt = 0; read(0, &c, 1) <= 0; cnt++)
261 1.1 cgd if (cnt > 100)
262 1.1 cgd exit(1);
263 1.1 cgd return c;
264 1.1 cgd }
265