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