4 * ----------------------------------------------------------------------
6 * Copyright (c) 2012 Free Software Foundation
8 * GNU SHOGI is based on GNU CHESS
10 * This file is part of GNU SHOGI.
12 * GNU Shogi is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 3 of the License,
15 * or (at your option) any later version.
17 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License along
23 * with GNU Shogi; see the file COPYING. If not, see
24 * <http://www.gnu.org/licenses/>.
25 * ----------------------------------------------------------------------
32 #define CHECK_DISTANCE
35 * See if any piece with color 'side' attacks sq.
36 * *blockable == attack could be blocked by drop
40 SqAttacked(short square, short side, short *blockable)
45 unsigned char *ppos, *pdir;
50 if (MatchSignature(threats_signature[side]))
52 *blockable = true; /* don't know */
53 return Anyattack(side, square);
57 * First check neighbouring squares,
60 * then (last) check Rooks,
65 /* try a capture from direct neighboured squares */
67 ptyp = ptype[black][king];
70 u = first_direction(ptyp, &d, square);
72 pdir = (*nextdir[ptyp])[square];
78 if (color[u] == side) /* can piece reach square in one step ? */
81 if (piece_distance(side, board[u], u, square) == 1)
87 short ptypv = ptype[side][board[u]];
90 v = first_direction(ptypv, &dv, u);
93 qdir = (*nextdir[ptypv])[u];
101 v = next_direction(ptypv, &dv, u);
111 u = next_direction(ptyp, &d, square);
118 /* try a knight capture (using xside's knight moves) */
120 ptyp = ptype[side ^ 1][knight];
123 u = first_direction(ptyp, &d, square);
125 pdir = (*nextdir[ptyp])[square];
131 if (color[u] == side && board[u] == knight)
135 u = next_direction(ptyp, &d, square);
144 /* try a (promoted) bishop capture */
146 ptyp = ptype[black][bishop];
149 u = first_direction(ptyp, &d, square);
151 ppos = (*nextpos[ptyp])[square];
152 pdir = (*nextdir[ptyp])[square];
158 if (color[u] == neutral)
160 u = next_position(ptyp, &d, square, u);
166 if (color[u] == side && (unpromoted[board[u]] == bishop))
170 u = next_direction(ptyp, &d, square);
178 /* try a (promoted) rook capture */
180 ptyp = ptype[black][rook];
183 u = first_direction(ptyp, &d, square);
185 ppos = (*nextpos[ptyp])[square];
186 pdir = (*nextdir[ptyp])[square];
192 if (color[u] == neutral)
195 u = next_position(ptyp, &d, square, u);
204 if (color[u] == side && (unpromoted[board[u]] == rook))
208 u = next_direction(ptyp, &d, square);
216 /* try a lance capture (using xside's lance moves) */
218 ptyp = ptype[side ^ 1][lance];
221 u = first_direction(ptyp, &d, square);
223 ppos = (*nextpos[ptyp])[square];
229 if (color[u] == neutral)
232 u = next_position(ptyp, &d, square, u);
241 if ((color[u] == side) && (board[u] == lance))