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