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