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