Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fix issue425 #430

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions optimisations/intelligent/guard_flights.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,35 @@
/* index of guarding piece currently being placed */
unsigned int index_of_guarding_piece;

guard_dir_struct GuardDir[5][maxsquare+4];
static guard_dir_struct GuardDirArray[5][maxsquare+4];

guard_dir_struct GuardDir(piece_walk_type p, square s)
{
assert((s >= 0) &&
(s < ((sizeof GuardDirArray[0])/(sizeof GuardDirArray[0][0]))));
if (p == Dummy)
{
guard_dir_struct const DummyGuardDir = {0};
return DummyGuardDir;
}
else
{
assert((p >= Pawn) &&
(p < (Pawn + ((sizeof GuardDirArray)/(sizeof GuardDirArray[0])))));
return GuardDirArray[p - Pawn][s];
}
}

static void init_guard_dirs_leaper(piece_walk_type guarder,
square target,
vec_index_type start, vec_index_type end,
numvec value)
{
vec_index_type i;
assert((guarder >= Pawn) &&
(guarder < (Pawn + ((sizeof GuardDirArray)/(sizeof GuardDirArray[0])))));
for (i = start; i <= end; ++i)
GuardDir[guarder-Pawn][target+vec[i]].dir = value;
GuardDirArray[guarder-Pawn][target+vec[i]].dir = value;
}

static void init_guard_dirs_rider(piece_walk_type guarder,
Expand All @@ -37,15 +56,17 @@ static void init_guard_dirs_rider(piece_walk_type guarder,
if (move_diff_code[abs(being_solved.king_square[Black]-start)]<=2)
{
/* start is a flight, too.
* GuardDir will be initialised from start in this dir */
* GuardDirArray will be initialised from start in this dir */
}
else
{
square s;
assert((guarder >= Pawn) &&
(guarder < (Pawn + ((sizeof GuardDirArray)/(sizeof GuardDirArray[0])))));
for (s = start; is_square_empty(s); s += dir)
{
GuardDir[guarder-Pawn][s].dir = -dir;
GuardDir[guarder-Pawn][s].target = flight;
GuardDirArray[guarder-Pawn][s].dir = -dir;
GuardDirArray[guarder-Pawn][s].target = flight;
}
}
}
Expand Down Expand Up @@ -169,10 +190,10 @@ static void init_guard_dirs_knight(square black_king_pos)

static void init_guard_dir_pawn(square flight, numvec dir)
{
GuardDir[Pawn-Pawn][flight+dir_down+dir_left].dir = dir;
GuardDir[Pawn-Pawn][flight+dir_down+dir_left].target = flight;
GuardDir[Pawn-Pawn][flight+dir_down+dir_right].dir = dir;
GuardDir[Pawn-Pawn][flight+dir_down+dir_right].target = flight;
GuardDirArray[Pawn-Pawn][flight+dir_down+dir_left].dir = dir;
GuardDirArray[Pawn-Pawn][flight+dir_down+dir_left].target = flight;
GuardDirArray[Pawn-Pawn][flight+dir_down+dir_right].dir = dir;
GuardDirArray[Pawn-Pawn][flight+dir_down+dir_right].target = flight;
}

static void init_guard_dirs_pawn(square black_king_pos)
Expand All @@ -194,12 +215,13 @@ static void init_guard_dirs_pawn(square black_king_pos)
*/
void init_guard_dirs(square black_king_pos)
{
memset(GuardDir, 0, sizeof GuardDir);
memset(GuardDirArray, 0, sizeof GuardDirArray);
init_guard_dirs_queen(black_king_pos);
init_guard_dirs_rook(black_king_pos);
init_guard_dirs_bishop(black_king_pos);
init_guard_dirs_knight(black_king_pos);
init_guard_dirs_pawn(black_king_pos);
/* Dummies can't guard anything, so we don't have to do anything for them. */
}

/* Does the white king guard a flight
Expand Down Expand Up @@ -340,21 +362,21 @@ static void place_rider(slice_index si,
TraceFunctionParamListEnd();

{
int const dir = GuardDir[rider_type-Pawn][guard_from].dir;
guard_dir_struct const guard_dir = GuardDir(rider_type,guard_from);

TraceValue("%d",dir);
TraceValue("%d",guard_dir.dir);
TraceValue("%d",guard_dir_check_uninterceptable);
TraceEOL();

switch (dir)
switch (guard_dir.dir)
{
case guard_dir_check_uninterceptable:
case 0:
break;

case guard_dir_guard_uninterceptable:
{
square const guarded = GuardDir[rider_type-Pawn][guard_from].target;
square const guarded = guard_dir.target;
TraceSquare(guarded);
TraceValue("%u",TSTFLAG(being_solved.spec[guarded],Black));
TraceWalk(get_walk_of_piece_on_square(guarded));
Expand All @@ -378,11 +400,11 @@ static void place_rider(slice_index si,

default:
{
square const guarded = GuardDir[rider_type-Pawn][guard_from].target;
square const guarded = guard_dir.target;
TraceSquare(guarded);
TraceValue("%u",TSTFLAG(being_solved.spec[guarded],Black));
TraceEOL();
if (!TSTFLAG(being_solved.spec[guarded],Black) && is_line_empty(guard_from,guarded,dir))
if (!TSTFLAG(being_solved.spec[guarded],Black) && is_line_empty(guard_from,guarded,guard_dir.dir))
{
occupy_square(guard_from,rider_type,white[index_of_guarding_piece].flags);
remember_to_keep_guard_line_open(guard_from,guarded,+1);
Expand Down Expand Up @@ -430,7 +452,7 @@ static void place_knight(slice_index si, square guard_from)
TraceSquare(guard_from);
TraceFunctionParamListEnd();

if (GuardDir[Knight-Pawn][guard_from].dir==guard_dir_guard_uninterceptable)
if (GuardDir(Knight,guard_from).dir==guard_dir_guard_uninterceptable)
{
occupy_square(guard_from,Knight,white[index_of_guarding_piece].flags);
intelligent_continue_guarding_flights(si);
Expand All @@ -453,7 +475,7 @@ static void unpromoted_pawn(slice_index si, square guard_from)
TraceFunctionParamListEnd();

if (!TSTFLAGMASK(sq_spec(guard_from),BIT(WhBaseSq)|BIT(WhPromSq))
&& GuardDir[Pawn-Pawn][guard_from].dir==guard_dir_guard_uninterceptable
&& GuardDir(Pawn,guard_from).dir==guard_dir_guard_uninterceptable
&& intelligent_reserve_white_pawn_moves_from_to_no_promotion(starts_from,
guard_from))
{
Expand Down
4 changes: 2 additions & 2 deletions optimisations/intelligent/guard_flights.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ typedef struct
/* index of guarding piece currently being placed */
extern unsigned int index_of_guarding_piece;

/* lookup doing something like GuardDir[Queen-Pawn][some_square] */
extern guard_dir_struct GuardDir[5][maxsquare+4];
/* lookup doing something like GuardDir(Queen, some_square) */
guard_dir_struct GuardDir(piece_walk_type p, square s);

/* Initialise GuardDir
* @param black_king_pos position of black king
Expand Down
4 changes: 2 additions & 2 deletions optimisations/intelligent/intercept_check_from_guard.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void place_officer(slice_index si,
if (/* avoid duplicate: if intercepter has already been used as guarding
* piece, it shouldn't guard now again */
!(index_of_intercepting_piece<index_of_guarding_piece
&& GuardDir[officer_type-Pawn][to_be_intercepted].dir!=0))
&& GuardDir(officer_type,to_be_intercepted).dir!=0))
{
occupy_square(to_be_intercepted,officer_type,intercepter_flags);
intelligent_continue_guarding_flights(si);
Expand Down Expand Up @@ -135,7 +135,7 @@ static void unpromoted_pawn(slice_index si,
{
square const intercepter_diagram_square = white[index_of_intercepting_piece].diagram_square;
Flags const intercepter_flags = white[index_of_intercepting_piece].flags;
numvec const guard_dir = GuardDir[Pawn-Pawn][to_be_intercepted].dir;
numvec const guard_dir = GuardDir(Pawn,to_be_intercepted).dir;

TraceFunctionEntry(__func__);
TraceSquare(to_be_intercepted);
Expand Down
5 changes: 2 additions & 3 deletions optimisations/intelligent/mate/generate_checking_moves.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ static void by_unpromoted_pawn(slice_index si,
TraceFunctionParamListEnd();

if (!TSTFLAGMASK(sq_spec(check_from),prom_square)
&& GuardDir[Pawn-Pawn][check_from].dir==guard_dir_check_uninterceptable
&& GuardDir(Pawn,check_from).dir==guard_dir_check_uninterceptable
&& intelligent_reserve_white_pawn_moves_from_to_checking(checker_from,check_from))
{
occupy_square(check_from,Pawn,checker_flags);
Expand Down Expand Up @@ -359,13 +359,12 @@ static void by_knight(slice_index si,

void intelligent_mate_generate_checking_moves(slice_index si)
{
unsigned int index;

TraceFunctionEntry(__func__);
TraceFunctionParamListEnd();

if (intelligent_reserve_masses(White,1,piece_gives_check))
{
unsigned int index;
for (index = 1; index<MaxPiece[White]; ++index)
{
square const *bnp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ static void front_check_by_pawn_promotion_without_capture(slice_index si,
for (pp = pieces_pawns_promotee_sequence[pieces_pawns_promotee_chain_orthodox][Empty]; pp!=Empty; pp = pieces_pawns_promotee_sequence[pieces_pawns_promotee_chain_orthodox][pp])
/* geometry doesn't allow for an interceptable check by a pawn that
* doesn't capture */
if (GuardDir[pp-Pawn][check_from].dir==guard_dir_check_uninterceptable)
if (GuardDir(pp,check_from).dir==guard_dir_check_uninterceptable)
{
occupy_square(check_from,pp,white[index_of_checker].flags);
pipe_solve_delegate(si);
Expand Down Expand Up @@ -321,13 +321,13 @@ static void front_check_by_pawn_promotion_with_capture(slice_index si,
for (pp = pieces_pawns_promotee_sequence[pieces_pawns_promotee_chain_orthodox][Empty]; pp!=Empty; pp = pieces_pawns_promotee_sequence[pieces_pawns_promotee_chain_orthodox][pp])
if (pp>=Queen && pp<=Bishop)
{
int const dir = CheckDir(pp)[being_solved.king_square[Black]-check_from];
if (dir!=0)
int const check_dir = CheckDir(pp)[being_solved.king_square[Black]-check_from];
if (check_dir!=0)
switch (pp)
{
case Queen:
case Rook:
if (is_line_empty(check_from,being_solved.king_square[Black],dir))
if (is_line_empty(check_from,being_solved.king_square[Black],check_dir))
{
occupy_square(check_from,pp,white[index_of_checker].flags);
remember_to_keep_checking_line_open(check_from,being_solved.king_square[Black],pp,+1);
Expand Down
49 changes: 23 additions & 26 deletions optimisations/intelligent/place_white_piece.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void intelligent_place_unpromoted_white_pawn(slice_index si,
TraceFunctionParamListEnd();

if (!TSTFLAGMASK(sq_spec(placed_on),BIT(WhBaseSq)|BIT(WhPromSq))
&& GuardDir[Pawn-Pawn][placed_on].dir<guard_dir_guard_uninterceptable
&& GuardDir(Pawn,placed_on).dir<guard_dir_guard_uninterceptable
&& intelligent_reserve_white_pawn_moves_from_to_no_promotion(placed_comes_from,
placed_on))
{
Expand All @@ -44,16 +44,15 @@ void intelligent_place_promoted_white_rider(slice_index si,
{
square const placed_comes_from = white[placed_index].diagram_square;
Flags const placed_flags = white[placed_index].flags;
int const dir = GuardDir[promotee_type-Pawn][placed_on].dir;
square const target = GuardDir[promotee_type-Pawn][placed_on].target;
guard_dir_struct const guard_dir = GuardDir(promotee_type,placed_on);

TraceFunctionEntry(__func__);
TraceWalk(promotee_type);
TraceFunctionParam("%u",placed_index);
TraceSquare(placed_on);
TraceFunctionParamListEnd();

if (dir>=guard_dir_guard_uninterceptable)
if (guard_dir.dir>=guard_dir_guard_uninterceptable)
{
/* nothing */
}
Expand All @@ -63,10 +62,10 @@ void intelligent_place_promoted_white_rider(slice_index si,
{
occupy_square(placed_on,promotee_type,placed_flags);

if (dir==0 || TSTFLAG(being_solved.spec[target],Black) || !is_line_empty(placed_on,target,dir))
if (guard_dir.dir==0 || TSTFLAG(being_solved.spec[guard_dir.target],Black) || !is_line_empty(placed_on,guard_dir.target,guard_dir.dir))
(*go_on)(si);
else
intelligent_intercept_guard_by_white(si,target,dir,go_on);
intelligent_intercept_guard_by_white(si,guard_dir.target,guard_dir.dir,go_on);

intelligent_unreserve();
}
Expand All @@ -88,7 +87,7 @@ void intelligent_place_promoted_white_knight(slice_index si,
TraceSquare(placed_on);
TraceFunctionParamListEnd();

if (GuardDir[Knight-Pawn][placed_on].dir<guard_dir_guard_uninterceptable
if (GuardDir(Knight,placed_on).dir<guard_dir_guard_uninterceptable
&& intelligent_reserve_promoting_white_pawn_moves_from_to(placed_comes_from,
Knight,
placed_on))
Expand Down Expand Up @@ -162,16 +161,15 @@ static stack_elmt_type const *stack_top = 0;
static void intercept_queen_diag(slice_index si)
{
square const placed_on = stack_top->placed_on;
int const dir_diag = GuardDir[Bishop-Pawn][placed_on].dir;
square const target_diag = GuardDir[Bishop-Pawn][placed_on].target;
guard_dir_struct const guard_dir = GuardDir(Bishop,placed_on);

TraceFunctionEntry(__func__);
TraceFunctionParamListEnd();

if (dir_diag==0 || TSTFLAG(being_solved.spec[target_diag],Black) || !is_line_empty(placed_on,target_diag,dir_diag))
if (guard_dir.dir==0 || TSTFLAG(being_solved.spec[guard_dir.target],Black) || !is_line_empty(placed_on,guard_dir.target,guard_dir.dir))
(*stack_top->go_on)(si);
else
intelligent_intercept_guard_by_white(si,target_diag,dir_diag,stack_top->go_on);
intelligent_intercept_guard_by_white(si,guard_dir.target,guard_dir.dir,stack_top->go_on);

TraceFunctionExit(__func__);
TraceFunctionResultEnd();
Expand All @@ -185,32 +183,32 @@ void intelligent_place_white_queen(slice_index si,
piece_walk_type const placed_type = white[placed_index].type;
Flags const placed_flags = white[placed_index].flags;
square const placed_comes_from = white[placed_index].diagram_square;
int const dir_ortho = GuardDir[Rook-Pawn][placed_on].dir;
int const dir_diag = GuardDir[Bishop-Pawn][placed_on].dir;
guard_dir_struct const rook_guard_dir = GuardDir(Rook,placed_on);
int const dir_diag = GuardDir(Bishop,placed_on).dir;

TraceFunctionEntry(__func__);
TraceFunctionParam("%u",placed_index);
TraceSquare(placed_on);
TraceFunctionParamListEnd();

if (dir_ortho<guard_dir_guard_uninterceptable
if (rook_guard_dir.dir<guard_dir_guard_uninterceptable
&& dir_diag<guard_dir_guard_uninterceptable
&& intelligent_reserve_officer_moves_from_to(White,
placed_comes_from,
placed_type,
placed_on))
{
square const target_ortho = GuardDir[Rook-Pawn][placed_on].target;
square const target_ortho = rook_guard_dir.target;

stack_elmt_type const new_top = { placed_index, placed_on, go_on, stack_top };
stack_top = &new_top;

occupy_square(placed_on,placed_type,placed_flags);

if (dir_ortho==0 || TSTFLAG(being_solved.spec[target_ortho],Black) || !is_line_empty(placed_on,target_ortho,dir_ortho))
if (rook_guard_dir.dir==0 || TSTFLAG(being_solved.spec[target_ortho],Black) || !is_line_empty(placed_on,target_ortho,rook_guard_dir.dir))
intercept_queen_diag(si);
else
intelligent_intercept_guard_by_white(si,target_ortho,dir_ortho,&intercept_queen_diag);
intelligent_intercept_guard_by_white(si,target_ortho,rook_guard_dir.dir,&intercept_queen_diag);

assert(stack_top==&new_top);
stack_top = stack_top->next;
Expand All @@ -230,20 +228,19 @@ void intelligent_place_white_rider(slice_index si,
piece_walk_type const placed_type = white[placed_index].type;
Flags const placed_flags = white[placed_index].flags;
square const placed_comes_from = white[placed_index].diagram_square;
int const dir = GuardDir[placed_type-Pawn][placed_on].dir;
square const target = GuardDir[placed_type-Pawn][placed_on].target;
guard_dir_struct const guard_dir = GuardDir(placed_type,placed_on);

TraceFunctionEntry(__func__);
TraceFunctionParam("%u",placed_index);
TraceSquare(placed_on);
TraceFunctionParamListEnd();

TraceValue("%d",dir);
TraceValue("%d",guard_dir.dir);
TraceValue("%d",guard_dir_guard_uninterceptable);
TraceValue("%u",index_of_guarding_piece);
TraceEOL();

switch (dir)
switch (guard_dir.dir)
{
case guard_dir_check_uninterceptable:
break;
Expand All @@ -270,12 +267,12 @@ void intelligent_place_white_rider(slice_index si,
occupy_square(placed_on,placed_type,placed_flags);

if (placed_index>index_of_guarding_piece
|| dir==0
|| TSTFLAG(being_solved.spec[target],Black)
|| !is_line_empty(placed_on,target,dir))
|| guard_dir.dir==0
|| TSTFLAG(being_solved.spec[guard_dir.target],Black)
|| !is_line_empty(placed_on,guard_dir.target,guard_dir.dir))
(*go_on)(si);
else
intelligent_intercept_guard_by_white(si,target,dir,go_on);
intelligent_intercept_guard_by_white(si,guard_dir.target,guard_dir.dir,go_on);

intelligent_unreserve();
}
Expand All @@ -299,7 +296,7 @@ void intelligent_place_white_knight(slice_index si,
TraceSquare(placed_on);
TraceFunctionParamListEnd();

switch (GuardDir[Knight-Pawn][placed_on].dir)
switch (GuardDir(Knight,placed_on).dir)
{
case guard_dir_check_uninterceptable:
break;
Expand Down
Loading