| 476 int result = eval_move(gamestate, |
476 int result = eval_move(gamestate, |
| 477 movestr, curcolor, &move); |
477 movestr, curcolor, &move); |
| 478 if (result == VALID_MOVE_SYNTAX) { |
478 if (result == VALID_MOVE_SYNTAX) { |
| 479 result = validate_move(gamestate, &move); |
479 result = validate_move(gamestate, &move); |
| 480 if (result == VALID_MOVE_SEMANTICS) { |
480 if (result == VALID_MOVE_SEMANTICS) { |
| |
481 format_move(gamestate, &move); |
| 481 apply_move(gamestate, &move); |
482 apply_move(gamestate, &move); |
| 482 if (is_game_running(gamestate)) { |
483 if (is_game_running(gamestate)) { |
| 483 return 0; |
484 return 0; |
| 484 } else { |
485 } else { |
| 485 return 1; |
486 return 1; |
| 494 } |
495 } |
| 495 } |
496 } |
| 496 } |
497 } |
| 497 } |
498 } |
| 498 |
499 |
| 499 #define NETMOVE_LEN 14 |
500 #define NETMOVE_LEN 16 |
| 500 #define NETGI_LEN 8 |
501 #define NETGI_LEN 8 |
| 501 |
502 |
| 502 static NetGameInfo hton_gi(GameInfo gi) { |
503 static NetGameInfo hton_gi(GameInfo gi) { |
| 503 return (NetGameInfo) { |
504 return (NetGameInfo) { |
| 504 .servercolor = gi.servercolor, |
505 .servercolor = gi.servercolor, |
| 505 .timecontrol = gi.timecontrol ? 1 : 0, |
506 .timecontrol = gi.timecontrol, |
| 506 .time = net_htons(gi.time), |
507 .time = net_htons(gi.time), |
| 507 .addtime = net_htons(gi.addtime), |
508 .addtime = net_htons(gi.addtime), |
| 508 .delay = net_htons(gi.delay) |
509 .delay = net_htons(gi.delay) |
| 509 }; |
510 }; |
| 510 } |
511 } |
| 524 .mtime_sec = net_htonl(move.movetime / 1000000ull), |
525 .mtime_sec = net_htonl(move.movetime / 1000000ull), |
| 525 .mtime_usec = net_htonl(move.movetime % 1000000ull), |
526 .mtime_usec = net_htonl(move.movetime % 1000000ull), |
| 526 .piece = move.piece, |
527 .piece = move.piece, |
| 527 .fromfile = move.fromfile, .fromrow = move.fromrow, |
528 .fromfile = move.fromfile, .fromrow = move.fromrow, |
| 528 .tofile = move.tofile, .torow = move.torow, |
529 .tofile = move.tofile, .torow = move.torow, |
| 529 .promotion = move.promotion |
530 .capture = move.capture, |
| |
531 .check_or_mate = move.checkmate + move.check, |
| |
532 .promotion = move.promotion, |
| 530 }; |
533 }; |
| 531 } |
534 } |
| 532 |
535 |
| 533 static Move ntoh_move(NetMove move) { |
536 static Move ntoh_move(NetMove move) { |
| 534 return (Move) { |
537 return (Move) { |
| 538 * 1000000ull + net_ntohl(move.mtime_usec), |
541 * 1000000ull + net_ntohl(move.mtime_usec), |
| 539 .piece = move.piece, |
542 .piece = move.piece, |
| 540 .fromfile = move.fromfile, .fromrow = move.fromrow, |
543 .fromfile = move.fromfile, .fromrow = move.fromrow, |
| 541 .tofile = move.tofile, .torow = move.torow, |
544 .tofile = move.tofile, .torow = move.torow, |
| 542 .promotion = move.promotion, |
545 .promotion = move.promotion, |
| 543 /* flags are also calculated before applying the move */ |
546 .check = move.check_or_mate, |
| 544 0 |
547 .checkmate = move.check_or_mate >> 1, |
| |
548 .capture = move.capture |
| 545 }; |
549 }; |
| 546 } |
550 } |
| 547 |
551 |
| 548 static int sendmove(GameState *gamestate, int opponent, uint8_t mycolor) { |
552 static int sendmove(GameState *gamestate, int opponent, uint8_t mycolor) { |
| 549 |
553 |
| 674 } else if (code == NETCODE_ACCEPT |
678 } else if (code == NETCODE_ACCEPT |
| 675 || code == NETCODE_CHECK |
679 || code == NETCODE_CHECK |
| 676 || code == NETCODE_CHECKMATE |
680 || code == NETCODE_CHECKMATE |
| 677 || code == NETCODE_STALEMATE |
681 || code == NETCODE_STALEMATE |
| 678 || code == NETCODE_NOMATERIAL) { |
682 || code == NETCODE_NOMATERIAL) { |
| |
683 format_move(gamestate, &move); |
| 679 apply_move(gamestate, &move); |
684 apply_move(gamestate, &move); |
| 680 if (is_game_running(gamestate)) { |
685 if (is_game_running(gamestate)) { |
| 681 return 0; |
686 return 0; |
| 682 } else { |
687 } else { |
| 683 return 1; |
688 return 1; |
| 810 break; |
815 break; |
| 811 case NETCODE_MOVE: { |
816 case NETCODE_MOVE: { |
| 812 NetMove nmove; |
817 NetMove nmove; |
| 813 net_recieve_data(opponent, &nmove, NETMOVE_LEN); |
818 net_recieve_data(opponent, &nmove, NETMOVE_LEN); |
| 814 Move move = ntoh_move(nmove); |
819 Move move = ntoh_move(nmove); |
| 815 format_move(gamestate, &move); |
|
| 816 code = validate_move(gamestate, &move); |
820 code = validate_move(gamestate, &move); |
| 817 if (code == VALID_MOVE_SEMANTICS) { |
821 if (code == VALID_MOVE_SEMANTICS) { |
| |
822 format_move(gamestate, &move); |
| 818 apply_move(gamestate, &move); |
823 apply_move(gamestate, &move); |
| 819 if (gamestate->checkmate) { |
824 if (gamestate->checkmate) { |
| 820 net_send_code(opponent, NETCODE_CHECKMATE); |
825 net_send_code(opponent, NETCODE_CHECKMATE); |
| 821 return 1; |
826 return 1; |
| 822 } else if (gamestate->stalemate) { |
827 } else if (gamestate->stalemate) { |