table.c revision 1.6 1 1.6 hubertf /* $NetBSD: table.c,v 1.6 1999/02/10 12:29:48 hubertf 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.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.5 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.3 cgd #if 0
39 1.3 cgd static char sccsid[] = "@(#)table.c 8.1 (Berkeley) 5/31/93";
40 1.3 cgd #else
41 1.6 hubertf __RCSID("$NetBSD: table.c,v 1.6 1999/02/10 12:29:48 hubertf Exp $");
42 1.3 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include "back.h"
46 1.1 cgd
47 1.6 hubertf const char *const help2[] = {
48 1.1 cgd " Enter moves as <s>-<f> or <s>/<r> where <s> is the starting",
49 1.1 cgd "position, <f> is the finishing position, and <r> is the roll.",
50 1.1 cgd "Remember, each die roll must be moved separately.",
51 1.1 cgd 0
52 1.1 cgd };
53 1.1 cgd
54 1.5 lukem struct state {
55 1.5 lukem char ch;
56 1.5 lukem int fcode;
57 1.5 lukem int newst;
58 1.1 cgd };
59 1.1 cgd
60 1.6 hubertf static const struct state atmata[] = {
61 1.1 cgd
62 1.5 lukem {'R', 1, 0}, {'?', 7, 0}, {'Q', 0, -3}, {'B', 8, 25},
63 1.5 lukem {'9', 2, 25}, {'8', 2, 25}, {'7', 2, 25}, {'6', 2, 25},
64 1.5 lukem {'5', 2, 25}, {'4', 2, 25}, {'3', 2, 25}, {'2', 2, 19},
65 1.5 lukem {'1', 2, 15}, {'0', 2, 25}, {'.', 0, 0}, {'9', 2, 25},
66 1.5 lukem {'8', 2, 25}, {'7', 2, 25}, {'6', 2, 25}, {'5', 2, 25},
67 1.5 lukem
68 1.5 lukem {'4', 2, 25}, {'3', 2, 25}, {'2', 2, 25}, {'1', 2, 25},
69 1.5 lukem {'0', 2, 25}, {'/', 0, 32}, {'-', 0, 39}, {'.', 0, 0},
70 1.5 lukem {'/', 5, 32}, {' ', 6, 3}, {',', 6, 3}, {'\n', 0, -1},
71 1.5 lukem {'6', 3, 28}, {'5', 3, 28}, {'4', 3, 28}, {'3', 3, 28},
72 1.5 lukem {'2', 3, 28}, {'1', 3, 28}, {'.', 0, 0}, {'H', 9, 61},
73 1.5 lukem
74 1.5 lukem {'9', 4, 61}, {'8', 4, 61}, {'7', 4, 61}, {'6', 4, 61},
75 1.5 lukem {'5', 4, 61}, {'4', 4, 61}, {'3', 4, 61}, {'2', 4, 53},
76 1.5 lukem {'1', 4, 51}, {'0', 4, 61}, {'.', 0, 0}, {'9', 4, 61},
77 1.5 lukem {'8', 4, 61}, {'7', 4, 61}, {'6', 4, 61}, {'5', 4, 61},
78 1.5 lukem {'4', 4, 61}, {'3', 4, 61}, {'2', 4, 61}, {'1', 4, 61},
79 1.1 cgd
80 1.5 lukem {'0', 4, 61}, {' ', 6, 3}, {',', 6, 3}, {'-', 5, 39},
81 1.5 lukem {'\n', 0, -1}, {'.', 0, 0}
82 1.1 cgd };
83 1.1 cgd
84 1.5 lukem int
85 1.5 lukem checkmove(ist)
86 1.5 lukem int ist;
87 1.1 cgd {
88 1.5 lukem int j, n;
89 1.5 lukem char c;
90 1.1 cgd
91 1.1 cgd domove:
92 1.5 lukem if (ist == 0) {
93 1.1 cgd if (tflag)
94 1.5 lukem curmove(curr, 32);
95 1.1 cgd else
96 1.5 lukem writel("\t\t");
97 1.5 lukem writel("Move: ");
98 1.1 cgd }
99 1.1 cgd ist = mvl = ncin = 0;
100 1.1 cgd for (j = 0; j < 5; j++)
101 1.1 cgd p[j] = g[j] = -1;
102 1.1 cgd
103 1.1 cgd dochar:
104 1.1 cgd c = readc();
105 1.1 cgd
106 1.5 lukem if (c == 'S') {
107 1.1 cgd raflag = 0;
108 1.5 lukem save(1);
109 1.5 lukem if (tflag) {
110 1.5 lukem curmove(cturn == -1 ? 18 : 19, 39);
111 1.1 cgd ist = -1;
112 1.1 cgd goto domove;
113 1.5 lukem } else {
114 1.5 lukem proll();
115 1.1 cgd ist = 0;
116 1.1 cgd goto domove;
117 1.1 cgd }
118 1.1 cgd }
119 1.5 lukem if (c == old.c_cc[VERASE] && ncin > 0) {
120 1.1 cgd if (tflag)
121 1.5 lukem curmove(curr, curc - 1);
122 1.5 lukem else {
123 1.4 mycroft if (old.c_cc[VERASE] == '\010')
124 1.5 lukem writel("\010 \010");
125 1.1 cgd else
126 1.5 lukem writec(cin[ncin - 1]);
127 1.1 cgd }
128 1.1 cgd ncin--;
129 1.1 cgd n = rsetbrd();
130 1.5 lukem if (n == 0) {
131 1.1 cgd n = -1;
132 1.1 cgd if (tflag)
133 1.1 cgd refresh();
134 1.1 cgd }
135 1.1 cgd if ((ist = n) > 0)
136 1.1 cgd goto dochar;
137 1.1 cgd goto domove;
138 1.1 cgd }
139 1.5 lukem if (c == old.c_cc[VKILL] && ncin > 0) {
140 1.5 lukem if (tflag) {
141 1.1 cgd refresh();
142 1.5 lukem curmove(curr, 39);
143 1.1 cgd ist = -1;
144 1.1 cgd goto domove;
145 1.5 lukem } else
146 1.5 lukem if (old.c_cc[VERASE] == '\010') {
147 1.5 lukem for (j = 0; j < ncin; j++)
148 1.5 lukem writel("\010 \010");
149 1.5 lukem ist = -1;
150 1.5 lukem goto domove;
151 1.5 lukem } else {
152 1.5 lukem writec('\\');
153 1.5 lukem writec('\n');
154 1.5 lukem proll();
155 1.5 lukem ist = 0;
156 1.5 lukem goto domove;
157 1.5 lukem }
158 1.1 cgd }
159 1.5 lukem n = dotable(c, ist);
160 1.5 lukem if (n >= 0) {
161 1.1 cgd cin[ncin++] = c;
162 1.1 cgd if (n > 2)
163 1.5 lukem if ((!tflag) || c != '\n')
164 1.5 lukem writec(c);
165 1.1 cgd ist = n;
166 1.1 cgd if (n)
167 1.1 cgd goto dochar;
168 1.1 cgd else
169 1.1 cgd goto domove;
170 1.1 cgd }
171 1.1 cgd if (n == -1 && mvl >= mvlim)
172 1.5 lukem return (0);
173 1.5 lukem if (n == -1 && mvl < mvlim - 1)
174 1.5 lukem return (-4);
175 1.5 lukem
176 1.5 lukem if (n == -6) {
177 1.5 lukem if (!tflag) {
178 1.5 lukem if (movokay(mvl + 1)) {
179 1.1 cgd wrboard();
180 1.5 lukem movback(mvl + 1);
181 1.1 cgd }
182 1.5 lukem proll();
183 1.5 lukem writel("\t\tMove: ");
184 1.1 cgd for (j = 0; j < ncin;)
185 1.5 lukem writec(cin[j++]);
186 1.5 lukem } else {
187 1.5 lukem if (movokay(mvl + 1)) {
188 1.1 cgd refresh();
189 1.5 lukem movback(mvl + 1);
190 1.1 cgd } else
191 1.5 lukem curmove(cturn == -1 ? 18 : 19, ncin + 39);
192 1.1 cgd }
193 1.1 cgd ist = n = rsetbrd();
194 1.1 cgd goto dochar;
195 1.1 cgd }
196 1.1 cgd if (n != -5)
197 1.5 lukem return (n);
198 1.5 lukem writec('\007');
199 1.1 cgd goto dochar;
200 1.1 cgd }
201 1.1 cgd
202 1.5 lukem int
203 1.5 lukem dotable(c, i)
204 1.5 lukem char c;
205 1.5 lukem int i;
206 1.1 cgd {
207 1.5 lukem int a;
208 1.5 lukem int test;
209 1.1 cgd
210 1.1 cgd test = (c == 'R');
211 1.1 cgd
212 1.5 lukem while ((a = atmata[i].ch) != '.') {
213 1.5 lukem if (a == c || (test && a == '\n')) {
214 1.5 lukem switch (atmata[i].fcode) {
215 1.1 cgd
216 1.1 cgd case 1:
217 1.1 cgd wrboard();
218 1.5 lukem if (tflag) {
219 1.5 lukem curmove(cturn == -1 ? 18 : 19, 0);
220 1.5 lukem proll();
221 1.5 lukem writel("\t\t");
222 1.1 cgd } else
223 1.5 lukem proll();
224 1.1 cgd break;
225 1.1 cgd
226 1.1 cgd case 2:
227 1.1 cgd if (p[mvl] == -1)
228 1.5 lukem p[mvl] = c - '0';
229 1.1 cgd else
230 1.5 lukem p[mvl] = p[mvl] * 10 + c - '0';
231 1.1 cgd break;
232 1.1 cgd
233 1.1 cgd case 3:
234 1.5 lukem if (g[mvl] != -1) {
235 1.1 cgd if (mvl < mvlim)
236 1.1 cgd mvl++;
237 1.5 lukem p[mvl] = p[mvl - 1];
238 1.1 cgd }
239 1.5 lukem g[mvl] = p[mvl] + cturn * (c - '0');
240 1.1 cgd if (g[mvl] < 0)
241 1.1 cgd g[mvl] = 0;
242 1.1 cgd if (g[mvl] > 25)
243 1.1 cgd g[mvl] = 25;
244 1.1 cgd break;
245 1.1 cgd
246 1.1 cgd case 4:
247 1.1 cgd if (g[mvl] == -1)
248 1.5 lukem g[mvl] = c - '0';
249 1.1 cgd else
250 1.5 lukem g[mvl] = g[mvl] * 10 + c - '0';
251 1.1 cgd break;
252 1.1 cgd
253 1.1 cgd case 5:
254 1.1 cgd if (mvl < mvlim)
255 1.1 cgd mvl++;
256 1.5 lukem p[mvl] = g[mvl - 1];
257 1.1 cgd break;
258 1.1 cgd
259 1.1 cgd case 6:
260 1.1 cgd if (mvl < mvlim)
261 1.1 cgd mvl++;
262 1.1 cgd break;
263 1.1 cgd
264 1.1 cgd case 7:
265 1.1 cgd if (tflag)
266 1.5 lukem curmove(20, 0);
267 1.1 cgd else
268 1.5 lukem writec('\n');
269 1.5 lukem (void) text(help2);
270 1.5 lukem if (tflag) {
271 1.5 lukem curmove(cturn == -1 ? 18 : 19, 39);
272 1.5 lukem } else {
273 1.5 lukem writec('\n');
274 1.1 cgd proll();
275 1.5 lukem writel("\t\tMove: ");
276 1.1 cgd }
277 1.1 cgd break;
278 1.1 cgd
279 1.1 cgd case 8:
280 1.1 cgd p[mvl] = bar;
281 1.1 cgd break;
282 1.1 cgd
283 1.1 cgd case 9:
284 1.1 cgd g[mvl] = home;
285 1.1 cgd }
286 1.1 cgd
287 1.5 lukem if (!test || a != '\n')
288 1.1 cgd return (atmata[i].newst);
289 1.1 cgd else
290 1.1 cgd return (-6);
291 1.1 cgd }
292 1.1 cgd i++;
293 1.1 cgd }
294 1.1 cgd
295 1.1 cgd return (-5);
296 1.1 cgd }
297 1.5 lukem
298 1.5 lukem int
299 1.5 lukem rsetbrd()
300 1.5 lukem {
301 1.5 lukem int i, j, n;
302 1.1 cgd
303 1.1 cgd n = 0;
304 1.1 cgd mvl = 0;
305 1.1 cgd for (i = 0; i < 4; i++)
306 1.1 cgd p[i] = g[i] = -1;
307 1.1 cgd for (j = 0; j < ncin; j++)
308 1.5 lukem n = dotable(cin[j], n);
309 1.1 cgd return (n);
310 1.1 cgd }
311