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