📄 map.cpp
字号:
#include "map.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
struct map_data map;
unsigned char * gat_read(char *fname, int *size)
{
FILE *in = NULL;
unsigned char *buf=NULL,*buf2=NULL;
char lfname[256];
int declen;
strncpy(lfname,fname,255);
lfname[255] = 0;
in = fopen(lfname,"rb");
if(in!=NULL)
{
fseek(in,0,2); // SEEK_END
declen = ftell(in); // size
fseek(in,0,0); // SEEK_SET
buf2 = (unsigned char *)malloc(declen+1024);
if (buf2==NULL)
{
//printf("file read memory allocate error : declen\n");
goto errret;
}
fread(buf2,1,declen,in);
fclose(in); in = NULL;
}
else
{
//printf("%s not found\n", fname);
goto errret;
}
if (size!=NULL)
*size = declen;
return buf2;
errret:
if (buf!=NULL) free(buf);
if (buf2!=NULL) free(buf2);
if (in!=NULL) fclose(in);
return NULL;
}
static struct WaterDescr
{
char mapname[24];
int waterheight;
} *waterlist=NULL;
#define NO_WATER 1000000
int map_waterheight(char *mapname)
{
if(waterlist){
int i;
for(i=0;waterlist[i].mapname[0] && i < MAX_MAP_PER_SERVER;i++)
if(strcmp(waterlist[i].mapname,mapname)==0)
return waterlist[i].waterheight;
}
return NO_WATER;
}
int map_readmap(char *fn)
{
unsigned char *gat;
int s;
int x,y,xs,ys;
struct gat_1cell {float high[4]; int type;} *p;
int wh;
// read & convert fn
gat=gat_read(fn, NULL);
if(gat==NULL)
return 0;
xs=map.xs=*(int*)(gat+6);
ys=map.ys=*(int*)(gat+10);
map.gat=(unsigned char *)malloc(s=map.xs*map.ys);
if(map.gat==NULL){
printf("out of memory : map_readmap gat\n");
exit(1);
}
memset(&map.flag,0,sizeof(map.flag));
wh=map_waterheight(map.name);
for(y=0;y<ys;y++){
p=(struct gat_1cell*)(gat+y*xs*20+14);
for(x=0;x<xs;x++){
if(wh!=NO_WATER && p->type==0){
// 悈応敾掕 : Water place decision
map.gat[x+y*xs]=(p->high[0]>wh || p->high[1]>wh || p->high[2]>wh || p->high[3]>wh) ? 3 : 0;
} else {
map.gat[x+y*xs]=p->type;
}
p++;
}
}
free(gat);
return 1;
}
int map_readwater(char *watertxt)
{
char line[1024],w1[1024];
FILE *fp;
int n=0;
fp=fopen(watertxt,"r");
if(fp==NULL){
return 0;
}
if(waterlist==NULL)
waterlist=(struct WaterDescr *)calloc(MAX_MAP_PER_SERVER,sizeof(*waterlist));
while(fgets(line,1020,fp) && n < MAX_MAP_PER_SERVER){
int wh,count;
if(line[0] == '/' && line[1] == '/')
continue;
if((count=sscanf(line,"%s%d",w1,&wh)) < 1){
continue;
}
strcpy(waterlist[n].mapname,w1);
if(count >= 2)
waterlist[n].waterheight = wh;
else
waterlist[n].waterheight = 3;
n++;
}
fclose(fp);
return 1;
}
void map_finish()
{
if(map.gat)
free(map.gat);
if(waterlist)
free(waterlist);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -