// **************************************************************************** // * * // * Velena Source Code V1.0 * // * Written by Giuliano Bertoletti * // * Based on the knowledged approach of Louis Victor Allis * // * Copyright (C) 1996-97 by Giuliano Bertoletti & GBE 32241 Software PR * // * * // **************************************************************************** // Portable engine version. // read the README file for further informations. // ========================================================================== #include #include #include #include #include "connect4.h" #include "con4vals.h" #include "rules.h" #include "pnsearch.h" #include "proto.h" #define DATABASEFILE "openbook.cn4" #define DATABASEBACKUP "openbook.bak" #define SHIPDISKFILE "tempship.$$$" #define BUFSIZE 0x4000 struct dbtree *mmnode; extern unsigned short *myscreen; unsigned char blk[12],position[64],tpos[64]; long blocks_wr,added_blocks,unused_blocks; void expand_block(unsigned char *blk,unsigned char *pss) { pss[ELM(0,0)]=(blk[0+0*2]>>6)&0x03; pss[ELM(1,0)]=(blk[0+0*2]>>4)&0x03; pss[ELM(2,0)]=(blk[0+0*2]>>2)&0x03; pss[ELM(3,0)]=(blk[0+0*2] )&0x03; pss[ELM(4,0)]=(blk[1+0*2]>>6)&0x03; pss[ELM(5,0)]=(blk[1+0*2]>>4)&0x03; pss[ELM(6,0)]=(blk[1+0*2]>>2)&0x03; pss[ELM(7,0)]=0xff; pss[ELM(0,1)]=(blk[0+1*2]>>6)&0x03; pss[ELM(1,1)]=(blk[0+1*2]>>4)&0x03; pss[ELM(2,1)]=(blk[0+1*2]>>2)&0x03; pss[ELM(3,1)]=(blk[0+1*2] )&0x03; pss[ELM(4,1)]=(blk[1+1*2]>>6)&0x03; pss[ELM(5,1)]=(blk[1+1*2]>>4)&0x03; pss[ELM(6,1)]=(blk[1+1*2]>>2)&0x03; pss[ELM(7,1)]=0xff; pss[ELM(0,2)]=(blk[0+2*2]>>6)&0x03; pss[ELM(1,2)]=(blk[0+2*2]>>4)&0x03; pss[ELM(2,2)]=(blk[0+2*2]>>2)&0x03; pss[ELM(3,2)]=(blk[0+2*2] )&0x03; pss[ELM(4,2)]=(blk[1+2*2]>>6)&0x03; pss[ELM(5,2)]=(blk[1+2*2]>>4)&0x03; pss[ELM(6,2)]=(blk[1+2*2]>>2)&0x03; pss[ELM(7,2)]=0xff; pss[ELM(0,3)]=(blk[0+3*2]>>6)&0x03; pss[ELM(1,3)]=(blk[0+3*2]>>4)&0x03; pss[ELM(2,3)]=(blk[0+3*2]>>2)&0x03; pss[ELM(3,3)]=(blk[0+3*2] )&0x03; pss[ELM(4,3)]=(blk[1+3*2]>>6)&0x03; pss[ELM(5,3)]=(blk[1+3*2]>>4)&0x03; pss[ELM(6,3)]=(blk[1+3*2]>>2)&0x03; pss[ELM(7,3)]=0xff; pss[ELM(0,4)]=(blk[0+4*2]>>6)&0x03; pss[ELM(1,4)]=(blk[0+4*2]>>4)&0x03; pss[ELM(2,4)]=(blk[0+4*2]>>2)&0x03; pss[ELM(3,4)]=(blk[0+4*2] )&0x03; pss[ELM(4,4)]=(blk[1+4*2]>>6)&0x03; pss[ELM(5,4)]=(blk[1+4*2]>>4)&0x03; pss[ELM(6,4)]=(blk[1+4*2]>>2)&0x03; pss[ELM(7,4)]=0xff; pss[ELM(0,5)]=(blk[0+5*2]>>6)&0x03; pss[ELM(1,5)]=(blk[0+5*2]>>4)&0x03; pss[ELM(2,5)]=(blk[0+5*2]>>2)&0x03; pss[ELM(3,5)]=(blk[0+5*2] )&0x03; pss[ELM(4,5)]=(blk[1+5*2]>>6)&0x03; pss[ELM(5,5)]=(blk[1+5*2]>>4)&0x03; pss[ELM(6,5)]=(blk[1+5*2]>>2)&0x03; pss[ELM(7,5)]=0xff; memset(&pss[48],0xff,16); } void collapse_position(unsigned char *mypos,unsigned char *blk) { short y; for(y=0;ycpos,tpos); cmp=bin_compare(tpos,mypos); if(cmp<0) return test_dbtree_position(root->lson,mypos); else if(cmp>0) return test_dbtree_position(root->rson,mypos); return root->value; } void add_dbtree(struct dbtree *root,unsigned char *mypos,short value) { short cmp; if(!root) fatal_error("Tryied to build an unallocated tree!"); expand_block(root->cpos,tpos); cmp=bin_compare(tpos,mypos); if(cmp<0) { if(root->lson) add_dbtree(root->lson,mypos,value); else { root->lson=(struct dbtree *)c4_malloc(sizeof(struct dbtree)); if(!root->lson) printf ("add_dbtree1\n"); root->lson->parent=root; root->lson->lson=NULL; root->lson->rson=NULL; root->lson->value=value; root->lson->stacked=NO; collapse_position(mypos,root->lson->cpos); } } else if(cmp>0) { if(root->rson) add_dbtree(root->rson,mypos,value); else { root->rson=(struct dbtree *)c4_malloc(sizeof(struct dbtree)); if(!root->rson) printf ("add_dbtree2\n"); root->rson->parent=root; root->rson->lson=NULL; root->rson->rson=NULL; root->rson->value=value; root->rson->stacked=NO; collapse_position(mypos,root->rson->cpos); } } return; } void my_add_dbtree(struct dbtree *root,unsigned char *mypos,short value,long posit) { short cmp; if(!root) fatal_error("Tryied to build an unallocated tree!"); expand_block(root->cpos,tpos); cmp=bin_compare(tpos,mypos); if(cmp<0) { if(root->lson) my_add_dbtree(root->lson,mypos,value,posit); else { root->lson=(struct dbtree *)&mmnode[posit]; if(!root->lson) fatal_error("Not enough memory"); root->lson->parent=root; root->lson->lson=NULL; root->lson->rson=NULL; root->lson->value=value; root->lson->stacked=YES; collapse_position(mypos,root->lson->cpos); } } else if(cmp>0) { if(root->rson) my_add_dbtree(root->rson,mypos,value,posit); else { root->rson=(struct dbtree *)&mmnode[posit]; if(!root->rson) fatal_error("Not enough memory"); root->rson->parent=root; root->rson->lson=NULL; root->rson->rson=NULL; root->rson->value=value; root->rson->stacked=YES; collapse_position(mypos,root->rson->cpos); } } return; } struct dbtree *init_dbase(unsigned char *currposit,short cancel_sym) { return NULL; } void my_free_dbtree(struct dbtree *root) { if(root->lson) my_free_dbtree(root->lson); if(root->rson) my_free_dbtree(root->rson); if(!root->stacked) free(root); } void free_dbtree(struct dbtree *root) { my_free_dbtree(root); if(mmnode) free(mmnode); } void flush_tree(struct dbtree *root,void *h1) { if(!root) return; if(root->cpos[0]!=0xff) { blocks_wr++; if((blocks_wr&0x03ff)==0) printf("Blocks written %ld\r",blocks_wr); } } void flush_dbtree(struct dbtree *root) { } void copy_dbtree() { } void update_dbtree(struct bintree *treeroot,struct dbtree *dbroot,short side) { short vl; if(treeroot->lson) update_dbtree(treeroot->lson,dbroot,side); if(treeroot->rson) update_dbtree(treeroot->rson,dbroot,side); if(side==WHITE) { if(treeroot->node->value==PROVED) vl=PROVED; else vl=DISPROVED; } else { if(treeroot->node->value==PROVED) vl=DISPROVED; else vl=PROVED; } if(treeroot->node->value!=UNKNOWN && !treeroot->node->direct) add_dbtree(dbroot,treeroot->node->square,vl); } /* --------------------------------------------------------------------- */ long myrandom(long maxval) { return(long)(((double)rand()*maxval)/32768.0); } void shuffle(void) { } /* ----------------------------------------------------------------- */ void free_small_tree(struct small_tree *root) { if(root->lson) free_small_tree(root->lson); if(root->rson) free_small_tree(root->rson); free(root); } void flush_smtree(struct small_tree *root,void *h1) { } void flush_small_tree(struct small_tree *root) { } short compare_small_tree(unsigned char *b1,unsigned char *b2) { short x; for(x=0;x<14;x++) { if(b1[x]lson=NULL; root->rson=NULL; memcpy(root->buffer,buffer,14); } else { short cmp; cmp=compare_small_tree(root->buffer,buffer); if(cmp<0) { root->lson=check_and_add(root->lson,buffer); root->lson->parent=root; } else if(cmp>0) { root->rson=check_and_add(root->rson,buffer); root->rson->parent=root; } } return root; } short merge_file(char *name) { return 1; } void merge() { unsigned short key; char filename[255]; printf("Enter name of file to merge: "); gets(filename); printf("\n"); printf("Sure you want to merge '%s' ? (Y/N)\n\n",filename); do { key=readkey(); } while(key!='n' && key!='y'); if(key=='y') { if(merge_file(filename)) printf("Everything went ok!\n"); else printf("Something went wrong, perhaps file has not been found!\n"); } else printf("Nothing has been done...\n"); printf("\n"); printf("Any key to continue!\n"); while(readkey()==0); } void erase_temp_file() { }