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