Home | History | Annotate | Line # | Download | only in common_source
subs.c revision 1.20
      1  1.20     joerg /*	$NetBSD: subs.c,v 1.20 2013/09/13 20:46:50 joerg Exp $	*/
      2   1.3       cgd 
      3   1.1       cgd /*
      4   1.3       cgd  * Copyright (c) 1980, 1993
      5   1.3       cgd  *	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.14       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.6     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.3       cgd #if 0
     35   1.3       cgd static char sccsid[] = "@(#)subs.c	8.1 (Berkeley) 5/31/93";
     36   1.3       cgd #else
     37  1.20     joerg __RCSID("$NetBSD: subs.c,v 1.20 2013/09/13 20:46:50 joerg Exp $");
     38   1.3       cgd #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #include "back.h"
     42   1.1       cgd 
     43   1.6     lukem int     buffnum;
     44  1.17  dholland static char outbuff[BUFSIZ];
     45   1.1       cgd 
     46  1.10   hubertf static const char plred[] = "Player is red, computer is white.";
     47  1.10   hubertf static const char plwhite[] = "Player is white, computer is red.";
     48  1.10   hubertf static const char nocomp[] = "(No computer play.)";
     49   1.1       cgd 
     50   1.6     lukem void
     51  1.15       jmc errexit(const char *s)
     52   1.1       cgd {
     53   1.6     lukem 	write(2, "\n", 1);
     54   1.6     lukem 	perror(s);
     55   1.6     lukem 	getout(0);
     56   1.1       cgd }
     57   1.1       cgd 
     58  1.13     lukem int
     59  1.15       jmc addbuf(int c)
     60   1.1       cgd {
     61   1.1       cgd 	buffnum++;
     62   1.6     lukem 	if (buffnum == BUFSIZ) {
     63   1.6     lukem 		if (write(1, outbuff, BUFSIZ) != BUFSIZ)
     64   1.6     lukem 			errexit("addbuf (write):");
     65   1.1       cgd 		buffnum = 0;
     66   1.1       cgd 	}
     67   1.1       cgd 	outbuff[buffnum] = c;
     68  1.13     lukem 	return (0);
     69   1.1       cgd }
     70   1.1       cgd 
     71   1.6     lukem void
     72  1.15       jmc buflush(void)
     73   1.6     lukem {
     74   1.1       cgd 	if (buffnum < 0)
     75   1.1       cgd 		return;
     76   1.1       cgd 	buffnum++;
     77   1.6     lukem 	if (write(1, outbuff, buffnum) != buffnum)
     78   1.6     lukem 		errexit("buflush (write):");
     79   1.1       cgd 	buffnum = -1;
     80   1.1       cgd }
     81   1.1       cgd 
     82   1.6     lukem int
     83  1.15       jmc readc(void)
     84   1.6     lukem {
     85   1.6     lukem 	char    c;
     86   1.1       cgd 
     87   1.6     lukem 	if (tflag) {
     88   1.1       cgd 		cline();
     89   1.1       cgd 		newpos();
     90   1.1       cgd 	}
     91   1.1       cgd 	buflush();
     92   1.6     lukem 	if (read(0, &c, 1) != 1)
     93   1.6     lukem 		errexit("readc");
     94   1.1       cgd #ifdef WHY_IS_THIS_HARDWIRED_IN_HERE
     95   1.1       cgd 	if (c == '\177')
     96   1.6     lukem 		getout(0);
     97   1.1       cgd #endif
     98   1.1       cgd 	if (c == '\033' || c == '\015')
     99   1.1       cgd 		return ('\n');
    100   1.1       cgd 	if (cflag)
    101   1.1       cgd 		return (c);
    102   1.1       cgd 	if (c == '\014')
    103   1.1       cgd 		return ('R');
    104   1.1       cgd 	if (c >= 'a' && c <= 'z')
    105   1.1       cgd 		return (c & 0137);
    106   1.1       cgd 	return (c);
    107   1.1       cgd }
    108   1.1       cgd 
    109   1.6     lukem void
    110  1.15       jmc writec(int c)
    111   1.1       cgd {
    112   1.1       cgd 	if (tflag)
    113   1.6     lukem 		fancyc(c);
    114   1.1       cgd 	else
    115   1.6     lukem 		addbuf(c);
    116   1.1       cgd }
    117   1.1       cgd 
    118   1.6     lukem void
    119  1.15       jmc writel(const char *l)
    120   1.1       cgd {
    121   1.1       cgd #ifdef DEBUG
    122  1.10   hubertf 	const char   *s;
    123   1.1       cgd 
    124   1.1       cgd 	if (trace == NULL)
    125   1.6     lukem 		trace = fopen("bgtrace", "w");
    126   1.6     lukem 
    127   1.6     lukem 	fprintf(trace, "writel: \"");
    128   1.1       cgd 	for (s = l; *s; s++) {
    129   1.1       cgd 		if (*s < ' ' || *s == '\177')
    130   1.6     lukem 			fprintf(trace, "^%c", (*s) ^ 0100);
    131   1.1       cgd 		else
    132   1.6     lukem 			putc(*s, trace);
    133   1.1       cgd 	}
    134   1.6     lukem 	fprintf(trace, "\"\n");
    135   1.6     lukem 	fflush(trace);
    136   1.1       cgd #endif
    137   1.1       cgd 
    138   1.1       cgd 	while (*l)
    139   1.6     lukem 		writec(*l++);
    140   1.1       cgd }
    141   1.1       cgd 
    142   1.6     lukem void
    143  1.19  dholland proll(struct move *mm)
    144   1.6     lukem {
    145  1.18  dholland 	if (mm->d0)
    146  1.18  dholland 		mswap(mm);
    147   1.1       cgd 	if (cturn == 1)
    148   1.6     lukem 		writel("Red's roll:  ");
    149   1.1       cgd 	else
    150   1.6     lukem 		writel("White's roll:  ");
    151  1.18  dholland 	writec(mm->D0 + '0');
    152   1.6     lukem 	writec('\040');
    153  1.18  dholland 	writec(mm->D1 + '0');
    154   1.1       cgd 	if (tflag)
    155   1.1       cgd 		cline();
    156   1.1       cgd }
    157   1.1       cgd 
    158   1.6     lukem void
    159  1.15       jmc wrint(int n)
    160   1.1       cgd {
    161   1.6     lukem 	int     i, j, t;
    162   1.1       cgd 
    163   1.6     lukem 	for (i = 4; i > 0; i--) {
    164   1.1       cgd 		t = 1;
    165   1.6     lukem 		for (j = 0; j < i; j++)
    166   1.1       cgd 			t *= 10;
    167   1.6     lukem 		if (n > t - 1)
    168   1.6     lukem 			writec((n / t) % 10 + '0');
    169   1.1       cgd 	}
    170   1.6     lukem 	writec(n % 10 + '0');
    171   1.1       cgd }
    172   1.1       cgd 
    173   1.6     lukem void
    174  1.15       jmc gwrite(void)
    175   1.6     lukem {
    176   1.6     lukem 	int     r, c;
    177   1.1       cgd 
    178   1.6     lukem 	r = c = 0;
    179   1.6     lukem 	if (tflag) {
    180   1.1       cgd 		r = curr;
    181   1.1       cgd 		c = curc;
    182   1.6     lukem 		curmove(16, 0);
    183   1.1       cgd 	}
    184   1.6     lukem 	if (gvalue > 1) {
    185   1.6     lukem 		writel("Game value:  ");
    186   1.6     lukem 		wrint(gvalue);
    187   1.6     lukem 		writel(".  ");
    188   1.1       cgd 		if (dlast == -1)
    189   1.6     lukem 			writel(color[0]);
    190   1.1       cgd 		else
    191   1.6     lukem 			writel(color[1]);
    192   1.6     lukem 		writel(" doubled last.");
    193   1.6     lukem 	} else {
    194   1.6     lukem 		switch (pnum) {
    195   1.6     lukem 		case -1:	/* player is red */
    196   1.6     lukem 			writel(plred);
    197   1.1       cgd 			break;
    198   1.6     lukem 		case 0:	/* player is both colors */
    199   1.6     lukem 			writel(nocomp);
    200   1.1       cgd 			break;
    201   1.6     lukem 		case 1:	/* player is white */
    202   1.6     lukem 			writel(plwhite);
    203   1.1       cgd 		}
    204   1.1       cgd 	}
    205   1.1       cgd 
    206   1.6     lukem 	if (rscore || wscore) {
    207   1.6     lukem 		writel("  ");
    208   1.1       cgd 		wrscore();
    209   1.1       cgd 	}
    210   1.6     lukem 	if (tflag) {
    211   1.1       cgd 		cline();
    212   1.6     lukem 		curmove(r, c);
    213   1.1       cgd 	}
    214   1.1       cgd }
    215   1.1       cgd 
    216   1.6     lukem int
    217  1.19  dholland quit(struct move *mm)
    218   1.6     lukem {
    219   1.1       cgd 
    220   1.6     lukem 	if (tflag) {
    221   1.6     lukem 		curmove(20, 0);
    222   1.1       cgd 		clend();
    223   1.1       cgd 	} else
    224   1.6     lukem 		writec('\n');
    225   1.6     lukem 	writel("Are you sure you want to quit?");
    226   1.6     lukem 	if (yorn(0)) {
    227   1.6     lukem 		if (rfl) {
    228   1.6     lukem 			writel("Would you like to save this game?");
    229   1.1       cgd 			if (yorn(0))
    230  1.19  dholland 				save(mm, 0);
    231   1.1       cgd 		}
    232   1.1       cgd 		cturn = 0;
    233   1.1       cgd 		return (1);
    234   1.1       cgd 	}
    235   1.1       cgd 	return (0);
    236   1.1       cgd }
    237   1.1       cgd 
    238   1.6     lukem int
    239  1.15       jmc yorn(int special)
    240   1.1       cgd {
    241   1.6     lukem 	char    c;
    242   1.6     lukem 	int     i;
    243   1.1       cgd 
    244   1.1       cgd 	i = 1;
    245   1.6     lukem 	while ((c = readc()) != 'Y' && c != 'N') {
    246   1.1       cgd 		if (special && c == special)
    247   1.1       cgd 			return (2);
    248   1.6     lukem 		if (i) {
    249   1.6     lukem 			if (special) {
    250   1.6     lukem 				writel("  (Y, N, or ");
    251   1.6     lukem 				writec(special);
    252   1.6     lukem 				writec(')');
    253   1.1       cgd 			} else
    254   1.6     lukem 				writel("  (Y or N)");
    255   1.1       cgd 			i = 0;
    256   1.1       cgd 		} else
    257   1.6     lukem 			writec('\007');
    258   1.1       cgd 	}
    259   1.1       cgd 	if (c == 'Y')
    260   1.6     lukem 		writel("  Yes.\n");
    261   1.1       cgd 	else
    262   1.6     lukem 		writel("  No.\n");
    263   1.1       cgd 	if (tflag)
    264   1.1       cgd 		buflush();
    265   1.1       cgd 	return (c == 'Y');
    266   1.1       cgd }
    267   1.1       cgd 
    268   1.6     lukem void
    269  1.15       jmc wrhit(int i)
    270   1.1       cgd {
    271   1.6     lukem 	writel("Blot hit on ");
    272   1.6     lukem 	wrint(i);
    273   1.6     lukem 	writec('.');
    274   1.6     lukem 	writec('\n');
    275   1.1       cgd }
    276   1.1       cgd 
    277   1.6     lukem void
    278  1.15       jmc nexturn(void)
    279   1.6     lukem {
    280   1.6     lukem 	int     c;
    281   1.1       cgd 
    282   1.1       cgd 	cturn = -cturn;
    283   1.6     lukem 	c = cturn / abs(cturn);
    284   1.1       cgd 	home = bar;
    285   1.6     lukem 	bar = 25 - bar;
    286   1.1       cgd 	offptr += c;
    287   1.1       cgd 	offopp -= c;
    288   1.1       cgd 	inptr += c;
    289   1.1       cgd 	inopp -= c;
    290   1.1       cgd 	Colorptr += c;
    291   1.1       cgd 	colorptr += c;
    292   1.1       cgd }
    293   1.1       cgd 
    294   1.6     lukem void
    295  1.19  dholland getarg(struct move *mm, char ***arg)
    296   1.1       cgd {
    297   1.6     lukem 	char  **s;
    298   1.1       cgd 
    299   1.6     lukem 	/* process arguments here.  dashes are ignored, nbrw are ignored if
    300   1.6     lukem 	 * the game is being recovered */
    301   1.1       cgd 
    302   1.1       cgd 	s = *arg;
    303   1.9   frueauf 	while (*s && s[0][0] == '-') {
    304   1.6     lukem 		switch (s[0][1]) {
    305   1.1       cgd 
    306   1.6     lukem 			/* don't ask if rules or instructions needed */
    307   1.1       cgd 		case 'n':
    308   1.1       cgd 			if (rflag)
    309   1.1       cgd 				break;
    310   1.1       cgd 			aflag = 0;
    311   1.1       cgd 			args[acnt++] = 'n';
    312   1.1       cgd 			break;
    313   1.1       cgd 
    314   1.7   hubertf 			/* player is both red and white */
    315   1.1       cgd 		case 'b':
    316   1.1       cgd 			if (rflag)
    317   1.1       cgd 				break;
    318   1.1       cgd 			pnum = 0;
    319   1.1       cgd 			aflag = 0;
    320   1.1       cgd 			args[acnt++] = 'b';
    321   1.1       cgd 			break;
    322   1.1       cgd 
    323   1.6     lukem 			/* player is red */
    324   1.1       cgd 		case 'r':
    325   1.1       cgd 			if (rflag)
    326   1.1       cgd 				break;
    327   1.1       cgd 			pnum = -1;
    328   1.1       cgd 			aflag = 0;
    329   1.1       cgd 			args[acnt++] = 'r';
    330   1.1       cgd 			break;
    331   1.1       cgd 
    332   1.6     lukem 			/* player is white */
    333   1.1       cgd 		case 'w':
    334   1.1       cgd 			if (rflag)
    335   1.1       cgd 				break;
    336   1.1       cgd 			pnum = 1;
    337   1.1       cgd 			aflag = 0;
    338   1.1       cgd 			args[acnt++] = 'w';
    339   1.1       cgd 			break;
    340   1.1       cgd 
    341   1.6     lukem 			/* print board after move according to following
    342   1.6     lukem 			 * character */
    343   1.1       cgd 		case 'p':
    344   1.1       cgd 			if (s[0][2] != 'r' && s[0][2] != 'w' && s[0][2] != 'b')
    345   1.1       cgd 				break;
    346   1.1       cgd 			args[acnt++] = 'p';
    347   1.1       cgd 			args[acnt++] = s[0][2];
    348   1.1       cgd 			if (s[0][2] == 'r')
    349   1.1       cgd 				bflag = 1;
    350   1.1       cgd 			if (s[0][2] == 'w')
    351   1.1       cgd 				bflag = -1;
    352   1.1       cgd 			if (s[0][2] == 'b')
    353   1.1       cgd 				bflag = 0;
    354   1.1       cgd 			break;
    355   1.1       cgd 
    356   1.1       cgd 		case 't':
    357   1.1       cgd 			if (s[0][2] == '\0') {	/* get terminal caps */
    358   1.1       cgd 				s++;
    359   1.6     lukem 				tflag = getcaps(*s);
    360   1.1       cgd 			} else
    361   1.6     lukem 				tflag = getcaps(&s[0][2]);
    362   1.1       cgd 			break;
    363   1.1       cgd 
    364   1.1       cgd 		case 's':
    365   1.1       cgd 			s++;
    366   1.1       cgd 			/* recover file */
    367  1.11   hubertf 			if (s[0] == NULL) {
    368  1.11   hubertf 				writel("No save file named\n");
    369  1.11   hubertf 				getout(0);
    370  1.11   hubertf 			} else
    371  1.19  dholland 				recover(mm, s[0]);
    372   1.1       cgd 			break;
    373   1.1       cgd 		}
    374   1.1       cgd 		s++;
    375   1.1       cgd 	}
    376   1.1       cgd 	if (s[0] != 0)
    377  1.19  dholland 		recover(mm, s[0]);
    378   1.1       cgd }
    379   1.1       cgd 
    380   1.6     lukem void
    381  1.15       jmc init(void)
    382   1.6     lukem {
    383   1.6     lukem 	int     i;
    384   1.6     lukem 
    385   1.1       cgd 	for (i = 0; i < 26;)
    386   1.1       cgd 		board[i++] = 0;
    387   1.1       cgd 	board[1] = 2;
    388   1.1       cgd 	board[6] = board[13] = -5;
    389   1.1       cgd 	board[8] = -3;
    390   1.1       cgd 	board[12] = board[19] = 5;
    391   1.1       cgd 	board[17] = 3;
    392   1.1       cgd 	board[24] = -2;
    393   1.1       cgd 	off[0] = off[1] = -15;
    394   1.1       cgd 	in[0] = in[1] = 5;
    395   1.1       cgd 	gvalue = 1;
    396   1.1       cgd 	dlast = 0;
    397   1.1       cgd }
    398   1.1       cgd 
    399   1.6     lukem void
    400  1.15       jmc wrscore(void)
    401   1.6     lukem {
    402   1.6     lukem 	writel("Score:  ");
    403   1.6     lukem 	writel(color[1]);
    404   1.6     lukem 	writec(' ');
    405   1.6     lukem 	wrint(rscore);
    406   1.6     lukem 	writel(", ");
    407   1.6     lukem 	writel(color[0]);
    408   1.6     lukem 	writec(' ');
    409   1.6     lukem 	wrint(wscore);
    410   1.1       cgd }
    411   1.1       cgd 
    412   1.6     lukem void
    413  1.15       jmc fixtty(struct termios *t)
    414   1.1       cgd {
    415   1.1       cgd 	if (tflag)
    416   1.1       cgd 		newpos();
    417   1.1       cgd 	buflush();
    418   1.6     lukem 	if (tcsetattr(0, TCSADRAIN, t) < 0)
    419   1.1       cgd 		errexit("fixtty");
    420   1.1       cgd }
    421   1.1       cgd 
    422   1.6     lukem void
    423  1.16     perry getout(int dummy __unused)
    424   1.6     lukem {
    425   1.1       cgd 	/* go to bottom of screen */
    426   1.6     lukem 	if (tflag) {
    427   1.6     lukem 		curmove(23, 0);
    428   1.1       cgd 		cline();
    429   1.1       cgd 	} else
    430   1.6     lukem 		writec('\n');
    431   1.1       cgd 
    432   1.1       cgd 	/* fix terminal status */
    433   1.6     lukem 	fixtty(&old);
    434   1.4       cgd 	exit(0);
    435   1.1       cgd }
    436   1.1       cgd 
    437   1.6     lukem void
    438  1.19  dholland roll(struct move *mm)
    439   1.6     lukem {
    440   1.6     lukem 	char    c;
    441   1.6     lukem 	int     row;
    442   1.6     lukem 	int     col;
    443   1.6     lukem 
    444   1.6     lukem 	row = col = 0;
    445   1.6     lukem 	if (iroll) {
    446   1.6     lukem 		if (tflag) {
    447   1.1       cgd 			row = curr;
    448   1.1       cgd 			col = curc;
    449   1.6     lukem 			curmove(17, 0);
    450   1.1       cgd 		} else
    451   1.6     lukem 			writec('\n');
    452   1.6     lukem 		writel("ROLL: ");
    453   1.1       cgd 		c = readc();
    454   1.6     lukem 		if (c != '\n') {
    455   1.1       cgd 			while (c < '1' || c > '6')
    456   1.1       cgd 				c = readc();
    457  1.18  dholland 			mm->D0 = c - '0';
    458   1.6     lukem 			writec(' ');
    459   1.6     lukem 			writec(c);
    460   1.1       cgd 			c = readc();
    461   1.1       cgd 			while (c < '1' || c > '6')
    462   1.1       cgd 				c = readc();
    463  1.18  dholland 			mm->D1 = c - '0';
    464   1.6     lukem 			writec(' ');
    465   1.6     lukem 			writec(c);
    466   1.6     lukem 			if (tflag) {
    467   1.6     lukem 				curmove(17, 0);
    468   1.1       cgd 				cline();
    469   1.6     lukem 				curmove(row, col);
    470   1.1       cgd 			} else
    471   1.6     lukem 				writec('\n');
    472   1.1       cgd 			return;
    473   1.1       cgd 		}
    474   1.6     lukem 		if (tflag) {
    475   1.6     lukem 			curmove(17, 0);
    476   1.1       cgd 			cline();
    477   1.6     lukem 			curmove(row, col);
    478   1.1       cgd 		} else
    479   1.6     lukem 			writec('\n');
    480   1.1       cgd 	}
    481  1.18  dholland 	mm->D0 = rnum(6) + 1;
    482  1.18  dholland 	mm->D1 = rnum(6) + 1;
    483  1.18  dholland 	mm->d0 = 0;
    484   1.1       cgd }
    485