1 /* Playout policy by stochastically applying a fixed set of decision
2 * rules in given order - modelled after the intelligent playouts
3 * in the Mogo engine. */
13 #include "joseki/base.h"
17 #include "playout/moggy.h"
19 #include "tactics/1lib.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)
26 /* Whether to avoid capturing/atariing doomed groups (this is big
27 * performance hit and may reduce playouts balance; it does increase
28 * the strength, but not quite proportionally to the performance). */
29 //#define NO_DOOMED_GROUPS
32 /* Move queue tags: */
44 /* Note that the context can be shared by multiple threads! */
47 unsigned int lcapturerate
, atarirate
, capturerate
, patternrate
, korate
, josekirate
;
48 unsigned int selfatarirate
, alwaysccaprate
;
49 unsigned int fillboardtries
;
51 /* Whether to look for patterns around second-to-last move. */
53 /* Whether, when self-atari attempt is detected, to play the other
54 * group's liberty if that is non-self-atari. */
56 /* Whether to always pick from moves capturing all groups in
57 * global_atari_check(). */
60 struct joseki_dict
*jdict
;
61 struct pattern3s patterns
;
63 /* Gamma values for queue tags - correspond to probabilities. */
65 double mq_prob
[MQ_MAX
], tenuki_prob
;
69 static char moggy_patterns_src
[][11] = {
70 /* hane pattern - enclosing hane */
74 /* hane pattern - non-cutting hane */
78 /* hane pattern - magari */
82 /* hane pattern - thin hane */
86 /* generic pattern - katatsuke or diagonal attachment; similar to magari */
90 /* cut1 pattern (kiri) - unprotected cut */
94 /* cut1 pattern (kiri) - peeped cut */
98 /* cut2 pattern (de) */
102 /* cut keima (not in Mogo) */
105 "???", /* o?? has some pathological tsumego cases */
106 /* side pattern - chase */
110 /* side pattern - block side cut */
114 /* side pattern - block side connection */
118 /* side pattern - sagari (SUSPICIOUS) */
120 "x.x" /* Mogo has "x.?" */
121 "###" /* Mogo has "X" */,
122 /* side pattern - throw-in (SUSPICIOUS) */
128 /* side pattern - cut (SUSPICIOUS) */
131 "###" /* Mogo has "X" */,
133 #define moggy_patterns_src_n sizeof(moggy_patterns_src) / sizeof(moggy_patterns_src[0])
136 test_pattern3_here(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
138 struct moggy_policy
*pp
= p
->data
;
139 /* Check if 3x3 pattern is matched by given move... */
140 if (!pattern3_move_here(&pp
->patterns
, b
, m
))
142 /* ...and the move is not obviously stupid. */
143 if (is_bad_selfatari(b
, m
->color
, m
->coord
))
145 /* Ladder moves are stupid. */
146 group_t atari_neighbor
= board_get_atari_neighbor(b
, m
->coord
, m
->color
);
147 if (atari_neighbor
&& is_ladder(b
, m
->coord
, atari_neighbor
))
153 apply_pattern_here(struct playout_policy
*p
, struct board
*b
, coord_t c
, enum stone color
, struct move_queue
*q
)
155 struct move m2
= { .coord
= c
, .color
= color
};
156 if (board_is_valid_move(b
, &m2
) && test_pattern3_here(p
, b
, &m2
))
157 mq_add(q
, c
, 1<<MQ_PAT3
);
160 /* Check if we match any pattern around given move (with the other color to play). */
162 apply_pattern(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move
*mm
, struct move_queue
*q
)
164 /* Suicides do not make any patterns and confuse us. */
165 if (board_at(b
, m
->coord
) == S_NONE
|| board_at(b
, m
->coord
) == S_OFFBOARD
)
168 foreach_8neighbor(b
, m
->coord
) {
169 apply_pattern_here(p
, b
, c
, stone_other(m
->color
), q
);
170 } foreach_8neighbor_end
;
172 if (mm
) { /* Second move for pattern searching */
173 foreach_8neighbor(b
, mm
->coord
) {
174 if (coord_is_8adjecent(m
->coord
, c
, b
))
176 apply_pattern_here(p
, b
, c
, stone_other(m
->color
), q
);
177 } foreach_8neighbor_end
;
181 mq_print(q
, b
, "Pattern");
186 joseki_check(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
188 struct moggy_policy
*pp
= p
->data
;
192 for (int i
= 0; i
< 4; i
++) {
193 hash_t h
= b
->qhash
[i
] & joseki_hash_mask
;
194 coord_t
*cc
= pp
->jdict
->patterns
[h
].moves
[to_play
];
196 for (; !is_pass(*cc
); cc
++) {
197 if (coord_quadrant(*cc
, b
) != i
)
199 mq_add(q
, *cc
, 1<<MQ_JOSEKI
);
203 if (q
->moves
> 0 && PLDEBUGL(5))
204 mq_print(q
, b
, "Joseki");
208 global_atari_check(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
213 struct moggy_policy
*pp
= p
->data
;
214 if (pp
->capcheckall
) {
215 for (int g
= 0; g
< b
->clen
; g
++)
216 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, q
, NULL
, 1<<MQ_GATARI
);
218 mq_print(q
, b
, "Global atari");
222 int g_base
= fast_random(b
->clen
);
223 for (int g
= g_base
; 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
);
226 /* XXX: Try carrying on. */
228 mq_print(q
, b
, "Global atari");
232 for (int g
= 0; g
< g_base
; g
++) {
233 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, group_base(b
->c
[g
])), to_play
, q
, NULL
, 1<<MQ_GATARI
);
235 /* XXX: Try carrying on. */
237 mq_print(q
, b
, "Global atari");
245 local_atari_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move_queue
*q
)
247 struct moggy_policy
*pp
= p
->data
;
249 /* Did the opponent play a self-atari? */
250 if (board_group_info(b
, group_at(b
, m
->coord
)).libs
== 1) {
251 group_atari_check(pp
->alwaysccaprate
, b
, group_at(b
, m
->coord
), stone_other(m
->color
), q
, NULL
, 1<<MQ_LATARI
);
254 foreach_neighbor(b
, m
->coord
, {
255 group_t g
= group_at(b
, c
);
256 if (!g
|| board_group_info(b
, g
).libs
!= 1)
258 group_atari_check(pp
->alwaysccaprate
, b
, g
, stone_other(m
->color
), q
, NULL
, 1<<MQ_LATARI
);
262 mq_print(q
, b
, "Local atari");
266 miai_2lib(struct board
*b
, group_t group
, enum stone color
)
268 bool can_connect
= false, can_pull_out
= false;
269 /* We have miai if we can either connect on both libs,
270 * or connect on one lib and escape on another. (Just
271 * having two escape routes can be risky.) We must make
272 * sure that we don't consider following as miai:
275 * O O X O - left dot would be pull-out, right dot connect */
276 foreach_neighbor(b
, board_group_info(b
, group
).lib
[0], {
277 enum stone cc
= board_at(b
, c
);
278 if (cc
== S_NONE
&& cc
!= board_at(b
, board_group_info(b
, group
).lib
[1])) {
280 } else if (cc
!= color
) {
284 group_t cg
= group_at(b
, c
);
285 if (cg
&& cg
!= group
&& board_group_info(b
, cg
).libs
> 1)
288 foreach_neighbor(b
, board_group_info(b
, group
).lib
[1], {
289 enum stone cc
= board_at(b
, c
);
290 if (c
== board_group_info(b
, group
).lib
[0])
292 if (cc
== S_NONE
&& can_connect
) {
294 } else if (cc
!= color
) {
298 group_t cg
= group_at(b
, c
);
299 if (cg
&& cg
!= group
&& board_group_info(b
, cg
).libs
> 1)
300 return (can_connect
|| can_pull_out
);
306 check_group_atari(struct board
*b
, group_t group
, enum stone owner
,
307 enum stone to_play
, struct move_queue
*q
)
309 for (int i
= 0; i
< 2; i
++) {
310 coord_t lib
= board_group_info(b
, group
).lib
[i
];
311 assert(board_at(b
, lib
) == S_NONE
);
312 if (!board_is_valid_play(b
, to_play
, lib
))
315 /* Don't play at the spot if it is extremely short
317 /* XXX: This looks harmful, could significantly
318 * prefer atari to throwin:
324 if (neighbor_count_at(b
, lib
, stone_other(owner
)) + immediate_liberty_count(b
, lib
) < 2)
328 /* If the move is too "lumpy", do not play it:
331 * ..O.X.X <- always play the left one!
333 if (neighbor_count_at(b
, lib
, stone_other(owner
)) + neighbor_count_at(b
, lib
, S_OFFBOARD
) == 3)
336 #ifdef NO_DOOMED_GROUPS
337 /* If the owner can't play at the spot, we don't want
338 * to bother either. */
339 if (is_bad_selfatari(b
, owner
, lib
))
343 /* Of course we don't want to play bad selfatari
344 * ourselves, if we are the attacker... */
346 #ifdef NO_DOOMED_GROUPS
349 is_bad_selfatari(b
, to_play
, lib
))
352 /* Tasty! Crispy! Good! */
353 mq_add(q
, lib
, 1<<MQ_L2LIB
);
359 group_2lib_check(struct playout_policy
*p
, struct board
*b
, group_t group
, enum stone to_play
,
360 struct move_queue
*q
)
362 enum stone color
= board_at(b
, group_base(group
));
363 assert(color
!= S_OFFBOARD
&& color
!= S_NONE
);
366 fprintf(stderr
, "[%s] 2lib check of color %d\n",
367 coord2sstr(group
, b
), color
);
369 /* Do not try to atari groups that cannot be harmed. */
370 if (miai_2lib(b
, group
, color
))
373 check_group_atari(b
, group
, color
, to_play
, q
);
375 /* Can we counter-atari another group, if we are the defender? */
376 if (to_play
!= color
)
378 foreach_in_group(b
, group
) {
379 foreach_neighbor(b
, c
, {
380 if (board_at(b
, c
) != stone_other(color
))
382 group_t g2
= group_at(b
, c
);
383 if (board_group_info(b
, g2
).libs
== 1) {
384 /* We can capture a neighbor. */
385 mq_add(q
, board_group_info(b
, g2
).lib
[0], 1<<MQ_L2LIB
);
389 if (board_group_info(b
, g2
).libs
!= 2)
391 check_group_atari(b
, g2
, color
, to_play
, q
);
393 } foreach_in_group_end
;
397 local_2lib_check(struct playout_policy
*p
, struct board
*b
, struct move
*m
, struct move_queue
*q
)
399 /* Does the opponent have just two liberties? */
400 if (board_group_info(b
, group_at(b
, m
->coord
)).libs
== 2) {
401 group_2lib_check(p
, b
, group_at(b
, m
->coord
), stone_other(m
->color
), q
);
403 /* We always prefer to take off an enemy chain liberty
404 * before pulling out ourselves. */
405 /* XXX: We aren't guaranteed to return to that group
408 return q
->move
[fast_random(q
->moves
)];
412 /* Then he took a third liberty from neighboring chain? */
413 foreach_neighbor(b
, m
->coord
, {
414 group_t g
= group_at(b
, c
);
415 if (!g
|| board_group_info(b
, g
).libs
!= 2)
417 group_2lib_check(p
, b
, g
, stone_other(m
->color
), q
);
421 mq_print(q
, b
, "Local 2lib");
425 fillboard_check(struct playout_policy
*p
, struct board
*b
)
427 struct moggy_policy
*pp
= p
->data
;
428 unsigned int fbtries
= b
->flen
/ 8;
429 if (pp
->fillboardtries
< fbtries
)
430 fbtries
= pp
->fillboardtries
;
432 for (unsigned int i
= 0; i
< fbtries
; i
++) {
433 coord_t coord
= b
->f
[fast_random(b
->flen
)];
434 if (immediate_liberty_count(b
, coord
) != 4)
436 foreach_diag_neighbor(b
, coord
) {
437 if (board_at(b
, c
) != S_NONE
)
439 } foreach_diag_neighbor_end
;
447 playout_moggy_partchoose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
)
449 struct moggy_policy
*pp
= p
->data
;
452 board_print(b
, stderr
);
455 if (!is_pass(b
->last_ko
.coord
) && is_pass(b
->ko
.coord
)
456 && b
->moves
- b
->last_ko_age
< pp
->koage
457 && pp
->korate
> fast_random(100)) {
458 if (board_is_valid_play(b
, to_play
, b
->last_ko
.coord
)
459 && !is_bad_selfatari(b
, to_play
, b
->last_ko
.coord
))
460 return b
->last_ko
.coord
;
464 if (!is_pass(b
->last_move
.coord
)) {
465 /* Local group in atari? */
466 if (pp
->lcapturerate
> fast_random(100)) {
467 struct move_queue q
; q
.moves
= 0;
468 local_atari_check(p
, b
, &b
->last_move
, &q
);
473 /* Local group can be PUT in atari? */
474 if (pp
->atarirate
> fast_random(100)) {
475 struct move_queue q
; q
.moves
= 0;
476 local_2lib_check(p
, b
, &b
->last_move
, &q
);
481 /* Check for patterns we know */
482 if (pp
->patternrate
> fast_random(100)) {
483 struct move_queue q
; q
.moves
= 0;
484 apply_pattern(p
, b
, &b
->last_move
,
485 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
494 /* Any groups in atari? */
495 if (pp
->capturerate
> fast_random(100)) {
496 struct move_queue q
; q
.moves
= 0;
497 global_atari_check(p
, b
, to_play
, &q
);
503 if (pp
->josekirate
> fast_random(100)) {
504 struct move_queue q
; q
.moves
= 0;
505 joseki_check(p
, b
, to_play
, &q
);
511 if (pp
->fillboardtries
> 0) {
512 coord_t c
= fillboard_check(p
, b
);
520 /* Pick a move from queue q, giving different likelihoods to moves
521 * based on their tags. */
523 mq_tagged_choose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
, struct move_queue
*q
)
525 struct moggy_policy
*pp
= p
->data
;
527 /* First, merge all entries for a move. */
528 /* We use a naive O(N^2) since the average length of the queue
530 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
531 for (unsigned int j
= i
+ 1; j
< q
->moves
; j
++) {
532 if (q
->move
[i
] != q
->move
[j
])
534 q
->tag
[i
] |= q
->tag
[j
];
536 q
->tag
[j
] = q
->tag
[q
->moves
];
537 q
->move
[j
] = q
->move
[q
->moves
];
541 /* Now, construct a probdist. */
544 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
546 assert(q
->tag
[i
] != 0);
547 for (int j
= 1; j
< MQ_MAX
; j
++)
548 if (q
->tag
[i
] & (1<<j
)) {
549 //fprintf(stderr, "%s(%x) %d %f *= %f\n", coord2sstr(q->move[i], b), q->tag[i], j, val, pp->mq_prob[j]);
550 val
*= pp
->mq_prob
[j
];
552 pd
[i
] = double_to_fixp(val
);
555 total
+= double_to_fixp(pp
->tenuki_prob
);
557 /* Finally, pick a move! */
558 fixp_t stab
= fast_irandom(total
);
559 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
560 //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));
567 assert(stab
< double_to_fixp(pp
->tenuki_prob
));
572 playout_moggy_fullchoose(struct playout_policy
*p
, struct board
*b
, enum stone to_play
)
574 struct moggy_policy
*pp
= p
->data
;
575 struct move_queue q
; q
.moves
= 0;
578 board_print(b
, stderr
);
581 if (!is_pass(b
->last_ko
.coord
) && is_pass(b
->ko
.coord
)
582 && b
->moves
- b
->last_ko_age
< pp
->koage
583 && pp
->korate
> fast_random(100)) {
584 if (board_is_valid_play(b
, to_play
, b
->last_ko
.coord
)
585 && !is_bad_selfatari(b
, to_play
, b
->last_ko
.coord
))
586 mq_add(&q
, b
->last_ko
.coord
, 1<<MQ_KO
);
590 if (!is_pass(b
->last_move
.coord
)) {
591 /* Local group in atari? */
592 if (pp
->lcapturerate
> fast_random(100)) {
593 local_atari_check(p
, b
, &b
->last_move
, &q
);
596 /* Local group can be PUT in atari? */
597 if (pp
->atarirate
> fast_random(100)) {
598 local_2lib_check(p
, b
, &b
->last_move
, &q
);
601 /* Check for patterns we know */
602 if (pp
->patternrate
> fast_random(100)) {
603 apply_pattern(p
, b
, &b
->last_move
,
604 pp
->pattern2
&& b
->last_move2
.coord
>= 0 ? &b
->last_move2
: NULL
,
611 /* Any groups in atari? */
612 if (pp
->capturerate
> fast_random(100)) {
613 global_atari_check(p
, b
, to_play
, &q
);
617 if (pp
->josekirate
> fast_random(100)) {
618 joseki_check(p
, b
, to_play
, &q
);
622 /* Average length of the queue is 1.4 move. */
623 printf("MQL %d ", q
.moves
);
624 for (unsigned int i
= 0; i
< q
.moves
; i
++)
625 printf("%s ", coord2sstr(q
.move
[i
], b
));
630 return mq_tagged_choose(p
, b
, to_play
, &q
);
633 if (pp
->fillboardtries
> 0) {
634 coord_t c
= fillboard_check(p
, b
);
644 selfatari_cousin(struct board
*b
, enum stone color
, coord_t coord
)
646 group_t groups
[4]; int groups_n
= 0;
647 foreach_neighbor(b
, coord
, {
648 enum stone s
= board_at(b
, c
);
649 if (s
!= color
) continue;
650 group_t g
= group_at(b
, c
);
651 if (board_group_info(b
, g
).libs
== 2)
652 groups
[groups_n
++] = g
;
657 group_t group
= groups
[fast_random(groups_n
)];
659 coord_t lib2
= board_group_other_lib(b
, group
, coord
);
660 if (is_bad_selfatari(b
, color
, lib2
))
666 playout_moggy_assess_group(struct playout_policy
*p
, struct prior_map
*map
, group_t g
, int games
)
668 struct moggy_policy
*pp
= p
->data
;
669 struct board
*b
= map
->b
;
670 struct move_queue q
; q
.moves
= 0;
672 if (board_group_info(b
, g
).libs
> 2)
676 fprintf(stderr
, "ASSESS of group %s:\n", coord2sstr(g
, b
));
677 board_print(b
, stderr
);
680 if (board_group_info(b
, g
).libs
== 2) {
683 group_2lib_check(p
, b
, g
, map
->to_play
, &q
);
685 coord_t coord
= q
.move
[q
.moves
];
687 fprintf(stderr
, "1.0: 2lib %s\n", coord2sstr(coord
, b
));
688 int assess
= games
/ 2;
689 add_prior_value(map
, coord
, 1, assess
);
694 /* This group, sir, is in atari! */
696 coord_t ladder
= pass
;
697 group_atari_check(pp
->alwaysccaprate
, b
, g
, map
->to_play
, &q
, &ladder
, 0);
699 coord_t coord
= q
.move
[q
.moves
];
701 /* _Never_ play here if this move plays out
702 * a caught ladder. */
703 if (coord
== ladder
&& !board_playing_ko_threat(b
)) {
704 /* Note that the opposite is not guarded against;
705 * we do not advise against capturing a laddered
706 * group (but we don't encourage it either). Such
707 * a move can simplify tactical situations if we
709 if (map
->to_play
!= board_at(b
, g
))
711 /* FIXME: We give the malus even if this move
712 * captures another group. */
714 fprintf(stderr
, "0.0: ladder %s\n", coord2sstr(coord
, b
));
715 add_prior_value(map
, coord
, 0, games
);
719 if (!pp
->capturerate
&& !pp
->lcapturerate
)
723 fprintf(stderr
, "1.0: atari %s\n", coord2sstr(coord
, b
));
724 int assess
= games
* 2;
725 add_prior_value(map
, coord
, 1, assess
);
730 playout_moggy_assess_one(struct playout_policy
*p
, struct prior_map
*map
, coord_t coord
, int games
)
732 struct moggy_policy
*pp
= p
->data
;
733 struct board
*b
= map
->b
;
736 fprintf(stderr
, "ASSESS of move %s:\n", coord2sstr(coord
, b
));
737 board_print(b
, stderr
);
740 /* Is this move a self-atari? */
741 if (pp
->selfatarirate
) {
742 if (!board_playing_ko_threat(b
) && is_bad_selfatari(b
, map
->to_play
, coord
)) {
744 fprintf(stderr
, "0.0: self-atari\n");
745 add_prior_value(map
, coord
, 0, games
);
746 if (!pp
->selfatari_other
)
748 /* If we can play on the other liberty of the
749 * endangered group, do! */
750 coord
= selfatari_cousin(b
, map
->to_play
, coord
);
754 fprintf(stderr
, "1.0: self-atari redirect %s\n", coord2sstr(coord
, b
));
755 add_prior_value(map
, coord
, 1.0, games
);
761 if (pp
->patternrate
) {
762 struct move m
= { .color
= map
->to_play
, .coord
= coord
};
763 if (test_pattern3_here(p
, b
, &m
)) {
765 fprintf(stderr
, "1.0: pattern\n");
766 add_prior_value(map
, coord
, 1, games
);
774 playout_moggy_assess(struct playout_policy
*p
, struct prior_map
*map
, int games
)
776 struct moggy_policy
*pp
= p
->data
;
778 /* First, go through all endangered groups. */
779 for (group_t g
= 1; g
< board_size2(map
->b
); g
++)
780 if (group_at(map
->b
, g
) == g
)
781 playout_moggy_assess_group(p
, map
, g
, games
);
783 /* Then, assess individual moves. */
784 if (!pp
->patternrate
&& !pp
->selfatarirate
)
786 foreach_free_point(map
->b
) {
787 if (map
->consider
[c
])
788 playout_moggy_assess_one(p
, map
, c
, games
);
789 } foreach_free_point_end
;
793 playout_moggy_permit(struct playout_policy
*p
, struct board
*b
, struct move
*m
)
795 struct moggy_policy
*pp
= p
->data
;
797 /* The idea is simple for now - never allow self-atari moves.
798 * They suck in general, but this also permits us to actually
799 * handle seki in the playout stage. */
801 if (fast_random(100) >= pp
->selfatarirate
) {
803 fprintf(stderr
, "skipping sar test\n");
806 bool selfatari
= is_bad_selfatari(b
, m
->color
, m
->coord
);
809 fprintf(stderr
, "__ Prohibiting self-atari %s %s\n",
810 stone2str(m
->color
), coord2sstr(m
->coord
, b
));
811 if (pp
->selfatari_other
) {
812 /* Ok, try the other liberty of the atari'd group. */
813 coord_t c
= selfatari_cousin(b
, m
->color
, m
->coord
);
814 if (is_pass(c
)) return false;
816 fprintf(stderr
, "___ Redirecting to other lib %s\n",
827 struct playout_policy
*
828 playout_moggy_init(char *arg
, struct board
*b
, struct joseki_dict
*jdict
)
830 struct playout_policy
*p
= calloc2(1, sizeof(*p
));
831 struct moggy_policy
*pp
= calloc2(1, sizeof(*pp
));
833 p
->choose
= playout_moggy_partchoose
;
834 p
->assess
= playout_moggy_assess
;
835 p
->permit
= playout_moggy_permit
;
841 pp
->lcapturerate
= pp
->atarirate
= pp
->capturerate
= pp
->patternrate
842 = pp
->selfatarirate
= pp
->josekirate
= -1U;
843 pp
->korate
= 0; pp
->koage
= 4;
844 pp
->alwaysccaprate
= 0;
845 pp
->selfatari_other
= true;
848 double mq_prob_default
[MQ_MAX
] = {
856 memcpy(pp
->mq_prob
, mq_prob_default
, sizeof(pp
->mq_prob
));
859 char *optspec
, *next
= arg
;
862 next
+= strcspn(next
, ":");
863 if (*next
) { *next
++ = 0; } else { *next
= 0; }
865 char *optname
= optspec
;
866 char *optval
= strchr(optspec
, '=');
867 if (optval
) *optval
++ = 0;
869 if (!strcasecmp(optname
, "debug") && optval
) {
870 p
->debug_level
= atoi(optval
);
871 } else if (!strcasecmp(optname
, "lcapturerate") && optval
) {
872 pp
->lcapturerate
= atoi(optval
);
873 } else if (!strcasecmp(optname
, "atarirate") && optval
) {
874 pp
->atarirate
= atoi(optval
);
875 } else if (!strcasecmp(optname
, "capturerate") && optval
) {
876 pp
->capturerate
= atoi(optval
);
877 } else if (!strcasecmp(optname
, "patternrate") && optval
) {
878 pp
->patternrate
= atoi(optval
);
879 } else if (!strcasecmp(optname
, "selfatarirate") && optval
) {
880 pp
->selfatarirate
= atoi(optval
);
881 } else if (!strcasecmp(optname
, "korate") && optval
) {
882 pp
->korate
= atoi(optval
);
883 } else if (!strcasecmp(optname
, "josekirate") && optval
) {
884 pp
->josekirate
= atoi(optval
);
885 } else if (!strcasecmp(optname
, "alwaysccaprate") && optval
) {
886 pp
->alwaysccaprate
= atoi(optval
);
887 } else if (!strcasecmp(optname
, "rate") && optval
) {
889 } else if (!strcasecmp(optname
, "fillboardtries")) {
890 pp
->fillboardtries
= atoi(optval
);
891 } else if (!strcasecmp(optname
, "koage") && optval
) {
892 pp
->koage
= atoi(optval
);
893 } else if (!strcasecmp(optname
, "pattern2")) {
894 pp
->pattern2
= optval
&& *optval
== '0' ? false : true;
895 } else if (!strcasecmp(optname
, "selfatari_other")) {
896 pp
->selfatari_other
= optval
&& *optval
== '0' ? false : true;
897 } else if (!strcasecmp(optname
, "capcheckall")) {
898 pp
->capcheckall
= optval
&& *optval
== '0' ? false : true;
899 } else if (!strcasecmp(optname
, "fullchoose")) {
900 p
->choose
= optval
&& *optval
== '0' ? playout_moggy_partchoose
: playout_moggy_fullchoose
;
901 } else if (!strcasecmp(optname
, "mqprob") && optval
) {
902 /* KO%LATARI%L2LIB%PAT3%GATARI%JOSEKI */
903 for (int i
= 1; *optval
&& i
< MQ_MAX
; i
++, optval
+= strcspn(optval
, "%")) {
905 pp
->mq_prob
[i
] = atof(optval
);
907 } else if (!strcasecmp(optname
, "tenukiprob") && optval
) {
908 pp
->tenuki_prob
= atof(optval
);
910 fprintf(stderr
, "playout-moggy: Invalid policy argument %s or missing value\n", optname
);
915 if (pp
->lcapturerate
== -1U) pp
->lcapturerate
= rate
;
916 if (pp
->atarirate
== -1U) pp
->atarirate
= rate
;
917 if (pp
->capturerate
== -1U) pp
->capturerate
= rate
;
918 if (pp
->patternrate
== -1U) pp
->patternrate
= rate
;
919 if (pp
->selfatarirate
== -1U) pp
->selfatarirate
= rate
;
920 if (pp
->korate
== -1U) pp
->korate
= rate
;
921 if (pp
->josekirate
== -1U) pp
->josekirate
= rate
;
922 if (pp
->alwaysccaprate
== -1U) pp
->alwaysccaprate
= rate
;
924 pattern3s_init(&pp
->patterns
, moggy_patterns_src
, moggy_patterns_src_n
);