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