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