src/main.c

changeset 182
04c65336777f
parent 180
0fed680c487c
--- a/src/main.c	Sat Aug 22 15:15:52 2026 +0200
+++ b/src/main.c	Sat Aug 22 16:22:05 2026 +0200
@@ -473,7 +473,7 @@
                 /* ignore empty move strings and ask again */
             } else {
                 Move move;
-                int result = eval_move_lazy(gamestate,
+                int result = eval_move(gamestate,
                     movestr, curcolor, &move);
                 if (result == VALID_MOVE_SYNTAX) {
                     result = validate_move(gamestate, &move);
@@ -496,6 +496,31 @@
     }
 }
 
+static NetMove hton_move(const Move *move) {
+    return (NetMove) {
+        .mtime_sec = htonl(move->movetime / 1000000ull),
+        .mtime_usec = htonl(move->movetime % 1000000ull),
+        .piece = move->piece,
+        .fromfile = move->fromfile, .fromrow = move->fromrow,
+        .tofile = move->tofile, .torow = move->torow,
+        .promotion = move->promotion
+    };
+}
+
+static Move ntoh_move(const NetMove *move) {
+    return (Move) {
+        .string = {0}, /* calculated before applying the move */
+        .timestamp = {0}, /* planned to be removed */
+        .movetime = (uint64_t)move->mtime_sec * 1000000ull + move->mtime_usec,
+        .piece = move->piece,
+        .fromfile = move->fromfile, .fromrow = move->fromrow,
+        .tofile = move->tofile, .torow = move->torow,
+        .promotion = move->promotion,
+        /* flags are also calculated before applying the move */
+        0
+    };
+}
+
 static int sendmove(GameState *gamestate, int opponent, uint8_t mycolor) {
 
     size_t bufpos = 0;
@@ -607,11 +632,12 @@
                 /* ignore empty move strings and ask again */
             } else {
                 Move move;
-                int eval_result = eval_move_lazy(gamestate,
+                int eval_result = eval_move(gamestate,
                     movestr, mycolor, &move);
                 switch (eval_result) {
                 case VALID_MOVE_SYNTAX:
-                    net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
+                    NetMove nmove = hton_move(&move);
+                    net_send_data(opponent, NETCODE_MOVE, &nmove, NETMOVE_LEN);
                     code = net_recieve_code(opponent);
                     /* we could validate the move's check/checkmate flag with
                      * the network response code, but we choose not to do it */
@@ -760,8 +786,10 @@
             }
             break;
         case NETCODE_MOVE: {
-            Move move;
-            net_recieve_data(opponent, &move, sizeof(Move));
+            NetMove nmove;
+            net_recieve_data(opponent, &nmove, NETMOVE_LEN);
+            Move move = ntoh_move(&nmove);
+            format_move(gamestate, &move);
             code = validate_move(gamestate, &move);
             if (code == VALID_MOVE_SEMANTICS) {
                 apply_move(gamestate, &move);
@@ -1049,13 +1077,18 @@
     if (settings.continuepgn) {
         /* Continue game, send PGN data */
         uint16_t mc = gamestate->movecount;
-        size_t pgndata_size = sizeof(GameInfo)+sizeof(mc)+mc*sizeof(Move);
+        size_t pgndata_size = sizeof(GameInfo) + 2 + mc * NETMOVE_LEN;
         char *pgndata = malloc(pgndata_size);
         memcpy(pgndata, &(gamestate->info), sizeof(GameInfo));
         unsigned offset = sizeof(GameInfo);
-        memcpy(pgndata+offset, &mc, sizeof(mc));
-        offset += sizeof(mc);
-        memcpy(pgndata+offset, gamestate->moves, mc*sizeof(Move));
+        uint16_t mcn = htons(mc);
+        memcpy(pgndata+offset, &mcn, 2);
+        offset += 2;
+        for (unsigned i = 0 ; i < mc ; i++) {
+            NetMove nmove = hton_move(gamestate->moves + i);
+            memcpy(pgndata+offset, &nmove, NETMOVE_LEN);
+            offset += NETMOVE_LEN;
+        }
         net_send_data(fd, NETCODE_PGNDATA, pgndata, pgndata_size);
         free(pgndata);
     } else {
@@ -1150,12 +1183,19 @@
         net_recieve_data(server.fd, &(gamestate->info), sizeof(GameInfo));
         uint16_t mc;
         net_recieve_data(server.fd, &mc, sizeof(mc));
-        Move *moves = calloc(mc, sizeof(Move));
-        net_recieve_data(server.fd, moves, mc*sizeof(Move));
+        mc = ntohs(mc);
+        char *movedata = malloc(mc * NETMOVE_LEN);
+        net_recieve_data(server.fd, movedata, mc*NETMOVE_LEN);
+        unsigned offset = 0;
         for (size_t i = 0 ; i < mc ; i++) {
-            apply_move(gamestate, &(moves[i]));
+            NetMove nmove;
+            memcpy(&nmove, movedata+offset, NETMOVE_LEN);
+            offset += NETMOVE_LEN;
+            Move move = ntoh_move(&nmove);
+            format_move(gamestate, &move);
+            apply_move(gamestate, &move);
         }
-        free(moves);
+        free(movedata);
         dump_gameinfo(gamestate);
         if (prompt_yesno(
                 "\n\nServer wants to continue a game. Accept challenge")) {

mercurial