Home | History | Annotate | Line # | Download | only in mille
misc.c revision 1.11
      1 /*	$NetBSD: misc.c,v 1.11 2003/08/07 09:37:25 agc 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)misc.c	8.1 (Berkeley) 5/31/93";
     36 #else
     37 __RCSID("$NetBSD: misc.c,v 1.11 2003/08/07 09:37:25 agc Exp $");
     38 #endif
     39 #endif /* not lint */
     40 
     41 #include <sys/file.h>
     42 #include <termios.h>
     43 
     44 #include <stdarg.h>
     45 
     46 #include	"mille.h"
     47 #ifndef	unctrl
     48 #include	"unctrl.h"
     49 #endif
     50 
     51 
     52 /*
     53  * @(#)misc.c	1.2 (Berkeley) 3/28/83
     54  */
     55 
     56 #define	NUMSAFE	4
     57 
     58 bool
     59 error(const char *str, ...)
     60 {
     61 	va_list ap;
     62 
     63 	va_start(ap, str);
     64 	wmove(Score, ERR_Y, ERR_X);
     65 	vwprintw(Score, str, ap);
     66 	wclrtoeol(Score);
     67 	putchar('\07');
     68 	refresh();
     69 	va_end(ap);
     70 	return FALSE;
     71 }
     72 
     73 CARD
     74 getcard()
     75 {
     76 	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 int
    123 check_ext(forcomp)
    124 	bool	forcomp;
    125 {
    126 
    127 
    128 	if (End == 700)
    129 		if (Play == PLAYER) {
    130 			if (getyn(EXTENSIONPROMPT)) {
    131 extend:
    132 				if (!forcomp)
    133 					End = 1000;
    134 				return TRUE;
    135 			}
    136 			else {
    137 done:
    138 				if (!forcomp)
    139 					Finished = TRUE;
    140 				return FALSE;
    141 			}
    142 		}
    143 		else {
    144 			PLAY	*pp, *op;
    145 			int	i, safe, miles;
    146 
    147 			pp = &Player[COMP];
    148 			op = &Player[PLAYER];
    149 			for (safe = 0, i = 0; i < NUMSAFE; i++)
    150 				if (pp->safety[i] != S_UNKNOWN)
    151 					safe++;
    152 			if (safe < 2)
    153 				goto done;
    154 			if (op->mileage == 0 || onecard(op)
    155 			    || (op->can_go && op->mileage >= 500))
    156 				goto done;
    157 			for (miles = 0, i = 0; i < NUMSAFE; i++)
    158 				if (op->safety[i] != S_PLAYED
    159 				    && pp->safety[i] == S_UNKNOWN)
    160 					miles++;
    161 			if (miles + safe == NUMSAFE)
    162 				goto extend;
    163 			for (miles = 0, i = 0; i < HAND_SZ; i++)
    164 				if ((safe = pp->hand[i]) <= C_200)
    165 					miles += Value[safe];
    166 			if (miles + (Topcard - Deck) * 3 > 1000)
    167 				goto extend;
    168 			goto done;
    169 		}
    170 	else
    171 		goto done;
    172 }
    173 
    174 /*
    175  *	Get a yes or no answer to the given question.  Saves are
    176  * also allowed.  Return TRUE if the answer was yes, FALSE if no.
    177  */
    178 int
    179 getyn(promptno)
    180 	int	promptno;
    181 {
    182 	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 void
    224 check_more()
    225 {
    226 	On_exit = TRUE;
    227 	if (Player[PLAYER].total >= 5000 || Player[COMP].total >= 5000)
    228 		if (getyn(ANOTHERGAMEPROMPT))
    229 			return;
    230 		else {
    231 			/*
    232 			 * must do accounting normally done in main()
    233 			 */
    234 			if (Player[PLAYER].total > Player[COMP].total)
    235 				Player[PLAYER].games++;
    236 			else if (Player[PLAYER].total < Player[COMP].total)
    237 				Player[COMP].games++;
    238 			Player[COMP].total = 0;
    239 			Player[PLAYER].total = 0;
    240 		}
    241 	else
    242 		if (getyn(ANOTHERHANDPROMPT))
    243 			return;
    244 	if (!Saved && getyn(SAVEGAMEPROMPT))
    245 		if (!save())
    246 			return;
    247 	die(0);
    248 }
    249 
    250 int
    251 readch()
    252 {
    253 	int	cnt;
    254 	static char	c;
    255 
    256 	for (cnt = 0; read(0, &c, 1) <= 0; cnt++)
    257 		if (cnt > 100)
    258 			exit(1);
    259 	return c;
    260 }
    261