Moggy middle_ladder: Support for turning off middle ladder reading
[pachi/nmclean.git] / tactics / 1lib.c
blob4ad1b49d48fa4ba1468252dc4e1d7f653dcda183
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
5 #define DEBUG
6 #include "board.h"
7 #include "debug.h"
8 #include "mq.h"
9 #include "tactics/1lib.h"
10 #include "tactics/ladder.h"
11 #include "tactics/selfatari.h"
14 /* Whether to avoid capturing/atariing doomed groups (this is big
15 * performance hit and may reduce playouts balance; it does increase
16 * the strength, but not quite proportionally to the performance). */
17 //#define NO_DOOMED_GROUPS
20 static bool
21 can_play_on_lib(struct board *b, group_t g, enum stone to_play)
23 coord_t capture = board_group_info(b, g).lib[0];
24 if (DEBUGL(6))
25 fprintf(stderr, "can capture group %d (%s)?\n",
26 g, coord2sstr(capture, b));
27 /* Does playing on the liberty usefully capture the group? */
28 if (board_is_valid_play(b, to_play, capture)
29 && !is_bad_selfatari(b, to_play, capture))
30 return true;
32 return false;
35 /* For given position @c, decide if this is a group that is in danger from
36 * @capturer and @to_play can do anything about it (play at the last
37 * liberty to either capture or escape). */
38 /* Note that @to_play is important; e.g. consider snapback, it's good
39 * to play at the last liberty by attacker, but not defender. */
40 static __attribute__((always_inline)) bool
41 capturable_group(struct board *b, enum stone capturer, coord_t c,
42 enum stone to_play)
44 group_t g = group_at(b, c);
45 if (likely(board_at(b, c) != stone_other(capturer)
46 || board_group_info(b, g).libs > 1))
47 return false;
49 return can_play_on_lib(b, g, to_play);
52 bool
53 can_countercapture(struct board *b, enum stone owner, group_t g,
54 enum stone to_play, struct move_queue *q, int tag)
56 if (b->clen < 2)
57 return false;
59 unsigned int qmoves_prev = q ? q->moves : 0;
61 foreach_in_group(b, g) {
62 foreach_neighbor(b, c, {
63 if (!capturable_group(b, owner, c, to_play))
64 continue;
66 if (!q) {
67 return true;
69 mq_add(q, board_group_info(b, group_at(b, c)).lib[0], tag);
70 mq_nodup(q);
71 });
72 } foreach_in_group_end;
74 bool can = q ? q->moves > qmoves_prev : false;
75 return can;
78 #ifdef NO_DOOMED_GROUPS
79 static bool
80 can_be_rescued(struct board *b, group_t group, enum stone color, int tag)
82 /* Does playing on the liberty rescue the group? */
83 if (can_play_on_lib(b, group, color))
84 return true;
86 /* Then, maybe we can capture one of our neighbors? */
87 return can_countercapture(b, color, group, color, NULL, tag);
89 #endif
91 void
92 group_atari_check(unsigned int alwaysccaprate, struct board *b, group_t group, enum stone to_play,
93 struct move_queue *q, coord_t *ladder, bool middle_ladder, int tag)
95 enum stone color = board_at(b, group_base(group));
96 coord_t lib = board_group_info(b, group).lib[0];
98 assert(color != S_OFFBOARD && color != S_NONE);
99 if (DEBUGL(5))
100 fprintf(stderr, "[%s] atariiiiiiiii %s of color %d\n",
101 coord2sstr(group, b), coord2sstr(lib, b), color);
102 assert(board_at(b, lib) == S_NONE);
104 if (to_play != color) {
105 /* We are the attacker! In that case, do not try defending
106 * our group, since we can capture the culprit. */
107 #ifdef NO_DOOMED_GROUPS
108 /* Do not remove group that cannot be saved by the opponent. */
109 if (!can_be_rescued(b, group, color, tag))
110 return;
111 #endif
112 if (can_play_on_lib(b, group, to_play)) {
113 mq_add(q, lib, tag);
114 mq_nodup(q);
116 return;
119 /* Can we capture some neighbor? */
120 bool ccap = can_countercapture(b, color, group, to_play, q, tag);
121 if (ccap && !ladder && alwaysccaprate > fast_random(100))
122 return;
124 /* Otherwise, do not save kos. */
125 if (group_is_onestone(b, group)
126 && neighbor_count_at(b, lib, color) + neighbor_count_at(b, lib, S_OFFBOARD) == 4)
127 return;
129 /* Do not suicide... */
130 if (!can_play_on_lib(b, group, to_play))
131 return;
132 if (DEBUGL(6))
133 fprintf(stderr, "...escape route valid\n");
135 /* ...or play out ladders (unless we can counter-capture anytime). */
136 if (!ccap) {
137 if (is_ladder(b, lib, group, middle_ladder)) {
138 /* Sometimes we want to keep the ladder move in the
139 * queue in order to discourage it. */
140 if (!ladder)
141 return;
142 else
143 *ladder = lib;
144 } else if (DEBUGL(6))
145 fprintf(stderr, "...no ladder\n");
148 mq_add(q, lib, tag);
149 mq_nodup(q);