From: H.G. Muller Date: Fri, 6 May 2011 16:06:48 +0000 (+0200) Subject: Make book-edit function WB X-Git-Url: http://winboard.nl/cgi-bin?a=commitdiff_plain;h=6445f59125a3dff0cbb9426a0c0bd300a31075d1;p=xboard.git Make book-edit function WB The tags dialog is used to display the GUI book moves from the current position, and will be updated when you step or move to another one. The edited move list (with weights) can be written back to the book. --- diff --git a/backend.c b/backend.c index 2658402..8dcfbd6 100644 --- a/backend.c +++ b/backend.c @@ -9250,6 +9250,7 @@ ShowMove(fromX, fromY, toX, toY) DrawPosition(FALSE, boards[currentMove]); DisplayBothClocks(); HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1); + DisplayBook(currentMove); } void SendEgtPath(ChessProgramState *cps) @@ -12311,6 +12312,7 @@ void EditTagsEvent() { char *tags = PGNTags(&gameInfo); + bookUp = FALSE; EditTagsPopUp(tags, NULL); free(tags); } @@ -13491,6 +13493,7 @@ ForwardInner(target) if ( !matchMode && gameMode != Training) { // [HGM] PV info: routine tests if empty DisplayComment(currentMove - 1, commentList[currentMove]); } + DisplayBook(currentMove); } @@ -13593,6 +13596,7 @@ BackwardInner(target) HistorySet(parseList,backwardMostMove,forwardMostMove,currentMove-1); // [HGM] PV info: routine tests if comment empty DisplayComment(currentMove - 1, commentList[currentMove]); + DisplayBook(currentMove); } void diff --git a/backend.h b/backend.h index 005b0ff..8e499dd 100644 --- a/backend.h +++ b/backend.h @@ -111,6 +111,7 @@ extern ProcRef firstProgramPR, secondProgramPR; extern Board boards[]; extern char marker[BOARD_RANKS][BOARD_FILES]; extern char lastMsg[MSG_SIZ]; +extern Boolean bookUp; char *CmailMsg P((void)); /* Tord: Added the useFEN960 parameter in PositionToFEN() below */ @@ -232,6 +233,9 @@ Boolean LoadMultiPV P((int x, int y, char *buf, int index, int *start, int *end) void UnLoadPV P(()); void MovePV P((int x, int y, int h)); int PromoScroll P((int x, int y)); +void EditBookEvent P((void)); +Boolean DisplayBook P((int moveNr)); +void SaveToBook P((char *text)); char *StrStr P((char *string, char *match)); char *StrCaseStr P((char *string, char *match)); 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