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