Home | History | Annotate | Line # | Download | only in common_source
table.c revision 1.7
      1  1.7      agc /*	$NetBSD: table.c,v 1.7 2003/08/07 09:36:57 agc 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.7      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.5    lukem #include <sys/cdefs.h>
     33  1.1      cgd #ifndef lint
     34  1.3      cgd #if 0
     35  1.3      cgd static char sccsid[] = "@(#)table.c	8.1 (Berkeley) 5/31/93";
     36  1.3      cgd #else
     37  1.7      agc __RCSID("$NetBSD: table.c,v 1.7 2003/08/07 09:36:57 agc 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  hubertf const char   *const help2[] = {
     44  1.1      cgd 	"   Enter moves as <s>-<f> or <s>/<r> where <s> is the starting",
     45  1.1      cgd 	"position, <f> is the finishing position, and <r> is the roll.",
     46  1.1      cgd 	"Remember, each die roll must be moved separately.",
     47  1.1      cgd 	0
     48  1.1      cgd };
     49  1.1      cgd 
     50  1.5    lukem struct state {
     51  1.5    lukem 	char    ch;
     52  1.5    lukem 	int     fcode;
     53  1.5    lukem 	int     newst;
     54  1.1      cgd };
     55  1.1      cgd 
     56  1.6  hubertf static const struct state atmata[] = {
     57  1.1      cgd 
     58  1.5    lukem 	{'R', 1, 0},	{'?', 7, 0},	{'Q', 0, -3},	{'B', 8, 25},
     59  1.5    lukem 	{'9', 2, 25},	{'8', 2, 25},	{'7', 2, 25},	{'6', 2, 25},
     60  1.5    lukem 	{'5', 2, 25},	{'4', 2, 25},	{'3', 2, 25},	{'2', 2, 19},
     61  1.5    lukem 	{'1', 2, 15},	{'0', 2, 25},	{'.', 0, 0},	{'9', 2, 25},
     62  1.5    lukem 	{'8', 2, 25},	{'7', 2, 25},	{'6', 2, 25},	{'5', 2, 25},
     63  1.5    lukem 
     64  1.5    lukem 	{'4', 2, 25},	{'3', 2, 25},	{'2', 2, 25},	{'1', 2, 25},
     65  1.5    lukem 	{'0', 2, 25},	{'/', 0, 32},	{'-', 0, 39},	{'.', 0, 0},
     66  1.5    lukem 	{'/', 5, 32},	{' ', 6, 3},	{',', 6, 3},	{'\n', 0, -1},
     67  1.5    lukem 	{'6', 3, 28},	{'5', 3, 28},	{'4', 3, 28},	{'3', 3, 28},
     68  1.5    lukem 	{'2', 3, 28},	{'1', 3, 28},	{'.', 0, 0},	{'H', 9, 61},
     69  1.5    lukem 
     70  1.5    lukem 	{'9', 4, 61},	{'8', 4, 61},	{'7', 4, 61},	{'6', 4, 61},
     71  1.5    lukem 	{'5', 4, 61},	{'4', 4, 61},	{'3', 4, 61},	{'2', 4, 53},
     72  1.5    lukem 	{'1', 4, 51},	{'0', 4, 61},	{'.', 0, 0},	{'9', 4, 61},
     73  1.5    lukem 	{'8', 4, 61},	{'7', 4, 61},	{'6', 4, 61},	{'5', 4, 61},
     74  1.5    lukem 	{'4', 4, 61},	{'3', 4, 61},	{'2', 4, 61},	{'1', 4, 61},
     75  1.1      cgd 
     76  1.5    lukem 	{'0', 4, 61},	{' ', 6, 3},	{',', 6, 3},	{'-', 5, 39},
     77  1.5    lukem 	{'\n', 0, -1},	{'.', 0, 0}
     78  1.1      cgd };
     79  1.1      cgd 
     80  1.5    lukem int
     81  1.5    lukem checkmove(ist)
     82  1.5    lukem 	int     ist;
     83  1.1      cgd {
     84  1.5    lukem 	int     j, n;
     85  1.5    lukem 	char    c;
     86  1.1      cgd 
     87  1.1      cgd domove:
     88  1.5    lukem 	if (ist == 0) {
     89  1.1      cgd 		if (tflag)
     90  1.5    lukem 			curmove(curr, 32);
     91  1.1      cgd 		else
     92  1.5    lukem 			writel("\t\t");
     93  1.5    lukem 		writel("Move:  ");
     94  1.1      cgd 	}
     95  1.1      cgd 	ist = mvl = ncin = 0;
     96  1.1      cgd 	for (j = 0; j < 5; j++)
     97  1.1      cgd 		p[j] = g[j] = -1;
     98  1.1      cgd 
     99  1.1      cgd dochar:
    100  1.1      cgd 	c = readc();
    101  1.1      cgd 
    102  1.5    lukem 	if (c == 'S') {
    103  1.1      cgd 		raflag = 0;
    104  1.5    lukem 		save(1);
    105  1.5    lukem 		if (tflag) {
    106  1.5    lukem 			curmove(cturn == -1 ? 18 : 19, 39);
    107  1.1      cgd 			ist = -1;
    108  1.1      cgd 			goto domove;
    109  1.5    lukem 		} else {
    110  1.5    lukem 			proll();
    111  1.1      cgd 			ist = 0;
    112  1.1      cgd 			goto domove;
    113  1.1      cgd 		}
    114  1.1      cgd 	}
    115  1.5    lukem 	if (c == old.c_cc[VERASE] && ncin > 0) {
    116  1.1      cgd 		if (tflag)
    117  1.5    lukem 			curmove(curr, curc - 1);
    118  1.5    lukem 		else {
    119  1.4  mycroft 			if (old.c_cc[VERASE] == '\010')
    120  1.5    lukem 				writel("\010 \010");
    121  1.1      cgd 			else
    122  1.5    lukem 				writec(cin[ncin - 1]);
    123  1.1      cgd 		}
    124  1.1      cgd 		ncin--;
    125  1.1      cgd 		n = rsetbrd();
    126  1.5    lukem 		if (n == 0) {
    127  1.1      cgd 			n = -1;
    128  1.1      cgd 			if (tflag)
    129  1.1      cgd 				refresh();
    130  1.1      cgd 		}
    131  1.1      cgd 		if ((ist = n) > 0)
    132  1.1      cgd 			goto dochar;
    133  1.1      cgd 		goto domove;
    134  1.1      cgd 	}
    135  1.5    lukem 	if (c == old.c_cc[VKILL] && ncin > 0) {
    136  1.5    lukem 		if (tflag) {
    137  1.1      cgd 			refresh();
    138  1.5    lukem 			curmove(curr, 39);
    139  1.1      cgd 			ist = -1;
    140  1.1      cgd 			goto domove;
    141  1.5    lukem 		} else
    142  1.5    lukem 			if (old.c_cc[VERASE] == '\010') {
    143  1.5    lukem 				for (j = 0; j < ncin; j++)
    144  1.5    lukem 					writel("\010 \010");
    145  1.5    lukem 				ist = -1;
    146  1.5    lukem 				goto domove;
    147  1.5    lukem 			} else {
    148  1.5    lukem 				writec('\\');
    149  1.5    lukem 				writec('\n');
    150  1.5    lukem 				proll();
    151  1.5    lukem 				ist = 0;
    152  1.5    lukem 				goto domove;
    153  1.5    lukem 			}
    154  1.1      cgd 	}
    155  1.5    lukem 	n = dotable(c, ist);
    156  1.5    lukem 	if (n >= 0) {
    157  1.1      cgd 		cin[ncin++] = c;
    158  1.1      cgd 		if (n > 2)
    159  1.5    lukem 			if ((!tflag) || c != '\n')
    160  1.5    lukem 				writec(c);
    161  1.1      cgd 		ist = n;
    162  1.1      cgd 		if (n)
    163  1.1      cgd 			goto dochar;
    164  1.1      cgd 		else
    165  1.1      cgd 			goto domove;
    166  1.1      cgd 	}
    167  1.1      cgd 	if (n == -1 && mvl >= mvlim)
    168  1.5    lukem 		return (0);
    169  1.5    lukem 	if (n == -1 && mvl < mvlim - 1)
    170  1.5    lukem 		return (-4);
    171  1.5    lukem 
    172  1.5    lukem 	if (n == -6) {
    173  1.5    lukem 		if (!tflag) {
    174  1.5    lukem 			if (movokay(mvl + 1)) {
    175  1.1      cgd 				wrboard();
    176  1.5    lukem 				movback(mvl + 1);
    177  1.1      cgd 			}
    178  1.5    lukem 			proll();
    179  1.5    lukem 			writel("\t\tMove:  ");
    180  1.1      cgd 			for (j = 0; j < ncin;)
    181  1.5    lukem 				writec(cin[j++]);
    182  1.5    lukem 		} else {
    183  1.5    lukem 			if (movokay(mvl + 1)) {
    184  1.1      cgd 				refresh();
    185  1.5    lukem 				movback(mvl + 1);
    186  1.1      cgd 			} else
    187  1.5    lukem 				curmove(cturn == -1 ? 18 : 19, ncin + 39);
    188  1.1      cgd 		}
    189  1.1      cgd 		ist = n = rsetbrd();
    190  1.1      cgd 		goto dochar;
    191  1.1      cgd 	}
    192  1.1      cgd 	if (n != -5)
    193  1.5    lukem 		return (n);
    194  1.5    lukem 	writec('\007');
    195  1.1      cgd 	goto dochar;
    196  1.1      cgd }
    197  1.1      cgd 
    198  1.5    lukem int
    199  1.5    lukem dotable(c, i)
    200  1.5    lukem 	char    c;
    201  1.5    lukem 	int     i;
    202  1.1      cgd {
    203  1.5    lukem 	int     a;
    204  1.5    lukem 	int     test;
    205  1.1      cgd 
    206  1.1      cgd 	test = (c == 'R');
    207  1.1      cgd 
    208  1.5    lukem 	while ((a = atmata[i].ch) != '.') {
    209  1.5    lukem 		if (a == c || (test && a == '\n')) {
    210  1.5    lukem 			switch (atmata[i].fcode) {
    211  1.1      cgd 
    212  1.1      cgd 			case 1:
    213  1.1      cgd 				wrboard();
    214  1.5    lukem 				if (tflag) {
    215  1.5    lukem 					curmove(cturn == -1 ? 18 : 19, 0);
    216  1.5    lukem 					proll();
    217  1.5    lukem 					writel("\t\t");
    218  1.1      cgd 				} else
    219  1.5    lukem 					proll();
    220  1.1      cgd 				break;
    221  1.1      cgd 
    222  1.1      cgd 			case 2:
    223  1.1      cgd 				if (p[mvl] == -1)
    224  1.5    lukem 					p[mvl] = c - '0';
    225  1.1      cgd 				else
    226  1.5    lukem 					p[mvl] = p[mvl] * 10 + c - '0';
    227  1.1      cgd 				break;
    228  1.1      cgd 
    229  1.1      cgd 			case 3:
    230  1.5    lukem 				if (g[mvl] != -1) {
    231  1.1      cgd 					if (mvl < mvlim)
    232  1.1      cgd 						mvl++;
    233  1.5    lukem 					p[mvl] = p[mvl - 1];
    234  1.1      cgd 				}
    235  1.5    lukem 				g[mvl] = p[mvl] + cturn * (c - '0');
    236  1.1      cgd 				if (g[mvl] < 0)
    237  1.1      cgd 					g[mvl] = 0;
    238  1.1      cgd 				if (g[mvl] > 25)
    239  1.1      cgd 					g[mvl] = 25;
    240  1.1      cgd 				break;
    241  1.1      cgd 
    242  1.1      cgd 			case 4:
    243  1.1      cgd 				if (g[mvl] == -1)
    244  1.5    lukem 					g[mvl] = c - '0';
    245  1.1      cgd 				else
    246  1.5    lukem 					g[mvl] = g[mvl] * 10 + c - '0';
    247  1.1      cgd 				break;
    248  1.1      cgd 
    249  1.1      cgd 			case 5:
    250  1.1      cgd 				if (mvl < mvlim)
    251  1.1      cgd 					mvl++;
    252  1.5    lukem 				p[mvl] = g[mvl - 1];
    253  1.1      cgd 				break;
    254  1.1      cgd 
    255  1.1      cgd 			case 6:
    256  1.1      cgd 				if (mvl < mvlim)
    257  1.1      cgd 					mvl++;
    258  1.1      cgd 				break;
    259  1.1      cgd 
    260  1.1      cgd 			case 7:
    261  1.1      cgd 				if (tflag)
    262  1.5    lukem 					curmove(20, 0);
    263  1.1      cgd 				else
    264  1.5    lukem 					writec('\n');
    265  1.5    lukem 				(void) text(help2);
    266  1.5    lukem 				if (tflag) {
    267  1.5    lukem 					curmove(cturn == -1 ? 18 : 19, 39);
    268  1.5    lukem 				} else {
    269  1.5    lukem 					writec('\n');
    270  1.1      cgd 					proll();
    271  1.5    lukem 					writel("\t\tMove:  ");
    272  1.1      cgd 				}
    273  1.1      cgd 				break;
    274  1.1      cgd 
    275  1.1      cgd 			case 8:
    276  1.1      cgd 				p[mvl] = bar;
    277  1.1      cgd 				break;
    278  1.1      cgd 
    279  1.1      cgd 			case 9:
    280  1.1      cgd 				g[mvl] = home;
    281  1.1      cgd 			}
    282  1.1      cgd 
    283  1.5    lukem 			if (!test || a != '\n')
    284  1.1      cgd 				return (atmata[i].newst);
    285  1.1      cgd 			else
    286  1.1      cgd 				return (-6);
    287  1.1      cgd 		}
    288  1.1      cgd 		i++;
    289  1.1      cgd 	}
    290  1.1      cgd 
    291  1.1      cgd 	return (-5);
    292  1.1      cgd }
    293  1.5    lukem 
    294  1.5    lukem int
    295  1.5    lukem rsetbrd()
    296  1.5    lukem {
    297  1.5    lukem 	int     i, j, n;
    298  1.1      cgd 
    299  1.1      cgd 	n = 0;
    300  1.1      cgd 	mvl = 0;
    301  1.1      cgd 	for (i = 0; i < 4; i++)
    302  1.1      cgd 		p[i] = g[i] = -1;
    303  1.1      cgd 	for (j = 0; j < ncin; j++)
    304  1.5    lukem 		n = dotable(cin[j], n);
    305  1.1      cgd 	return (n);
    306  1.1      cgd }
    307