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