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