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