misc.c revision 1.5 1 /* $NetBSD: misc.c,v 1.5 1997/05/23 23:09:40 jtc Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 5/31/93";
39 #else
40 static char rcsid[] = "$NetBSD: misc.c,v 1.5 1997/05/23 23:09:40 jtc Exp $";
41 #endif
42 #endif /* not lint */
43
44 #include <sys/file.h>
45 #include <termios.h>
46
47 #include "mille.h"
48 #ifndef unctrl
49 #include "unctrl.h"
50 #endif
51
52
53 # ifdef attron
54 # include <term.h>
55 # endif attron
56
57 /*
58 * @(#)misc.c 1.2 (Berkeley) 3/28/83
59 */
60
61 #define NUMSAFE 4
62
63 /* VARARGS1 */
64 error(str, arg)
65 char *str;
66 {
67 stdscr = Score;
68 mvprintw(ERR_Y, ERR_X, str, arg);
69 clrtoeol();
70 putchar('\07');
71 refresh();
72 stdscr = Board;
73 return FALSE;
74 }
75
76 CARD
77 getcard()
78 {
79 register int c, c1;
80
81 for (;;) {
82 while ((c = readch()) == '\n' || c == '\r' || c == ' ')
83 continue;
84 if (islower(c))
85 c = toupper(c);
86 if (c == killchar() || c == erasechar())
87 return -1;
88 addstr(unctrl(c));
89 clrtoeol();
90 switch (c) {
91 case '1': case '2': case '3':
92 case '4': case '5': case '6':
93 c -= '0';
94 break;
95 case '0': case 'P': case 'p':
96 c = 0;
97 break;
98 default:
99 putchar('\07');
100 addch('\b');
101 if (!isprint(c))
102 addch('\b');
103 c = -1;
104 break;
105 }
106 refresh();
107 if (c >= 0) {
108 while ((c1=readch()) != '\r' && c1 != '\n' && c1 != ' ')
109 if (c1 == killchar())
110 return -1;
111 else if (c1 == erasechar()) {
112 addch('\b');
113 clrtoeol();
114 refresh();
115 goto cont;
116 }
117 else
118 write(0, "\07", 1);
119 return c;
120 }
121 cont: ;
122 }
123 }
124
125 check_ext(forcomp)
126 register bool forcomp; {
127
128
129 if (End == 700)
130 if (Play == PLAYER) {
131 if (getyn(EXTENSIONPROMPT)) {
132 extend:
133 if (!forcomp)
134 End = 1000;
135 return TRUE;
136 }
137 else {
138 done:
139 if (!forcomp)
140 Finished = TRUE;
141 return FALSE;
142 }
143 }
144 else {
145 register PLAY *pp, *op;
146 register int i, safe, miles;
147
148 pp = &Player[COMP];
149 op = &Player[PLAYER];
150 for (safe = 0, i = 0; i < NUMSAFE; i++)
151 if (pp->safety[i] != S_UNKNOWN)
152 safe++;
153 if (safe < 2)
154 goto done;
155 if (op->mileage == 0 || onecard(op)
156 || (op->can_go && op->mileage >= 500))
157 goto done;
158 for (miles = 0, i = 0; i < NUMSAFE; i++)
159 if (op->safety[i] != S_PLAYED
160 && pp->safety[i] == S_UNKNOWN)
161 miles++;
162 if (miles + safe == NUMSAFE)
163 goto extend;
164 for (miles = 0, i = 0; i < HAND_SZ; i++)
165 if ((safe = pp->hand[i]) <= C_200)
166 miles += Value[safe];
167 if (miles + (Topcard - Deck) * 3 > 1000)
168 goto extend;
169 goto done;
170 }
171 else
172 goto done;
173 }
174
175 /*
176 * Get a yes or no answer to the given question. Saves are
177 * also allowed. Return TRUE if the answer was yes, FALSE if no.
178 */
179 getyn(promptno)
180 register int promptno;
181 {
182 register char c;
183
184 Saved = FALSE;
185 for (;;) {
186 leaveok(Board, FALSE);
187 prompt(promptno);
188 clrtoeol();
189 refresh();
190 switch (c = readch()) {
191 case 'n': case 'N':
192 addch('N');
193 refresh();
194 leaveok(Board, TRUE);
195 return FALSE;
196 case 'y': case 'Y':
197 addch('Y');
198 refresh();
199 leaveok(Board, TRUE);
200 return TRUE;
201 case 's': case 'S':
202 addch('S');
203 refresh();
204 Saved = save();
205 continue;
206 case CTRL('L'):
207 wrefresh(curscr);
208 break;
209 default:
210 addstr(unctrl(c));
211 refresh();
212 putchar('\07');
213 break;
214 }
215 }
216 }
217
218 /*
219 * Check to see if more games are desired. If not, and game
220 * came from a saved file, make sure that they don't want to restore
221 * it. Exit appropriately.
222 */
223 check_more() {
224
225 On_exit = TRUE;
226 if (Player[PLAYER].total >= 5000 || Player[COMP].total >= 5000)
227 if (getyn(ANOTHERGAMEPROMPT))
228 return;
229 else {
230 /*
231 * must do accounting normally done in main()
232 */
233 if (Player[PLAYER].total > Player[COMP].total)
234 Player[PLAYER].games++;
235 else if (Player[PLAYER].total < Player[COMP].total)
236 Player[COMP].games++;
237 Player[COMP].total = 0;
238 Player[PLAYER].total = 0;
239 }
240 else
241 if (getyn(ANOTHERHANDPROMPT))
242 return;
243 if (!Saved && getyn(SAVEGAMEPROMPT))
244 if (!save())
245 return;
246 die(0);
247 }
248
249 readch()
250 {
251 register int cnt;
252 static char c;
253
254 for (cnt = 0; read(0, &c, 1) <= 0; cnt++)
255 if (cnt > 100)
256 exit(1);
257 return c;
258 }
259