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