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