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