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