pickmove.c revision 1.62 1 /* $NetBSD: pickmove.c,v 1.62 2022/05/29 17:01:42 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.62 2022/05/29 17:01:42 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] |= (1U << ((b) % BITS_PER_INT)))
50 #define BIT_TEST(a, b) (((a)[(b)/BITS_PER_INT] & (1U << ((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 player_color 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 unsigned int forcemap[MAPSZ]; /* map for blocking <1,x> combos */
68 static unsigned 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, player_color);
72 static void scanframes(int);
73 static void makecombo2(struct combostr *, struct spotstr *, u_char, u_short);
74 static void addframes(unsigned int);
75 static void makecombo(struct combostr *, struct spotstr *, u_char, u_short);
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 u_short, 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 spot_index
87 pickmove(player_color 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 player_color 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 &&
188 !(Ocp->cv_force <= 1 &&
189 Tcp->cv_force + Tcp->cv_win >= Ocp->cv_force + Ocp->cv_win))
190 return s2;
191 return s1;
192 }
193
194 /*
195 * Return true if spot 's' is better than spot 's1' for color 'us'.
196 */
197 static bool
198 better(spot_index s, spot_index s1, player_color us)
199 {
200 const struct spotstr *sp = &board[s], *sp1 = &board[s1];
201
202 if (/* .... */ sp->s_combo[us].s != sp1->s_combo[us].s)
203 return sp->s_combo[us].s < sp1->s_combo[us].s;
204 if (/* .... */ sp->s_level[us] != sp1->s_level[us])
205 return sp->s_level[us] < sp1->s_level[us];
206 if (/* .... */ sp->s_nforce[us] != sp1->s_nforce[us])
207 return sp->s_nforce[us] > sp1->s_nforce[us];
208
209 player_color them = us != BLACK ? BLACK : WHITE;
210 if (BIT_TEST(forcemap, s) != BIT_TEST(forcemap, s1))
211 return BIT_TEST(forcemap, s);
212
213 if (/* .... */ sp->s_combo[them].s != sp1->s_combo[them].s)
214 return sp->s_combo[them].s < sp1->s_combo[them].s;
215 if (/* .... */ sp->s_level[them] != sp1->s_level[them])
216 return sp->s_level[them] < sp1->s_level[them];
217 if (/* .... */ sp->s_nforce[them] != sp1->s_nforce[them])
218 return sp->s_nforce[them] > sp1->s_nforce[them];
219
220 if (/* .... */ sp->s_wval != sp1->s_wval)
221 return sp->s_wval > sp1->s_wval;
222
223 return (random() & 1) != 0;
224 }
225
226 static int curcolor; /* implicit parameter to makecombo() */
227 static unsigned int curlevel; /* implicit parameter to makecombo() */
228
229 static bool
230 four_in_a_row(int color, spot_index s, direction r)
231 {
232
233 struct spotstr *sp = &board[s];
234 union comboval cb = { .s = sp->s_fval[color][r].s };
235 if (cb.s >= 0x101)
236 return false;
237
238 for (int off = 5 + cb.cv_win, d = dd[r]; off-- > 0; sp += d) {
239 if (sp->s_occ != EMPTY)
240 continue;
241 sp->s_combo[color].s = cb.s;
242 sp->s_level[color] = 1;
243 }
244 return true;
245 }
246
247 /*
248 * Scan the sorted list of non-empty frames and
249 * update the minimum combo values for each empty spot.
250 * Also, try to combine frames to find more complex (chained) moves.
251 */
252 static void
253 scanframes(int color)
254 {
255 struct combostr *ecbp;
256 struct spotstr *sp;
257 union comboval *cp;
258 struct elist *nep;
259 int r, n;
260 union comboval cb;
261
262 curcolor = color;
263
264 /* check for empty list of frames */
265 struct combostr *cbp = sortframes[color];
266 if (cbp == NULL)
267 return;
268
269 if (four_in_a_row(color, cbp->c_vertex, cbp->c_dir))
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 u_char off;
284 if (cp->cv_win != 0) {
285 /*
286 * Since this is the first spot of an open-ended
287 * frame, we treat it as a closed frame.
288 */
289 cb.cv_force = cp->cv_force + 1;
290 cb.cv_win = 0;
291 if (cb.s < sp->s_combo[color].s) {
292 sp->s_combo[color].s = cb.s;
293 sp->s_level[color] = 1;
294 }
295 /*
296 * Try combining other frames that intersect
297 * at this spot.
298 */
299 makecombo2(cbp, sp, 0, cb.s);
300 if (cp->s != 0x101)
301 cb.s = cp->s;
302 else if (color != nextcolor)
303 memset(tmpmap, 0, sizeof(tmpmap));
304 sp += delta;
305 off = 1;
306 } else {
307 cb.s = cp->s;
308 off = 0;
309 }
310 for (; off < 5; off++, sp += delta) { /* for each spot */
311 if (sp->s_occ != EMPTY)
312 continue;
313 if (cp->s < sp->s_combo[color].s) {
314 sp->s_combo[color].s = cp->s;
315 sp->s_level[color] = 1;
316 }
317 if (cp->s == 0x101) {
318 sp->s_nforce[color]++;
319 if (color != nextcolor) {
320 /* XXX: suspicious use of 'n' */
321 n = (spot_index)(sp - board);
322 BIT_SET(tmpmap, (spot_index)n);
323 }
324 }
325 /*
326 * Try combining other frames that intersect
327 * at this spot.
328 */
329 makecombo2(cbp, sp, off, cb.s);
330 }
331 if (cp->s == 0x101 && color != nextcolor) {
332 if (nforce == 0)
333 memcpy(forcemap, tmpmap, sizeof(tmpmap));
334 else {
335 for (int i = 0; (unsigned int)i < MAPSZ; i++)
336 forcemap[i] &= tmpmap[i];
337 }
338 }
339 /* mark frame as having been processed */
340 board[cbp->c_vertex].s_flags |= MFLAG << r;
341 } while ((cbp = cbp->c_next) != ecbp);
342
343 /*
344 * Try to make new 3rd level combos, 4th level, etc.
345 * Limit the search depth early in the game.
346 */
347 for (unsigned int level = 2;
348 level <= 1 + game.nmoves / 2 && combolen > n; level++) {
349 if (level >= 9)
350 break; /* Do not think too long. */
351 if (debug != 0) {
352 debuglog("%cL%u %d %d %d", "BW"[color],
353 level, combolen - n, combocnt, elistcnt);
354 refresh();
355 }
356 n = combolen;
357 addframes(level);
358 }
359
360 /* scan for combos at empty spots */
361 for (unsigned pos = PT(BSZ, BSZ + 1); pos-- > PT(1, 1); ) {
362 sp = &board[pos];
363 for (struct elist *ep = sp->s_empty; ep != NULL; ep = nep) {
364 cbp = ep->e_combo;
365 if (cbp->c_combo.s <= sp->s_combo[color].s) {
366 if (cbp->c_combo.s != sp->s_combo[color].s) {
367 sp->s_combo[color].s = cbp->c_combo.s;
368 sp->s_level[color] = cbp->c_nframes;
369 } else if (cbp->c_nframes < sp->s_level[color])
370 sp->s_level[color] = cbp->c_nframes;
371 }
372 nep = ep->e_next;
373 free(ep);
374 elistcnt--;
375 }
376 sp->s_empty = NULL;
377 for (struct elist *ep = sp->s_nempty; ep != NULL; ep = nep) {
378 cbp = ep->e_combo;
379 if (cbp->c_combo.s <= sp->s_combo[color].s) {
380 if (cbp->c_combo.s != sp->s_combo[color].s) {
381 sp->s_combo[color].s = cbp->c_combo.s;
382 sp->s_level[color] = cbp->c_nframes;
383 } else if (cbp->c_nframes < sp->s_level[color])
384 sp->s_level[color] = cbp->c_nframes;
385 }
386 nep = ep->e_next;
387 free(ep);
388 elistcnt--;
389 }
390 sp->s_nempty = NULL;
391 }
392
393 /* remove old combos */
394 if ((cbp = sortcombos) != NULL) {
395 struct combostr *ncbp;
396
397 /* scan the list */
398 ecbp = cbp;
399 do {
400 ncbp = cbp->c_next;
401 free(cbp);
402 combocnt--;
403 } while ((cbp = ncbp) != ecbp);
404 sortcombos = NULL;
405 }
406 combolen = 0;
407
408 #ifdef DEBUG
409 if (combocnt != 0) {
410 debuglog("scanframes: %c combocnt %d", "BW"[color],
411 combocnt);
412 whatsup(0);
413 }
414 if (elistcnt != 0) {
415 debuglog("scanframes: %c elistcnt %d", "BW"[color],
416 elistcnt);
417 whatsup(0);
418 }
419 #endif
420 }
421
422 /*
423 * Compute all level 2 combos of frames intersecting spot 'osp'
424 * within the frame 'ocbp' and combo value 'cv'.
425 */
426 static void
427 makecombo2(struct combostr *ocbp, struct spotstr *osp, u_char off, u_short cv)
428 {
429 struct combostr *ncbp;
430 int baseB, fcnt, emask;
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
441 for (direction r = 4; r-- > 0; ) {
442 /* don't include frames that overlap in the same direction */
443 if (r == ocbp->c_dir)
444 continue;
445 int d = dd[r];
446 /*
447 * Frame A combined with B is the same value as B combined with A
448 * so skip frames that have already been processed (MFLAG).
449 * Also skip blocked frames (BFLAG) and frames that are <1,x>
450 * since combining another frame with it isn't valid.
451 */
452 int bmask = (BFLAG | FFLAG | MFLAG) << r;
453 struct spotstr *fsp = osp;
454 for (u_char f = 0; f < 5; f++, fsp -= d) { /* for each frame */
455 if (fsp->s_occ == BORDER)
456 break;
457 if ((fsp->s_flags & bmask) != 0)
458 continue;
459
460 /* don't include frames of the wrong color */
461 fcb.s = fsp->s_fval[curcolor][r].s;
462 if (fcb.cv_force >= 6)
463 continue;
464
465 /*
466 * Get the combo value for this frame.
467 * If this is the end point of the frame,
468 * use the closed ended value for the frame.
469 */
470 if ((f == 0 && fcb.cv_win != 0) || fcb.s == 0x101) {
471 fcb.cv_force++;
472 fcb.cv_win = 0;
473 }
474
475 /* compute combo value */
476 int c = fcb.cv_force + ocb.cv_force - 3;
477 if (c > 4)
478 continue;
479 int n = fcb.cv_force + fcb.cv_win - 1;
480 if (baseB < n)
481 n = baseB;
482
483 /* make a new combo! */
484 ncbp = (struct combostr *)malloc(sizeof(struct combostr) +
485 2 * sizeof(struct combostr *));
486 if (ncbp == NULL)
487 panic("Out of memory!");
488 scbpp = (void *)(ncbp + 1);
489 fcbp = &frames[fsp->s_frame[r]];
490 if (ocbp < fcbp) {
491 scbpp[0] = ocbp;
492 scbpp[1] = fcbp;
493 } else {
494 scbpp[0] = fcbp;
495 scbpp[1] = ocbp;
496 }
497 ncbp->c_combo.cv_force = c;
498 ncbp->c_combo.cv_win = n;
499 ncbp->c_link[0] = ocbp;
500 ncbp->c_link[1] = fcbp;
501 ncbp->c_linkv[0].s = ocb.s;
502 ncbp->c_linkv[1].s = fcb.s;
503 ncbp->c_voff[0] = off;
504 ncbp->c_voff[1] = f;
505 ncbp->c_vertex = (spot_index)(osp - board);
506 ncbp->c_nframes = 2;
507 ncbp->c_dir = 0;
508 ncbp->c_frameindex = 0;
509 ncbp->c_flags = ocb.cv_win != 0 ? C_OPEN_0 : 0;
510 if (fcb.cv_win != 0)
511 ncbp->c_flags |= C_OPEN_1;
512 ncbp->c_framecnt[0] = fcnt;
513 ncbp->c_emask[0] = emask;
514 ncbp->c_framecnt[1] = fcb.cv_force - 2;
515 ncbp->c_emask[1] = ncbp->c_framecnt[1] != 0 ?
516 ((fcb.cv_win != 0 ? 0x1E : 0x1F) & ~(1 << f)) : 0;
517 combocnt++;
518
519 if ((c == 1 && debug > 1) || debug > 3) {
520 debuglog("%c c %d %d m %x %x o %d %d",
521 "bw"[curcolor],
522 ncbp->c_framecnt[0], ncbp->c_framecnt[1],
523 ncbp->c_emask[0], ncbp->c_emask[1],
524 ncbp->c_voff[0], ncbp->c_voff[1]);
525 printcombo(ncbp, tmp, sizeof(tmp));
526 debuglog("%s", tmp);
527 }
528 if (c > 1) {
529 /* record the empty spots that will complete this combo */
530 makeempty(ncbp);
531
532 /* add the new combo to the end of the list */
533 appendcombo(ncbp, curcolor);
534 } else {
535 updatecombo(ncbp, curcolor);
536 free(ncbp);
537 combocnt--;
538 }
539 #ifdef DEBUG
540 if ((c == 1 && debug > 1) || debug > 5) {
541 markcombo(ncbp);
542 bdisp();
543 whatsup(0);
544 clearcombo(ncbp, 0);
545 }
546 #endif /* DEBUG */
547 }
548 }
549 }
550
551 /*
552 * Scan the sorted list of frames and try to add a frame to
553 * combinations of 'level' number of frames.
554 */
555 static void
556 addframes(unsigned int level)
557 {
558 struct combostr *cbp, *ecbp;
559 struct spotstr *fsp;
560 struct elist *nep;
561 int r;
562 struct combostr **cbpp, *pcbp;
563 union comboval fcb, cb;
564
565 curlevel = level;
566
567 /* scan for combos at empty spots */
568 int c = curcolor;
569 for (unsigned pos = PT(BSZ, BSZ + 1); pos-- > PT(1, 1); ) {
570 struct spotstr *sp = &board[pos];
571 for (struct elist *ep = sp->s_empty; ep != NULL; ep = nep) {
572 cbp = ep->e_combo;
573 if (cbp->c_combo.s <= sp->s_combo[c].s) {
574 if (cbp->c_combo.s != sp->s_combo[c].s) {
575 sp->s_combo[c].s = cbp->c_combo.s;
576 sp->s_level[c] = cbp->c_nframes;
577 } else if (cbp->c_nframes < sp->s_level[c])
578 sp->s_level[c] = cbp->c_nframes;
579 }
580 nep = ep->e_next;
581 free(ep);
582 elistcnt--;
583 }
584 sp->s_empty = sp->s_nempty;
585 sp->s_nempty = NULL;
586 }
587
588 /* try to add frames to the uncompleted combos at level curlevel */
589 cbp = ecbp = sortframes[curcolor];
590 do {
591 fsp = &board[cbp->c_vertex];
592 r = cbp->c_dir;
593 /* skip frames that are part of a <1,x> combo */
594 if ((fsp->s_flags & (FFLAG << r)) != 0)
595 continue;
596
597 /*
598 * Don't include <1,x> combo frames,
599 * treat it as a closed three in a row instead.
600 */
601 fcb.s = fsp->s_fval[curcolor][r].s;
602 if (fcb.s == 0x101)
603 fcb.s = 0x200;
604
605 /*
606 * If this is an open-ended frame, use
607 * the combo value with the end closed.
608 */
609 if (fsp->s_occ == EMPTY) {
610 if (fcb.cv_win != 0) {
611 cb.cv_force = fcb.cv_force + 1;
612 cb.cv_win = 0;
613 } else
614 cb.s = fcb.s;
615 makecombo(cbp, fsp, 0, cb.s);
616 }
617
618 /*
619 * The next four spots are handled the same for both
620 * open and closed ended frames.
621 */
622 int d = dd[r];
623 struct spotstr *sp = fsp + d;
624 for (u_char off = 1; off < 5; off++, sp += d) {
625 if (sp->s_occ != EMPTY)
626 continue;
627 makecombo(cbp, sp, off, fcb.s);
628 }
629 } while ((cbp = cbp->c_next) != ecbp);
630
631 /* put all the combos in the hash list on the sorted list */
632 cbpp = &hashcombos[FAREA];
633 do {
634 cbp = *--cbpp;
635 if (cbp == NULL)
636 continue;
637 *cbpp = NULL;
638 ecbp = sortcombos;
639 if (ecbp == NULL)
640 sortcombos = cbp;
641 else {
642 /* append to sort list */
643 pcbp = ecbp->c_prev;
644 pcbp->c_next = cbp;
645 ecbp->c_prev = cbp->c_prev;
646 cbp->c_prev->c_next = ecbp;
647 cbp->c_prev = pcbp;
648 }
649 } while (cbpp != hashcombos);
650 }
651
652 /*
653 * Compute all level N combos of frames intersecting spot 'osp'
654 * within the frame 'ocbp' and combo value 'cv'.
655 */
656 static void
657 makecombo(struct combostr *ocbp, struct spotstr *osp, u_char off, u_short cv)
658 {
659 struct combostr *cbp;
660 struct spotstr *sp;
661 struct combostr **scbpp;
662 int baseB, fcnt, emask, verts;
663 union comboval ocb;
664 struct overlap_info vertices[1];
665 char tmp[128];
666
667 /*
668 * XXX: when I made functions static gcc started warning about
669 * some members of vertices[0] maybe being used uninitialized.
670 * For now, I'm just going to clear it rather than wade through
671 * the logic to find out whether gcc or the code is wrong. I
672 * wouldn't be surprised if it were the code though. - dholland
673 */
674 memset(vertices, 0, sizeof(vertices));
675
676 ocb.s = cv;
677 baseB = ocb.cv_force + ocb.cv_win - 1;
678 fcnt = ocb.cv_force - 2;
679 emask = fcnt != 0 ? ((ocb.cv_win != 0 ? 0x1E : 0x1F) & ~(1 << off)) : 0;
680 for (struct elist *ep = osp->s_empty; ep != NULL; ep = ep->e_next) {
681 /* check for various kinds of overlap */
682 cbp = ep->e_combo;
683 verts = checkframes(cbp, ocbp, osp, cv, vertices);
684 if (verts < 0)
685 continue;
686
687 /* check to see if this frame forms a valid loop */
688 if (verts > 0) {
689 sp = &board[vertices[0].o_intersect];
690 #ifdef DEBUG
691 if (sp->s_occ != EMPTY) {
692 debuglog("loop: %c %s", "BW"[curcolor],
693 stoc((spot_index)(sp - board)));
694 whatsup(0);
695 }
696 #endif
697 /*
698 * It is a valid loop if the intersection spot
699 * of the frame we are trying to attach is one
700 * of the completion spots of the combostr
701 * we are trying to attach the frame to.
702 */
703 for (struct elist *nep = sp->s_empty;
704 nep != NULL; nep = nep->e_next) {
705 if (nep->e_combo == cbp)
706 goto fnd;
707 if (nep->e_combo->c_nframes < cbp->c_nframes)
708 break;
709 }
710 /* frame overlaps but not at a valid spot */
711 continue;
712 fnd:
713 ;
714 }
715
716 /* compute the first half of the combo value */
717 int c = cbp->c_combo.cv_force + ocb.cv_force - verts - 3;
718 if (c > 4)
719 continue;
720
721 /* compute the second half of the combo value */
722 int n = ep->e_fval.cv_force + ep->e_fval.cv_win - 1;
723 if (baseB < n)
724 n = baseB;
725
726 /* make a new combo! */
727 struct combostr *ncbp = malloc(sizeof(struct combostr) +
728 (cbp->c_nframes + 1) * sizeof(struct combostr *));
729 if (ncbp == NULL)
730 panic("Out of memory!");
731 scbpp = (void *)(ncbp + 1);
732 if (sortcombo(scbpp, (void *)(cbp + 1), ocbp)) {
733 free(ncbp);
734 continue;
735 }
736 combocnt++;
737
738 ncbp->c_combo.cv_force = c;
739 ncbp->c_combo.cv_win = n;
740 ncbp->c_link[0] = cbp;
741 ncbp->c_link[1] = ocbp;
742 ncbp->c_linkv[1].s = ocb.s;
743 ncbp->c_voff[1] = off;
744 ncbp->c_vertex = (spot_index)(osp - board);
745 ncbp->c_nframes = cbp->c_nframes + 1;
746 ncbp->c_flags = ocb.cv_win != 0 ? C_OPEN_1 : 0;
747 ncbp->c_frameindex = ep->e_frameindex;
748 /*
749 * Update the completion spot mask of the frame we
750 * are attaching 'ocbp' to so the intersection isn't
751 * listed twice.
752 */
753 ncbp->c_framecnt[0] = ep->e_framecnt;
754 ncbp->c_emask[0] = ep->e_emask;
755 if (verts != 0) {
756 ncbp->c_flags |= C_LOOP;
757 ncbp->c_dir = vertices[0].o_frameindex;
758 ncbp->c_framecnt[1] = fcnt - 1;
759 if (ncbp->c_framecnt[1] != 0) {
760 n = (vertices[0].o_intersect - ocbp->c_vertex) /
761 dd[ocbp->c_dir];
762 ncbp->c_emask[1] = emask & ~(1 << n);
763 } else
764 ncbp->c_emask[1] = 0;
765 ncbp->c_voff[0] = vertices[0].o_off;
766 } else {
767 ncbp->c_dir = 0;
768 ncbp->c_framecnt[1] = fcnt;
769 ncbp->c_emask[1] = emask;
770 ncbp->c_voff[0] = ep->e_off;
771 }
772
773 if ((c == 1 && debug > 1) || debug > 3) {
774 debuglog("%c v%d i%d d%d c %d %d m %x %x o %d %d",
775 "bw"[curcolor], verts, ncbp->c_frameindex, ncbp->c_dir,
776 ncbp->c_framecnt[0], ncbp->c_framecnt[1],
777 ncbp->c_emask[0], ncbp->c_emask[1],
778 ncbp->c_voff[0], ncbp->c_voff[1]);
779 printcombo(ncbp, tmp, sizeof(tmp));
780 debuglog("%s", tmp);
781 }
782 if (c > 1) {
783 /* record the empty spots that will complete this combo */
784 makeempty(ncbp);
785 combolen++;
786 } else {
787 /* update board values */
788 updatecombo(ncbp, curcolor);
789 }
790 #ifdef DEBUG
791 if ((c == 1 && debug > 1) || debug > 4) {
792 markcombo(ncbp);
793 bdisp();
794 whatsup(0);
795 clearcombo(ncbp, 0);
796 }
797 #endif /* DEBUG */
798 }
799 }
800
801 #define MAXDEPTH 100
802 static struct elist einfo[MAXDEPTH];
803 static struct combostr *ecombo[MAXDEPTH]; /* separate from elist to save space */
804
805 /*
806 * Add the combostr 'ocbp' to the empty spots list for each empty spot
807 * in 'ocbp' that will complete the combo.
808 */
809 static void
810 makeempty(struct combostr *ocbp)
811 {
812 struct combostr *cbp, **cbpp;
813 struct elist *ep, *nep;
814 struct spotstr *sp;
815 int d, emask, i;
816 int nframes;
817 char tmp[128];
818
819 if (debug > 2) {
820 printcombo(ocbp, tmp, sizeof(tmp));
821 debuglog("E%c %s", "bw"[curcolor], tmp);
822 }
823
824 /* should never happen but check anyway */
825 if ((nframes = ocbp->c_nframes) >= MAXDEPTH)
826 return;
827
828 /*
829 * The lower level combo can be pointed to by more than one
830 * higher level 'struct combostr' so we can't modify the
831 * lower level. Therefore, higher level combos store the
832 * real mask of the lower level frame in c_emask[0] and the
833 * frame number in c_frameindex.
834 *
835 * First we traverse the tree from top to bottom and save the
836 * connection info. Then we traverse the tree from bottom to
837 * top overwriting lower levels with the newer emask information.
838 */
839 ep = &einfo[nframes];
840 cbpp = &ecombo[nframes];
841 for (cbp = ocbp; cbp->c_link[1] != NULL; cbp = cbp->c_link[0]) {
842 ep--;
843 ep->e_combo = cbp;
844 *--cbpp = cbp->c_link[1];
845 ep->e_off = cbp->c_voff[1];
846 ep->e_frameindex = cbp->c_frameindex;
847 ep->e_fval.s = cbp->c_linkv[1].s;
848 ep->e_framecnt = cbp->c_framecnt[1];
849 ep->e_emask = cbp->c_emask[1];
850 }
851 cbp = ep->e_combo;
852 ep--;
853 ep->e_combo = cbp;
854 *--cbpp = cbp->c_link[0];
855 ep->e_off = cbp->c_voff[0];
856 ep->e_frameindex = 0;
857 ep->e_fval.s = cbp->c_linkv[0].s;
858 ep->e_framecnt = cbp->c_framecnt[0];
859 ep->e_emask = cbp->c_emask[0];
860
861 /* now update the emask info */
862 int n = 0;
863 for (i = 2, ep += 2; i < nframes; i++, ep++) {
864 cbp = ep->e_combo;
865 nep = &einfo[ep->e_frameindex];
866 nep->e_framecnt = cbp->c_framecnt[0];
867 nep->e_emask = cbp->c_emask[0];
868
869 if ((cbp->c_flags & C_LOOP) != 0) {
870 n++;
871 /*
872 * Account for the fact that this frame connects
873 * to a previous one (thus forming a loop).
874 */
875 nep = &einfo[cbp->c_dir];
876 if (--nep->e_framecnt != 0)
877 nep->e_emask &= ~(1 << cbp->c_voff[0]);
878 else
879 nep->e_emask = 0;
880 }
881 }
882
883 /*
884 * We only need to update the emask values of "complete" loops
885 * to include the intersection spots.
886 */
887 if (n != 0 && ocbp->c_combo.cv_force == 2) {
888 /* process loops from the top down */
889 ep = &einfo[nframes];
890 do {
891 ep--;
892 cbp = ep->e_combo;
893 if ((cbp->c_flags & C_LOOP) == 0)
894 continue;
895
896 /*
897 * Update the emask values to include the
898 * intersection spots.
899 */
900 nep = &einfo[cbp->c_dir];
901 nep->e_framecnt = 1;
902 nep->e_emask = 1 << cbp->c_voff[0];
903 ep->e_framecnt = 1;
904 ep->e_emask = 1 << ep->e_off;
905 ep = &einfo[ep->e_frameindex];
906 do {
907 ep->e_framecnt = 1;
908 ep->e_emask = 1 << ep->e_off;
909 ep = &einfo[ep->e_frameindex];
910 } while (ep > nep);
911 } while (ep != einfo);
912 }
913
914 /* check all the frames for completion spots */
915 for (i = 0, ep = einfo, cbpp = ecombo; i < nframes; i++, ep++, cbpp++) {
916 /* skip this frame if there are no incomplete spots in it */
917 if ((emask = ep->e_emask) == 0)
918 continue;
919 cbp = *cbpp;
920 sp = &board[cbp->c_vertex];
921 d = dd[cbp->c_dir];
922 for (int off = 0, m = 1; off < 5; off++, sp += d, m <<= 1) {
923 if (sp->s_occ != EMPTY || (emask & m) == 0)
924 continue;
925
926 /* add the combo to the list of empty spots */
927 nep = (struct elist *)malloc(sizeof(struct elist));
928 if (nep == NULL)
929 panic("Out of memory!");
930 nep->e_combo = ocbp;
931 nep->e_off = off;
932 nep->e_frameindex = i;
933 if (ep->e_framecnt > 1) {
934 nep->e_framecnt = ep->e_framecnt - 1;
935 nep->e_emask = emask & ~m;
936 } else {
937 nep->e_framecnt = 0;
938 nep->e_emask = 0;
939 }
940 nep->e_fval.s = ep->e_fval.s;
941 if (debug > 2) {
942 debuglog("e %s o%d i%d c%d m%x %x",
943 stoc((spot_index)(sp - board)),
944 nep->e_off,
945 nep->e_frameindex,
946 nep->e_framecnt,
947 nep->e_emask,
948 nep->e_fval.s);
949 }
950
951 /* sort by the number of frames in the combo */
952 nep->e_next = sp->s_nempty;
953 sp->s_nempty = nep;
954 elistcnt++;
955 }
956 }
957 }
958
959 /*
960 * Update the board value based on the combostr.
961 * This is called only if 'cbp' is a <1,x> combo.
962 * We handle things differently depending on whether the next move
963 * would be trying to "complete" the combo or trying to block it.
964 */
965 static void
966 updatecombo(struct combostr *cbp, int color)
967 {
968 struct combostr *tcbp;
969 union comboval cb;
970
971 int flags = 0;
972 /* save the top level value for the whole combo */
973 cb.cv_force = cbp->c_combo.cv_force;
974 u_char 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 off = (flags & C_OPEN_1) != 0 ? 6 : 5;
999 for (; --off >= 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 off = (flags & C_OPEN_0) != 0 ? 6 : 5;
1024 for (; --off >= 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 (int 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 u_short cv, struct overlap_info *vertices)
1085 {
1086 struct combostr *tcbp, *lcbp;
1087 int ovbit, n, mask, flags, 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 int verts = 0;
1097 u_char myindex = cbp->c_nframes;
1098 n = (frame_index)(fcbp - frames) * FAREA;
1099 str = &overlap[n];
1100 spot_index *ip = &intersect[n];
1101 /*
1102 * ovbit == which overlap bit to test based on whether 'fcbp' is
1103 * an open or closed frame.
1104 */
1105 ovbit = 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 = ovbit + ((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 = ovbit + ((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