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