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