Revert "Moggy ko check: Aside of connecting the ko, also consider countercapturing...
[pachi.git] / playout / moggy.c
blob83945a5c84fc3dd219e565f5e8e2001d18b6516b
1 /* Heuristical playout (and tree prior) policy modelled primarily after
2 * the description of the Mogo engine. */
4 #include <assert.h>
5 #include <math.h>
6 #include <stdio.h>
7 #include <stdlib.h>
9 #define DEBUG
10 #include "board.h"
11 #include "debug.h"
12 #include "joseki/base.h"
13 #include "mq.h"
14 #include "pattern3.h"
15 #include "playout.h"
16 #include "playout/moggy.h"
17 #include "random.h"
18 #include "tactics/1lib.h"
19 #include "tactics/2lib.h"
20 #include "tactics/ladder.h"
21 #include "tactics/selfatari.h"
22 #include "uct/prior.h"
24 #define PLDEBUGL(n) DEBUGL_(p->debug_level, n)
27 /* In case "seqchoose" move picker is enabled (i.e. no "fullchoose"
28 * parameter passed), we stochastically apply fixed set of decision
29 * rules in given order.
31 * In "fullchoose" mode, we instead build a move queue of variously
32 * tagged candidates, then consider a probability distribution over
33 * them and pick a move from that. */
35 /* Move queue tags. Some may be even undesirable - these moves then
36 * receive a penalty; penalty tags should be used only when it is
37 * certain the move would be considered anyway. */
38 enum mq_tag {
39 MQ_KO = 0,
40 MQ_LATARI,
41 MQ_L2LIB,
42 MQ_PAT3,
43 MQ_GATARI,
44 MQ_JOSEKI,
45 MQ_MAX
49 /* Note that the context can be shared by multiple threads! */
51 struct moggy_policy {
52 unsigned int lcapturerate, atarirate, capturerate, patternrate, korate, josekirate;
53 unsigned int selfatarirate, alwaysccaprate;
54 unsigned int fillboardtries;
55 int koage;
56 /* Whether to look for patterns around second-to-last move. */
57 bool pattern2;
58 /* Whether, when self-atari attempt is detected, to play the other
59 * group's liberty if that is non-self-atari. */
60 bool selfatari_other;
61 /* Whether to always pick from moves capturing all groups in
62 * global_atari_check(). */
63 bool capcheckall;
64 /* 2lib settings: */
65 bool atari_def_no_hopeless;
66 bool atari_miaisafe;
68 struct joseki_dict *jdict;
69 struct pattern3s patterns;
71 /* Gamma values for queue tags - correspond to probabilities. */
72 /* XXX: Tune. */
73 double mq_prob[MQ_MAX], tenuki_prob;
77 static char moggy_patterns_src[][11] = {
78 /* hane pattern - enclosing hane */
79 "XOX"
80 "..."
81 "???",
82 /* hane pattern - non-cutting hane */
83 "YO."
84 "..."
85 "?.?",
86 /* hane pattern - magari */
87 "XO?"
88 "X.."
89 "x.?",
90 /* hane pattern - thin hane */
91 "XOO"
92 "..."
93 "?.?" "X",
94 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
95 ".Q."
96 "Y.."
97 "...",
98 /* cut1 pattern (kiri) - unprotected cut */
99 "XO?"
100 "O.o"
101 "?o?",
102 /* cut1 pattern (kiri) - peeped cut */
103 "XO?"
104 "O.X"
105 "???",
106 /* cut2 pattern (de) */
107 "?X?"
108 "O.O"
109 "ooo",
110 /* cut keima (not in Mogo) */
111 "OX?"
112 "o.O"
113 "???", /* o?? has some pathological tsumego cases */
114 /* side pattern - chase */
115 "X.?"
116 "O.?"
117 "##?",
118 /* side pattern - block side cut */
119 "OX?"
120 "X.O"
121 "###",
122 /* side pattern - block side connection */
123 "?X?"
124 "x.O"
125 "###",
126 /* side pattern - sagari (SUSPICIOUS) */
127 "?XQ"
128 "x.x" /* Mogo has "x.?" */
129 "###" /* Mogo has "X" */,
130 /* side pattern - throw-in (SUSPICIOUS) */
131 #if 0
132 "?OX"
133 "o.O"
134 "?##" "X",
135 #endif
136 /* side pattern - cut (SUSPICIOUS) */
137 "?OY"
138 "Y.O"
139 "###" /* Mogo has "X" */,
140 /* side pattern - eye piercing:
141 * # O O O .
142 * # O . O .
143 * # . . . .
144 * # # # # # */
145 #if 0
146 "Oxx"
147 "..."
148 "###",
149 #endif
151 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
153 static inline bool
154 test_pattern3_here(struct playout_policy *p, struct board *b, struct move *m)
156 struct moggy_policy *pp = p->data;
157 /* Check if 3x3 pattern is matched by given move... */
158 if (!pattern3_move_here(&pp->patterns, b, m))
159 return false;
160 /* ...and the move is not obviously stupid. */
161 if (is_bad_selfatari(b, m->color, m->coord))
162 return false;
163 /* Ladder moves are stupid. */
164 group_t atari_neighbor = board_get_atari_neighbor(b, m->coord, m->color);
165 if (atari_neighbor && is_ladder(b, m->coord, atari_neighbor))
166 return false;
167 return true;
170 static void
171 apply_pattern_here(struct playout_policy *p, struct board *b, coord_t c, enum stone color, struct move_queue *q)
173 struct move m2 = { .coord = c, .color = color };
174 if (board_is_valid_move(b, &m2) && test_pattern3_here(p, b, &m2))
175 mq_add(q, c, 1<<MQ_PAT3);
178 /* Check if we match any pattern around given move (with the other color to play). */
179 static void
180 apply_pattern(struct playout_policy *p, struct board *b, struct move *m, struct move *mm, struct move_queue *q)
182 /* Suicides do not make any patterns and confuse us. */
183 if (board_at(b, m->coord) == S_NONE || board_at(b, m->coord) == S_OFFBOARD)
184 return;
186 foreach_8neighbor(b, m->coord) {
187 apply_pattern_here(p, b, c, stone_other(m->color), q);
188 } foreach_8neighbor_end;
190 if (mm) { /* Second move for pattern searching */
191 foreach_8neighbor(b, mm->coord) {
192 if (coord_is_8adjecent(m->coord, c, b))
193 continue;
194 apply_pattern_here(p, b, c, stone_other(m->color), q);
195 } foreach_8neighbor_end;
198 if (PLDEBUGL(5))
199 mq_print(q, b, "Pattern");
203 static void
204 joseki_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
206 struct moggy_policy *pp = p->data;
207 if (!pp->jdict)
208 return;
210 for (int i = 0; i < 4; i++) {
211 hash_t h = b->qhash[i] & joseki_hash_mask;
212 coord_t *cc = pp->jdict->patterns[h].moves[to_play];
213 if (!cc) continue;
214 for (; !is_pass(*cc); cc++) {
215 if (coord_quadrant(*cc, b) != i)
216 continue;
217 mq_add(q, *cc, 1<<MQ_JOSEKI);
221 if (q->moves > 0 && PLDEBUGL(5))
222 mq_print(q, b, "Joseki");
225 static void
226 global_atari_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
228 if (b->clen == 0)
229 return;
231 struct moggy_policy *pp = p->data;
232 if (pp->capcheckall) {
233 for (int g = 0; g < b->clen; g++)
234 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
235 if (PLDEBUGL(5))
236 mq_print(q, b, "Global atari");
237 return;
240 int g_base = fast_random(b->clen);
241 for (int g = g_base; g < b->clen; g++) {
242 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
243 if (q->moves > 0) {
244 /* XXX: Try carrying on. */
245 if (PLDEBUGL(5))
246 mq_print(q, b, "Global atari");
247 return;
250 for (int g = 0; g < g_base; g++) {
251 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
252 if (q->moves > 0) {
253 /* XXX: Try carrying on. */
254 if (PLDEBUGL(5))
255 mq_print(q, b, "Global atari");
256 return;
259 return;
262 static void
263 local_atari_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
265 struct moggy_policy *pp = p->data;
267 /* Did the opponent play a self-atari? */
268 if (board_group_info(b, group_at(b, m->coord)).libs == 1) {
269 group_atari_check(pp->alwaysccaprate, b, group_at(b, m->coord), stone_other(m->color), q, NULL, 1<<MQ_LATARI);
272 foreach_neighbor(b, m->coord, {
273 group_t g = group_at(b, c);
274 if (!g || board_group_info(b, g).libs != 1)
275 continue;
276 group_atari_check(pp->alwaysccaprate, b, g, stone_other(m->color), q, NULL, 1<<MQ_LATARI);
279 if (PLDEBUGL(5))
280 mq_print(q, b, "Local atari");
284 static void
285 local_2lib_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
287 struct moggy_policy *pp = p->data;
289 /* Does the opponent have just two liberties? */
290 if (board_group_info(b, group_at(b, m->coord)).libs == 2) {
291 group_2lib_check(b, group_at(b, m->coord), stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
292 #if 0
293 /* We always prefer to take off an enemy chain liberty
294 * before pulling out ourselves. */
295 /* XXX: We aren't guaranteed to return to that group
296 * later. */
297 if (q->moves)
298 return q->move[fast_random(q->moves)];
299 #endif
302 /* Then he took a third liberty from neighboring chain? */
303 foreach_neighbor(b, m->coord, {
304 group_t g = group_at(b, c);
305 if (!g || board_group_info(b, g).libs != 2)
306 continue;
307 group_2lib_check(b, g, stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
310 if (PLDEBUGL(5))
311 mq_print(q, b, "Local 2lib");
314 coord_t
315 fillboard_check(struct playout_policy *p, struct board *b)
317 struct moggy_policy *pp = p->data;
318 unsigned int fbtries = b->flen / 8;
319 if (pp->fillboardtries < fbtries)
320 fbtries = pp->fillboardtries;
322 for (unsigned int i = 0; i < fbtries; i++) {
323 coord_t coord = b->f[fast_random(b->flen)];
324 if (immediate_liberty_count(b, coord) != 4)
325 continue;
326 foreach_diag_neighbor(b, coord) {
327 if (board_at(b, c) != S_NONE)
328 goto next_try;
329 } foreach_diag_neighbor_end;
330 return coord;
331 next_try:;
333 return pass;
336 coord_t
337 playout_moggy_seqchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
339 struct moggy_policy *pp = p->data;
341 if (PLDEBUGL(5))
342 board_print(b, stderr);
344 /* Ko fight check */
345 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
346 && b->moves - b->last_ko_age < pp->koage
347 && pp->korate > fast_random(100)) {
348 if (board_is_valid_play(b, to_play, b->last_ko.coord)
349 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
350 return b->last_ko.coord;
353 /* Local checks */
354 if (!is_pass(b->last_move.coord)) {
355 /* Local group in atari? */
356 if (pp->lcapturerate > fast_random(100)) {
357 struct move_queue q; q.moves = 0;
358 local_atari_check(p, b, &b->last_move, &q);
359 if (q.moves > 0)
360 return mq_pick(&q);
363 /* Local group can be PUT in atari? */
364 if (pp->atarirate > fast_random(100)) {
365 struct move_queue q; q.moves = 0;
366 local_2lib_check(p, b, &b->last_move, &q);
367 if (q.moves > 0)
368 return mq_pick(&q);
371 /* Check for patterns we know */
372 if (pp->patternrate > fast_random(100)) {
373 struct move_queue q; q.moves = 0;
374 apply_pattern(p, b, &b->last_move,
375 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
376 &q);
377 if (q.moves > 0)
378 return mq_pick(&q);
382 /* Global checks */
384 /* Any groups in atari? */
385 if (pp->capturerate > fast_random(100)) {
386 struct move_queue q; q.moves = 0;
387 global_atari_check(p, b, to_play, &q);
388 if (q.moves > 0)
389 return mq_pick(&q);
392 /* Joseki moves? */
393 if (pp->josekirate > fast_random(100)) {
394 struct move_queue q; q.moves = 0;
395 joseki_check(p, b, to_play, &q);
396 if (q.moves > 0)
397 return mq_pick(&q);
400 /* Fill board */
401 if (pp->fillboardtries > 0) {
402 coord_t c = fillboard_check(p, b);
403 if (!is_pass(c))
404 return c;
407 return pass;
410 /* Pick a move from queue q, giving different likelihoods to moves
411 * based on their tags. */
412 coord_t
413 mq_tagged_choose(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
415 struct moggy_policy *pp = p->data;
417 /* First, merge all entries for a move. */
418 /* We use a naive O(N^2) since the average length of the queue
419 * is about 1.4. */
420 for (unsigned int i = 0; i < q->moves; i++) {
421 for (unsigned int j = i + 1; j < q->moves; j++) {
422 if (q->move[i] != q->move[j])
423 continue;
424 q->tag[i] |= q->tag[j];
425 q->moves--;
426 q->tag[j] = q->tag[q->moves];
427 q->move[j] = q->move[q->moves];
431 /* Now, construct a probdist. */
432 fixp_t total = 0;
433 fixp_t pd[q->moves];
434 for (unsigned int i = 0; i < q->moves; i++) {
435 double val = 1.0;
436 assert(q->tag[i] != 0);
437 for (int j = 0; j < MQ_MAX; j++)
438 if (q->tag[i] & (1<<j)) {
439 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
440 val *= pp->mq_prob[j];
442 pd[i] = double_to_fixp(val);
443 total += pd[i];
445 total += double_to_fixp(pp->tenuki_prob);
447 /* Finally, pick a move! */
448 fixp_t stab = fast_irandom(total);
449 for (unsigned int i = 0; i < q->moves; i++) {
450 //fprintf(stderr, "%s(%x) %f (%f/%f)\n", coord2sstr(q->move[i], b), q->tag[i], fixp_to_double(stab), fixp_to_double(pd[i]), fixp_to_double(total));
451 if (stab < pd[i])
452 return q->move[i];
453 stab -= pd[i];
456 /* Tenuki. */
457 assert(stab < double_to_fixp(pp->tenuki_prob));
458 return pass;
461 coord_t
462 playout_moggy_fullchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
464 struct moggy_policy *pp = p->data;
465 struct move_queue q; q.moves = 0;
467 if (PLDEBUGL(5))
468 board_print(b, stderr);
470 /* Ko fight check */
471 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
472 && b->moves - b->last_ko_age < pp->koage) {
473 if (board_is_valid_play(b, to_play, b->last_ko.coord)
474 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
475 mq_add(&q, b->last_ko.coord, 1<<MQ_KO);
478 /* Local checks */
479 if (!is_pass(b->last_move.coord)) {
480 /* Local group in atari? */
481 local_atari_check(p, b, &b->last_move, &q);
483 /* Local group can be PUT in atari? */
484 local_2lib_check(p, b, &b->last_move, &q);
486 /* Check for patterns we know */
487 apply_pattern(p, b, &b->last_move,
488 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
489 &q);
492 /* Global checks */
494 /* Any groups in atari? */
495 global_atari_check(p, b, to_play, &q);
497 /* Joseki moves? */
498 joseki_check(p, b, to_play, &q);
500 #if 0
501 /* Average length of the queue is 1.4 move. */
502 printf("MQL %d ", q.moves);
503 for (unsigned int i = 0; i < q.moves; i++)
504 printf("%s ", coord2sstr(q.move[i], b));
505 printf("\n");
506 #endif
508 if (q.moves > 0)
509 return mq_tagged_choose(p, b, to_play, &q);
511 /* Fill board */
512 if (pp->fillboardtries > 0) {
513 coord_t c = fillboard_check(p, b);
514 if (!is_pass(c))
515 return c;
518 return pass;
522 void
523 playout_moggy_assess_group(struct playout_policy *p, struct prior_map *map, group_t g, int games)
525 struct moggy_policy *pp = p->data;
526 struct board *b = map->b;
527 struct move_queue q; q.moves = 0;
529 if (board_group_info(b, g).libs > 2)
530 return;
532 if (PLDEBUGL(5)) {
533 fprintf(stderr, "ASSESS of group %s:\n", coord2sstr(g, b));
534 board_print(b, stderr);
537 if (board_group_info(b, g).libs == 2) {
538 if (!pp->atarirate)
539 return;
540 group_2lib_check(b, g, map->to_play, &q, 0, pp->atari_miaisafe, pp->atari_def_no_hopeless);
541 while (q.moves--) {
542 coord_t coord = q.move[q.moves];
543 if (PLDEBUGL(5))
544 fprintf(stderr, "1.0: 2lib %s\n", coord2sstr(coord, b));
545 int assess = games / 2;
546 add_prior_value(map, coord, 1, assess);
548 return;
551 /* This group, sir, is in atari! */
553 coord_t ladder = pass;
554 group_atari_check(pp->alwaysccaprate, b, g, map->to_play, &q, &ladder, 0);
555 while (q.moves--) {
556 coord_t coord = q.move[q.moves];
558 /* _Never_ play here if this move plays out
559 * a caught ladder. */
560 if (coord == ladder && !board_playing_ko_threat(b)) {
561 /* Note that the opposite is not guarded against;
562 * we do not advise against capturing a laddered
563 * group (but we don't encourage it either). Such
564 * a move can simplify tactical situations if we
565 * can afford it. */
566 if (map->to_play != board_at(b, g))
567 continue;
568 /* FIXME: We give the malus even if this move
569 * captures another group. */
570 if (PLDEBUGL(5))
571 fprintf(stderr, "0.0: ladder %s\n", coord2sstr(coord, b));
572 add_prior_value(map, coord, 0, games);
573 continue;
576 if (!pp->capturerate && !pp->lcapturerate)
577 continue;
579 if (PLDEBUGL(5))
580 fprintf(stderr, "1.0: atari %s\n", coord2sstr(coord, b));
581 int assess = games * 2;
582 add_prior_value(map, coord, 1, assess);
586 void
587 playout_moggy_assess_one(struct playout_policy *p, struct prior_map *map, coord_t coord, int games)
589 struct moggy_policy *pp = p->data;
590 struct board *b = map->b;
592 if (PLDEBUGL(5)) {
593 fprintf(stderr, "ASSESS of move %s:\n", coord2sstr(coord, b));
594 board_print(b, stderr);
597 /* Is this move a self-atari? */
598 if (pp->selfatarirate) {
599 if (!board_playing_ko_threat(b) && is_bad_selfatari(b, map->to_play, coord)) {
600 if (PLDEBUGL(5))
601 fprintf(stderr, "0.0: self-atari\n");
602 add_prior_value(map, coord, 0, games);
603 if (!pp->selfatari_other)
604 return;
605 /* If we can play on the other liberty of the
606 * endangered group, do! */
607 coord = selfatari_cousin(b, map->to_play, coord);
608 if (is_pass(coord))
609 return;
610 if (PLDEBUGL(5))
611 fprintf(stderr, "1.0: self-atari redirect %s\n", coord2sstr(coord, b));
612 add_prior_value(map, coord, 1.0, games);
613 return;
617 /* Pattern check */
618 if (pp->patternrate) {
619 struct move m = { .color = map->to_play, .coord = coord };
620 if (test_pattern3_here(p, b, &m)) {
621 if (PLDEBUGL(5))
622 fprintf(stderr, "1.0: pattern\n");
623 add_prior_value(map, coord, 1, games);
627 return;
630 void
631 playout_moggy_assess(struct playout_policy *p, struct prior_map *map, int games)
633 struct moggy_policy *pp = p->data;
635 /* First, go through all endangered groups. */
636 for (group_t g = 1; g < board_size2(map->b); g++)
637 if (group_at(map->b, g) == g)
638 playout_moggy_assess_group(p, map, g, games);
640 /* Then, assess individual moves. */
641 if (!pp->patternrate && !pp->selfatarirate)
642 return;
643 foreach_free_point(map->b) {
644 if (map->consider[c])
645 playout_moggy_assess_one(p, map, c, games);
646 } foreach_free_point_end;
649 bool
650 playout_moggy_permit(struct playout_policy *p, struct board *b, struct move *m)
652 struct moggy_policy *pp = p->data;
654 /* The idea is simple for now - never allow self-atari moves.
655 * They suck in general, but this also permits us to actually
656 * handle seki in the playout stage. */
658 if (fast_random(100) >= pp->selfatarirate) {
659 if (PLDEBUGL(5))
660 fprintf(stderr, "skipping sar test\n");
661 return true;
663 bool selfatari = is_bad_selfatari(b, m->color, m->coord);
664 if (selfatari) {
665 if (PLDEBUGL(5))
666 fprintf(stderr, "__ Prohibiting self-atari %s %s\n",
667 stone2str(m->color), coord2sstr(m->coord, b));
668 if (pp->selfatari_other) {
669 /* Ok, try the other liberty of the atari'd group. */
670 coord_t c = selfatari_cousin(b, m->color, m->coord);
671 if (is_pass(c)) return false;
672 if (PLDEBUGL(5))
673 fprintf(stderr, "___ Redirecting to other lib %s\n",
674 coord2sstr(c, b));
675 m->coord = c;
676 return true;
678 return false;
680 return true;
684 struct playout_policy *
685 playout_moggy_init(char *arg, struct board *b, struct joseki_dict *jdict)
687 struct playout_policy *p = calloc2(1, sizeof(*p));
688 struct moggy_policy *pp = calloc2(1, sizeof(*pp));
689 p->data = pp;
690 p->choose = playout_moggy_seqchoose;
691 p->assess = playout_moggy_assess;
692 p->permit = playout_moggy_permit;
694 pp->jdict = jdict;
696 /* These settings are tuned for 19x19 play with several threads
697 * on reasonable time limits (i.e., rather large number of playouts).
698 * XXX: no 9x9 tuning has been done recently. */
699 int rate = board_large(b) ? 80 : 90;
701 pp->lcapturerate = pp->atarirate = pp->patternrate
702 = pp->selfatarirate = pp->josekirate = -1U;
703 if (board_large(b)) {
704 pp->lcapturerate = pp->patternrate = 100;
705 pp->pattern2 = true;
707 pp->korate = 20; pp->koage = 4;
708 pp->alwaysccaprate = 20;
709 pp->selfatari_other = true;
710 pp->atari_def_no_hopeless = true;
711 pp->atari_miaisafe = true;
713 /* C is stupid. */
714 double mq_prob_default[MQ_MAX] = {
715 [MQ_KO] = 6.0,
716 [MQ_LATARI] = 5.0,
717 [MQ_L2LIB] = 4.0,
718 [MQ_PAT3] = 3.0,
719 [MQ_GATARI] = 2.0,
720 [MQ_JOSEKI] = 1.0,
722 memcpy(pp->mq_prob, mq_prob_default, sizeof(pp->mq_prob));
724 if (arg) {
725 char *optspec, *next = arg;
726 while (*next) {
727 optspec = next;
728 next += strcspn(next, ":");
729 if (*next) { *next++ = 0; } else { *next = 0; }
731 char *optname = optspec;
732 char *optval = strchr(optspec, '=');
733 if (optval) *optval++ = 0;
735 if (!strcasecmp(optname, "debug") && optval) {
736 p->debug_level = atoi(optval);
737 } else if (!strcasecmp(optname, "lcapturerate") && optval) {
738 pp->lcapturerate = atoi(optval);
739 } else if (!strcasecmp(optname, "atarirate") && optval) {
740 pp->atarirate = atoi(optval);
741 } else if (!strcasecmp(optname, "capturerate") && optval) {
742 pp->capturerate = atoi(optval);
743 } else if (!strcasecmp(optname, "patternrate") && optval) {
744 pp->patternrate = atoi(optval);
745 } else if (!strcasecmp(optname, "selfatarirate") && optval) {
746 pp->selfatarirate = atoi(optval);
747 } else if (!strcasecmp(optname, "korate") && optval) {
748 pp->korate = atoi(optval);
749 } else if (!strcasecmp(optname, "josekirate") && optval) {
750 pp->josekirate = atoi(optval);
751 } else if (!strcasecmp(optname, "alwaysccaprate") && optval) {
752 pp->alwaysccaprate = atoi(optval);
753 } else if (!strcasecmp(optname, "rate") && optval) {
754 rate = atoi(optval);
755 } else if (!strcasecmp(optname, "fillboardtries")) {
756 pp->fillboardtries = atoi(optval);
757 } else if (!strcasecmp(optname, "koage") && optval) {
758 pp->koage = atoi(optval);
759 } else if (!strcasecmp(optname, "pattern2")) {
760 pp->pattern2 = optval && *optval == '0' ? false : true;
761 } else if (!strcasecmp(optname, "selfatari_other")) {
762 pp->selfatari_other = optval && *optval == '0' ? false : true;
763 } else if (!strcasecmp(optname, "capcheckall")) {
764 pp->capcheckall = optval && *optval == '0' ? false : true;
765 } else if (!strcasecmp(optname, "atari_miaisafe")) {
766 pp->atari_miaisafe = optval && *optval == '0' ? false : true;
767 } else if (!strcasecmp(optname, "atari_def_no_hopeless")) {
768 pp->atari_def_no_hopeless = optval && *optval == '0' ? false : true;
769 } else if (!strcasecmp(optname, "fullchoose")) {
770 p->choose = optval && *optval == '0' ? playout_moggy_seqchoose : playout_moggy_fullchoose;
771 } else if (!strcasecmp(optname, "mqprob") && optval) {
772 /* KO%LATARI%L2LIB%PAT3%GATARI%JOSEKI */
773 for (int i = 0; *optval && i < MQ_MAX; i++, optval += strcspn(optval, "%")) {
774 optval++;
775 pp->mq_prob[i] = atof(optval);
777 } else if (!strcasecmp(optname, "tenukiprob") && optval) {
778 pp->tenuki_prob = atof(optval);
779 } else {
780 fprintf(stderr, "playout-moggy: Invalid policy argument %s or missing value\n", optname);
781 exit(1);
785 if (pp->lcapturerate == -1U) pp->lcapturerate = rate;
786 if (pp->atarirate == -1U) pp->atarirate = rate;
787 if (pp->capturerate == -1U) pp->capturerate = rate;
788 if (pp->patternrate == -1U) pp->patternrate = rate;
789 if (pp->selfatarirate == -1U) pp->selfatarirate = rate;
790 if (pp->korate == -1U) pp->korate = rate;
791 if (pp->josekirate == -1U) pp->josekirate = rate;
792 if (pp->alwaysccaprate == -1U) pp->alwaysccaprate = rate;
794 pattern3s_init(&pp->patterns, moggy_patterns_src, moggy_patterns_src_n);
796 return p;