📄 readmap.c
字号:
#include "version.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <dirent.h>#include <errno.h>#include <unistd.h>#include <netinet/in.h>#include <sys/stat.h>#include "common.h"#include "object.h"#include "readmap.h"#include "buf.h"#include "util.h"#include "char.h"#include "lssproto_serv.h"#include "configfile.h"#define MAP_MAGIC "LS2MAP"static void MAP_makeDefaultImageData( MAP_ImageData* one ){ one->data[MAP_WALKABLE] = TRUE; one->data[MAP_HAVEHEIGHT] = FALSE; one->data[MAP_DEFENCE] = 0; one->data[MAP_INTODAMAGE] = 0; one->data[MAP_OUTOFDAMAGE] = 0; one->data[MAP_SETED_BATTLEMAP] = 0; one->data[MAP_BATTLEMAP] = 0; one->data[MAP_BATTLEMAP2] = 0; one->data[MAP_BATTLEMAP3] = 0; one->data[MAP_INTODARKNESS] = 0; one->data[MAP_INTOCONFUSION] = 0; one->data[MAP_OUTOFPOISON] = 0; one->data[MAP_OUTOFPARALYSIS] = 0; one->data[MAP_OUTOFSILENCE] = 0; one->data[MAP_OUTOFSTONE] = 0; one->data[MAP_OUTOFDARKNESS] = 0; one->data[MAP_OUTOFCONFUSION] = 0;}static int MAP_flgSet( char* string ){ int tmp; tmp = atoi(string); if( tmp == 0 ) return 0; return 1;}typedef enum{ MAP_INTENTRY=0, MAP_CHARENTRY=1, MAP_INTFUNC=2, MAP_CHARFUNC=3,}MAP_DATATYPECATEGORY;static struct MAP_itemconfentry{ int type; int index; void* func;}MAP_confentries[MAP_DATAINT_NUM+MAP_DATACHAR_NUM]={ {MAP_INTENTRY, MAP_WALKABLE, MAP_flgSet}, {MAP_INTENTRY, MAP_HAVEHEIGHT, MAP_flgSet}, {MAP_INTENTRY, MAP_DEFENCE, NULL}, {MAP_INTENTRY, MAP_INTODAMAGE, NULL}, {MAP_INTENTRY, MAP_OUTOFDAMAGE, NULL}, {MAP_INTENTRY, MAP_SETED_BATTLEMAP, NULL}, {MAP_INTENTRY, MAP_BATTLEMAP, NULL}, {MAP_INTENTRY, MAP_BATTLEMAP2, NULL}, {MAP_INTENTRY, MAP_BATTLEMAP3, NULL}, {MAP_INTENTRY, MAP_INTODARKNESS, NULL}, {MAP_INTENTRY, MAP_INTOCONFUSION, NULL}, {MAP_INTENTRY, MAP_OUTOFPOISON, NULL}, {MAP_INTENTRY, MAP_OUTOFPARALYSIS, NULL}, {MAP_INTENTRY, MAP_OUTOFSILENCE, NULL}, {MAP_INTENTRY, MAP_OUTOFSTONE, NULL}, {MAP_INTENTRY, MAP_OUTOFDARKNESS, NULL}, {MAP_INTENTRY, MAP_OUTOFCONFUSION, NULL},};static MAP_ImageData* MAP_imagedata;static unsigned int MAP_imagedatanum;static int MAP_imgfilt[65535];BOOL 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]; BOOL 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 ++ ){ BOOL ret; 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;}#if 1BOOL MAP_readBattleMapConfFile( char* filename ){ FILE* file; char line[512]; int i, j; int linenum=0; int BattleMapNo[3], iRet, iWork, iPre[3],iFirst, iLast; file = fopen( filename , "r" ); if( file == NULL ){ fprint( "无法打开 %s\n", filename ); return FALSE; } BattleMapNo[0] = 0; BattleMapNo[1] = 0; BattleMapNo[2] = 0; while( fgets( line , sizeof( line ) , file ) ){ char *p; linenum++; pohcd( line, " \t" ); dchop(line, " \t"); if( line[0] == '#' )continue; if( line[0] == '\n' )continue; chomp( line ); if( line[0] == '$' ){ iRet = sscanf( line+1, "%d %d %d", &iPre[0], &iPre[1], &iPre[2] ); BattleMapNo[0] = BattleMapNo[1] = BattleMapNo[2] = iPre[0]; if( iRet < 1 ){ print( "!!!!!错误 无法读取战斗地图(%s)( line %d )\n", filename, linenum ); } for( i = 0; i < iRet; i ++ ){ BattleMapNo[i] = iPre[i]; if( BattleMapNo[i] < 0 ){ print( "!!!!!错误 战斗地图号码为负值 (%s)( line %d )\n", filename, linenum ); } } continue; } if( sscanf( line, "%d", &iWork ) != 1 ){ continue; } if( iWork < 0 ){ print( "!!!!!错误 金钱号码为负值 (%s)( line %d )\n", filename, linenum ); continue; } iFirst = iWork; p = strstr( line, "to" ); if( p != NULL ){ if( sscanf( p+2, "%d", &iWork ) != 1 ){ iLast = iFirst; }else{ iLast = iWork; } if( iWork < 0 ){ print( "!!!!!错误 金钱号码为负值 (%s)( line %d )\n", filename, linenum ); continue; } }else{ iLast = iFirst; } for( i = iFirst; i <= iLast; i ++ ){ if( MAP_getImageInt( i, MAP_SETED_BATTLEMAP ) > 0 ){ print( "!!!!!错误 相同金钱重复设定了两次(%s)( line %d )(%d)(%d & %d)\n", filename, linenum, i, MAP_getImageInt( i, MAP_BATTLEMAP ), BattleMapNo[0] ); } // 飓 寞恳仄中井" if( IsValidImagenumber( i ) == FALSE ){ continue; } MAP_setImageInt( i, MAP_BATTLEMAP, BattleMapNo[0] ); MAP_setImageInt( i, MAP_BATTLEMAP2, BattleMapNo[1] ); MAP_setImageInt( i, MAP_BATTLEMAP3, BattleMapNo[2] ); MAP_setImageInt( i, MAP_SETED_BATTLEMAP, 1 ); } } fclose( file ); for( j = 0; j < arraysizeof( MAP_imgfilt ); j ++ ){ if( MAP_imgfilt[j] == -1 )continue; if( MAP_getImageInt( j, MAP_SETED_BATTLEMAP ) == 0 ){ print( "!!!!!错误 金钱(%d)尚未设定 (%s)\n", j, filename ); } } return TRUE;}#endifBOOL IsValidImagenumber( int imagenumber ){ if( imagenumber < 0 || imagenumber >= arraysizeof( MAP_imgfilt ) ) return FALSE; if( MAP_imgfilt[imagenumber] == -1 ) return FALSE; return TRUE;}int MAP_getImageInt( int imagenumber, int element ){ if( imagenumber < 0 || imagenumber >= arraysizeof( MAP_imgfilt )) return FALSE; if( MAP_imgfilt[imagenumber] == -1 )return FALSE; return MAP_imagedata[MAP_imgfilt[imagenumber]].data[element];}BOOL MAP_setImageInt( int imagenumber, int element, int value ){ if( imagenumber < 0 || imagenumber >= arraysizeof( MAP_imgfilt )) return FALSE; if( MAP_imgfilt[imagenumber] == -1 )return FALSE; MAP_imagedata[MAP_imgfilt[imagenumber]].data[element] = value; return TRUE;}static MAP_Map* MAP_map;static unsigned int MAP_mapnum;static unsigned int MAP_mapnum_index;static int* MAP_idjumptbl;static int MAP_idtblsize;BOOL MAP_initMapArray( int num ){ MAP_mapnum = num ; MAP_map = allocateMemory( sizeof( MAP_Map ) * MAP_mapnum ); if( MAP_map == NULL )return FALSE; MAP_mapnum_index = 0; return TRUE;}void MAP_endMapArray( void ){ int i; for( i = 0 ; i < MAP_mapnum ; i ++ ){ if( MAP_map[i].tile != NULL ) freeMemory( MAP_map[i].tile ); if( MAP_map[i].obj != NULL) freeMemory( MAP_map[i].obj ); } MAP_mapnum = 0; MAP_mapnum_index = 0; freeMemory( MAP_map ); MAP_map = NULL;}static BOOL MAP_IsMapFile( char* filename ){ FILE* f; char buf[16]; int ret; f = fopen(filename, "r"); if( f == NULL ){ errorprint; return FALSE; } ret = fread( buf , sizeof( char ) , 6 , f); if( ret != 6 ) goto FCLOSERETURNFALSE; buf[ret] = '\0'; if( strcmp( buf , MAP_MAGIC ) != 0 ) goto FCLOSERETURNFALSE; else goto FCLOSERETURNTRUE;FCLOSERETURNFALSE: fclose(f); return FALSE;FCLOSERETURNTRUE: fclose(f); return TRUE;}static BOOL MAP_readMapOne( char* filename ){ FILE* f; /* 白央奶伙 */ char buf[16]; /* 穴斥永弁瓜件田□ 心迕 */ short data[1024]; /* 扑亦□玄 心迕田永白央 */ int ret; /* 忒曰袄熬仃潸曰迕 */ int i; /* 伙□皿 醒 */ int mapindex; int id=0,xsiz=0,ysiz=0; /* 穴永皿犯□正及域凛伞 桦赭 */ short *tile=NULL; short *obj=NULL; MAP_Objlink** olink=NULL; char showstring[32]; /* 穴永皿犯□正及域凛伞 桦赭 */ struct stat filestat; /* 民尼永弁迕卞银丹 */ BOOL invaliddata=FALSE; if( MAP_mapnum_index >= MAP_mapnum ){ fprint( "这里没有足够空间装载地图数组.\n" ); return FALSE; } mapindex=MAP_mapnum_index; f = fopen(filename, "r"); if( f == NULL ){ errorprint; return FALSE; } if( fstat( fileno(f) , &filestat ) != 0 ){ errorprint; goto FCLOSERETURNFALSE; } ret = fread( buf , sizeof( char ) , 6 , f); if( ret != 6 ){ errorprint; goto FCLOSERETURNFALSE; } buf[ret] = '\0'; if( strcmp( buf , MAP_MAGIC ) != 0 ) goto FCLOSERETURNFALSE; ret = fread( data , sizeof( short ) , 1 , f); if( ret != 1 ){ errorprint; goto FCLOSERETURNFALSE; } id = ntohs( data[0] ); ret = fread( showstring , sizeof(char), arraysizeof(showstring), f); if( ret != arraysizeof(showstring) ){ errorprint; goto FCLOSERETURNFALSE; } showstring[arraysizeof(showstring)-1] = '\0'; ret = fread( data, sizeof( short ) , 1 , f); if( ret != 1 ){ errorprint; goto FCLOSERETURNFALSE; } xsiz = ntohs( data[0] ); ret = fread( data, sizeof( short ) , 1 , f); if( ret != 1 ){ errorprint; goto FCLOSERETURNFALSE; } ysiz = ntohs( data[0] ); tile = allocateMemory( sizeof( short ) * xsiz * ysiz ); if( tile == NULL ){ fprint( "无法分配内存给地图名称:%s xsiz:%d ysiz:%d\n", filename, xsiz, ysiz); goto FREEOBJHP; } obj = allocateMemory( sizeof( short ) * xsiz * ysiz ); if( obj == NULL ){ fprint( "无法分配内存给对象\n"); goto FREETILE; } olink = allocateMemory( sizeof(MAP_Objlink*) * xsiz * ysiz ); if( olink == NULL ){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -