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