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