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