pickmove.c 38 KB

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