{ position_status.pas -- set up a position and display the board
  plus everything curr_e (sandef.h) tracks about it: side to move,
  castling rights, the en passant target square, the halfmove
  clock, and the fullmove number. Pascal translation of
  position_status.c.

  curr_e is the first extern *record* (rather than a scalar or a
  simple array) this set of Pascal examples binds -- see TEnv's own
  header comment in sankit.pas for how its field-by-field layout was
  checked, against the real C struct's sizeof()/offsetof() output,
  rather than assumed. }

program position_status;

uses
  SanKit;

{ #define map_file_sq(sq) ((sq) & fileM) / map_rank_sq(sq) ((sq) >>
  fileQ) in sandef.h -- pure bit arithmetic in C, with no function
  of their own to bind, so reproduced directly here instead }
function MapFileSq(sq: LongInt): LongInt;
begin
  MapFileSq := sq and 7;
end;

function MapRankSq(sq: LongInt): LongInt;
begin
  MapRankSq := sq shr 3;
end;

begin
  SANInit;
  SANFENSet(
    'rnbqkbnr/ppp1pppp/8/3pP3/8/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 3');

  SANDspBoard;
  FlushCOutput(nil);

  WriteLn;
  if curr_e.e_actc = c_w then
    WriteLn('Side to move:     White')
  else
    WriteLn('Side to move:     Black');

  Write('Castling rights:  ');
  if (curr_e.e_cast and cast_wk) <> 0 then Write('K');
  if (curr_e.e_cast and cast_wq) <> 0 then Write('Q');
  if (curr_e.e_cast and cast_bk) <> 0 then Write('k');
  if (curr_e.e_cast and cast_bq) <> 0 then Write('q');
  WriteLn;

  if curr_e.e_epsq = sq_nil then
    WriteLn('En passant:       (none)')
  else
    WriteLn('En passant:       ',
            acfv[MapFileSq(curr_e.e_epsq)],
            acrv[MapRankSq(curr_e.e_epsq)]);

  WriteLn('Halfmove clock:   ', curr_e.e_hmvc);
  WriteLn('Fullmove number:  ', curr_e.e_fmvn);
end.
