X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=book.c;h=aecb30df8c9dfa85c1b938cfe76a4543a048bbb6;hb=6445f59125a3dff0cbb9426a0c0bd300a31075d1;hp=bd3a49ff14521f327b200f04a672c68a5d1e4d0f;hpb=7fb59a9a90f59e4e94ff49e2eb55ba7c24b2ef7b;p=xboard.git diff --git a/book.c b/book.c index bd3a49f..aecb30d 100644 --- a/book.c +++ b/book.c @@ -455,25 +455,22 @@ void move_to_string(char move_s[6], uint16 move) } } -char *ProbeBook(int moveNr, char *book) -{ +int GetBookMoves(int moveNr, char *book, entry_t entries[]) +{ // retrieve all entries for given position from book in 'entries', return number. FILE *f; entry_t entry; int offset; uint64 key; - entry_t entries[MOVE_BUF]; - int count=0; - int ret, i, j; - static char move_s[6]; - int total_weight; + int count; + int ret; - if(book == NULL || moveNr >= 2*appData.bookDepth) return NULL; -// if(gameInfo.variant != VariantNormal) return NULL; // Zobrist scheme only works for normal Chess, so far + if(book == NULL || moveNr >= 2*appData.bookDepth) return -1; +// if(gameInfo.variant != VariantNormal) return -1; // Zobrist scheme only works for normal Chess, so far f=fopen(book,"rb"); if(!f){ - DisplayError("Polyglot book not valid", 0); + DisplayError("Polyglot book not valid", 0); appData.usePolyglotBook = FALSE; - return NULL; + return -1; } key = hash(moveNr); @@ -482,7 +479,7 @@ char *ProbeBook(int moveNr, char *book) offset=find_key(f, key, &entry); if(entry.key != key) { fclose(f); - return NULL; + return FALSE; } entries[0] = entry; count=1; @@ -498,6 +495,20 @@ char *ProbeBook(int moveNr, char *book) if(count == MOVE_BUF) break; entries[count++] = entry; } + fclose(f); + return count; +} + +char *ProbeBook(int moveNr, char *book) +{ // + entry_t entries[MOVE_BUF]; + int count; + int i, j; + static char move_s[6]; + int total_weight; + + if((count = GetBookMoves(moveNr, book, entries)) <= 0) return NULL; // no book, or no hit + if(appData.bookStrength != 50) { // transform weights double power = 0, maxWeight = 0.0; if(appData.bookStrength) power = (100.-appData.bookStrength)/appData.bookStrength; @@ -522,6 +533,126 @@ char *ProbeBook(int moveNr, char *book) move_to_string(move_s, entries[i].move); if(appData.debugMode) fprintf(debugFP, "book move field = %d\n", entries[i].move); - fclose(f); return move_s; } + +extern char yy_textstr[]; +entry_t lastEntries[MOVE_BUF]; + +char *MovesToText(int count, entry_t *entries) +{ + int i, totalWeight = 0; + char algMove[6]; + char *p = (char*) malloc(30*count+1); + for(i=0; i=0;i--) fputc(r>>8*i & 255, f); +} + +void entry_to_file(FILE *f, entry_t *entry) +{ + int_to_file(f,8,entry->key); + int_to_file(f,2,entry->move); + int_to_file(f,2,entry->weight); + int_to_file(f,4,entry->learn); +} + +char buf1[4096], buf2[4096]; + +void SaveToBook(char *text) +{ + entry_t entries[MOVE_BUF], entry; + int count = TextToMoves(text, currentMove, entries); + int offset, i, len1=0, len2, readpos=0, writepos=0; + FILE *f; + if(!count) return; + f=fopen(appData.polyglotBook, "rb+"); + if(!f){ DisplayError("Polyglot book not valid", 0); return; } + offset=find_key(f, entries[0].key, &entry); + if(entries[0].key != entry.key) { + DisplayError("Hash keys are different", 0); + fclose(f); + return; + } + if(count != currentCount) { + readpos = 16*(offset + currentCount); + writepos = 16*(offset + count); + fseek(f, readpos, SEEK_SET); + readpos += len1 = fread(buf1, 1, 4096 - 16*currentCount, f); // salvage some entries immediately behind change + } + fseek(f, 16*(offset), SEEK_SET); + for(i=0; i