1 /* $OpenBSD: table.c,v 1.9 2015/11/30 08:19:25 tb 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 #include <ctype.h>
33 #include "back.h"
34
35 const char *const help2[] = {
36 " Enter moves as <s>-<f> or <s>/<r> where <s> is the starting",
37 "position, <f> is the finishing position, and <r> is the roll.",
38 "Remember, each die roll must be moved separately.",
39 0
40 };
41
42 struct state {
43 char ch;
44 int fcode;
45 int newst;
46 };
47
48 static const struct state atmata[] = {
49
50 {'R', 1, 0}, {'?', 7, 0}, {'Q', 0, -3}, {'B', 8, 25},
51 {'9', 2, 25}, {'8', 2, 25}, {'7', 2, 25}, {'6', 2, 25},
52 {'5', 2, 25}, {'4', 2, 25}, {'3', 2, 25}, {'2', 2, 19},
53 {'1', 2, 15}, {'0', 2, 25}, {'.', 0, 0}, {'9', 2, 25},
54 {'8', 2, 25}, {'7', 2, 25}, {'6', 2, 25}, {'5', 2, 25},
55
56 {'4', 2, 25}, {'3', 2, 25}, {'2', 2, 25}, {'1', 2, 25},
57 {'0', 2, 25}, {'/', 0, 32}, {'-', 0, 39}, {'.', 0, 0},
58 {'/', 5, 32}, {' ', 6, 3}, {',', 6, 3}, {'\n', 0, -1},
59 {'6', 3, 28}, {'5', 3, 28}, {'4', 3, 28}, {'3', 3, 28},
60 {'2', 3, 28}, {'1', 3, 28}, {'.', 0, 0}, {'H', 9, 61},
61
62 {'9', 4, 61}, {'8', 4, 61}, {'7', 4, 61}, {'6', 4, 61},
63 {'5', 4, 61}, {'4', 4, 61}, {'3', 4, 61}, {'2', 4, 53},
64 {'1', 4, 51}, {'0', 4, 61}, {'.', 0, 0}, {'9', 4, 61},
65 {'8', 4, 61}, {'7', 4, 61}, {'6', 4, 61}, {'5', 4, 61},
66 {'4', 4, 61}, {'3', 4, 61}, {'2', 4, 61}, {'1', 4, 61},
67
68 {'0', 4, 61}, {' ', 6, 3}, {',', 6, 3}, {'-', 5, 39},
69 {'\n', 0, -1}, {'.', 0, 0}
70 };
71
72 int
checkmove(int ist)73 checkmove(int ist)
74 {
75 int curr, curc;
76 int j, n;
77 int c;
78
79 domove:
80 getyx(stdscr, curr, curc);
81 if (ist == 0)
82 mvprintw(curr, 32, "Move: ");
83 ist = mvl = ncin = 0;
84 for (j = 0; j < 5; j++)
85 p[j] = g[j] = -1;
86
87 dochar:
88 c = readc();
89
90 if (c == 'S') {
91 raflag = 0;
92 save(1);
93 move(cturn == -1 ? 18 : 19, 39);
94 ist = -1;
95 goto domove;
96 }
97 if ((c == KEY_BACKSPACE || c == 0177) && ncin > 0) {
98 getyx(stdscr, curr, curc);
99 move(curr, curc - 1);
100 ncin--;
101 n = rsetbrd();
102 if (n == 0) {
103 n = -1;
104 refresh();
105 }
106 if ((ist = n) > 0)
107 goto dochar;
108 getyx(stdscr, curr, curc);
109 move(curr, 39);
110 clrtoeol();
111 goto domove;
112 } else if (c == KEY_DL && ncin > 0) {
113 getyx(stdscr, curr, curc);
114 move(curr, 39);
115 clrtoeol();
116 ist = -1;
117 refresh();
118 goto domove;
119 }
120 if (!isascii(c) || (ncin >= CIN_SIZE - 1)) {
121 beep();
122 goto domove;
123 }
124 n = dotable(c, ist);
125 if (n >= 0) {
126 cin[ncin++] = c;
127 if (n > 2)
128 if (c != '\n')
129 addch(c);
130 ist = n;
131 if (n)
132 goto dochar;
133 else
134 goto domove;
135 }
136 if (n == -1 && mvl >= mvlim)
137 return(0);
138 if (n == -1 && mvl < mvlim-1)
139 return(-4);
140 if (n == -6) {
141 if (movokay(mvl + 1)) {
142 moveplayers();
143 movback(mvl + 1);
144 } else
145 move(cturn == -1 ? 18 : 19, ncin + 39);
146 ist = n = rsetbrd();
147 goto dochar;
148 }
149 if (n != -5)
150 return(n);
151 beep();
152 goto dochar;
153 }
154
155 int
dotable(char c,int i)156 dotable(char c, int i)
157 {
158 int a;
159 int test;
160
161 test = (c == 'R');
162
163 while ((a = atmata[i].ch) != '.') {
164 if (a == c || (test && a == '\n')) {
165 switch (atmata[i].fcode) {
166 case 1:
167 wrboard();
168 move(cturn == -1 ? 18 : 19, 0);
169 proll();
170 addstr("\t\t");
171 break;
172
173 case 2:
174 if (p[mvl] == -1)
175 p[mvl] = c - '0';
176 else
177 p[mvl] = p[mvl] * 10 + c - '0';
178 break;
179
180 case 3:
181 if (g[mvl] != -1) {
182 if (mvl < mvlim)
183 mvl++;
184 p[mvl] = p[mvl - 1];
185 }
186 g[mvl] = p[mvl] + cturn * (c - '0');
187 if (g[mvl] < 0)
188 g[mvl] = 0;
189 if (g[mvl] > 25)
190 g[mvl] = 25;
191 break;
192
193 case 4:
194 if (g[mvl] == -1)
195 g[mvl] = c - '0';
196 else
197 g[mvl] = g[mvl] * 10 + c - '0';
198 break;
199
200 case 5:
201 if (mvl < mvlim)
202 mvl++;
203 p[mvl] = g[mvl - 1];
204 break;
205
206 case 6:
207 if (mvl < mvlim)
208 mvl++;
209 break;
210
211 case 7:
212 move(20, 0);
213 text(help2);
214 move(cturn == -1 ? 18 : 19, 39);
215 break;
216
217 case 8:
218 p[mvl] = bar;
219 break;
220
221 case 9:
222 g[mvl] = home;
223 }
224
225 if (!test || a != '\n')
226 return(atmata[i].newst);
227 else
228 return(-6);
229 }
230 i++;
231 }
232 return (-5);
233 }
234
235 int
rsetbrd(void)236 rsetbrd(void)
237 {
238 int i, j, n;
239
240 n = 0;
241 mvl = 0;
242 for (i = 0; i < 4; i++)
243 p[i] = g[i] = -1;
244 for (j = 0; j < ncin; j++)
245 if ((n = dotable(cin[j], n)) < 0)
246 return (n);
247 return(n);
248 }
249