// TRANSLATE AND OUTPUT MOVES - we can either remove this completely,
// or update it so that Draft4's moves are displayed on-screen.

void TROUT (int8_t OLDPOS, int8_t NEWPOS, int8_t * MODEp) {
#define MODE (*MODEp)
//int8_t OLDX, OLDY, NEWX, NEWY;
  (void)OLDPOS;(void)NEWPOS;
  //OLDY = OLDPOS / 10;
  //OLDX = OLDPOS - 10 * OLDY;
  //NEWY = NEWPOS / 10;
  //NEWX = NEWPOS - 10 * NEWY;
  if (MODE == 0) { // MODE == 1 is when adding subsequent moves after jumps to output.
    // p ("\nDRAFT4's move is ");
    MODE = 1;
  } else {
    // p (" , ");
  }
  // print_symbol ((char) OLDX + 'A');
  // print_symbol ((char) OLDY + '1');
  // print_symbol ('-');
  // print_symbol ((char) NEWX + 'A');
  // print_symbol ((char) NEWY + '1');
}

int8_t position_of (char S1, char S2) {
  return (int8_t) ((S1 - 'A') + 10 * (S2 - '1'));
}

static int8_t STATE;
static int8_t STATE_PieceNum;
static int8_t STATE_FromX;
static int8_t STATE_FromY;
static int8_t STATE_ToPos;

#define STATE_TIMEOUT STATE_ToPos       /* re-using a variable which is not needed when outputting error */
static int8_t STATE_ToX;
static int8_t STATE_ToY;
static int8_t STATE_icon_style;

static inline void POS2XY (int8_t POS, int8_t *XP, int8_t *YP) {
  int8_t X, Y;

  Y = POS / 10;
  X = POS - 10 * Y;             // 1:8
  X -= 4;
  Y -= 4;
  X *= 20;
  Y *= 20;
  *XP = X;
  *YP = Y;
}

static inline void GetIcon (int8_t I, int8_t *XP, int8_t *YP, int8_t *icon_style) {
  if (I <= 12) {
    *icon_style = COMP_COLOUR == 'B' ? 0 : 1;
    POS = COMP_POS (I);
    if (CKTYPE(COMP_TYPE (I)) == CROWN)
      *icon_style |= 2;
  } else {
    I -= 12; assert(1 <= I && I <= 12);
    *icon_style = COMP_COLOUR == 'B' ? 1 : 0;
    POS = OPP_POS (I);
    if (CKTYPE(OPP_TYPE (I)) == CROWN)
      *icon_style |= 2;
  }
  POS2XY (POS, XP, YP);
}

static inline void Display_Piece (int8_t X, int8_t Y, int8_t style) {
  static const int white_big[] = {
  // 0,   0,   0,  // adjustment for corner/center!
    -1, 120, 120, -1, 120,0,  -1, 120,-120, -1,0,-120,
    -1,-120,-120, -1,-120,0,  -1,-120, 120, -1,0, 120,
    1
  };
  set_scale(BIG_SCALE);
  Reset0Ref();
  switch (style) {
  case 0: /* white uncrowned */
    Moveto_d(Y+1/*+3*/,X+9);
    set_scale(TINY_SCALE);
    Intensity_a((uint8_t)(WHITE_INTENSITY-Dim));
    break;
  case 1: /* black uncrowned */
    Moveto_d(Y+1/*+3*/,X+9);
    set_scale(TINY_SCALE);
    Intensity_a((uint8_t)(BLACK_INTENSITY-Dim));
    break;
  case 2: /* white crowned */
    Moveto_d(Y+1/*+3*/,X+9);
    set_scale(TINY_SCALE);
    Intensity_a((uint8_t)(WHITE_INTENSITY-Dim));
#ifdef PIZERO
    Draw_Line_d(120,120);
    Draw_Line_d(120,0);
    Draw_Line_d(120,-120);
    Draw_Line_d(0,-120);
    Draw_Line_d(-120,-120);
    Draw_Line_d(-120,0);
    Draw_Line_d(-120,120);
    Draw_Line_d(0,120);
    Moveto_d(100,-25);
#else
    Draw_VLp((int8_t *)white_big);
    Moveto_d(-20,35);
#endif
    set_scale(TINY_SCALE_CROWNED);
    break;
  case 3: /* black crowned */
    Moveto_d(Y+1/*+3*/,X+9);
    set_scale(TINY_SCALE);
    Intensity_a((uint8_t)(BLACK_INTENSITY-Dim));
#ifdef PIZERO
    Draw_Line_d(120,120);
    Draw_Line_d(120,0);
    Draw_Line_d(120,-120);
    Draw_Line_d(0,-120);
    Draw_Line_d(-120,-120);
    Draw_Line_d(-120,0);
    Draw_Line_d(-120,120);
    Draw_Line_d(0,120);
    Moveto_d(100,-25);
#else
    Draw_VLp((int8_t *)white_big);
    Moveto_d(-20,35);
#endif
    set_scale(TINY_SCALE_CROWNED);
    break;
  case 4: /* cursor box */;
    Moveto_d (Y, X);
    set_scale (SMALL_SCALE);
    if ((Flash&128) == 0) Intensity_a((uint8_t)(WHITE_INTENSITY-Dim)); else Intensity_a((uint8_t)(BLACK_INTENSITY-Dim));
    //Draw_VLp((int8_t *)box);
    Moveto_d(-10,-11);
    Draw_Line_d(0,80);
    Draw_Line_d(80,0);
    Draw_Line_d(0,-80);
    Draw_Line_d(-80,0);
    //Moveto_d(-20,35);
    return;
  }
  // BUG: the diagonal sides should not be 120,120 but LENGTH 120, so 120/sqrt(2) - leave for now
#ifdef PIZERO
  Draw_Line_d(120,120);
  Draw_Line_d(120,0);
  Draw_Line_d(120,-120);
  Draw_Line_d(0,-120);
  Draw_Line_d(-120,-120);
  Draw_Line_d(-120,0);
  Draw_Line_d(-120,120);
  Draw_Line_d(0,120);
#else
  Draw_VLp((int8_t *)white_big);
#endif
}

void Fadein_text(void) {
  Reset0Ref ();
#ifdef BOARDHACK
  if (STATE_TIMEOUT==0) Intensity_a((uint8_t)(DIM)); else Intensity_a((uint8_t)(Dim));
#else
  Intensity_a(0x7f);
#endif
}

void Dim_Board(void) {
#ifdef BOARDHACK
  if (STATE_TIMEOUT < 0) Dim += 1;
  else if (STATE_TIMEOUT > 0) Dim -= 1;
  if (Dim >= MAXDIM) Dim = MAXDIM;
  if (Dim < 0) Dim = 0;
#endif
  Dim=MAXDIM;Display_board (0);Dim=0;
}

void Display_board (int8_t IgnoringP) {
  int8_t icon_style, I, J, X, Y, intensity_of_missing_piece=0x7F;
  (void)J;
#ifdef DRAWGRID
  set_scale(0x51);
  Intensity_a(BACKGROUND_INTENSITY-(uint8_t)Dim);
  // verticals
#ifdef INTERLACE
  if (Flash&1)
#endif
  for (I = 0; I < 9; I++) {
    Reset0Ref(); Intensity_a(BACKGROUND_INTENSITY-(uint8_t)Dim);Moveto_d(120, (I-4)*31-4); Draw_Line_d(-124,0);Draw_Line_d(-124,0);
  }
  // horizontals
#ifdef INTERLACE
  if (!(Flash&1))
#endif
  for (I = 0; I < 9; I++) {
    Reset0Ref(); Intensity_a(BACKGROUND_INTENSITY-(uint8_t)Dim);Moveto_d((I-4)*31-4, 120); Draw_Line_d(0,-124);Draw_Line_d(0,-124);
  }
#endif
  // and optionally, diagonals through the unused square colour
  // (Officially, the pieces are black and red, and the board is red and green!)
#ifdef DRAWHATCH
  set_scale(0x51);
  for (J = 120; J >= -4; J-=124) {
    for (I = 0; I < 8; I+=2) {
      Reset0Ref(); Intensity_a(BACKGROUND_INTENSITY-(uint8_t)Dim);Moveto_d(J, (I-4)*31-4);
      X = 4*31; Y = -124;
      if (I > 4) { X = (8-I)*31; Y = ((4-(8-I))*31)-124; }
      Draw_Line_d(Y,X);
    }
    Reset0Ref(); Intensity_a(BACKGROUND_INTENSITY-(uint8_t)Dim);Moveto_d(J-62, -128); Draw_Line_d(-62,62);
  }
#endif
#ifdef DRAWHATCHBACK
  // hatch in other direction.  needs different calculation - not a mirror...
#endif
  // If Piece IgnoringP is on the board, *do not* draw it.
  // IgnoringP in range 1:12 and 13:24

  // Computer's pieces:
  Intensity_a(BLACK_INTENSITY-(uint8_t)Dim);
  for (I = 1; I <= COMP_NUM; I++) {
    if (IgnoringP != I) {
      GetIcon (I, &X, &Y, &icon_style);
      Display_Piece (X, Y, icon_style);
    } else intensity_of_missing_piece = BLACK_INTENSITY-Dim;
  }

  IgnoringP -= 12;
  // Human's pieces:
  Intensity_a(WHITE_INTENSITY-(uint8_t)Dim);
  for (I = 1; I <= OPP_NUM; I++) {
    if (IgnoringP != I) {
      GetIcon (I + 12, &X, &Y, &icon_style);
      Display_Piece (X, Y, icon_style);
    } else intensity_of_missing_piece = WHITE_INTENSITY-Dim;
  }

  Intensity_a((uint8_t)intensity_of_missing_piece); // assumes missing piece is displayed immediately after...
}

void Animate (int8_t PieceNum, int8_t NewPos) {
  STATE_PieceNum = PieceNum;
  STATE_ToPos = NewPos;
  GetIcon (PieceNum, &STATE_FromX, &STATE_FromY, &STATE_icon_style);
  POS2XY (STATE_ToPos, &STATE_ToX, &STATE_ToY);
}

static int8_t Cursor_X, Cursor_Y;