]> git.sesse.net Git - pgn-extract/blobdiff - output.c
Store a hash of the previous board, to be able to disambigutate transpositions. ...
[pgn-extract] / output.c
index 297cf8b37b3e107e6922cf1256b296c51e390893..2792bd807fbb043fed42aa7c9fdd68dc941f5442 100644 (file)
--- a/output.c
+++ b/output.c
@@ -24,6 +24,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <ctype.h>
+#include <time.h>
 #include "bool.h"
 #include "defs.h"
 #include "typedef.h"
@@ -35,6 +36,7 @@
 #include "output.h"
 #include "mymalloc.h"
 #include "eco.h"
+#include "farmhash-c.h"
 
 
             /* Functions for outputting games in the required format. */
@@ -1195,29 +1197,63 @@ output_sesse_bin_game(Game current_game,FILE *outputfile,
         return;
     }
 
-    // Find Black and White Elos. Skip games with no Elo.
+    // Find Black and White Elos.
     const char *white_elo_tag = current_game.tags[WHITE_ELO_TAG];
     const char *black_elo_tag = current_game.tags[BLACK_ELO_TAG];
-    if (white_elo_tag == NULL || black_elo_tag == NULL) {
-        return;
+    int white_elo = 0, black_elo = 0;
+    if (white_elo_tag) {
+        white_elo = atoi(white_elo_tag);
+    }
+    if (black_elo_tag) {
+        black_elo = atoi(black_elo_tag);
+    }
+    int file_num = current_game.file_number;
+    long start_position = current_game.start_position;
+
+    // Parse date and time, if it exists. Set invalid dates to year 3000.
+    const char *date_tag = current_game.tags[DATE_TAG];
+    const char *time_tag = current_game.tags[TIME_TAG];
+    struct tm tm = {0};
+    time_t timestamp;
+    int year, month, day;
+    if (date_tag && sscanf(date_tag, "%u.%u.%u", &year, &month, &day) == 3) {
+        int hour, minute, second;
+        tm.tm_year = year - 1900;
+        tm.tm_mon = month - 1;
+        tm.tm_mday = day;
+
+        if (time_tag && sscanf(time_tag, "%u:%u:%u", &hour, &minute, &second) == 3) {
+            tm.tm_hour = hour;
+            tm.tm_min = minute;
+            tm.tm_sec = second;
+        }
+        timestamp = mktime(&tm);
+    } else {
+        timestamp = 32503680000;
     }
 
-    int white_elo = atoi(white_elo_tag);
-    int black_elo = atoi(black_elo_tag);
+    uint16_t prev_board_hash = 0;
 
     for (move = current_game.moves; move != NULL; move = move->next) {
-        unsigned int opening = move->eco ? move->eco->cumulative_hash_value : 0;
+        unsigned int opening = move->eco ? move->eco->cumulative_hash_value : 0;  // Truncate to 32 bits.
 
         // key
-        putc(move->bpfen_len + strlen((char *)move->move), outputfile);
+        putc(move->bpfen_len + sizeof(prev_board_hash), outputfile);
         fwrite(move->bpfen, move->bpfen_len, 1, outputfile);
-        fwrite(move->move, strlen((char *)move->move), 1, outputfile);
+        fwrite(&prev_board_hash, sizeof(prev_board_hash), 1, outputfile);
 
         // value
         putc(result_int, outputfile);
         fwrite(&white_elo, sizeof(white_elo), 1, outputfile);
         fwrite(&black_elo, sizeof(black_elo), 1, outputfile);
         fwrite(&opening, sizeof(opening), 1, outputfile);
+        fwrite(&timestamp, sizeof(timestamp), 1, outputfile);
+        fwrite(&file_num, sizeof(file_num), 1, outputfile);
+        fwrite(&start_position, sizeof(start_position), 1, outputfile);
+        putc(strlen((char *)move->move), outputfile);
+        fwrite(move->move, strlen((char *)move->move), 1, outputfile);
+
+        prev_board_hash = farmhash_32(move->bpfen, move->bpfen_len);
     }
 }