1*6767Srrh static char sccsid[] = "	table.c	1.1	82/05/11	";
2*6767Srrh 
3*6767Srrh #include "back.h"
4*6767Srrh 
5*6767Srrh char	*help2[] = {
6*6767Srrh 	"   Enter moves as <s>-<f> or <s>/<r> where <s> is the starting",
7*6767Srrh 	"position, <f> is the finishing position, and <r> is the roll.",
8*6767Srrh 	"Remember, each die roll must be moved separately.",
9*6767Srrh 	0
10*6767Srrh };
11*6767Srrh 
12*6767Srrh struct state	{
13*6767Srrh 	char	ch;
14*6767Srrh 	int	fcode;
15*6767Srrh 	int	newst;
16*6767Srrh };
17*6767Srrh 
18*6767Srrh struct state	atmata[] = {
19*6767Srrh 
20*6767Srrh 	'R', 1, 0,	'?', 7, 0,	'Q', 0, -3,	'B', 8, 25,
21*6767Srrh 	'9', 2, 25,	'8', 2, 25,	'7', 2, 25,	'6', 2, 25,
22*6767Srrh 	'5', 2, 25,	'4', 2, 25,	'3', 2, 25,	'2', 2, 19,
23*6767Srrh 	'1', 2, 15,	'0', 2, 25,	'.', 0, 0,	'9', 2, 25,
24*6767Srrh 	'8', 2, 25,	'7', 2, 25,	'6', 2, 25,	'5', 2, 25,
25*6767Srrh 
26*6767Srrh 	'4', 2, 25,	'3', 2, 25,	'2', 2, 25,	'1', 2, 25,
27*6767Srrh 	'0', 2, 25,	'/', 0, 32,	'-', 0, 39,	'.', 0, 0,
28*6767Srrh 	'/', 5, 32,	' ', 6, 3,	',', 6, 3,	'\n', 0, -1,
29*6767Srrh 	'6', 3, 28,	'5', 3, 28,	'4', 3, 28,	'3', 3, 28,
30*6767Srrh 	'2', 3, 28,	'1', 3, 28,	'.', 0, 0,	'H', 9, 61,
31*6767Srrh 
32*6767Srrh 	'9', 4, 61,	'8', 4, 61,	'7', 4, 61,	'6', 4, 61,
33*6767Srrh 	'5', 4, 61,	'4', 4, 61,	'3', 4, 61,	'2', 4, 53,
34*6767Srrh 	'1', 4, 51,	'0', 4, 61,	'.', 0, 0,	'9', 4, 61,
35*6767Srrh 	'8', 4, 61,	'7', 4, 61,	'6', 4, 61,	'5', 4, 61,
36*6767Srrh 	'4', 4, 61,	'3', 4, 61,	'2', 4, 61,	'1', 4, 61,
37*6767Srrh 
38*6767Srrh 	'0', 4, 61,	' ', 6, 3,	',', 6, 3,	'-', 5, 39,
39*6767Srrh 	'\n', 0, -1,	'.', 0, 0
40*6767Srrh };
41*6767Srrh 
42*6767Srrh checkmove (ist)
43*6767Srrh 
44*6767Srrh int	ist;
45*6767Srrh 
46*6767Srrh {
47*6767Srrh 	register int	j, n;
48*6767Srrh 	register char	c;
49*6767Srrh 	char		a;
50*6767Srrh 
51*6767Srrh domove:
52*6767Srrh 	if (ist == 0)  {
53*6767Srrh 		if (tflag)
54*6767Srrh 			curmove (curr,32);
55*6767Srrh 		else
56*6767Srrh 			writel ("\t\t");
57*6767Srrh 		writel ("Move:  ");
58*6767Srrh 	}
59*6767Srrh 	ist = mvl = ncin = 0;
60*6767Srrh 	for (j = 0; j < 5; j++)
61*6767Srrh 		p[j] = g[j] = -1;
62*6767Srrh 
63*6767Srrh dochar:
64*6767Srrh 	c = readc();
65*6767Srrh 
66*6767Srrh 	if (c == 'S')  {
67*6767Srrh 		raflag = 0;
68*6767Srrh 		save (1);
69*6767Srrh 		if (tflag)  {
70*6767Srrh 			curmove (cturn == -1? 18: 19,39);
71*6767Srrh 			ist = -1;
72*6767Srrh 			goto domove;
73*6767Srrh 		} else  {
74*6767Srrh 			proll ();
75*6767Srrh 			ist = 0;
76*6767Srrh 			goto domove;
77*6767Srrh 		}
78*6767Srrh 	}
79*6767Srrh 
80*6767Srrh 	if (c == tty.sg_erase && ncin > 0)  {
81*6767Srrh 		if (tflag)
82*6767Srrh 			curmove (curr,curc-1);
83*6767Srrh 		else  {
84*6767Srrh 			if (tty.sg_erase == '\010')
85*6767Srrh 				writel ("\010 \010");
86*6767Srrh 			else
87*6767Srrh 				writec (cin[ncin-1]);
88*6767Srrh 		}
89*6767Srrh 		ncin--;
90*6767Srrh 		n = rsetbrd();
91*6767Srrh 		if (n == 0)  {
92*6767Srrh 			n = -1;
93*6767Srrh 			if (tflag)
94*6767Srrh 				refresh();
95*6767Srrh 		}
96*6767Srrh 		if ((ist = n) > 0)
97*6767Srrh 			goto dochar;
98*6767Srrh 		goto domove;
99*6767Srrh 	}
100*6767Srrh 
101*6767Srrh 	if (c == tty.sg_kill && ncin > 0)  {
102*6767Srrh 		if (tflag)  {
103*6767Srrh 			refresh();
104*6767Srrh 			curmove (curr,39);
105*6767Srrh 			ist = -1;
106*6767Srrh 			goto domove;
107*6767Srrh 		} else  if (tty.sg_erase == '\010')  {
108*6767Srrh 			for (j = 0; j < ncin; j++)
109*6767Srrh 				writel ("\010 \010");
110*6767Srrh 			ist = -1;
111*6767Srrh 			goto domove;
112*6767Srrh 		} else  {
113*6767Srrh 			writec ('\\');
114*6767Srrh 			writec ('\n');
115*6767Srrh 			proll ();
116*6767Srrh 			ist = 0;
117*6767Srrh 			goto domove;
118*6767Srrh 		}
119*6767Srrh 	}
120*6767Srrh 
121*6767Srrh 	n = dotable(c,ist);
122*6767Srrh 	if (n >= 0)  {
123*6767Srrh 		cin[ncin++] = c;
124*6767Srrh 		if (n > 2)
125*6767Srrh 		if ((! tflag) || c != '\n')
126*6767Srrh 			writec (c);
127*6767Srrh 		ist = n;
128*6767Srrh 		if (n)
129*6767Srrh 			goto dochar;
130*6767Srrh 		else
131*6767Srrh 			goto domove;
132*6767Srrh 	}
133*6767Srrh 
134*6767Srrh 	if (n == -1 && mvl >= mvlim)
135*6767Srrh 		return(0);
136*6767Srrh 	if (n == -1 && mvl < mvlim-1)
137*6767Srrh 		return(-4);
138*6767Srrh 
139*6767Srrh 	if (n == -6)  {
140*6767Srrh 		if (! tflag)  {
141*6767Srrh 			if (movokay(mvl+1))  {
142*6767Srrh 				wrboard();
143*6767Srrh 				movback (mvl+1);
144*6767Srrh 			}
145*6767Srrh 			proll ();
146*6767Srrh 			writel ("\t\tMove:  ");
147*6767Srrh 			for (j = 0; j < ncin;)
148*6767Srrh 				writec (cin[j++]);
149*6767Srrh 		} else  {
150*6767Srrh 			if (movokay(mvl+1))  {
151*6767Srrh 				refresh();
152*6767Srrh 				movback (mvl+1);
153*6767Srrh 			} else
154*6767Srrh 				curmove (cturn == -1? 18:19,ncin+39);
155*6767Srrh 		}
156*6767Srrh 		ist = n = rsetbrd();
157*6767Srrh 		goto dochar;
158*6767Srrh 	}
159*6767Srrh 
160*6767Srrh 	if (n != -5)
161*6767Srrh 		return(n);
162*6767Srrh 	writec ('\007');
163*6767Srrh 	goto dochar;
164*6767Srrh }
165*6767Srrh 
166*6767Srrh dotable (c,i)
167*6767Srrh char		c;
168*6767Srrh register int	i;
169*6767Srrh 
170*6767Srrh {
171*6767Srrh 	register int	a, j;
172*6767Srrh 	int		test;
173*6767Srrh 
174*6767Srrh 	test = (c == 'R');
175*6767Srrh 
176*6767Srrh 	while ( (a = atmata[i].ch) != '.')  {
177*6767Srrh 		if (a == c || (test && a == '\n'))  {
178*6767Srrh 			switch  (atmata[i].fcode)  {
179*6767Srrh 
180*6767Srrh 			case 1:
181*6767Srrh 				wrboard();
182*6767Srrh 				if (tflag)  {
183*6767Srrh 					curmove (cturn == -1? 18: 19,0);
184*6767Srrh 					proll ();
185*6767Srrh 					writel ("\t\t");
186*6767Srrh 				} else
187*6767Srrh 					proll ();
188*6767Srrh 				break;
189*6767Srrh 
190*6767Srrh 			case 2:
191*6767Srrh 				if (p[mvl] == -1)
192*6767Srrh 					p[mvl] = c-'0';
193*6767Srrh 				else
194*6767Srrh 					p[mvl] = p[mvl]*10+c-'0';
195*6767Srrh 				break;
196*6767Srrh 
197*6767Srrh 			case 3:
198*6767Srrh 				if (g[mvl] != -1)  {
199*6767Srrh 					if (mvl < mvlim)
200*6767Srrh 						mvl++;
201*6767Srrh 					p[mvl] = p[mvl-1];
202*6767Srrh 				}
203*6767Srrh 				g[mvl] = p[mvl]+cturn*(c-'0');
204*6767Srrh 				if (g[mvl] < 0)
205*6767Srrh 					g[mvl] = 0;
206*6767Srrh 				if (g[mvl] > 25)
207*6767Srrh 					g[mvl] = 25;
208*6767Srrh 				break;
209*6767Srrh 
210*6767Srrh 			case 4:
211*6767Srrh 				if (g[mvl] == -1)
212*6767Srrh 					g[mvl] = c-'0';
213*6767Srrh 				else
214*6767Srrh 					g[mvl] = g[mvl]*10+c-'0';
215*6767Srrh 				break;
216*6767Srrh 
217*6767Srrh 			case 5:
218*6767Srrh 				if (mvl < mvlim)
219*6767Srrh 					mvl++;
220*6767Srrh 				p[mvl] = g[mvl-1];
221*6767Srrh 				break;
222*6767Srrh 
223*6767Srrh 			case 6:
224*6767Srrh 				if (mvl < mvlim)
225*6767Srrh 					mvl++;
226*6767Srrh 				break;
227*6767Srrh 
228*6767Srrh 			case 7:
229*6767Srrh 				if (tflag)
230*6767Srrh 					curmove (20,0);
231*6767Srrh 				else
232*6767Srrh 					writec ('\n');
233*6767Srrh 				text (help2);
234*6767Srrh 				if (tflag)  {
235*6767Srrh 					curmove (cturn == -1? 18: 19,39);
236*6767Srrh 				} else  {
237*6767Srrh 					writec ('\n');
238*6767Srrh 					proll();
239*6767Srrh 					writel ("\t\tMove:  ");
240*6767Srrh 				}
241*6767Srrh 				break;
242*6767Srrh 
243*6767Srrh 			case 8:
244*6767Srrh 				p[mvl] = bar;
245*6767Srrh 				break;
246*6767Srrh 
247*6767Srrh 			case 9:
248*6767Srrh 				g[mvl] = home;
249*6767Srrh 			}
250*6767Srrh 
251*6767Srrh 			if (! test || a != '\n')
252*6767Srrh 				return (atmata[i].newst);
253*6767Srrh 			else
254*6767Srrh 				return (-6);
255*6767Srrh 		}
256*6767Srrh 
257*6767Srrh 		i++;
258*6767Srrh 	}
259*6767Srrh 
260*6767Srrh 	return (-5);
261*6767Srrh }
262*6767Srrh 
263*6767Srrh rsetbrd ()  {
264*6767Srrh 	register int	i, j, n;
265*6767Srrh 
266*6767Srrh 	n = 0;
267*6767Srrh 	mvl = 0;
268*6767Srrh 	for (i = 0; i < 4; i++)
269*6767Srrh 		p[i] = g[i] = -1;
270*6767Srrh 	for (j = 0; j < ncin; j++)
271*6767Srrh 		n = dotable (cin[j],n);
272*6767Srrh 	return (n);
273*6767Srrh }
274