home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / ARASAN_S.ZIP / BOOKREAD.CPP < prev    next >
C/C++ Source or Header  |  1994-08-06  |  2KB  |  75 lines

  1. // Copyright 1993 by Jon Dart.  All Rights Reserved.
  2.  
  3. #include "bookread.h"
  4. #include "constant.h"
  5. #include "bhash.h"
  6. #include <assert.h>
  7. #include <wpglob.h>
  8. #include <string.h>
  9.  
  10. const int Entry_Size = sizeof(Book_Entry);
  11. const int Header_Size = 8;
  12. // version of opening book, change it if the format changes:
  13. const int Book_Version = 2;
  14.  
  15. Book_Reader::Book_Reader(const char *const filename)
  16. {
  17.    HRSRC hr = FindResource(App.getHinst(),filename,"BOOK");
  18.    hBook = LoadResource(App.getHinst(),hr);
  19.    is_open = hBook == 0 ? False : True;
  20.    if (!is_open)
  21.       return;
  22.    char version;
  23.    LPSTR lpBook = LockResource(hBook);
  24.    version = lpBook[0];
  25.    if (version != Book_Version)
  26.    {
  27.       MessageBox(NULL,"Wrong version of opening book!","",MB_OK);
  28.       UnlockResource(hBook);
  29.       is_open = False;
  30.       return;
  31.    }
  32.    unsigned char lo_size = lpBook[1];
  33.    unsigned char hi_size = lpBook[2];
  34.    my_hash_size = hi_size*256 + lo_size;
  35.    lo_size = lpBook[3];
  36.    hi_size = lpBook[4];
  37.    my_size = hi_size*256 + lo_size;
  38.    UnlockResource(hBook);
  39. }
  40.  
  41. Book_Reader::~Book_Reader()
  42. {
  43.    FreeResource(hBook);   
  44. }
  45.  
  46. const unsigned Book_Reader::Head( const Board & b)
  47. {
  48. #ifdef RANGE_CHECK
  49.    assert(is_open);
  50. #endif
  51.    byte *lpBook = (byte*)LockResource(hBook);
  52.    unsigned probe = Board_Hash::HashCode2(b) % my_hash_size;
  53.    unsigned ret_val;
  54.    memcpy(&ret_val,lpBook+Header_Size+2*probe,2);
  55.    UnlockResource(hBook);
  56.    return ret_val;
  57. }
  58.  
  59. void Book_Reader::Fetch( const uint16 n, Book_Entry &book_entry )
  60. {
  61. #ifdef RANGE_CHECK
  62.    assert(is_open);
  63.    assert(n<my_size);
  64. #endif
  65.    byte *lpBook = (byte*)LockResource(hBook);
  66.    byte *entry = lpBook + Header_Size + my_hash_size*2 + n*Entry_Size;
  67.    memcpy(&book_entry.hash_code,entry,sizeof(unsigned long));
  68.    book_entry.recommend = entry[4];
  69.    book_entry.move_index = entry[5];
  70.    book_entry.next = entry[6] + entry[7]*256;
  71.    UnlockResource(hBook);
  72. }
  73.  
  74.  
  75.