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