pickmove.c revision 1.2 1 1.2 tls /* $NetBSD: pickmove.c,v 1.2 1996/12/28 18:57:04 tls Exp $
2 1.2 tls */
3 1.1 tls /*
4 1.1 tls * Copyright (c) 1994
5 1.1 tls * The Regents of the University of California. All rights reserved.
6 1.1 tls *
7 1.1 tls * This code is derived from software contributed to Berkeley by
8 1.1 tls * Ralph Campbell.
9 1.1 tls *
10 1.1 tls * Redistribution and use in source and binary forms, with or without
11 1.1 tls * modification, are permitted provided that the following conditions
12 1.1 tls * are met:
13 1.1 tls * 1. Redistributions of source code must retain the above copyright
14 1.1 tls * notice, this list of conditions and the following disclaimer.
15 1.1 tls * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tls * notice, this list of conditions and the following disclaimer in the
17 1.1 tls * documentation and/or other materials provided with the distribution.
18 1.1 tls * 3. All advertising materials mentioning features or use of this software
19 1.1 tls * must display the following acknowledgement:
20 1.1 tls * This product includes software developed by the University of
21 1.1 tls * California, Berkeley and its contributors.
22 1.1 tls * 4. Neither the name of the University nor the names of its contributors
23 1.1 tls * may be used to endorse or promote products derived from this software
24 1.1 tls * without specific prior written permission.
25 1.1 tls *
26 1.1 tls * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 tls * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 tls * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 tls * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 tls * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 tls * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 tls * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 tls * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 tls * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 tls * SUCH DAMAGE.
37 1.1 tls */
38 1.1 tls
39 1.1 tls #ifndef lint
40 1.2 tls #if 0
41 1.1 tls static char sccsid[] = "@(#)pickmove.c 8.2 (Berkeley) 5/3/95";
42 1.2 tls #else
43 1.2 tls static char rcsid[] = "$NetBSD: pickmove.c,v 1.2 1996/12/28 18:57:04 tls Exp $";
44 1.2 tls #endif
45 1.1 tls #endif /* not lint */
46 1.1 tls
47 1.1 tls #include <stdio.h>
48 1.1 tls #include <curses.h>
49 1.1 tls #include <machine/limits.h>
50 1.1 tls
51 1.1 tls #include "gomoku.h"
52 1.1 tls
53 1.1 tls #define BITS_PER_INT (sizeof(int) * CHAR_BIT)
54 1.1 tls #define MAPSZ (BAREA / BITS_PER_INT)
55 1.1 tls
56 1.1 tls #define BIT_SET(a, b) ((a)[(b)/BITS_PER_INT] |= (1 << ((b) % BITS_PER_INT)))
57 1.1 tls #define BIT_CLR(a, b) ((a)[(b)/BITS_PER_INT] &= ~(1 << ((b) % BITS_PER_INT)))
58 1.1 tls #define BIT_TEST(a, b) ((a)[(b)/BITS_PER_INT] & (1 << ((b) % BITS_PER_INT)))
59 1.1 tls
60 1.1 tls struct combostr *hashcombos[FAREA]; /* hash list for finding duplicates */
61 1.1 tls struct combostr *sortcombos; /* combos at higher levels */
62 1.1 tls int combolen; /* number of combos in sortcombos */
63 1.1 tls int nextcolor; /* color of next move */
64 1.1 tls int elistcnt; /* count of struct elist allocated */
65 1.1 tls int combocnt; /* count of struct combostr allocated */
66 1.1 tls int forcemap[MAPSZ]; /* map for blocking <1,x> combos */
67 1.1 tls int tmpmap[MAPSZ]; /* map for blocking <1,x> combos */
68 1.1 tls int nforce; /* count of opponent <1,x> combos */
69 1.1 tls
70 1.1 tls pickmove(us)
71 1.1 tls int us;
72 1.1 tls {
73 1.1 tls register struct spotstr *sp, *sp1, *sp2;
74 1.1 tls register union comboval *Ocp, *Tcp;
75 1.1 tls char *str;
76 1.1 tls int i, j, m;
77 1.1 tls
78 1.1 tls /* first move is easy */
79 1.1 tls if (movenum == 1)
80 1.1 tls return (PT(K,10));
81 1.1 tls
82 1.1 tls /* initialize all the board values */
83 1.1 tls for (sp = &board[PT(T,20)]; --sp >= &board[PT(A,1)]; ) {
84 1.1 tls sp->s_combo[BLACK].s = MAXCOMBO + 1;
85 1.1 tls sp->s_combo[WHITE].s = MAXCOMBO + 1;
86 1.1 tls sp->s_level[BLACK] = 255;
87 1.1 tls sp->s_level[WHITE] = 255;
88 1.1 tls sp->s_nforce[BLACK] = 0;
89 1.1 tls sp->s_nforce[WHITE] = 0;
90 1.1 tls sp->s_flg &= ~(FFLAGALL | MFLAGALL);
91 1.1 tls }
92 1.1 tls nforce = 0;
93 1.1 tls memset(forcemap, 0, sizeof(forcemap));
94 1.1 tls
95 1.1 tls /* compute new values */
96 1.1 tls nextcolor = us;
97 1.1 tls scanframes(BLACK);
98 1.1 tls scanframes(WHITE);
99 1.1 tls
100 1.1 tls /* find the spot with the highest value */
101 1.1 tls for (sp = sp1 = sp2 = &board[PT(T,19)]; --sp >= &board[PT(A,1)]; ) {
102 1.1 tls if (sp->s_occ != EMPTY)
103 1.1 tls continue;
104 1.1 tls if (debug && (sp->s_combo[BLACK].c.a == 1 ||
105 1.1 tls sp->s_combo[WHITE].c.a == 1)) {
106 1.1 tls sprintf(fmtbuf, "- %s %x/%d %d %x/%d %d %d", stoc(sp - board),
107 1.1 tls sp->s_combo[BLACK].s, sp->s_level[BLACK],
108 1.1 tls sp->s_nforce[BLACK],
109 1.1 tls sp->s_combo[WHITE].s, sp->s_level[WHITE],
110 1.1 tls sp->s_nforce[WHITE],
111 1.1 tls sp->s_wval);
112 1.1 tls dlog(fmtbuf);
113 1.1 tls }
114 1.1 tls /* pick the best black move */
115 1.1 tls if (better(sp, sp1, BLACK))
116 1.1 tls sp1 = sp;
117 1.1 tls /* pick the best white move */
118 1.1 tls if (better(sp, sp2, WHITE))
119 1.1 tls sp2 = sp;
120 1.1 tls }
121 1.1 tls
122 1.1 tls if (debug) {
123 1.1 tls sprintf(fmtbuf, "B %s %x/%d %d %x/%d %d %d %d",
124 1.1 tls stoc(sp1 - board),
125 1.1 tls sp1->s_combo[BLACK].s, sp1->s_level[BLACK],
126 1.1 tls sp1->s_nforce[BLACK],
127 1.1 tls sp1->s_combo[WHITE].s, sp1->s_level[WHITE],
128 1.1 tls sp1->s_nforce[WHITE], sp1->s_wval);
129 1.1 tls dlog(fmtbuf);
130 1.1 tls sprintf(fmtbuf, "W %s %x/%d %d %x/%d %d %d %d",
131 1.1 tls stoc(sp2 - board),
132 1.1 tls sp2->s_combo[WHITE].s, sp2->s_level[WHITE],
133 1.1 tls sp2->s_nforce[WHITE],
134 1.1 tls sp2->s_combo[BLACK].s, sp2->s_level[BLACK],
135 1.1 tls sp2->s_nforce[BLACK], sp2->s_wval);
136 1.1 tls dlog(fmtbuf);
137 1.1 tls /*
138 1.1 tls * Check for more than one force that can't
139 1.1 tls * all be blocked with one move.
140 1.1 tls */
141 1.1 tls sp = (us == BLACK) ? sp2 : sp1;
142 1.1 tls m = sp - board;
143 1.1 tls if (sp->s_combo[!us].c.a == 1 && !BIT_TEST(forcemap, m))
144 1.1 tls dlog("*** Can't be blocked");
145 1.1 tls }
146 1.1 tls if (us == BLACK) {
147 1.1 tls Ocp = &sp1->s_combo[BLACK];
148 1.1 tls Tcp = &sp2->s_combo[WHITE];
149 1.1 tls } else {
150 1.1 tls Tcp = &sp1->s_combo[BLACK];
151 1.1 tls Ocp = &sp2->s_combo[WHITE];
152 1.1 tls sp = sp1;
153 1.1 tls sp1 = sp2;
154 1.1 tls sp2 = sp;
155 1.1 tls }
156 1.1 tls /*
157 1.1 tls * Block their combo only if we have to (i.e., if they are one move
158 1.1 tls * away from completing a force and we don't have a force that
159 1.1 tls * we can complete which takes fewer moves to win).
160 1.1 tls */
161 1.1 tls if (Tcp->c.a <= 1 && (Ocp->c.a > 1 ||
162 1.1 tls Tcp->c.a + Tcp->c.b < Ocp->c.a + Ocp->c.b))
163 1.1 tls return (sp2 - board);
164 1.1 tls return (sp1 - board);
165 1.1 tls }
166 1.1 tls
167 1.1 tls /*
168 1.1 tls * Return true if spot 'sp' is better than spot 'sp1' for color 'us'.
169 1.1 tls */
170 1.1 tls better(sp, sp1, us)
171 1.1 tls struct spotstr *sp;
172 1.1 tls struct spotstr *sp1;
173 1.1 tls int us;
174 1.1 tls {
175 1.1 tls int them, s, s1;
176 1.1 tls
177 1.1 tls if (sp->s_combo[us].s < sp1->s_combo[us].s)
178 1.1 tls return (1);
179 1.1 tls if (sp->s_combo[us].s != sp1->s_combo[us].s)
180 1.1 tls return (0);
181 1.1 tls if (sp->s_level[us] < sp1->s_level[us])
182 1.1 tls return (1);
183 1.1 tls if (sp->s_level[us] != sp1->s_level[us])
184 1.1 tls return (0);
185 1.1 tls if (sp->s_nforce[us] > sp1->s_nforce[us])
186 1.1 tls return (1);
187 1.1 tls if (sp->s_nforce[us] != sp1->s_nforce[us])
188 1.1 tls return (0);
189 1.1 tls
190 1.1 tls them = !us;
191 1.1 tls s = sp - board;
192 1.1 tls s1 = sp1 - board;
193 1.1 tls if (BIT_TEST(forcemap, s) && !BIT_TEST(forcemap, s1))
194 1.1 tls return (1);
195 1.1 tls if (!BIT_TEST(forcemap, s) && BIT_TEST(forcemap, s1))
196 1.1 tls return (0);
197 1.1 tls if (sp->s_combo[them].s < sp1->s_combo[them].s)
198 1.1 tls return (1);
199 1.1 tls if (sp->s_combo[them].s != sp1->s_combo[them].s)
200 1.1 tls return (0);
201 1.1 tls if (sp->s_level[them] < sp1->s_level[them])
202 1.1 tls return (1);
203 1.1 tls if (sp->s_level[them] != sp1->s_level[them])
204 1.1 tls return (0);
205 1.1 tls if (sp->s_nforce[them] > sp1->s_nforce[them])
206 1.1 tls return (1);
207 1.1 tls if (sp->s_nforce[them] != sp1->s_nforce[them])
208 1.1 tls return (0);
209 1.1 tls
210 1.1 tls if (sp->s_wval > sp1->s_wval)
211 1.1 tls return (1);
212 1.1 tls if (sp->s_wval != sp1->s_wval)
213 1.1 tls return (0);
214 1.1 tls
215 1.1 tls #ifdef SVR4
216 1.1 tls return (rand() & 1);
217 1.1 tls #else
218 1.1 tls return (random() & 1);
219 1.1 tls #endif
220 1.1 tls }
221 1.1 tls
222 1.1 tls int curcolor; /* implicit parameter to makecombo() */
223 1.1 tls int curlevel; /* implicit parameter to makecombo() */
224 1.1 tls
225 1.1 tls /*
226 1.1 tls * Scan the sorted list of non-empty frames and
227 1.1 tls * update the minimum combo values for each empty spot.
228 1.1 tls * Also, try to combine frames to find more complex (chained) moves.
229 1.1 tls */
230 1.1 tls scanframes(color)
231 1.1 tls int color;
232 1.1 tls {
233 1.1 tls register struct combostr *cbp, *ecbp;
234 1.1 tls register struct spotstr *sp;
235 1.1 tls register union comboval *cp;
236 1.1 tls register struct elist *ep, *nep;
237 1.1 tls register int i, r, d, n;
238 1.1 tls union comboval cb;
239 1.1 tls
240 1.1 tls curcolor = color;
241 1.1 tls
242 1.1 tls /* check for empty list of frames */
243 1.1 tls cbp = sortframes[color];
244 1.1 tls if (cbp == (struct combostr *)0)
245 1.1 tls return;
246 1.1 tls
247 1.1 tls /* quick check for four in a row */
248 1.1 tls sp = &board[cbp->c_vertex];
249 1.1 tls cb.s = sp->s_fval[color][d = cbp->c_dir].s;
250 1.1 tls if (cb.s < 0x101) {
251 1.1 tls d = dd[d];
252 1.1 tls for (i = 5 + cb.c.b; --i >= 0; sp += d) {
253 1.1 tls if (sp->s_occ != EMPTY)
254 1.1 tls continue;
255 1.1 tls sp->s_combo[color].s = cb.s;
256 1.1 tls sp->s_level[color] = 1;
257 1.1 tls }
258 1.1 tls return;
259 1.1 tls }
260 1.1 tls
261 1.1 tls /*
262 1.1 tls * Update the minimum combo value for each spot in the frame
263 1.1 tls * and try making all combinations of two frames intersecting at
264 1.1 tls * an empty spot.
265 1.1 tls */
266 1.1 tls n = combolen;
267 1.1 tls ecbp = cbp;
268 1.1 tls do {
269 1.1 tls sp = &board[cbp->c_vertex];
270 1.1 tls cp = &sp->s_fval[color][r = cbp->c_dir];
271 1.1 tls d = dd[r];
272 1.1 tls if (cp->c.b) {
273 1.1 tls /*
274 1.1 tls * Since this is the first spot of an open ended
275 1.1 tls * frame, we treat it as a closed frame.
276 1.1 tls */
277 1.1 tls cb.c.a = cp->c.a + 1;
278 1.1 tls cb.c.b = 0;
279 1.1 tls if (cb.s < sp->s_combo[color].s) {
280 1.1 tls sp->s_combo[color].s = cb.s;
281 1.1 tls sp->s_level[color] = 1;
282 1.1 tls }
283 1.1 tls /*
284 1.1 tls * Try combining other frames that intersect
285 1.1 tls * at this spot.
286 1.1 tls */
287 1.1 tls makecombo2(cbp, sp, 0, cb.s);
288 1.1 tls if (cp->s != 0x101)
289 1.1 tls cb.s = cp->s;
290 1.1 tls else if (color != nextcolor)
291 1.1 tls memset(tmpmap, 0, sizeof(tmpmap));
292 1.1 tls sp += d;
293 1.1 tls i = 1;
294 1.1 tls } else {
295 1.1 tls cb.s = cp->s;
296 1.1 tls i = 0;
297 1.1 tls }
298 1.1 tls for (; i < 5; i++, sp += d) { /* for each spot */
299 1.1 tls if (sp->s_occ != EMPTY)
300 1.1 tls continue;
301 1.1 tls if (cp->s < sp->s_combo[color].s) {
302 1.1 tls sp->s_combo[color].s = cp->s;
303 1.1 tls sp->s_level[color] = 1;
304 1.1 tls }
305 1.1 tls if (cp->s == 0x101) {
306 1.1 tls sp->s_nforce[color]++;
307 1.1 tls if (color != nextcolor) {
308 1.1 tls n = sp - board;
309 1.1 tls BIT_SET(tmpmap, n);
310 1.1 tls }
311 1.1 tls }
312 1.1 tls /*
313 1.1 tls * Try combining other frames that intersect
314 1.1 tls * at this spot.
315 1.1 tls */
316 1.1 tls makecombo2(cbp, sp, i, cb.s);
317 1.1 tls }
318 1.1 tls if (cp->s == 0x101 && color != nextcolor) {
319 1.1 tls if (nforce == 0)
320 1.1 tls memcpy(forcemap, tmpmap, sizeof(tmpmap));
321 1.1 tls else {
322 1.1 tls for (i = 0; i < MAPSZ; i++)
323 1.1 tls forcemap[i] &= tmpmap[i];
324 1.1 tls }
325 1.1 tls }
326 1.1 tls /* mark frame as having been processed */
327 1.1 tls board[cbp->c_vertex].s_flg |= MFLAG << r;
328 1.1 tls } while ((cbp = cbp->c_next) != ecbp);
329 1.1 tls
330 1.1 tls /*
331 1.1 tls * Try to make new 3rd level combos, 4th level, etc.
332 1.1 tls * Limit the search depth early in the game.
333 1.1 tls */
334 1.1 tls d = 2;
335 1.1 tls while (d <= ((movenum + 1) >> 1) && combolen > n) {
336 1.1 tls if (debug) {
337 1.1 tls sprintf(fmtbuf, "%cL%d %d %d %d", "BW"[color],
338 1.1 tls d, combolen - n, combocnt, elistcnt);
339 1.1 tls dlog(fmtbuf);
340 1.1 tls refresh();
341 1.1 tls }
342 1.1 tls n = combolen;
343 1.1 tls addframes(d);
344 1.1 tls d++;
345 1.1 tls }
346 1.1 tls
347 1.1 tls /* scan for combos at empty spots */
348 1.1 tls for (sp = &board[PT(T,20)]; --sp >= &board[PT(A,1)]; ) {
349 1.1 tls for (ep = sp->s_empty; ep; ep = nep) {
350 1.1 tls cbp = ep->e_combo;
351 1.1 tls if (cbp->c_combo.s <= sp->s_combo[color].s) {
352 1.1 tls if (cbp->c_combo.s != sp->s_combo[color].s) {
353 1.1 tls sp->s_combo[color].s = cbp->c_combo.s;
354 1.1 tls sp->s_level[color] = cbp->c_nframes;
355 1.1 tls } else if (cbp->c_nframes < sp->s_level[color])
356 1.1 tls sp->s_level[color] = cbp->c_nframes;
357 1.1 tls }
358 1.1 tls nep = ep->e_next;
359 1.1 tls free(ep);
360 1.1 tls elistcnt--;
361 1.1 tls }
362 1.1 tls sp->s_empty = (struct elist *)0;
363 1.1 tls for (ep = sp->s_nempty; ep; ep = nep) {
364 1.1 tls cbp = ep->e_combo;
365 1.1 tls if (cbp->c_combo.s <= sp->s_combo[color].s) {
366 1.1 tls if (cbp->c_combo.s != sp->s_combo[color].s) {
367 1.1 tls sp->s_combo[color].s = cbp->c_combo.s;
368 1.1 tls sp->s_level[color] = cbp->c_nframes;
369 1.1 tls } else if (cbp->c_nframes < sp->s_level[color])
370 1.1 tls sp->s_level[color] = cbp->c_nframes;
371 1.1 tls }
372 1.1 tls nep = ep->e_next;
373 1.1 tls free(ep);
374 1.1 tls elistcnt--;
375 1.1 tls }
376 1.1 tls sp->s_nempty = (struct elist *)0;
377 1.1 tls }
378 1.1 tls
379 1.1 tls /* remove old combos */
380 1.1 tls if ((cbp = sortcombos) != (struct combostr *)0) {
381 1.1 tls struct combostr *ncbp;
382 1.1 tls
383 1.1 tls /* scan the list */
384 1.1 tls ecbp = cbp;
385 1.1 tls do {
386 1.1 tls ncbp = cbp->c_next;
387 1.1 tls free(cbp);
388 1.1 tls combocnt--;
389 1.1 tls } while ((cbp = ncbp) != ecbp);
390 1.1 tls sortcombos = (struct combostr *)0;
391 1.1 tls }
392 1.1 tls combolen = 0;
393 1.1 tls
394 1.1 tls #ifdef DEBUG
395 1.1 tls if (combocnt) {
396 1.1 tls sprintf(fmtbuf, "scanframes: %c combocnt %d", "BW"[color],
397 1.1 tls combocnt);
398 1.1 tls dlog(fmtbuf);
399 1.1 tls whatsup(0);
400 1.1 tls }
401 1.1 tls if (elistcnt) {
402 1.1 tls sprintf(fmtbuf, "scanframes: %c elistcnt %d", "BW"[color],
403 1.1 tls elistcnt);
404 1.1 tls dlog(fmtbuf);
405 1.1 tls whatsup(0);
406 1.1 tls }
407 1.1 tls #endif
408 1.1 tls }
409 1.1 tls
410 1.1 tls /*
411 1.1 tls * Compute all level 2 combos of frames intersecting spot 'osp'
412 1.1 tls * within the frame 'ocbp' and combo value 's'.
413 1.1 tls */
414 1.1 tls makecombo2(ocbp, osp, off, s)
415 1.1 tls struct combostr *ocbp;
416 1.1 tls struct spotstr *osp;
417 1.1 tls int off;
418 1.1 tls int s;
419 1.1 tls {
420 1.1 tls register struct spotstr *sp, *fsp;
421 1.1 tls register struct combostr *ncbp;
422 1.1 tls register int f, r, d, c;
423 1.1 tls int baseB, fcnt, emask, bmask, n;
424 1.1 tls union comboval ocb, fcb;
425 1.1 tls struct combostr **scbpp, *fcbp;
426 1.1 tls
427 1.1 tls /* try to combine a new frame with those found so far */
428 1.1 tls ocb.s = s;
429 1.1 tls baseB = ocb.c.a + ocb.c.b - 1;
430 1.1 tls fcnt = ocb.c.a - 2;
431 1.1 tls emask = fcnt ? ((ocb.c.b ? 0x1E : 0x1F) & ~(1 << off)) : 0;
432 1.1 tls for (r = 4; --r >= 0; ) { /* for each direction */
433 1.1 tls /* don't include frames that overlap in the same direction */
434 1.1 tls if (r == ocbp->c_dir)
435 1.1 tls continue;
436 1.1 tls d = dd[r];
437 1.1 tls /*
438 1.1 tls * Frame A combined with B is the same value as B combined with A
439 1.1 tls * so skip frames that have already been processed (MFLAG).
440 1.1 tls * Also skip blocked frames (BFLAG) and frames that are <1,x>
441 1.1 tls * since combining another frame with it isn't valid.
442 1.1 tls */
443 1.1 tls bmask = (BFLAG | FFLAG | MFLAG) << r;
444 1.1 tls fsp = osp;
445 1.1 tls for (f = 0; f < 5; f++, fsp -= d) { /* for each frame */
446 1.1 tls if (fsp->s_occ == BORDER)
447 1.1 tls break;
448 1.1 tls if (fsp->s_flg & bmask)
449 1.1 tls continue;
450 1.1 tls
451 1.1 tls /* don't include frames of the wrong color */
452 1.1 tls fcb.s = fsp->s_fval[curcolor][r].s;
453 1.1 tls if (fcb.c.a >= MAXA)
454 1.1 tls continue;
455 1.1 tls
456 1.1 tls /*
457 1.1 tls * Get the combo value for this frame.
458 1.1 tls * If this is the end point of the frame,
459 1.1 tls * use the closed ended value for the frame.
460 1.1 tls */
461 1.1 tls if (f == 0 && fcb.c.b || fcb.s == 0x101) {
462 1.1 tls fcb.c.a++;
463 1.1 tls fcb.c.b = 0;
464 1.1 tls }
465 1.1 tls
466 1.1 tls /* compute combo value */
467 1.1 tls c = fcb.c.a + ocb.c.a - 3;
468 1.1 tls if (c > 4)
469 1.1 tls continue;
470 1.1 tls n = fcb.c.a + fcb.c.b - 1;
471 1.1 tls if (baseB < n)
472 1.1 tls n = baseB;
473 1.1 tls
474 1.1 tls /* make a new combo! */
475 1.1 tls ncbp = (struct combostr *)malloc(sizeof(struct combostr) +
476 1.1 tls 2 * sizeof(struct combostr *));
477 1.1 tls scbpp = (struct combostr **)(ncbp + 1);
478 1.1 tls fcbp = fsp->s_frame[r];
479 1.1 tls if (ocbp < fcbp) {
480 1.1 tls scbpp[0] = ocbp;
481 1.1 tls scbpp[1] = fcbp;
482 1.1 tls } else {
483 1.1 tls scbpp[0] = fcbp;
484 1.1 tls scbpp[1] = ocbp;
485 1.1 tls }
486 1.1 tls ncbp->c_combo.c.a = c;
487 1.1 tls ncbp->c_combo.c.b = n;
488 1.1 tls ncbp->c_link[0] = ocbp;
489 1.1 tls ncbp->c_link[1] = fcbp;
490 1.1 tls ncbp->c_linkv[0].s = ocb.s;
491 1.1 tls ncbp->c_linkv[1].s = fcb.s;
492 1.1 tls ncbp->c_voff[0] = off;
493 1.1 tls ncbp->c_voff[1] = f;
494 1.1 tls ncbp->c_vertex = osp - board;
495 1.1 tls ncbp->c_nframes = 2;
496 1.1 tls ncbp->c_dir = 0;
497 1.1 tls ncbp->c_frameindex = 0;
498 1.1 tls ncbp->c_flg = (ocb.c.b) ? C_OPEN_0 : 0;
499 1.1 tls if (fcb.c.b)
500 1.1 tls ncbp->c_flg |= C_OPEN_1;
501 1.1 tls ncbp->c_framecnt[0] = fcnt;
502 1.1 tls ncbp->c_emask[0] = emask;
503 1.1 tls ncbp->c_framecnt[1] = fcb.c.a - 2;
504 1.1 tls ncbp->c_emask[1] = ncbp->c_framecnt[1] ?
505 1.1 tls ((fcb.c.b ? 0x1E : 0x1F) & ~(1 << f)) : 0;
506 1.1 tls combocnt++;
507 1.1 tls
508 1.1 tls if (c == 1 && debug > 1 || debug > 3) {
509 1.1 tls sprintf(fmtbuf, "%c c %d %d m %x %x o %d %d",
510 1.1 tls "bw"[curcolor],
511 1.1 tls ncbp->c_framecnt[0], ncbp->c_framecnt[1],
512 1.1 tls ncbp->c_emask[0], ncbp->c_emask[1],
513 1.1 tls ncbp->c_voff[0], ncbp->c_voff[1]);
514 1.1 tls dlog(fmtbuf);
515 1.1 tls printcombo(ncbp, fmtbuf);
516 1.1 tls dlog(fmtbuf);
517 1.1 tls }
518 1.1 tls if (c > 1) {
519 1.1 tls /* record the empty spots that will complete this combo */
520 1.1 tls makeempty(ncbp);
521 1.1 tls
522 1.1 tls /* add the new combo to the end of the list */
523 1.1 tls appendcombo(ncbp, curcolor);
524 1.1 tls } else {
525 1.1 tls updatecombo(ncbp, curcolor);
526 1.1 tls free(ncbp);
527 1.1 tls combocnt--;
528 1.1 tls }
529 1.1 tls #ifdef DEBUG
530 1.1 tls if (c == 1 && debug > 1 || debug > 5) {
531 1.1 tls markcombo(ncbp);
532 1.1 tls bdisp();
533 1.1 tls whatsup(0);
534 1.1 tls clearcombo(ncbp, 0);
535 1.1 tls }
536 1.1 tls #endif /* DEBUG */
537 1.1 tls }
538 1.1 tls }
539 1.1 tls }
540 1.1 tls
541 1.1 tls /*
542 1.1 tls * Scan the sorted list of frames and try to add a frame to
543 1.1 tls * combinations of 'level' number of frames.
544 1.1 tls */
545 1.1 tls addframes(level)
546 1.1 tls int level;
547 1.1 tls {
548 1.1 tls register struct combostr *cbp, *ecbp;
549 1.1 tls register struct spotstr *sp, *fsp;
550 1.1 tls register struct elist *ep, *nep;
551 1.1 tls register int i, r, d;
552 1.1 tls struct combostr **cbpp, *pcbp;
553 1.1 tls union comboval fcb, cb;
554 1.1 tls
555 1.1 tls curlevel = level;
556 1.1 tls
557 1.1 tls /* scan for combos at empty spots */
558 1.1 tls i = curcolor;
559 1.1 tls for (sp = &board[PT(T,20)]; --sp >= &board[PT(A,1)]; ) {
560 1.1 tls for (ep = sp->s_empty; ep; ep = nep) {
561 1.1 tls cbp = ep->e_combo;
562 1.1 tls if (cbp->c_combo.s <= sp->s_combo[i].s) {
563 1.1 tls if (cbp->c_combo.s != sp->s_combo[i].s) {
564 1.1 tls sp->s_combo[i].s = cbp->c_combo.s;
565 1.1 tls sp->s_level[i] = cbp->c_nframes;
566 1.1 tls } else if (cbp->c_nframes < sp->s_level[i])
567 1.1 tls sp->s_level[i] = cbp->c_nframes;
568 1.1 tls }
569 1.1 tls nep = ep->e_next;
570 1.1 tls free(ep);
571 1.1 tls elistcnt--;
572 1.1 tls }
573 1.1 tls sp->s_empty = sp->s_nempty;
574 1.1 tls sp->s_nempty = (struct elist *)0;
575 1.1 tls }
576 1.1 tls
577 1.1 tls /* try to add frames to the uncompleted combos at level curlevel */
578 1.1 tls cbp = ecbp = sortframes[curcolor];
579 1.1 tls do {
580 1.1 tls fsp = &board[cbp->c_vertex];
581 1.1 tls r = cbp->c_dir;
582 1.1 tls /* skip frames that are part of a <1,x> combo */
583 1.1 tls if (fsp->s_flg & (FFLAG << r))
584 1.1 tls continue;
585 1.1 tls
586 1.1 tls /*
587 1.1 tls * Don't include <1,x> combo frames,
588 1.1 tls * treat it as a closed three in a row instead.
589 1.1 tls */
590 1.1 tls fcb.s = fsp->s_fval[curcolor][r].s;
591 1.1 tls if (fcb.s == 0x101)
592 1.1 tls fcb.s = 0x200;
593 1.1 tls
594 1.1 tls /*
595 1.1 tls * If this is an open ended frame, use
596 1.1 tls * the combo value with the end closed.
597 1.1 tls */
598 1.1 tls if (fsp->s_occ == EMPTY) {
599 1.1 tls if (fcb.c.b) {
600 1.1 tls cb.c.a = fcb.c.a + 1;
601 1.1 tls cb.c.b = 0;
602 1.1 tls } else
603 1.1 tls cb.s = fcb.s;
604 1.1 tls makecombo(cbp, fsp, 0, cb.s);
605 1.1 tls }
606 1.1 tls
607 1.1 tls /*
608 1.1 tls * The next four spots are handled the same for both
609 1.1 tls * open and closed ended frames.
610 1.1 tls */
611 1.1 tls d = dd[r];
612 1.1 tls sp = fsp + d;
613 1.1 tls for (i = 1; i < 5; i++, sp += d) {
614 1.1 tls if (sp->s_occ != EMPTY)
615 1.1 tls continue;
616 1.1 tls makecombo(cbp, sp, i, fcb.s);
617 1.1 tls }
618 1.1 tls } while ((cbp = cbp->c_next) != ecbp);
619 1.1 tls
620 1.1 tls /* put all the combos in the hash list on the sorted list */
621 1.1 tls cbpp = &hashcombos[FAREA];
622 1.1 tls do {
623 1.1 tls cbp = *--cbpp;
624 1.1 tls if (cbp == (struct combostr *)0)
625 1.1 tls continue;
626 1.1 tls *cbpp = (struct combostr *)0;
627 1.1 tls ecbp = sortcombos;
628 1.1 tls if (ecbp == (struct combostr *)0)
629 1.1 tls sortcombos = cbp;
630 1.1 tls else {
631 1.1 tls /* append to sort list */
632 1.1 tls pcbp = ecbp->c_prev;
633 1.1 tls pcbp->c_next = cbp;
634 1.1 tls ecbp->c_prev = cbp->c_prev;
635 1.1 tls cbp->c_prev->c_next = ecbp;
636 1.1 tls cbp->c_prev = pcbp;
637 1.1 tls }
638 1.1 tls } while (cbpp != hashcombos);
639 1.1 tls }
640 1.1 tls
641 1.1 tls /*
642 1.1 tls * Compute all level N combos of frames intersecting spot 'osp'
643 1.1 tls * within the frame 'ocbp' and combo value 's'.
644 1.1 tls */
645 1.1 tls makecombo(ocbp, osp, off, s)
646 1.1 tls struct combostr *ocbp;
647 1.1 tls struct spotstr *osp;
648 1.1 tls int off;
649 1.1 tls int s;
650 1.1 tls {
651 1.1 tls register struct combostr *cbp, *ncbp;
652 1.1 tls register struct spotstr *sp;
653 1.1 tls register struct elist *ep;
654 1.1 tls register int n, c;
655 1.1 tls struct elist *nep, **epp;
656 1.1 tls struct combostr **scbpp;
657 1.1 tls int baseB, fcnt, emask, verts, d;
658 1.1 tls union comboval ocb, cb;
659 1.1 tls struct ovlp_info vertices[1];
660 1.1 tls
661 1.1 tls ocb.s = s;
662 1.1 tls baseB = ocb.c.a + ocb.c.b - 1;
663 1.1 tls fcnt = ocb.c.a - 2;
664 1.1 tls emask = fcnt ? ((ocb.c.b ? 0x1E : 0x1F) & ~(1 << off)) : 0;
665 1.1 tls for (ep = osp->s_empty; ep; ep = ep->e_next) {
666 1.1 tls /* check for various kinds of overlap */
667 1.1 tls cbp = ep->e_combo;
668 1.1 tls verts = checkframes(cbp, ocbp, osp, s, vertices);
669 1.1 tls if (verts < 0)
670 1.1 tls continue;
671 1.1 tls
672 1.1 tls /* check to see if this frame forms a valid loop */
673 1.1 tls if (verts) {
674 1.1 tls sp = &board[vertices[0].o_intersect];
675 1.1 tls #ifdef DEBUG
676 1.1 tls if (sp->s_occ != EMPTY) {
677 1.1 tls sprintf(fmtbuf, "loop: %c %s", "BW"[curcolor],
678 1.1 tls stoc(sp - board));
679 1.1 tls dlog(fmtbuf);
680 1.1 tls whatsup(0);
681 1.1 tls }
682 1.1 tls #endif
683 1.1 tls /*
684 1.1 tls * It is a valid loop if the intersection spot
685 1.1 tls * of the frame we are trying to attach is one
686 1.1 tls * of the completion spots of the combostr
687 1.1 tls * we are trying to attach the frame to.
688 1.1 tls */
689 1.1 tls for (nep = sp->s_empty; nep; nep = nep->e_next) {
690 1.1 tls if (nep->e_combo == cbp)
691 1.1 tls goto fnd;
692 1.1 tls if (nep->e_combo->c_nframes < cbp->c_nframes)
693 1.1 tls break;
694 1.1 tls }
695 1.1 tls /* frame overlaps but not at a valid spot */
696 1.1 tls continue;
697 1.1 tls fnd:
698 1.1 tls ;
699 1.1 tls }
700 1.1 tls
701 1.1 tls /* compute the first half of the combo value */
702 1.1 tls c = cbp->c_combo.c.a + ocb.c.a - verts - 3;
703 1.1 tls if (c > 4)
704 1.1 tls continue;
705 1.1 tls
706 1.1 tls /* compute the second half of the combo value */
707 1.1 tls n = ep->e_fval.c.a + ep->e_fval.c.b - 1;
708 1.1 tls if (baseB < n)
709 1.1 tls n = baseB;
710 1.1 tls
711 1.1 tls /* make a new combo! */
712 1.1 tls ncbp = (struct combostr *)malloc(sizeof(struct combostr) +
713 1.1 tls (cbp->c_nframes + 1) * sizeof(struct combostr *));
714 1.1 tls scbpp = (struct combostr **)(ncbp + 1);
715 1.1 tls if (sortcombo(scbpp, (struct combostr **)(cbp + 1), ocbp)) {
716 1.1 tls free(ncbp);
717 1.1 tls continue;
718 1.1 tls }
719 1.1 tls combocnt++;
720 1.1 tls
721 1.1 tls ncbp->c_combo.c.a = c;
722 1.1 tls ncbp->c_combo.c.b = n;
723 1.1 tls ncbp->c_link[0] = cbp;
724 1.1 tls ncbp->c_link[1] = ocbp;
725 1.1 tls ncbp->c_linkv[1].s = ocb.s;
726 1.1 tls ncbp->c_voff[1] = off;
727 1.1 tls ncbp->c_vertex = osp - board;
728 1.1 tls ncbp->c_nframes = cbp->c_nframes + 1;
729 1.1 tls ncbp->c_flg = ocb.c.b ? C_OPEN_1 : 0;
730 1.1 tls ncbp->c_frameindex = ep->e_frameindex;
731 1.1 tls /*
732 1.1 tls * Update the completion spot mask of the frame we
733 1.1 tls * are attaching 'ocbp' to so the intersection isn't
734 1.1 tls * listed twice.
735 1.1 tls */
736 1.1 tls ncbp->c_framecnt[0] = ep->e_framecnt;
737 1.1 tls ncbp->c_emask[0] = ep->e_emask;
738 1.1 tls if (verts) {
739 1.1 tls ncbp->c_flg |= C_LOOP;
740 1.1 tls ncbp->c_dir = vertices[0].o_frameindex;
741 1.1 tls ncbp->c_framecnt[1] = fcnt - 1;
742 1.1 tls if (ncbp->c_framecnt[1]) {
743 1.1 tls n = (vertices[0].o_intersect - ocbp->c_vertex) /
744 1.1 tls dd[ocbp->c_dir];
745 1.1 tls ncbp->c_emask[1] = emask & ~(1 << n);
746 1.1 tls } else
747 1.1 tls ncbp->c_emask[1] = 0;
748 1.1 tls ncbp->c_voff[0] = vertices[0].o_off;
749 1.1 tls } else {
750 1.1 tls ncbp->c_dir = 0;
751 1.1 tls ncbp->c_framecnt[1] = fcnt;
752 1.1 tls ncbp->c_emask[1] = emask;
753 1.1 tls ncbp->c_voff[0] = ep->e_off;
754 1.1 tls }
755 1.1 tls
756 1.1 tls if (c == 1 && debug > 1 || debug > 3) {
757 1.1 tls sprintf(fmtbuf, "%c v%d i%d d%d c %d %d m %x %x o %d %d",
758 1.1 tls "bw"[curcolor], verts, ncbp->c_frameindex, ncbp->c_dir,
759 1.1 tls ncbp->c_framecnt[0], ncbp->c_framecnt[1],
760 1.1 tls ncbp->c_emask[0], ncbp->c_emask[1],
761 1.1 tls ncbp->c_voff[0], ncbp->c_voff[1]);
762 1.1 tls dlog(fmtbuf);
763 1.1 tls printcombo(ncbp, fmtbuf);
764 1.1 tls dlog(fmtbuf);
765 1.1 tls }
766 1.1 tls if (c > 1) {
767 1.1 tls /* record the empty spots that will complete this combo */
768 1.1 tls makeempty(ncbp);
769 1.1 tls combolen++;
770 1.1 tls } else {
771 1.1 tls /* update board values */
772 1.1 tls updatecombo(ncbp, curcolor);
773 1.1 tls }
774 1.1 tls #ifdef DEBUG
775 1.1 tls if (c == 1 && debug > 1 || debug > 4) {
776 1.1 tls markcombo(ncbp);
777 1.1 tls bdisp();
778 1.1 tls whatsup(0);
779 1.1 tls clearcombo(ncbp, 0);
780 1.1 tls }
781 1.1 tls #endif /* DEBUG */
782 1.1 tls }
783 1.1 tls }
784 1.1 tls
785 1.1 tls #define MAXDEPTH 100
786 1.1 tls struct elist einfo[MAXDEPTH];
787 1.1 tls struct combostr *ecombo[MAXDEPTH]; /* separate from elist to save space */
788 1.1 tls
789 1.1 tls /*
790 1.1 tls * Add the combostr 'ocbp' to the empty spots list for each empty spot
791 1.1 tls * in 'ocbp' that will complete the combo.
792 1.1 tls */
793 1.1 tls makeempty(ocbp)
794 1.1 tls struct combostr *ocbp;
795 1.1 tls {
796 1.1 tls struct combostr *cbp, *tcbp, **cbpp;
797 1.1 tls struct elist *ep, *nep, **epp;
798 1.1 tls struct spotstr *sp;
799 1.1 tls int s, d, m, emask, i;
800 1.1 tls int nframes;
801 1.1 tls
802 1.1 tls if (debug > 2) {
803 1.1 tls sprintf(fmtbuf, "E%c ", "bw"[curcolor]);
804 1.1 tls printcombo(ocbp, fmtbuf + 3);
805 1.1 tls dlog(fmtbuf);
806 1.1 tls }
807 1.1 tls
808 1.1 tls /* should never happen but check anyway */
809 1.1 tls if ((nframes = ocbp->c_nframes) >= MAXDEPTH)
810 1.1 tls return;
811 1.1 tls
812 1.1 tls /*
813 1.1 tls * The lower level combo can be pointed to by more than one
814 1.1 tls * higher level 'struct combostr' so we can't modify the
815 1.1 tls * lower level. Therefore, higher level combos store the
816 1.1 tls * real mask of the lower level frame in c_emask[0] and the
817 1.1 tls * frame number in c_frameindex.
818 1.1 tls *
819 1.1 tls * First we traverse the tree from top to bottom and save the
820 1.1 tls * connection info. Then we traverse the tree from bottom to
821 1.1 tls * top overwriting lower levels with the newer emask information.
822 1.1 tls */
823 1.1 tls ep = &einfo[nframes];
824 1.1 tls cbpp = &ecombo[nframes];
825 1.1 tls for (cbp = ocbp; tcbp = cbp->c_link[1]; cbp = cbp->c_link[0]) {
826 1.1 tls ep--;
827 1.1 tls ep->e_combo = cbp;
828 1.1 tls *--cbpp = cbp->c_link[1];
829 1.1 tls ep->e_off = cbp->c_voff[1];
830 1.1 tls ep->e_frameindex = cbp->c_frameindex;
831 1.1 tls ep->e_fval.s = cbp->c_linkv[1].s;
832 1.1 tls ep->e_framecnt = cbp->c_framecnt[1];
833 1.1 tls ep->e_emask = cbp->c_emask[1];
834 1.1 tls }
835 1.1 tls cbp = ep->e_combo;
836 1.1 tls ep--;
837 1.1 tls ep->e_combo = cbp;
838 1.1 tls *--cbpp = cbp->c_link[0];
839 1.1 tls ep->e_off = cbp->c_voff[0];
840 1.1 tls ep->e_frameindex = 0;
841 1.1 tls ep->e_fval.s = cbp->c_linkv[0].s;
842 1.1 tls ep->e_framecnt = cbp->c_framecnt[0];
843 1.1 tls ep->e_emask = cbp->c_emask[0];
844 1.1 tls
845 1.1 tls /* now update the emask info */
846 1.1 tls s = 0;
847 1.1 tls for (i = 2, ep += 2; i < nframes; i++, ep++) {
848 1.1 tls cbp = ep->e_combo;
849 1.1 tls nep = &einfo[ep->e_frameindex];
850 1.1 tls nep->e_framecnt = cbp->c_framecnt[0];
851 1.1 tls nep->e_emask = cbp->c_emask[0];
852 1.1 tls
853 1.1 tls if (cbp->c_flg & C_LOOP) {
854 1.1 tls s++;
855 1.1 tls /*
856 1.1 tls * Account for the fact that this frame connects
857 1.1 tls * to a previous one (thus forming a loop).
858 1.1 tls */
859 1.1 tls nep = &einfo[cbp->c_dir];
860 1.1 tls if (--nep->e_framecnt)
861 1.1 tls nep->e_emask &= ~(1 << cbp->c_voff[0]);
862 1.1 tls else
863 1.1 tls nep->e_emask = 0;
864 1.1 tls }
865 1.1 tls }
866 1.1 tls
867 1.1 tls /*
868 1.1 tls * We only need to update the emask values of "complete" loops
869 1.1 tls * to include the intersection spots.
870 1.1 tls */
871 1.1 tls if (s && ocbp->c_combo.c.a == 2) {
872 1.1 tls /* process loops from the top down */
873 1.1 tls ep = &einfo[nframes];
874 1.1 tls do {
875 1.1 tls ep--;
876 1.1 tls cbp = ep->e_combo;
877 1.1 tls if (!(cbp->c_flg & C_LOOP))
878 1.1 tls continue;
879 1.1 tls
880 1.1 tls /*
881 1.1 tls * Update the emask values to include the
882 1.1 tls * intersection spots.
883 1.1 tls */
884 1.1 tls nep = &einfo[cbp->c_dir];
885 1.1 tls nep->e_framecnt = 1;
886 1.1 tls nep->e_emask = 1 << cbp->c_voff[0];
887 1.1 tls ep->e_framecnt = 1;
888 1.1 tls ep->e_emask = 1 << ep->e_off;
889 1.1 tls ep = &einfo[ep->e_frameindex];
890 1.1 tls do {
891 1.1 tls ep->e_framecnt = 1;
892 1.1 tls ep->e_emask = 1 << ep->e_off;
893 1.1 tls ep = &einfo[ep->e_frameindex];
894 1.1 tls } while (ep > nep);
895 1.1 tls } while (ep != einfo);
896 1.1 tls }
897 1.1 tls
898 1.1 tls /* check all the frames for completion spots */
899 1.1 tls for (i = 0, ep = einfo, cbpp = ecombo; i < nframes; i++, ep++, cbpp++) {
900 1.1 tls /* skip this frame if there are no incomplete spots in it */
901 1.1 tls if ((emask = ep->e_emask) == 0)
902 1.1 tls continue;
903 1.1 tls cbp = *cbpp;
904 1.1 tls sp = &board[cbp->c_vertex];
905 1.1 tls d = dd[cbp->c_dir];
906 1.1 tls for (s = 0, m = 1; s < 5; s++, sp += d, m <<= 1) {
907 1.1 tls if (sp->s_occ != EMPTY || !(emask & m))
908 1.1 tls continue;
909 1.1 tls
910 1.1 tls /* add the combo to the list of empty spots */
911 1.1 tls nep = (struct elist *)malloc(sizeof(struct elist));
912 1.1 tls nep->e_combo = ocbp;
913 1.1 tls nep->e_off = s;
914 1.1 tls nep->e_frameindex = i;
915 1.1 tls if (ep->e_framecnt > 1) {
916 1.1 tls nep->e_framecnt = ep->e_framecnt - 1;
917 1.1 tls nep->e_emask = emask & ~m;
918 1.1 tls } else {
919 1.1 tls nep->e_framecnt = 0;
920 1.1 tls nep->e_emask = 0;
921 1.1 tls }
922 1.1 tls nep->e_fval.s = ep->e_fval.s;
923 1.1 tls if (debug > 2) {
924 1.1 tls sprintf(fmtbuf, "e %s o%d i%d c%d m%x %x",
925 1.1 tls stoc(sp - board),
926 1.1 tls nep->e_off,
927 1.1 tls nep->e_frameindex,
928 1.1 tls nep->e_framecnt,
929 1.1 tls nep->e_emask,
930 1.1 tls nep->e_fval.s);
931 1.1 tls dlog(fmtbuf);
932 1.1 tls }
933 1.1 tls
934 1.1 tls /* sort by the number of frames in the combo */
935 1.1 tls nep->e_next = sp->s_nempty;
936 1.1 tls sp->s_nempty = nep;
937 1.1 tls elistcnt++;
938 1.1 tls }
939 1.1 tls }
940 1.1 tls }
941 1.1 tls
942 1.1 tls /*
943 1.1 tls * Update the board value based on the combostr.
944 1.1 tls * This is called only if 'cbp' is a <1,x> combo.
945 1.1 tls * We handle things differently depending on whether the next move
946 1.1 tls * would be trying to "complete" the combo or trying to block it.
947 1.1 tls */
948 1.1 tls updatecombo(cbp, color)
949 1.1 tls struct combostr *cbp;
950 1.1 tls int color;
951 1.1 tls {
952 1.1 tls register struct framestr *fp;
953 1.1 tls register struct spotstr *sp;
954 1.1 tls register struct combostr *tcbp;
955 1.1 tls register int i, d;
956 1.1 tls int nframes, flg, s;
957 1.1 tls union comboval cb;
958 1.1 tls
959 1.1 tls /* save the top level value for the whole combo */
960 1.1 tls cb.c.a = cbp->c_combo.c.a;
961 1.1 tls nframes = cbp->c_nframes;
962 1.1 tls
963 1.1 tls if (color != nextcolor)
964 1.1 tls memset(tmpmap, 0, sizeof(tmpmap));
965 1.1 tls
966 1.1 tls for (; tcbp = cbp->c_link[1]; cbp = cbp->c_link[0]) {
967 1.1 tls flg = cbp->c_flg;
968 1.1 tls cb.c.b = cbp->c_combo.c.b;
969 1.1 tls if (color == nextcolor) {
970 1.1 tls /* update the board value for the vertex */
971 1.1 tls sp = &board[cbp->c_vertex];
972 1.1 tls sp->s_nforce[color]++;
973 1.1 tls if (cb.s <= sp->s_combo[color].s) {
974 1.1 tls if (cb.s != sp->s_combo[color].s) {
975 1.1 tls sp->s_combo[color].s = cb.s;
976 1.1 tls sp->s_level[color] = nframes;
977 1.1 tls } else if (nframes < sp->s_level[color])
978 1.1 tls sp->s_level[color] = nframes;
979 1.1 tls }
980 1.1 tls } else {
981 1.1 tls /* update the board values for each spot in frame */
982 1.1 tls sp = &board[s = tcbp->c_vertex];
983 1.1 tls d = dd[tcbp->c_dir];
984 1.1 tls i = (flg & C_OPEN_1) ? 6 : 5;
985 1.1 tls for (; --i >= 0; sp += d, s += d) {
986 1.1 tls if (sp->s_occ != EMPTY)
987 1.1 tls continue;
988 1.1 tls sp->s_nforce[color]++;
989 1.1 tls if (cb.s <= sp->s_combo[color].s) {
990 1.1 tls if (cb.s != sp->s_combo[color].s) {
991 1.1 tls sp->s_combo[color].s = cb.s;
992 1.1 tls sp->s_level[color] = nframes;
993 1.1 tls } else if (nframes < sp->s_level[color])
994 1.1 tls sp->s_level[color] = nframes;
995 1.1 tls }
996 1.1 tls BIT_SET(tmpmap, s);
997 1.1 tls }
998 1.1 tls }
999 1.1 tls
1000 1.1 tls /* mark the frame as being part of a <1,x> combo */
1001 1.1 tls board[tcbp->c_vertex].s_flg |= FFLAG << tcbp->c_dir;
1002 1.1 tls }
1003 1.1 tls
1004 1.1 tls if (color != nextcolor) {
1005 1.1 tls /* update the board values for each spot in frame */
1006 1.1 tls sp = &board[s = cbp->c_vertex];
1007 1.1 tls d = dd[cbp->c_dir];
1008 1.1 tls i = (flg & C_OPEN_0) ? 6 : 5;
1009 1.1 tls for (; --i >= 0; sp += d, s += d) {
1010 1.1 tls if (sp->s_occ != EMPTY)
1011 1.1 tls continue;
1012 1.1 tls sp->s_nforce[color]++;
1013 1.1 tls if (cb.s <= sp->s_combo[color].s) {
1014 1.1 tls if (cb.s != sp->s_combo[color].s) {
1015 1.1 tls sp->s_combo[color].s = cb.s;
1016 1.1 tls sp->s_level[color] = nframes;
1017 1.1 tls } else if (nframes < sp->s_level[color])
1018 1.1 tls sp->s_level[color] = nframes;
1019 1.1 tls }
1020 1.1 tls BIT_SET(tmpmap, s);
1021 1.1 tls }
1022 1.1 tls if (nforce == 0)
1023 1.1 tls memcpy(forcemap, tmpmap, sizeof(tmpmap));
1024 1.1 tls else {
1025 1.1 tls for (i = 0; i < MAPSZ; i++)
1026 1.1 tls forcemap[i] &= tmpmap[i];
1027 1.1 tls }
1028 1.1 tls nforce++;
1029 1.1 tls }
1030 1.1 tls
1031 1.1 tls /* mark the frame as being part of a <1,x> combo */
1032 1.1 tls board[cbp->c_vertex].s_flg |= FFLAG << cbp->c_dir;
1033 1.1 tls }
1034 1.1 tls
1035 1.1 tls /*
1036 1.1 tls * Add combo to the end of the list.
1037 1.1 tls */
1038 1.1 tls appendcombo(cbp, color)
1039 1.1 tls struct combostr *cbp;
1040 1.1 tls int color;
1041 1.1 tls {
1042 1.1 tls struct combostr *pcbp, *ncbp;
1043 1.1 tls
1044 1.1 tls combolen++;
1045 1.1 tls ncbp = sortcombos;
1046 1.1 tls if (ncbp == (struct combostr *)0) {
1047 1.1 tls sortcombos = cbp;
1048 1.1 tls cbp->c_next = cbp;
1049 1.1 tls cbp->c_prev = cbp;
1050 1.1 tls return;
1051 1.1 tls }
1052 1.1 tls pcbp = ncbp->c_prev;
1053 1.1 tls cbp->c_next = ncbp;
1054 1.1 tls cbp->c_prev = pcbp;
1055 1.1 tls ncbp->c_prev = cbp;
1056 1.1 tls pcbp->c_next = cbp;
1057 1.1 tls }
1058 1.1 tls
1059 1.1 tls /*
1060 1.1 tls * Return zero if it is valid to combine frame 'fcbp' with the frames
1061 1.1 tls * in 'cbp' and forms a linked chain of frames (i.e., a tree; no loops).
1062 1.1 tls * Return positive if combining frame 'fcbp' to the frames in 'cbp'
1063 1.1 tls * would form some kind of valid loop. Also return the intersection spots
1064 1.1 tls * in 'vertices[]' beside the known intersection at spot 'osp'.
1065 1.1 tls * Return -1 if 'fcbp' should not be combined with 'cbp'.
1066 1.1 tls * 's' is the combo value for frame 'fcpb'.
1067 1.1 tls */
1068 1.1 tls checkframes(cbp, fcbp, osp, s, vertices)
1069 1.1 tls struct combostr *cbp;
1070 1.1 tls struct combostr *fcbp;
1071 1.1 tls struct spotstr *osp;
1072 1.1 tls int s;
1073 1.1 tls struct ovlp_info *vertices;
1074 1.1 tls {
1075 1.1 tls struct combostr *tcbp, *lcbp;
1076 1.1 tls int i, n, mask, flg, verts, loop, index, fcnt;
1077 1.1 tls union comboval cb;
1078 1.1 tls u_char *str;
1079 1.1 tls short *ip;
1080 1.1 tls
1081 1.1 tls cb.s = s;
1082 1.1 tls fcnt = cb.c.a - 2;
1083 1.1 tls verts = 0;
1084 1.1 tls loop = 0;
1085 1.1 tls index = cbp->c_nframes;
1086 1.1 tls n = (fcbp - frames) * FAREA;
1087 1.1 tls str = &overlap[n];
1088 1.1 tls ip = &intersect[n];
1089 1.1 tls /*
1090 1.1 tls * i == which overlap bit to test based on whether 'fcbp' is
1091 1.1 tls * an open or closed frame.
1092 1.1 tls */
1093 1.1 tls i = cb.c.b ? 2 : 0;
1094 1.1 tls for (; tcbp = cbp->c_link[1]; lcbp = cbp, cbp = cbp->c_link[0]) {
1095 1.1 tls if (tcbp == fcbp)
1096 1.1 tls return (-1); /* fcbp is already included */
1097 1.1 tls
1098 1.1 tls /* check for intersection of 'tcbp' with 'fcbp' */
1099 1.1 tls index--;
1100 1.1 tls mask = str[tcbp - frames];
1101 1.1 tls flg = cbp->c_flg;
1102 1.1 tls n = i + ((flg & C_OPEN_1) != 0);
1103 1.1 tls if (mask & (1 << n)) {
1104 1.1 tls /*
1105 1.1 tls * The two frames are not independent if they
1106 1.1 tls * both lie in the same line and intersect at
1107 1.1 tls * more than one point.
1108 1.1 tls */
1109 1.1 tls if (tcbp->c_dir == fcbp->c_dir && (mask & (0x10 << n)))
1110 1.1 tls return (-1);
1111 1.1 tls /*
1112 1.1 tls * If this is not the spot we are attaching
1113 1.1 tls * 'fcbp' to and it is a reasonable intersection
1114 1.1 tls * spot, then there might be a loop.
1115 1.1 tls */
1116 1.1 tls n = ip[tcbp - frames];
1117 1.1 tls if (osp != &board[n]) {
1118 1.1 tls /* check to see if this is a valid loop */
1119 1.1 tls if (verts)
1120 1.1 tls return (-1);
1121 1.1 tls if (fcnt == 0 || cbp->c_framecnt[1] == 0)
1122 1.1 tls return (-1);
1123 1.1 tls /*
1124 1.1 tls * Check to be sure the intersection is not
1125 1.1 tls * one of the end points if it is an open
1126 1.1 tls * ended frame.
1127 1.1 tls */
1128 1.1 tls if ((flg & C_OPEN_1) &&
1129 1.1 tls (n == tcbp->c_vertex ||
1130 1.1 tls n == tcbp->c_vertex + 5 * dd[tcbp->c_dir]))
1131 1.1 tls return (-1); /* invalid overlap */
1132 1.1 tls if (cb.c.b &&
1133 1.1 tls (n == fcbp->c_vertex ||
1134 1.1 tls n == fcbp->c_vertex + 5 * dd[fcbp->c_dir]))
1135 1.1 tls return (-1); /* invalid overlap */
1136 1.1 tls
1137 1.1 tls vertices->o_intersect = n;
1138 1.1 tls vertices->o_fcombo = cbp;
1139 1.1 tls vertices->o_link = 1;
1140 1.1 tls vertices->o_off = (n - tcbp->c_vertex) /
1141 1.1 tls dd[tcbp->c_dir];
1142 1.1 tls vertices->o_frameindex = index;
1143 1.1 tls verts++;
1144 1.1 tls }
1145 1.1 tls }
1146 1.1 tls n = i + ((flg & C_OPEN_0) != 0);
1147 1.1 tls }
1148 1.1 tls if (cbp == fcbp)
1149 1.1 tls return (-1); /* fcbp is already included */
1150 1.1 tls
1151 1.1 tls /* check for intersection of 'cbp' with 'fcbp' */
1152 1.1 tls mask = str[cbp - frames];
1153 1.1 tls if (mask & (1 << n)) {
1154 1.1 tls /*
1155 1.1 tls * The two frames are not independent if they
1156 1.1 tls * both lie in the same line and intersect at
1157 1.1 tls * more than one point.
1158 1.1 tls */
1159 1.1 tls if (cbp->c_dir == fcbp->c_dir && (mask & (0x10 << n)))
1160 1.1 tls return (-1);
1161 1.1 tls /*
1162 1.1 tls * If this is not the spot we are attaching
1163 1.1 tls * 'fcbp' to and it is a reasonable intersection
1164 1.1 tls * spot, then there might be a loop.
1165 1.1 tls */
1166 1.1 tls n = ip[cbp - frames];
1167 1.1 tls if (osp != &board[n]) {
1168 1.1 tls /* check to see if this is a valid loop */
1169 1.1 tls if (verts)
1170 1.1 tls return (-1);
1171 1.1 tls if (fcnt == 0 || lcbp->c_framecnt[0] == 0)
1172 1.1 tls return (-1);
1173 1.1 tls /*
1174 1.1 tls * Check to be sure the intersection is not
1175 1.1 tls * one of the end points if it is an open
1176 1.1 tls * ended frame.
1177 1.1 tls */
1178 1.1 tls if ((flg & C_OPEN_0) &&
1179 1.1 tls (n == cbp->c_vertex ||
1180 1.1 tls n == cbp->c_vertex + 5 * dd[cbp->c_dir]))
1181 1.1 tls return (-1); /* invalid overlap */
1182 1.1 tls if (cb.c.b &&
1183 1.1 tls (n == fcbp->c_vertex ||
1184 1.1 tls n == fcbp->c_vertex + 5 * dd[fcbp->c_dir]))
1185 1.1 tls return (-1); /* invalid overlap */
1186 1.1 tls
1187 1.1 tls vertices->o_intersect = n;
1188 1.1 tls vertices->o_fcombo = lcbp;
1189 1.1 tls vertices->o_link = 0;
1190 1.1 tls vertices->o_off = (n - cbp->c_vertex) /
1191 1.1 tls dd[cbp->c_dir];
1192 1.1 tls vertices->o_frameindex = 0;
1193 1.1 tls verts++;
1194 1.1 tls }
1195 1.1 tls }
1196 1.1 tls return (verts);
1197 1.1 tls }
1198 1.1 tls
1199 1.1 tls /*
1200 1.1 tls * Merge sort the frame 'fcbp' and the sorted list of frames 'cbpp' and
1201 1.1 tls * store the result in 'scbpp'. 'curlevel' is the size of the 'cbpp' array.
1202 1.1 tls * Return true if this list of frames is already in the hash list.
1203 1.1 tls * Otherwise, add the new combo to the hash list.
1204 1.1 tls */
1205 1.1 tls sortcombo(scbpp, cbpp, fcbp)
1206 1.1 tls struct combostr **scbpp;
1207 1.1 tls struct combostr **cbpp;
1208 1.1 tls struct combostr *fcbp;
1209 1.1 tls {
1210 1.1 tls struct combostr **spp, **cpp;
1211 1.1 tls struct combostr *cbp, *ecbp;
1212 1.1 tls int n, inx;
1213 1.1 tls
1214 1.1 tls #ifdef DEBUG
1215 1.1 tls if (debug > 3) {
1216 1.1 tls char *str;
1217 1.1 tls
1218 1.1 tls sprintf(fmtbuf, "sortc: %s%c l%d", stoc(fcbp->c_vertex),
1219 1.1 tls pdir[fcbp->c_dir], curlevel);
1220 1.1 tls dlog(fmtbuf);
1221 1.1 tls str = fmtbuf;
1222 1.1 tls for (cpp = cbpp; cpp < cbpp + curlevel; cpp++) {
1223 1.1 tls sprintf(str, " %s%c", stoc((*cpp)->c_vertex),
1224 1.1 tls pdir[(*cpp)->c_dir]);
1225 1.1 tls str += strlen(str);
1226 1.1 tls }
1227 1.1 tls dlog(fmtbuf);
1228 1.1 tls }
1229 1.1 tls #endif /* DEBUG */
1230 1.1 tls
1231 1.1 tls /* first build the new sorted list */
1232 1.1 tls n = curlevel + 1;
1233 1.1 tls spp = scbpp + n;
1234 1.1 tls cpp = cbpp + curlevel;
1235 1.1 tls do {
1236 1.1 tls cpp--;
1237 1.1 tls if (fcbp > *cpp) {
1238 1.1 tls *--spp = fcbp;
1239 1.1 tls do
1240 1.1 tls *--spp = *cpp;
1241 1.1 tls while (cpp-- != cbpp);
1242 1.1 tls goto inserted;
1243 1.1 tls }
1244 1.1 tls *--spp = *cpp;
1245 1.1 tls } while (cpp != cbpp);
1246 1.1 tls *--spp = fcbp;
1247 1.1 tls inserted:
1248 1.1 tls
1249 1.1 tls /* now check to see if this list of frames has already been seen */
1250 1.1 tls cbp = hashcombos[inx = *scbpp - frames];
1251 1.1 tls if (cbp == (struct combostr *)0) {
1252 1.1 tls /*
1253 1.1 tls * Easy case, this list hasn't been seen.
1254 1.1 tls * Add it to the hash list.
1255 1.1 tls */
1256 1.1 tls fcbp = (struct combostr *)
1257 1.1 tls ((char *)scbpp - sizeof(struct combostr));
1258 1.1 tls hashcombos[inx] = fcbp;
1259 1.1 tls fcbp->c_next = fcbp->c_prev = fcbp;
1260 1.1 tls return (0);
1261 1.1 tls }
1262 1.1 tls ecbp = cbp;
1263 1.1 tls do {
1264 1.1 tls cbpp = (struct combostr **)(cbp + 1);
1265 1.1 tls cpp = cbpp + n;
1266 1.1 tls spp = scbpp + n;
1267 1.1 tls cbpp++; /* first frame is always the same */
1268 1.1 tls do {
1269 1.1 tls if (*--spp != *--cpp)
1270 1.1 tls goto next;
1271 1.1 tls } while (cpp != cbpp);
1272 1.1 tls /* we found a match */
1273 1.1 tls #ifdef DEBUG
1274 1.1 tls if (debug > 3) {
1275 1.1 tls char *str;
1276 1.1 tls
1277 1.1 tls sprintf(fmtbuf, "sort1: n%d", n);
1278 1.1 tls dlog(fmtbuf);
1279 1.1 tls str = fmtbuf;
1280 1.1 tls for (cpp = scbpp; cpp < scbpp + n; cpp++) {
1281 1.1 tls sprintf(str, " %s%c", stoc((*cpp)->c_vertex),
1282 1.1 tls pdir[(*cpp)->c_dir]);
1283 1.1 tls str += strlen(str);
1284 1.1 tls }
1285 1.1 tls dlog(fmtbuf);
1286 1.1 tls printcombo(cbp, fmtbuf);
1287 1.1 tls dlog(fmtbuf);
1288 1.1 tls str = fmtbuf;
1289 1.1 tls cbpp--;
1290 1.1 tls for (cpp = cbpp; cpp < cbpp + n; cpp++) {
1291 1.1 tls sprintf(str, " %s%c", stoc((*cpp)->c_vertex),
1292 1.1 tls pdir[(*cpp)->c_dir]);
1293 1.1 tls str += strlen(str);
1294 1.1 tls }
1295 1.1 tls dlog(fmtbuf);
1296 1.1 tls }
1297 1.1 tls #endif /* DEBUG */
1298 1.1 tls return (1);
1299 1.1 tls next:
1300 1.1 tls ;
1301 1.1 tls } while ((cbp = cbp->c_next) != ecbp);
1302 1.1 tls /*
1303 1.1 tls * This list of frames hasn't been seen.
1304 1.1 tls * Add it to the hash list.
1305 1.1 tls */
1306 1.1 tls ecbp = cbp->c_prev;
1307 1.1 tls fcbp = (struct combostr *)((char *)scbpp - sizeof(struct combostr));
1308 1.1 tls fcbp->c_next = cbp;
1309 1.1 tls fcbp->c_prev = ecbp;
1310 1.1 tls cbp->c_prev = fcbp;
1311 1.1 tls ecbp->c_next = fcbp;
1312 1.1 tls return (0);
1313 1.1 tls }
1314 1.1 tls
1315 1.1 tls /*
1316 1.1 tls * Print the combo into string 'str'.
1317 1.1 tls */
1318 1.1 tls printcombo(cbp, str)
1319 1.1 tls struct combostr *cbp;
1320 1.1 tls char *str;
1321 1.1 tls {
1322 1.1 tls struct combostr *tcbp;
1323 1.1 tls
1324 1.1 tls sprintf(str, "%x/%d", cbp->c_combo.s, cbp->c_nframes);
1325 1.1 tls str += strlen(str);
1326 1.1 tls for (; tcbp = cbp->c_link[1]; cbp = cbp->c_link[0]) {
1327 1.1 tls sprintf(str, " %s%c%x", stoc(tcbp->c_vertex), pdir[tcbp->c_dir],
1328 1.1 tls cbp->c_flg);
1329 1.1 tls str += strlen(str);
1330 1.1 tls }
1331 1.1 tls sprintf(str, " %s%c", stoc(cbp->c_vertex), pdir[cbp->c_dir]);
1332 1.1 tls }
1333 1.1 tls
1334 1.1 tls #ifdef DEBUG
1335 1.1 tls markcombo(ocbp)
1336 1.1 tls struct combostr *ocbp;
1337 1.1 tls {
1338 1.1 tls struct combostr *cbp, *tcbp, **cbpp;
1339 1.1 tls struct elist *ep, *nep, **epp;
1340 1.1 tls struct spotstr *sp;
1341 1.1 tls int s, d, m, i;
1342 1.1 tls int nframes;
1343 1.1 tls int r, n, flg, cmask, omask;
1344 1.1 tls
1345 1.1 tls /* should never happen but check anyway */
1346 1.1 tls if ((nframes = ocbp->c_nframes) >= MAXDEPTH)
1347 1.1 tls return;
1348 1.1 tls
1349 1.1 tls /*
1350 1.1 tls * The lower level combo can be pointed to by more than one
1351 1.1 tls * higher level 'struct combostr' so we can't modify the
1352 1.1 tls * lower level. Therefore, higher level combos store the
1353 1.1 tls * real mask of the lower level frame in c_emask[0] and the
1354 1.1 tls * frame number in c_frameindex.
1355 1.1 tls *
1356 1.1 tls * First we traverse the tree from top to bottom and save the
1357 1.1 tls * connection info. Then we traverse the tree from bottom to
1358 1.1 tls * top overwriting lower levels with the newer emask information.
1359 1.1 tls */
1360 1.1 tls ep = &einfo[nframes];
1361 1.1 tls cbpp = &ecombo[nframes];
1362 1.1 tls for (cbp = ocbp; tcbp = cbp->c_link[1]; cbp = cbp->c_link[0]) {
1363 1.1 tls ep--;
1364 1.1 tls ep->e_combo = cbp;
1365 1.1 tls *--cbpp = cbp->c_link[1];
1366 1.1 tls ep->e_off = cbp->c_voff[1];
1367 1.1 tls ep->e_frameindex = cbp->c_frameindex;
1368 1.1 tls ep->e_fval.s = cbp->c_linkv[1].s;
1369 1.1 tls ep->e_framecnt = cbp->c_framecnt[1];
1370 1.1 tls ep->e_emask = cbp->c_emask[1];
1371 1.1 tls }
1372 1.1 tls cbp = ep->e_combo;
1373 1.1 tls ep--;
1374 1.1 tls ep->e_combo = cbp;
1375 1.1 tls *--cbpp = cbp->c_link[0];
1376 1.1 tls ep->e_off = cbp->c_voff[0];
1377 1.1 tls ep->e_frameindex = 0;
1378 1.1 tls ep->e_fval.s = cbp->c_linkv[0].s;
1379 1.1 tls ep->e_framecnt = cbp->c_framecnt[0];
1380 1.1 tls ep->e_emask = cbp->c_emask[0];
1381 1.1 tls
1382 1.1 tls /* now update the emask info */
1383 1.1 tls s = 0;
1384 1.1 tls for (i = 2, ep += 2; i < nframes; i++, ep++) {
1385 1.1 tls cbp = ep->e_combo;
1386 1.1 tls nep = &einfo[ep->e_frameindex];
1387 1.1 tls nep->e_framecnt = cbp->c_framecnt[0];
1388 1.1 tls nep->e_emask = cbp->c_emask[0];
1389 1.1 tls
1390 1.1 tls if (cbp->c_flg & C_LOOP) {
1391 1.1 tls s++;
1392 1.1 tls /*
1393 1.1 tls * Account for the fact that this frame connects
1394 1.1 tls * to a previous one (thus forming a loop).
1395 1.1 tls */
1396 1.1 tls nep = &einfo[cbp->c_dir];
1397 1.1 tls if (--nep->e_framecnt)
1398 1.1 tls nep->e_emask &= ~(1 << cbp->c_voff[0]);
1399 1.1 tls else
1400 1.1 tls nep->e_emask = 0;
1401 1.1 tls }
1402 1.1 tls }
1403 1.1 tls
1404 1.1 tls /*
1405 1.1 tls * We only need to update the emask values of "complete" loops
1406 1.1 tls * to include the intersection spots.
1407 1.1 tls */
1408 1.1 tls if (s && ocbp->c_combo.c.a == 2) {
1409 1.1 tls /* process loops from the top down */
1410 1.1 tls ep = &einfo[nframes];
1411 1.1 tls do {
1412 1.1 tls ep--;
1413 1.1 tls cbp = ep->e_combo;
1414 1.1 tls if (!(cbp->c_flg & C_LOOP))
1415 1.1 tls continue;
1416 1.1 tls
1417 1.1 tls /*
1418 1.1 tls * Update the emask values to include the
1419 1.1 tls * intersection spots.
1420 1.1 tls */
1421 1.1 tls nep = &einfo[cbp->c_dir];
1422 1.1 tls nep->e_framecnt = 1;
1423 1.1 tls nep->e_emask = 1 << cbp->c_voff[0];
1424 1.1 tls ep->e_framecnt = 1;
1425 1.1 tls ep->e_emask = 1 << ep->e_off;
1426 1.1 tls ep = &einfo[ep->e_frameindex];
1427 1.1 tls do {
1428 1.1 tls ep->e_framecnt = 1;
1429 1.1 tls ep->e_emask = 1 << ep->e_off;
1430 1.1 tls ep = &einfo[ep->e_frameindex];
1431 1.1 tls } while (ep > nep);
1432 1.1 tls } while (ep != einfo);
1433 1.1 tls }
1434 1.1 tls
1435 1.1 tls /* mark all the frames with the completion spots */
1436 1.1 tls for (i = 0, ep = einfo, cbpp = ecombo; i < nframes; i++, ep++, cbpp++) {
1437 1.1 tls m = ep->e_emask;
1438 1.1 tls cbp = *cbpp;
1439 1.1 tls sp = &board[cbp->c_vertex];
1440 1.1 tls d = dd[s = cbp->c_dir];
1441 1.1 tls cmask = CFLAG << s;
1442 1.1 tls omask = (IFLAG | CFLAG) << s;
1443 1.1 tls s = ep->e_fval.c.b ? 6 : 5;
1444 1.1 tls for (; --s >= 0; sp += d, m >>= 1)
1445 1.1 tls sp->s_flg |= (m & 1) ? omask : cmask;
1446 1.1 tls }
1447 1.1 tls }
1448 1.1 tls
1449 1.1 tls clearcombo(cbp, open)
1450 1.1 tls struct combostr *cbp;
1451 1.1 tls int open;
1452 1.1 tls {
1453 1.1 tls register struct spotstr *sp;
1454 1.1 tls struct combostr *tcbp;
1455 1.1 tls int d, n, mask;
1456 1.1 tls
1457 1.1 tls for (; tcbp = cbp->c_link[1]; cbp = cbp->c_link[0]) {
1458 1.1 tls clearcombo(tcbp, cbp->c_flg & C_OPEN_1);
1459 1.1 tls open = cbp->c_flg & C_OPEN_0;
1460 1.1 tls }
1461 1.1 tls sp = &board[cbp->c_vertex];
1462 1.1 tls d = dd[n = cbp->c_dir];
1463 1.1 tls mask = ~((IFLAG | CFLAG) << n);
1464 1.1 tls n = open ? 6 : 5;
1465 1.1 tls for (; --n >= 0; sp += d)
1466 1.1 tls sp->s_flg &= mask;
1467 1.1 tls }
1468 1.1 tls
1469 1.1 tls list_eq(scbpp, cbpp, n)
1470 1.1 tls struct combostr **scbpp;
1471 1.1 tls struct combostr **cbpp;
1472 1.1 tls int n;
1473 1.1 tls {
1474 1.1 tls struct combostr **spp, **cpp;
1475 1.1 tls
1476 1.1 tls spp = scbpp + n;
1477 1.1 tls cpp = cbpp + n;
1478 1.1 tls do {
1479 1.1 tls if (*--spp != *--cpp)
1480 1.1 tls return (0);
1481 1.1 tls } while (cpp != cbpp);
1482 1.1 tls /* we found a match */
1483 1.1 tls return (1);
1484 1.1 tls }
1485 1.1 tls #endif /* DEBUG */
1486