int MAP_readMapConfFile(char *filename) { FILE *file; char line[512]; int maximagenumber = 0; int i; int linenum = 0; int imgdataindex = 0; file = fopen(filename, "r"); if(file == NULL) { fprint("无法打开文件 %s\n", filename); return FALSE; } while(fgets(line, sizeof(line), file)) { char imgnum[16]; int ret; int imgnumber; deleteSequentChar(line, " \t"); pohcd(line, " \t"); dchop(line, " \t"); if(line[0] == '#')continue; if(line[0] == '\n')continue; chomp(line); ret = getStringFromIndexWithDelim(line, " ", 1, imgnum, sizeof(imgnum)); if(ret == FALSE)continue; imgnumber = atoi(imgnum); maximagenumber = max(maximagenumber, imgnumber); } MAP_imagedatanum = maximagenumber + 1; MAP_imagedata = allocateMemory(sizeof(MAP_ImageData) * MAP_imagedatanum); if(MAP_imagedata == NULL) { fprint("无法分配地图数据大小=%d\n", MAP_imagedatanum); return FALSE; } for(i = 0; i < arraysizeof(MAP_imgfilt); i++) MAP_imgfilt[i] = -1; if(fseek(file, 0, SEEK_SET) != 0) { fprint("无法查找 SEEK_SET %s\n", strerror(errno)); return FALSE; } while(fgets(line, sizeof(line), file)) { char token[64]; int imagenumber; int ret; int i; MAP_ImageData *one; int offset; linenum++; deleteSequentChar(line, " \t"); pohcd(line, " \t"); chomp(line); if(line[0] == '#')continue; if(line[0] == '\n')continue; chomp(line); ret = getStringFromIndexWithDelim(line, " ", 1, token, sizeof(token)); if(ret == FALSE)continue; imagenumber = atoi(token); MAP_imgfilt[imagenumber] = imgdataindex; one = &MAP_imagedata[imgdataindex]; imgdataindex++; MAP_makeDefaultImageData(one); offset = 4; for(i = 0; i < MAP_DATAINT_NUM + MAP_DATACHAR_NUM; i++) { int ret = getStringFromIndexWithDelim(line, " ", i + offset, token, sizeof(token)); if(ret == FALSE)continue; switch(MAP_confentries[i].type) { case MAP_INTENTRY: one->data[MAP_confentries[i].index] = atoi(token); break; case MAP_CHARENTRY: strcpysafe(one->string[MAP_confentries[i].index].string, sizeof(one->string[MAP_confentries[i].index]. string), token); break; case MAP_INTFUNC: { int (*intfunction)(char *); intfunction = MAP_confentries[i].func; one->data[MAP_confentries[i].index] = intfunction(token); break; } case MAP_CHARFUNC: { char *(*charfunction)(char *); charfunction = MAP_confentries[i].func; strcpysafe(one->string[MAP_confentries[i].index].string, sizeof(one->string[MAP_confentries[i].index]. string), charfunction(token)); break; } default: break; } } } fclose(file); return TRUE; }
/*------------------------------------------------------------ * NPCTemplate °×ÑëÄÌ»ïë·òÎç¹´ ¸ê£Û * ¦ÐÑ * filename char* ɬÀð×ÑëÄÌ»ïë ÐÄ炙§ÔÂÐþÓÀÃó·¸Å«ÒÁÛÍÐþØø * ߯Ի°À * ÔÀ TRUE(1) * ·Ö»§(³ðľ¶¯Ïþ°×ÑëÄÌ»ïë ÊÏÆ¥·´ÖÐØêئÖÐ) * Áà FALSE(0) ------------------------------------------------------------*/ BOOL NPC_readTemplateFile( char* filename ) { FILE* f; char line[512]; int linenum=0; int start=OFF; NPC_Template temp; int itmreadindex=0; char* ret; int randomdata[NPC_TEMPLATEINTNUM]; int i; if( NPC_template_readindex >= NPC_templatenum ){ print("Template Exceed configured buffer\n" ); print("configured template number is %d\n", NPC_templatenum); return FALSE; } for( i=0; i<NPC_TEMPLATEINTNUM ; i++ ) randomdata[i] = 0; NPC_setDefaultNPCTemplate( &temp ); f= fopen( filename ,"r"); if( f == NULL )return FALSE; ret = fgets( line, sizeof( line ), f ); if( ret == NULL )goto FCLOSERETURNFALSE; if( strcmp( NPC_TEMPLATEFILEMAGIC, line ) != 0 ){ print( "This file is not a template file.\n" ); goto FCLOSERETURNFALSE ; } linenum = 1; while( fgets( line , sizeof( line ) , f ) ){ linenum++; if( line[0] == '#' )continue; /* comment */ if( line[0] == '\n' )continue; /* none */ chomp( line ); switch( line[0] ){ case '{': if( start == ON ){ fprint( "Find {. But already START state. %s:%d\n", filename, linenum); goto FCLOSERETURNFALSE; }else start = ON; break; case '}': if( start == ON ){ int err=FALSE; if( strcmp(temp.chardata[NPC_TEMPLATENAME].string ,"") == 0 ){ fprint( "There is no name %s:%d\n", filename, linenum ); err = TRUE; }else if( temp.intdata[NPC_TEMPLATETYPE] == -1 ){ temp.intdata[NPC_TEMPLATETYPE] = SPR_pet001; } if( err ){ if( temp.intdata[NPC_TEMPLATEITEMNUM] != 0 ) freeMemory( temp.haveitem ); }else{ temp.hash = hashpjw(temp.chardata[NPC_TEMPLATENAME].string); temp.intdata[NPC_TEMPLATEITEMNUM] = itmreadindex; for( i=0 ; i<NPC_TEMPLATEINTNUM ; i++ ){ temp.randomdata[i] = randomdata[i]; randomdata[i]=0; } memcpy( &NPC_template[NPC_template_readindex], &temp, sizeof( NPC_Template ) ); NPC_template_readindex++; if( NPC_template_readindex >= NPC_templatenum ){ print("Template Exceed configured buffer\n" ); print("configured template number is %d\n", NPC_templatenum); goto FCLOSERETURNFALSE; } } NPC_setDefaultNPCTemplate( &temp ); itmreadindex=0; start=OFF; }else{ fprint( "Unresolved '}' at %s:%d\n",filename,linenum); goto FCLOSERETURNFALSE; } break; default: { typedef struct tagNPC_Readtemplate { char* keyword; NPC_TYPECATEGORYATREADFILE type; int index; void* func; }NPC_Readtemplate; static NPC_Readtemplate NPC_readtemplate[NPC_TEMPLATECHARNUM+NPC_TEMPLATEINTNUM]= { {"templatename" , NPC_CHARENTRY, NPC_TEMPLATENAME,NULL}, {"name" , NPC_CHARENTRY, NPC_TEMPLATECHARNAME, NULL}, {"makeatnobody", NPC_INTENTRY, NPC_TEMPLATEMAKEATNOBODY, NULL}, {"makeatnosee" , NPC_INTENTRY, NPC_TEMPLATEMAKEATNOSEE, NULL}, {"graphicname", NPC_INTFUNC, NPC_TEMPLATEIMAGENUMBER, NPC_seekGraphicNumberFromString}, {"type", NPC_INTFUNC, NPC_TEMPLATETYPE, NPC_seekGraphicNumberFromString}, {"hp", NPC_INTFUNC, NPC_TEMPLATEMINHP, NPC_getRandomValue}, {"mp", NPC_INTFUNC, NPC_TEMPLATEMINMP, NPC_getRandomValue}, {"str", NPC_INTFUNC, NPC_TEMPLATEMINSTR, NPC_getRandomValue}, {"tough", NPC_INTFUNC, NPC_TEMPLATEMINTOUGH, NPC_getRandomValue}, {"fly", NPC_INTENTRY,NPC_TEMPLATEISFLYING,NULL}, {"itemnum", NPC_INTENTRY, NPC_TEMPLATEITEMNUM, NULL,}, {"functionset",NPC_INTFUNC, NPC_TEMPLATEFUNCTIONINDEX, NPC_searchFunctionSet,}, {"initfunc", NPC_CHARENTRY, NPC_TEMPLATEINITFUNC,NULL,}, {"walkprefunc", NPC_CHARENTRY, NPC_TEMPLATEWALKPREFUNC, NULL,}, {"walkpostfunc",NPC_CHARENTRY, NPC_TEMPLATEWALKPOSTFUNC, NULL,}, {"preoverfunc", NPC_CHARENTRY, NPC_TEMPLATEPREOVERFUNC, NULL,}, {"postoverfunc",NPC_CHARENTRY, NPC_TEMPLATEPOSTOVERFUNC, NULL,}, {"watchfunc", NPC_CHARENTRY, NPC_TEMPLATEWATCHFUNC, NULL,}, {"loopfunc", NPC_CHARENTRY, NPC_TEMPLATELOOPFUNC, NULL,}, {"talkedfunc",NPC_CHARENTRY,NPC_TEMPLATETALKEDFUNC,NULL,}, {"dyingfunc",NPC_CHARENTRY,NPC_TEMPLATEDYINGFUNC,NULL,}, {"preattackedfunc",NPC_CHARENTRY, NPC_TEMPLATEPREATTACKEDFUNC,NULL,}, {"postattackedfunc",NPC_CHARENTRY, NPC_TEMPLATEPOSTATTACKEDFUNC,NULL,}, {"offfunc",NPC_CHARENTRY,NPC_TEMPLATEOFFFUNC,NULL,}, {"lookedfunc",NPC_CHARENTRY,NPC_TEMPLATELOOKEDFUNC,NULL,}, {"itemputfunc",NPC_CHARENTRY,NPC_TEMPLATEITEMPUTFUNC, NULL,}, {"specialtalkedfunc",NPC_CHARENTRY, NPC_TEMPLATESPECIALTALKEDFUNC, NULL,}, {"windowtalkedfunc",NPC_CHARENTRY, NPC_TEMPLATEWINDOWTALKEDFUNC, NULL,}, {"loopfunctime",NPC_INTENTRY,NPC_TEMPLATELOOPFUNCTIME, NULL,}, }; char firstToken[256]; int ret,i; ret = getStringFromIndexWithDelim( line, "=", 1, firstToken, sizeof( firstToken ) ); if( ret == FALSE ){ print( "Find error at %s in line %d. Ignore\n", filename , linenum); break; } if( strcasecmp(firstToken,"itm") == 0 ){ char one[128]; char two[128]; char three[128]; int itemnumber=0,rate=0,amount=1; char secondToken[256]={""}; if( itmreadindex < temp.intdata[NPC_TEMPLATEITEMNUM] && temp.haveitem != NULL ) ; else continue; ret = getStringFromIndexWithDelim( line, "=", 2, secondToken, sizeof(secondToken ) ); if( ret == FALSE ){ print( "Find error at %s in line %d. Ignore\n", filename , linenum); secondToken[0] = '\0'; } ret = getStringFromIndexWithDelim( secondToken, "|", 1, one,sizeof( one ) ); if( ret != FALSE )itemnumber=atoi(one); ret = getStringFromIndexWithDelim( secondToken, "|", 2, two,sizeof(two)); if( ret != FALSE )rate=atoi(two); ret = getStringFromIndexWithDelim( secondToken, "|", 3, three,sizeof(three)); if( ret != FALSE )amount=atoi(three); if( rate > 0 && amount > 0 ){ if( itemnumber == -1 ){ temp.haveitem[itmreadindex].itemnumber = -1; temp.haveitem[itmreadindex].haverate = rate; temp.haveitem[itmreadindex].havenum = amount; itmreadindex++; }else if( ITEM_CHECKITEMTABLE( itemnumber ) == TRUE ){ temp.haveitem[itmreadindex].itemnumber=itemnumber; temp.haveitem[itmreadindex].haverate = rate; temp.haveitem[itmreadindex].havenum = amount; itmreadindex++; }else print("this item is not item %s:%d\n", filename , linenum); }else print("rate or amount is odd setting. rate=%d " "amount=%d %s:%d\n", rate,amount, filename, linenum); goto NEXT; }else{ for( i = 0 ; i < arraysizeof(NPC_readtemplate) ; i ++ ){ if( strcasecmp( NPC_readtemplate[i].keyword, firstToken ) == 0 ){ char secondToken[256]={""}; ret = getStringFromIndexWithDelim( line, "=", 2, secondToken, sizeof(secondToken) ); if( ret == FALSE ) print( "Find error at %s in line %d. Ignore\n" ,filename , linenum); switch( NPC_readtemplate[i].type ){ case NPC_INTENTRY: temp.intdata[NPC_readtemplate[i].index]=atoi(secondToken); if( NPC_readtemplate[i].index == NPC_TEMPLATEITEMNUM ) NPC_templateallocitemdata( &temp ); break; case NPC_CHARENTRY: strcpysafe( temp.chardata[NPC_readtemplate[i].index].string, sizeof(temp.chardata[NPC_readtemplate[i].index].string), secondToken); break; case NPC_INTFUNC: { int (*intfunction)(char*,int* ); intfunction = NPC_readtemplate[i].func; temp.intdata[NPC_readtemplate[i].index] = intfunction( secondToken, &randomdata[NPC_readtemplate[i].index]); break; } case NPC_CHARFUNC: { char* (*charfunction)(char* ); charfunction = NPC_readtemplate[i].func; strcpysafe( temp.chardata[NPC_readtemplate[i].index].string, sizeof(temp.chardata[NPC_readtemplate[i].index].string), charfunction(secondToken)); break; } default: break; } goto NEXT; } } } fprint("%s:%d There is no such entry: %s\n" , filename, linenum, firstToken ); NEXT: break; } break; } } fclose(f); return TRUE; FCLOSERETURNFALSE: fclose(f); return FALSE; }