1 /*	$OpenBSD: table.c,v 1.6 2003/06/03 03:01:38 millert Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 #if 0
34 static char sccsid[] = "@(#)table.c	8.1 (Berkeley) 5/31/93";
35 #else
36 static char rcsid[] = "$OpenBSD: table.c,v 1.6 2003/06/03 03:01:38 millert Exp $";
37 #endif
38 #endif /* not lint */
39 
40 #include <ctype.h>
41 #include "back.h"
42 
43 const char   *const help2[] = {
44 	"   Enter moves as <s>-<f> or <s>/<r> where <s> is the starting",
45 	"position, <f> is the finishing position, and <r> is the roll.",
46 	"Remember, each die roll must be moved separately.",
47 	0
48 };
49 
50 struct state {
51 	char    ch;
52 	int     fcode;
53 	int     newst;
54 };
55 
56 static const struct state atmata[] = {
57 
58 	{'R', 1, 0},	{'?', 7, 0},	{'Q', 0, -3},	{'B', 8, 25},
59 	{'9', 2, 25},	{'8', 2, 25},	{'7', 2, 25},	{'6', 2, 25},
60 	{'5', 2, 25},	{'4', 2, 25},	{'3', 2, 25},	{'2', 2, 19},
61 	{'1', 2, 15},	{'0', 2, 25},	{'.', 0, 0},	{'9', 2, 25},
62 	{'8', 2, 25},	{'7', 2, 25},	{'6', 2, 25},	{'5', 2, 25},
63 
64 	{'4', 2, 25},	{'3', 2, 25},	{'2', 2, 25},	{'1', 2, 25},
65 	{'0', 2, 25},	{'/', 0, 32},	{'-', 0, 39},	{'.', 0, 0},
66 	{'/', 5, 32},	{' ', 6, 3},	{',', 6, 3},	{'\n', 0, -1},
67 	{'6', 3, 28},	{'5', 3, 28},	{'4', 3, 28},	{'3', 3, 28},
68 	{'2', 3, 28},	{'1', 3, 28},	{'.', 0, 0},	{'H', 9, 61},
69 
70 	{'9', 4, 61},	{'8', 4, 61},	{'7', 4, 61},	{'6', 4, 61},
71 	{'5', 4, 61},	{'4', 4, 61},	{'3', 4, 61},	{'2', 4, 53},
72 	{'1', 4, 51},	{'0', 4, 61},	{'.', 0, 0},	{'9', 4, 61},
73 	{'8', 4, 61},	{'7', 4, 61},	{'6', 4, 61},	{'5', 4, 61},
74 	{'4', 4, 61},	{'3', 4, 61},	{'2', 4, 61},	{'1', 4, 61},
75 
76 	{'0', 4, 61},	{' ', 6, 3},	{',', 6, 3},	{'-', 5, 39},
77 	{'\n', 0, -1},	{'.', 0, 0}
78 };
79 
80 int
81 checkmove(ist)
82 	int     ist;
83 {
84 	int     curr, curc;
85 	int     j, n;
86 	int    c;
87 
88 domove:
89 	getyx(stdscr, curr, curc);
90 	if (ist == 0)
91 		mvprintw(curr, 32, "Move:  ");
92 	ist = mvl = ncin = 0;
93 	for (j = 0; j < 5; j++)
94 		p[j] = g[j] = -1;
95 
96 dochar:
97 	c = readc();
98 
99 	if (c == 'S') {
100 		raflag = 0;
101 		save(1);
102 		move(cturn == -1 ? 18 : 19, 39);
103 		ist = -1;
104 		goto domove;
105 	}
106 	if ((c == KEY_BACKSPACE || c == 0177) && ncin > 0) {
107 		getyx(stdscr, curr, curc);
108 		move(curr, curc - 1);
109 		ncin--;
110 		n = rsetbrd();
111 		if (n == 0) {
112 			n = -1;
113 			refresh();
114 		}
115 		if ((ist = n) > 0)
116 			goto dochar;
117 		getyx(stdscr, curr, curc);
118 		move(curr, 39);
119 		clrtoeol();
120 		goto domove;
121 	} else if (c == KEY_DL && ncin > 0) {
122 		getyx(stdscr, curr, curc);
123 		move(curr, 39);
124 		clrtoeol();
125 		ist = -1;
126 		refresh();
127 		goto domove;
128 	}
129 	if (!isascii(c) || (ncin >= CIN_SIZE - 1)) {
130 		beep();
131 		goto domove;
132 	}
133 	n = dotable(c, ist);
134 	if (n >= 0) {
135 		cin[ncin++] = c;
136 		if (n > 2)
137 			if (c != '\n')
138 				addch(c);
139 		ist = n;
140 		if (n)
141 			goto dochar;
142 		else
143 			goto domove;
144 	}
145 	if (n == -1 && mvl >= mvlim)
146 		return(0);
147 	if (n == -1 && mvl < mvlim-1)
148 		return(-4);
149 	if (n == -6) {
150 		if (movokay(mvl + 1)) {
151 			moveplayers();
152 			movback(mvl + 1);
153 		} else
154 			move(cturn == -1 ? 18 : 19, ncin + 39);
155 		ist = n = rsetbrd();
156 		goto dochar;
157 	}
158 	if (n != -5)
159 		return(n);
160 	beep();
161 	goto dochar;
162 }
163 
164 int
165 dotable(c, i)
166 	char    c;
167 	int     i;
168 {
169 	int     a;
170 	int     test;
171 
172 	test = (c == 'R');
173 
174 	while ((a = atmata[i].ch) != '.') {
175 		if (a == c || (test && a == '\n')) {
176 			switch (atmata[i].fcode) {
177 			case 1:
178 				wrboard();
179 				move(cturn == -1 ? 18 : 19, 0);
180 				proll();
181 				addstr("\t\t");
182 				break;
183 
184 			case 2:
185 				if (p[mvl] == -1)
186 					p[mvl] = c - '0';
187 				else
188 					p[mvl] = p[mvl] * 10 + c - '0';
189 				break;
190 
191 			case 3:
192 				if (g[mvl] != -1) {
193 					if (mvl < mvlim)
194 						mvl++;
195 					p[mvl] = p[mvl - 1];
196 				}
197 				g[mvl] = p[mvl] + cturn * (c - '0');
198 				if (g[mvl] < 0)
199 					g[mvl] = 0;
200 				if (g[mvl] > 25)
201 					g[mvl] = 25;
202 				break;
203 
204 			case 4:
205 				if (g[mvl] == -1)
206 					g[mvl] = c - '0';
207 				else
208 					g[mvl] = g[mvl] * 10 + c - '0';
209 				break;
210 
211 			case 5:
212 				if (mvl < mvlim)
213 					mvl++;
214 				p[mvl] = g[mvl - 1];
215 				break;
216 
217 			case 6:
218 				if (mvl < mvlim)
219 					mvl++;
220 				break;
221 
222 			case 7:
223 				move(20, 0);
224 				text(help2);
225 				move(cturn == -1 ? 18 : 19, 39);
226 				break;
227 
228 			case 8:
229 				p[mvl] = bar;
230 				break;
231 
232 			case 9:
233 				g[mvl] = home;
234 			}
235 
236 			if (!test || a != '\n')
237 				return(atmata[i].newst);
238 			else
239 				return(-6);
240 		}
241 		i++;
242 	}
243 	return (-5);
244 }
245 
246 int
247 rsetbrd()
248 {
249 	int     i, j, n;
250 
251 	n = 0;
252 	mvl = 0;
253 	for (i = 0; i < 4; i++)
254 		p[i] = g[i] = -1;
255 	for (j = 0; j < ncin; j++)
256 		n = dotable(cin[j], n);
257 	return(n);
258 }
259