2lib: Introduce miaisafe, def_no_hopeless knobs, twiddle by respective Moggy flags
[pachi.git] / playout / moggy.c
blobc3fd5baeddcfa88ffde15709cea8390d9909a50c
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" */,
141 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
143 static inline bool
144 test_pattern3_here(struct playout_policy *p, struct board *b, struct move *m)
146 struct moggy_policy *pp = p->data;
147 /* Check if 3x3 pattern is matched by given move... */
148 if (!pattern3_move_here(&pp->patterns, b, m))
149 return false;
150 /* ...and the move is not obviously stupid. */
151 if (is_bad_selfatari(b, m->color, m->coord))
152 return false;
153 /* Ladder moves are stupid. */
154 group_t atari_neighbor = board_get_atari_neighbor(b, m->coord, m->color);
155 if (atari_neighbor && is_ladder(b, m->coord, atari_neighbor))
156 return false;
157 return true;
160 static void
161 apply_pattern_here(struct playout_policy *p, struct board *b, coord_t c, enum stone color, struct move_queue *q)
163 struct move m2 = { .coord = c, .color = color };
164 if (board_is_valid_move(b, &m2) && test_pattern3_here(p, b, &m2))
165 mq_add(q, c, 1<<MQ_PAT3);
168 /* Check if we match any pattern around given move (with the other color to play). */
169 static void
170 apply_pattern(struct playout_policy *p, struct board *b, struct move *m, struct move *mm, struct move_queue *q)
172 /* Suicides do not make any patterns and confuse us. */
173 if (board_at(b, m->coord) == S_NONE || board_at(b, m->coord) == S_OFFBOARD)
174 return;
176 foreach_8neighbor(b, m->coord) {
177 apply_pattern_here(p, b, c, stone_other(m->color), q);
178 } foreach_8neighbor_end;
180 if (mm) { /* Second move for pattern searching */
181 foreach_8neighbor(b, mm->coord) {
182 if (coord_is_8adjecent(m->coord, c, b))
183 continue;
184 apply_pattern_here(p, b, c, stone_other(m->color), q);
185 } foreach_8neighbor_end;
188 if (PLDEBUGL(5))
189 mq_print(q, b, "Pattern");
193 static void
194 joseki_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
196 struct moggy_policy *pp = p->data;
197 if (!pp->jdict)
198 return;
200 for (int i = 0; i < 4; i++) {
201 hash_t h = b->qhash[i] & joseki_hash_mask;
202 coord_t *cc = pp->jdict->patterns[h].moves[to_play];
203 if (!cc) continue;
204 for (; !is_pass(*cc); cc++) {
205 if (coord_quadrant(*cc, b) != i)
206 continue;
207 mq_add(q, *cc, 1<<MQ_JOSEKI);
211 if (q->moves > 0 && PLDEBUGL(5))
212 mq_print(q, b, "Joseki");
215 static void
216 global_atari_check(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
218 if (b->clen == 0)
219 return;
221 struct moggy_policy *pp = p->data;
222 if (pp->capcheckall) {
223 for (int g = 0; g < b->clen; g++)
224 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
225 if (PLDEBUGL(5))
226 mq_print(q, b, "Global atari");
227 return;
230 int g_base = fast_random(b->clen);
231 for (int g = g_base; g < b->clen; g++) {
232 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
233 if (q->moves > 0) {
234 /* XXX: Try carrying on. */
235 if (PLDEBUGL(5))
236 mq_print(q, b, "Global atari");
237 return;
240 for (int g = 0; g < g_base; g++) {
241 group_atari_check(pp->alwaysccaprate, b, group_at(b, group_base(b->c[g])), to_play, q, NULL, 1<<MQ_GATARI);
242 if (q->moves > 0) {
243 /* XXX: Try carrying on. */
244 if (PLDEBUGL(5))
245 mq_print(q, b, "Global atari");
246 return;
249 return;
252 static void
253 local_atari_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
255 struct moggy_policy *pp = p->data;
257 /* Did the opponent play a self-atari? */
258 if (board_group_info(b, group_at(b, m->coord)).libs == 1) {
259 group_atari_check(pp->alwaysccaprate, b, group_at(b, m->coord), stone_other(m->color), q, NULL, 1<<MQ_LATARI);
262 foreach_neighbor(b, m->coord, {
263 group_t g = group_at(b, c);
264 if (!g || board_group_info(b, g).libs != 1)
265 continue;
266 group_atari_check(pp->alwaysccaprate, b, g, stone_other(m->color), q, NULL, 1<<MQ_LATARI);
269 if (PLDEBUGL(5))
270 mq_print(q, b, "Local atari");
274 static void
275 local_2lib_check(struct playout_policy *p, struct board *b, struct move *m, struct move_queue *q)
277 struct moggy_policy *pp = p->data;
279 /* Does the opponent have just two liberties? */
280 if (board_group_info(b, group_at(b, m->coord)).libs == 2) {
281 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);
282 #if 0
283 /* We always prefer to take off an enemy chain liberty
284 * before pulling out ourselves. */
285 /* XXX: We aren't guaranteed to return to that group
286 * later. */
287 if (q->moves)
288 return q->move[fast_random(q->moves)];
289 #endif
292 /* Then he took a third liberty from neighboring chain? */
293 foreach_neighbor(b, m->coord, {
294 group_t g = group_at(b, c);
295 if (!g || board_group_info(b, g).libs != 2)
296 continue;
297 group_2lib_check(b, g, stone_other(m->color), q, 1<<MQ_L2LIB, pp->atari_miaisafe, pp->atari_def_no_hopeless);
300 if (PLDEBUGL(5))
301 mq_print(q, b, "Local 2lib");
304 coord_t
305 fillboard_check(struct playout_policy *p, struct board *b)
307 struct moggy_policy *pp = p->data;
308 unsigned int fbtries = b->flen / 8;
309 if (pp->fillboardtries < fbtries)
310 fbtries = pp->fillboardtries;
312 for (unsigned int i = 0; i < fbtries; i++) {
313 coord_t coord = b->f[fast_random(b->flen)];
314 if (immediate_liberty_count(b, coord) != 4)
315 continue;
316 foreach_diag_neighbor(b, coord) {
317 if (board_at(b, c) != S_NONE)
318 goto next_try;
319 } foreach_diag_neighbor_end;
320 return coord;
321 next_try:;
323 return pass;
326 coord_t
327 playout_moggy_seqchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
329 struct moggy_policy *pp = p->data;
331 if (PLDEBUGL(5))
332 board_print(b, stderr);
334 /* Ko fight check */
335 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
336 && b->moves - b->last_ko_age < pp->koage
337 && pp->korate > fast_random(100)) {
338 if (board_is_valid_play(b, to_play, b->last_ko.coord)
339 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
340 return b->last_ko.coord;
343 /* Local checks */
344 if (!is_pass(b->last_move.coord)) {
345 /* Local group in atari? */
346 if (pp->lcapturerate > fast_random(100)) {
347 struct move_queue q; q.moves = 0;
348 local_atari_check(p, b, &b->last_move, &q);
349 if (q.moves > 0)
350 return mq_pick(&q);
353 /* Local group can be PUT in atari? */
354 if (pp->atarirate > fast_random(100)) {
355 struct move_queue q; q.moves = 0;
356 local_2lib_check(p, b, &b->last_move, &q);
357 if (q.moves > 0)
358 return mq_pick(&q);
361 /* Check for patterns we know */
362 if (pp->patternrate > fast_random(100)) {
363 struct move_queue q; q.moves = 0;
364 apply_pattern(p, b, &b->last_move,
365 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
366 &q);
367 if (q.moves > 0)
368 return mq_pick(&q);
372 /* Global checks */
374 /* Any groups in atari? */
375 if (pp->capturerate > fast_random(100)) {
376 struct move_queue q; q.moves = 0;
377 global_atari_check(p, b, to_play, &q);
378 if (q.moves > 0)
379 return mq_pick(&q);
382 /* Joseki moves? */
383 if (pp->josekirate > fast_random(100)) {
384 struct move_queue q; q.moves = 0;
385 joseki_check(p, b, to_play, &q);
386 if (q.moves > 0)
387 return mq_pick(&q);
390 /* Fill board */
391 if (pp->fillboardtries > 0) {
392 coord_t c = fillboard_check(p, b);
393 if (!is_pass(c))
394 return c;
397 return pass;
400 /* Pick a move from queue q, giving different likelihoods to moves
401 * based on their tags. */
402 coord_t
403 mq_tagged_choose(struct playout_policy *p, struct board *b, enum stone to_play, struct move_queue *q)
405 struct moggy_policy *pp = p->data;
407 /* First, merge all entries for a move. */
408 /* We use a naive O(N^2) since the average length of the queue
409 * is about 1.4. */
410 for (unsigned int i = 0; i < q->moves; i++) {
411 for (unsigned int j = i + 1; j < q->moves; j++) {
412 if (q->move[i] != q->move[j])
413 continue;
414 q->tag[i] |= q->tag[j];
415 q->moves--;
416 q->tag[j] = q->tag[q->moves];
417 q->move[j] = q->move[q->moves];
421 /* Now, construct a probdist. */
422 fixp_t total = 0;
423 fixp_t pd[q->moves];
424 for (unsigned int i = 0; i < q->moves; i++) {
425 double val = 1.0;
426 assert(q->tag[i] != 0);
427 for (int j = 0; j < MQ_MAX; j++)
428 if (q->tag[i] & (1<<j)) {
429 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
430 val *= pp->mq_prob[j];
432 pd[i] = double_to_fixp(val);
433 total += pd[i];
435 total += double_to_fixp(pp->tenuki_prob);
437 /* Finally, pick a move! */
438 fixp_t stab = fast_irandom(total);
439 for (unsigned int i = 0; i < q->moves; i++) {
440 //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));
441 if (stab < pd[i])
442 return q->move[i];
443 stab -= pd[i];
446 /* Tenuki. */
447 assert(stab < double_to_fixp(pp->tenuki_prob));
448 return pass;
451 coord_t
452 playout_moggy_fullchoose(struct playout_policy *p, struct playout_setup *s, struct board *b, enum stone to_play)
454 struct moggy_policy *pp = p->data;
455 struct move_queue q; q.moves = 0;
457 if (PLDEBUGL(5))
458 board_print(b, stderr);
460 /* Ko fight check */
461 if (!is_pass(b->last_ko.coord) && is_pass(b->ko.coord)
462 && b->moves - b->last_ko_age < pp->koage) {
463 if (board_is_valid_play(b, to_play, b->last_ko.coord)
464 && !is_bad_selfatari(b, to_play, b->last_ko.coord))
465 mq_add(&q, b->last_ko.coord, 1<<MQ_KO);
468 /* Local checks */
469 if (!is_pass(b->last_move.coord)) {
470 /* Local group in atari? */
471 local_atari_check(p, b, &b->last_move, &q);
473 /* Local group can be PUT in atari? */
474 local_2lib_check(p, b, &b->last_move, &q);
476 /* Check for patterns we know */
477 apply_pattern(p, b, &b->last_move,
478 pp->pattern2 && b->last_move2.coord >= 0 ? &b->last_move2 : NULL,
479 &q);
482 /* Global checks */
484 /* Any groups in atari? */
485 global_atari_check(p, b, to_play, &q);
487 /* Joseki moves? */
488 joseki_check(p, b, to_play, &q);
490 #if 0
491 /* Average length of the queue is 1.4 move. */
492 printf("MQL %d ", q.moves);
493 for (unsigned int i = 0; i < q.moves; i++)
494 printf("%s ", coord2sstr(q.move[i], b));
495 printf("\n");
496 #endif
498 if (q.moves > 0)
499 return mq_tagged_choose(p, b, to_play, &q);
501 /* Fill board */
502 if (pp->fillboardtries > 0) {
503 coord_t c = fillboard_check(p, b);
504 if (!is_pass(c))
505 return c;
508 return pass;
512 void
513 playout_moggy_assess_group(struct playout_policy *p, struct prior_map *map, group_t g, int games)
515 struct moggy_policy *pp = p->data;
516 struct board *b = map->b;
517 struct move_queue q; q.moves = 0;
519 if (board_group_info(b, g).libs > 2)
520 return;
522 if (PLDEBUGL(5)) {
523 fprintf(stderr, "ASSESS of group %s:\n", coord2sstr(g, b));
524 board_print(b, stderr);
527 if (board_group_info(b, g).libs == 2) {
528 if (!pp->atarirate)
529 return;
530 group_2lib_check(b, g, map->to_play, &q, 0, pp->atari_miaisafe, pp->atari_def_no_hopeless);
531 while (q.moves--) {
532 coord_t coord = q.move[q.moves];
533 if (PLDEBUGL(5))
534 fprintf(stderr, "1.0: 2lib %s\n", coord2sstr(coord, b));
535 int assess = games / 2;
536 add_prior_value(map, coord, 1, assess);
538 return;
541 /* This group, sir, is in atari! */
543 coord_t ladder = pass;
544 group_atari_check(pp->alwaysccaprate, b, g, map->to_play, &q, &ladder, 0);
545 while (q.moves--) {
546 coord_t coord = q.move[q.moves];
548 /* _Never_ play here if this move plays out
549 * a caught ladder. */
550 if (coord == ladder && !board_playing_ko_threat(b)) {
551 /* Note that the opposite is not guarded against;
552 * we do not advise against capturing a laddered
553 * group (but we don't encourage it either). Such
554 * a move can simplify tactical situations if we
555 * can afford it. */
556 if (map->to_play != board_at(b, g))
557 continue;
558 /* FIXME: We give the malus even if this move
559 * captures another group. */
560 if (PLDEBUGL(5))
561 fprintf(stderr, "0.0: ladder %s\n", coord2sstr(coord, b));
562 add_prior_value(map, coord, 0, games);
563 continue;
566 if (!pp->capturerate && !pp->lcapturerate)
567 continue;
569 if (PLDEBUGL(5))
570 fprintf(stderr, "1.0: atari %s\n", coord2sstr(coord, b));
571 int assess = games * 2;
572 add_prior_value(map, coord, 1, assess);
576 void
577 playout_moggy_assess_one(struct playout_policy *p, struct prior_map *map, coord_t coord, int games)
579 struct moggy_policy *pp = p->data;
580 struct board *b = map->b;
582 if (PLDEBUGL(5)) {
583 fprintf(stderr, "ASSESS of move %s:\n", coord2sstr(coord, b));
584 board_print(b, stderr);
587 /* Is this move a self-atari? */
588 if (pp->selfatarirate) {
589 if (!board_playing_ko_threat(b) && is_bad_selfatari(b, map->to_play, coord)) {
590 if (PLDEBUGL(5))
591 fprintf(stderr, "0.0: self-atari\n");
592 add_prior_value(map, coord, 0, games);
593 if (!pp->selfatari_other)
594 return;
595 /* If we can play on the other liberty of the
596 * endangered group, do! */
597 coord = selfatari_cousin(b, map->to_play, coord);
598 if (is_pass(coord))
599 return;
600 if (PLDEBUGL(5))
601 fprintf(stderr, "1.0: self-atari redirect %s\n", coord2sstr(coord, b));
602 add_prior_value(map, coord, 1.0, games);
603 return;
607 /* Pattern check */
608 if (pp->patternrate) {
609 struct move m = { .color = map->to_play, .coord = coord };
610 if (test_pattern3_here(p, b, &m)) {
611 if (PLDEBUGL(5))
612 fprintf(stderr, "1.0: pattern\n");
613 add_prior_value(map, coord, 1, games);
617 return;
620 void
621 playout_moggy_assess(struct playout_policy *p, struct prior_map *map, int games)
623 struct moggy_policy *pp = p->data;
625 /* First, go through all endangered groups. */
626 for (group_t g = 1; g < board_size2(map->b); g++)
627 if (group_at(map->b, g) == g)
628 playout_moggy_assess_group(p, map, g, games);
630 /* Then, assess individual moves. */
631 if (!pp->patternrate && !pp->selfatarirate)
632 return;
633 foreach_free_point(map->b) {
634 if (map->consider[c])
635 playout_moggy_assess_one(p, map, c, games);
636 } foreach_free_point_end;
639 bool
640 playout_moggy_permit(struct playout_policy *p, struct board *b, struct move *m)
642 struct moggy_policy *pp = p->data;
644 /* The idea is simple for now - never allow self-atari moves.
645 * They suck in general, but this also permits us to actually
646 * handle seki in the playout stage. */
648 if (fast_random(100) >= pp->selfatarirate) {
649 if (PLDEBUGL(5))
650 fprintf(stderr, "skipping sar test\n");
651 return true;
653 bool selfatari = is_bad_selfatari(b, m->color, m->coord);
654 if (selfatari) {
655 if (PLDEBUGL(5))
656 fprintf(stderr, "__ Prohibiting self-atari %s %s\n",
657 stone2str(m->color), coord2sstr(m->coord, b));
658 if (pp->selfatari_other) {
659 /* Ok, try the other liberty of the atari'd group. */
660 coord_t c = selfatari_cousin(b, m->color, m->coord);
661 if (is_pass(c)) return false;
662 if (PLDEBUGL(5))
663 fprintf(stderr, "___ Redirecting to other lib %s\n",
664 coord2sstr(c, b));
665 m->coord = c;
666 return true;
668 return false;
670 return true;
674 struct playout_policy *
675 playout_moggy_init(char *arg, struct board *b, struct joseki_dict *jdict)
677 struct playout_policy *p = calloc2(1, sizeof(*p));
678 struct moggy_policy *pp = calloc2(1, sizeof(*pp));
679 p->data = pp;
680 p->choose = playout_moggy_seqchoose;
681 p->assess = playout_moggy_assess;
682 p->permit = playout_moggy_permit;
684 pp->jdict = jdict;
686 int rate = 90;
688 pp->lcapturerate = pp->atarirate = pp->capturerate = pp->patternrate
689 = pp->selfatarirate = pp->josekirate = -1U;
690 pp->korate = 20; pp->koage = 4;
691 pp->alwaysccaprate = 20;
692 pp->selfatari_other = true;
693 pp->atari_def_no_hopeless = true;
694 pp->atari_miaisafe = true;
696 /* C is stupid. */
697 double mq_prob_default[MQ_MAX] = {
698 [MQ_KO] = 6.0,
699 [MQ_LATARI] = 5.0,
700 [MQ_L2LIB] = 4.0,
701 [MQ_PAT3] = 3.0,
702 [MQ_GATARI] = 2.0,
703 [MQ_JOSEKI] = 1.0,
705 memcpy(pp->mq_prob, mq_prob_default, sizeof(pp->mq_prob));
707 if (arg) {
708 char *optspec, *next = arg;
709 while (*next) {
710 optspec = next;
711 next += strcspn(next, ":");
712 if (*next) { *next++ = 0; } else { *next = 0; }
714 char *optname = optspec;
715 char *optval = strchr(optspec, '=');
716 if (optval) *optval++ = 0;
718 if (!strcasecmp(optname, "debug") && optval) {
719 p->debug_level = atoi(optval);
720 } else if (!strcasecmp(optname, "lcapturerate") && optval) {
721 pp->lcapturerate = atoi(optval);
722 } else if (!strcasecmp(optname, "atarirate") && optval) {
723 pp->atarirate = atoi(optval);
724 } else if (!strcasecmp(optname, "capturerate") && optval) {
725 pp->capturerate = atoi(optval);
726 } else if (!strcasecmp(optname, "patternrate") && optval) {
727 pp->patternrate = atoi(optval);
728 } else if (!strcasecmp(optname, "selfatarirate") && optval) {
729 pp->selfatarirate = atoi(optval);
730 } else if (!strcasecmp(optname, "korate") && optval) {
731 pp->korate = atoi(optval);
732 } else if (!strcasecmp(optname, "josekirate") && optval) {
733 pp->josekirate = atoi(optval);
734 } else if (!strcasecmp(optname, "alwaysccaprate") && optval) {
735 pp->alwaysccaprate = atoi(optval);
736 } else if (!strcasecmp(optname, "rate") && optval) {
737 rate = atoi(optval);
738 } else if (!strcasecmp(optname, "fillboardtries")) {
739 pp->fillboardtries = atoi(optval);
740 } else if (!strcasecmp(optname, "koage") && optval) {
741 pp->koage = atoi(optval);
742 } else if (!strcasecmp(optname, "pattern2")) {
743 pp->pattern2 = optval && *optval == '0' ? false : true;
744 } else if (!strcasecmp(optname, "selfatari_other")) {
745 pp->selfatari_other = optval && *optval == '0' ? false : true;
746 } else if (!strcasecmp(optname, "capcheckall")) {
747 pp->capcheckall = optval && *optval == '0' ? false : true;
748 } else if (!strcasecmp(optname, "atari_miaisafe")) {
749 pp->atari_miaisafe = optval && *optval == '0' ? false : true;
750 } else if (!strcasecmp(optname, "atari_def_no_hopeless")) {
751 pp->atari_def_no_hopeless = optval && *optval == '0' ? false : true;
752 } else if (!strcasecmp(optname, "fullchoose")) {
753 p->choose = optval && *optval == '0' ? playout_moggy_seqchoose : playout_moggy_fullchoose;
754 } else if (!strcasecmp(optname, "mqprob") && optval) {
755 /* KO%LATARI%L2LIB%PAT3%GATARI%JOSEKI */
756 for (int i = 0; *optval && i < MQ_MAX; i++, optval += strcspn(optval, "%")) {
757 optval++;
758 pp->mq_prob[i] = atof(optval);
760 } else if (!strcasecmp(optname, "tenukiprob") && optval) {
761 pp->tenuki_prob = atof(optval);
762 } else {
763 fprintf(stderr, "playout-moggy: Invalid policy argument %s or missing value\n", optname);
764 exit(1);
768 if (pp->lcapturerate == -1U) pp->lcapturerate = rate;
769 if (pp->atarirate == -1U) pp->atarirate = rate;
770 if (pp->capturerate == -1U) pp->capturerate = rate;
771 if (pp->patternrate == -1U) pp->patternrate = rate;
772 if (pp->selfatarirate == -1U) pp->selfatarirate = rate;
773 if (pp->korate == -1U) pp->korate = rate;
774 if (pp->josekirate == -1U) pp->josekirate = rate;
775 if (pp->alwaysccaprate == -1U) pp->alwaysccaprate = rate;
777 pattern3s_init(&pp->patterns, moggy_patterns_src, moggy_patterns_src_n);
779 return p;