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