574 } |
574 } |
575 |
575 |
576 static int getlocation(GameState *gamestate, Move *move) { |
576 static int getlocation(GameState *gamestate, Move *move) { |
577 |
577 |
578 uint8_t color = move->piece & COLOR_MASK; |
578 uint8_t color = move->piece & COLOR_MASK; |
|
579 uint8_t piece = move->piece & PIECE_MASK; |
579 _Bool incheck = gamestate->lastmove?gamestate->lastmove->move.check:0; |
580 _Bool incheck = gamestate->lastmove?gamestate->lastmove->move.check:0; |
580 |
581 |
581 Move threats[16], *threat = NULL; |
582 Move threats[16], *threat = NULL; |
582 uint8_t threatcount; |
583 uint8_t threatcount; |
583 |
584 |
586 |
587 |
587 int reason = INVALID_POSITION; |
588 int reason = INVALID_POSITION; |
588 |
589 |
589 /* find threats for the specified position */ |
590 /* find threats for the specified position */ |
590 for (uint8_t i = 0 ; i < threatcount ; i++) { |
591 for (uint8_t i = 0 ; i < threatcount ; i++) { |
591 if ((threats[i].piece & (PIECE_MASK | COLOR_MASK)) |
592 if ((threats[i].piece & PIECE_MASK) == piece |
592 == move->piece && |
593 && (threats[i].piece & COLOR_MASK) == color && |
593 (move->fromrow == POS_UNSPECIFIED || |
594 (move->fromrow == POS_UNSPECIFIED || |
594 move->fromrow == threats[i].fromrow) && |
595 move->fromrow == threats[i].fromrow) && |
595 (move->fromfile == POS_UNSPECIFIED || |
596 (move->fromfile == POS_UNSPECIFIED || |
596 move->fromfile == threats[i].fromfile)) { |
597 move->fromfile == threats[i].fromfile)) { |
597 |
598 |
598 if (threat) { |
599 if (threat) { |
599 return AMBIGUOUS_MOVE; |
600 return AMBIGUOUS_MOVE; |
600 } else { |
601 } else { |
601 /* found threat is no real threat */ |
602 /* found threat is no real threat */ |
602 if (is_pinned(gamestate, &(threats[i]))) { |
603 if (is_pinned(gamestate, &(threats[i]))) { |
603 reason = incheck?KING_IN_CHECK:PIECE_PINNED; |
604 reason = incheck?KING_IN_CHECK: |
|
605 (piece==KING?KING_MOVES_INTO_CHECK:PIECE_PINNED); |
604 } else { |
606 } else { |
605 threat = &(threats[i]); |
607 threat = &(threats[i]); |
606 } |
608 } |
607 } |
609 } |
608 } |
610 } |