📄 npc.c
字号:
int x,y;
do {
x=rand()%(map_data[m].xs-1);
y=rand()%(map_data[m].ys-1);
} while(map_data[m].gat[x+y*map_data[m].xs]==1 || map_data[m].gat[x+y*map_data[m].xs]==5);
map_data[m].npc[n]->u.mons.to_x=map_data[m].npc[n]->x=x;
map_data[m].npc[n]->u.mons.to_y=map_data[m].npc[n]->y=y;
return 0;
}
int mons_walk(int tid,unsigned int tick,int m,int n)
{
int x,y,dx,dy;
struct map_session_data sd;
int ret,i;
x=map_data[m].npc[n]->x;
y=map_data[m].npc[n]->y;
dx= map_data[m].npc[n]->u.mons.to_x - x;
dy= map_data[m].npc[n]->u.mons.to_y - y;
if(dx || dy){
// step
if(dx<0) dx=-1;
else if(dx>0) dx=1;
if(dy<0) dy=-1;
else if(dy>0) dy=1;
if((x+dx)/BLOCK_SIZE != x/BLOCK_SIZE || (y+dy)/BLOCK_SIZE != y/BLOCK_SIZE){
unsigned char buf[256];
int bx,by;
bx=x/BLOCK_SIZE;
by=y/BLOCK_SIZE;
WBUFW(buf,0)=0x80;
WBUFL(buf,2)=map_data[m].npc[n]->id;
WBUFB(buf,6)=0; // no effect?
if((x+dx)/BLOCK_SIZE != x/BLOCK_SIZE && bx-dx*AREA_SIZE>=0 && bx-dx*AREA_SIZE<map_data[m].bxs){ // x
for(i=-AREA_SIZE;i<=AREA_SIZE;i++){ // x
if(by+i < 0 || by+i >= map_data[m].bys)
continue;
mmo_map_sendblock(m,bx-dx*AREA_SIZE,by+i,buf,packet_len_table[0x80],0,0);
}
}
if((y+dy)/BLOCK_SIZE != y/BLOCK_SIZE && by-dy*AREA_SIZE>=0 && by-dy*AREA_SIZE<map_data[m].bys){ // y
for(i=-AREA_SIZE;i<=AREA_SIZE;i++){ // y
if(bx+i < 0 || bx+i >= map_data[m].bxs)
continue;
mmo_map_sendblock(m,bx+i,by-dy*AREA_SIZE,buf,packet_len_table[0x80],0,0);
}
}
del_block(&map_data[m].npc[n]->block);
add_block(&map_data[m].npc[n]->block,m,x+dx,y+dy);
memset(buf,0,256);
WBUFW(buf,0)=0x7b;
WBUFL(buf,2)=map_data[m].npc[n]->id;
WBUFW(buf,6)=map_data[m].npc[n]->u.mons.speed;
WBUFW(buf,14)=map_data[m].npc[n]->class;
WBUFL(buf,22)=tick;
set_2pos(WBUFP(buf,50),x,y,map_data[m].npc[n]->u.mons.to_x,map_data[m].npc[n]->u.mons.to_y);
WBUFB(buf,55)=0;
WBUFB(buf,56)=5;
WBUFB(buf,57)=5;
bx=(x+dx)/BLOCK_SIZE;
by=(y+dy)/BLOCK_SIZE;
if((x+dx)/BLOCK_SIZE != x/BLOCK_SIZE && bx+dx*AREA_SIZE>=0 && bx+dx*AREA_SIZE<map_data[m].bxs){ // x
for(i=-AREA_SIZE;i<=AREA_SIZE;i++){ // x
if(by+i < 0 || by+i >= map_data[m].bys)
continue;
mmo_map_sendblock(m,bx+dx*AREA_SIZE,by+i,buf,packet_len_table[0x7b],0,0);
}
}
if((y+dy)/BLOCK_SIZE != y/BLOCK_SIZE && by+dy*AREA_SIZE>=0 && by+dy*AREA_SIZE<map_data[m].bys){ // y
for(i=-AREA_SIZE;i<=AREA_SIZE;i++){ // y
if(bx+i < 0 || bx+i >= map_data[m].bxs)
continue;
mmo_map_sendblock(m,bx+i,by+dy*AREA_SIZE,buf,packet_len_table[0x7b],0,0);
}
}
}
map_data[m].npc[n]->x+=dx;
map_data[m].npc[n]->y+=dy;
dx= map_data[m].npc[n]->u.mons.to_x - map_data[m].npc[n]->x;
dy= map_data[m].npc[n]->u.mons.to_y - map_data[m].npc[n]->y;
if(dx==0 && dy==0){
map_data[m].npc[n]->u.mons.timer=
add_timer(tick+rand()%5000+5000,mons_walk,m,n);
} else {
map_data[m].npc[n]->u.mons.timer=
add_timer(tick+map_data[m].npc[n]->u.mons.speed*((dx&&dy)?14:10)/10,mons_walk,m,n);
}
return 0;
}
// search target
for(i=0;i<5;i++){
dx=rand()%(12*2+1)-12;
dy=rand()%(12*2+1)-12;
ret=search_path(&sd,m,map_data[m].npc[n]->x,map_data[m].npc[n]->y,map_data[m].npc[n]->x+dx,map_data[m].npc[n]->y+dy,1);
if(ret==0)
break;
}
if(ret==0){
unsigned char buf[256];
memset(buf,0,256);
WBUFW(buf,0)=0x7b;
WBUFL(buf,2)=map_data[m].npc[n]->id;
WBUFW(buf,6)=map_data[m].npc[n]->u.mons.speed;
WBUFW(buf,14)=map_data[m].npc[n]->class;
WBUFL(buf,22)=tick;
set_2pos(WBUFP(buf,50),map_data[m].npc[n]->x,map_data[m].npc[n]->y,map_data[m].npc[n]->x+dx,map_data[m].npc[n]->y+dy);
WBUFB(buf,55)=0;
WBUFB(buf,56)=5;
WBUFB(buf,57)=5;
mmo_map_sendarea_mxy(m,map_data[m].npc[n]->x,map_data[m].npc[n]->y,buf,packet_len_table[0x7b]);
map_data[m].npc[n]->u.mons.to_x=map_data[m].npc[n]->x+dx;
map_data[m].npc[n]->u.mons.to_y=map_data[m].npc[n]->y+dy;
map_data[m].npc[n]->u.mons.timer=
add_timer(tick+map_data[m].npc[n]->u.mons.speed/2*((dx&&dy)?14:10)/10,mons_walk,m,n);
return 0;
}
map_data[m].npc[n]->u.mons.timer=
add_timer(tick+rand()%5000+5000,mons_walk,m,n);
return 0;
}
int read_npcdata(void)
{
char line_l[1024];
FILE *fl;
// parse npc.txt
fl=fopen("monster.txt","r");
if(fl==NULL)
return -1;
while(fgets(line_l,1020,fl)){
char l1[1024],l2[1024],l3[1024],l4[1024];
int move;
int base_xp,job_xp,group;
if(sscanf(line_l,"%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]",l1,l2,l3,l4)!=4 || sscanf(l1,"%d,%d,%d,%d",&move,&base_xp,&job_xp,&group)!=4)
continue;
if(strcmp(l2,"monster_info")==0){
int class,num,hp;
sscanf(l4,"%d,%d,%d",&class,&hp);
m_data[class].base_exp = base_xp;
m_data[class].job_exp = job_xp;
m_data[class].group = group;
m_data[class].health_points = hp;
m_data[class].move = move;
if (m_data[class].health_points == 0) {m_data[class].health_points = 1000; }
printf("monster_info %s read done\n",l3);
}
}
fclose(fl);
int i;
char line[1024];
FILE *fp;
// parse npc.txt
fp=fopen(npc_txt,"r");
if(fp==NULL)
return -1;
while(fgets(line,1020,fp)){
char mapname[256],w1[1024],w2[1024],w3[1024],w4[1024];
int x,y,dir;
if(sscanf(line,"%[^\t]\t%[^\t]\t%[^\t]\t%[^\t]",w1,w2,w3,w4)!=4 || sscanf(w1,"%[^,],%d,%d,%d",mapname,&x,&y,&dir)!=4)
continue;
for(i=0;map[i][0];i++)
if(strcmp(map[i],mapname)==0)
break;
if(map[i][0]==0)
continue;
if(strcmp(w2,"warp")==0){
int j,k,xs,ys,to_x,to_y;
map_data[i].npc[map_data[i].npc_num]=malloc(sizeof(struct npc_data));
map_data[i].npc[map_data[i].npc_num]->m=i;
map_data[i].npc[map_data[i].npc_num]->x=x;
map_data[i].npc[map_data[i].npc_num]->y=y;
memcpy(map_data[i].npc[map_data[i].npc_num]->name,w3,24);
map_data[i].npc[map_data[i].npc_num]->class=WARP_CLASS;
map_data[i].npc[map_data[i].npc_num]->id=npc_id++;
sscanf(w4,"%d,%d,%[^,],%d,%d",
&xs,&ys,
map_data[i].npc[map_data[i].npc_num]->u.warp.name,
&to_x,&to_y);
map_data[i].npc[map_data[i].npc_num]->u.warp.x=to_x;
map_data[i].npc[map_data[i].npc_num]->u.warp.y=to_y;
map_data[i].npc[map_data[i].npc_num]->u.warp.xs=xs;
map_data[i].npc[map_data[i].npc_num]->u.warp.ys=ys;
for(j=0;j<ys;j++){
unsigned char *p;
p = &map_data[i].gat[x-xs/2 + (y-ys/2+j)* map_data[i].xs ];
for(k=0;k<xs;k++,p++){
if(*p==1 || *p==5)
continue;
*p|=0x80;
}
}
printf("npc %s %d read done\n",map[i],map_data[i].npc[map_data[i].npc_num]->id);
map_data[i].npc[map_data[i].npc_num]->block.subtype=WARP;
add_block_npc(i,map_data[i].npc_num);
map_data[i].npc_num++;
} else if(strcmp(w2,"monster")==0){
int j,class,num;
sscanf(w4,"%d,%d",&class,&num);
for(j=0;j<num;j++){
map_data[i].npc[map_data[i].npc_num]=malloc(sizeof(struct npc_data));
set_monster_random_point(i,map_data[i].npc_num);
map_data[i].npc[map_data[i].npc_num]->class=class;
monster_nums[npc_id] = map_data[i].npc_num;
monster_i[npc_id] = i;
map_data[i].npc[map_data[i].npc_num]->id=npc_id++;
memcpy(map_data[i].npc[map_data[i].npc_num]->name,w3,24);
if (m_data[class].move == 1)
{
map_data[i].npc[map_data[i].npc_num]->u.mons.timer=
add_timer(gettick()+rand()%5000+5000,mons_walk,i,map_data[i].npc_num);
}
map_data[i].npc[map_data[i].npc_num]->u.mons.speed=200;
map_data[i].npc[map_data[i].npc_num]->block.subtype=MONS;
add_block_npc(i,map_data[i].npc_num);
map_data[i].npc_num++;
}
printf("monster %s read done\n",w3);
} else if(strcmp(w2,"shop")==0){
char *p;
int max=32,pos=0;
struct npc_item_list *item_list;
map_data[i].npc[map_data[i].npc_num]=malloc(sizeof(struct npc_data));
map_data[i].npc[map_data[i].npc_num]->m=i;
map_data[i].npc[map_data[i].npc_num]->x=x;
map_data[i].npc[map_data[i].npc_num]->y=y;
map_data[i].npc[map_data[i].npc_num]->dir=dir;
memcpy(map_data[i].npc[map_data[i].npc_num]->name,w3,24);
map_data[i].npc[map_data[i].npc_num]->class=atoi(w4);
map_data[i].npc[map_data[i].npc_num]->id=npc_id++;
map_data[i].npc[map_data[i].npc_num]->block.subtype=SHOP;
item_list=malloc(sizeof(item_list[0])*(max+1));
p=strchr(w4,',');
while(p && pos<max){
int nameid,value;
p++;
if(sscanf(p,"%d:%d",&nameid,&value)!=2)
break;
item_list[pos].nameid=nameid;
item_list[pos].value=value;
pos++;
p=strchr(p,',');
}
item_list=realloc(item_list,sizeof(item_list[0])*(pos+1));
item_list[pos].nameid=0;
map_data[i].npc[map_data[i].npc_num]->u.shop_item=item_list;
add_block_npc(i,map_data[i].npc_num);
printf("shop %s read done\n",w3);
map_data[i].npc_num++;
} else if(strcmp(w2,"script")==0){
char *buf;
int size=65536,j;
map_data[i].npc[map_data[i].npc_num]=malloc(sizeof(struct npc_data));
map_data[i].npc[map_data[i].npc_num]->m=i;
map_data[i].npc[map_data[i].npc_num]->x=x;
map_data[i].npc[map_data[i].npc_num]->y=y;
map_data[i].npc[map_data[i].npc_num]->dir=dir;
memcpy(map_data[i].npc[map_data[i].npc_num]->name,w3,24);
map_data[i].npc[map_data[i].npc_num]->class=atoi(w4);
map_data[i].npc[map_data[i].npc_num]->id=npc_id++;
map_data[i].npc[map_data[i].npc_num]->block.subtype=SCRIPT;
buf=malloc(size);
if(strchr(w4,'{'))
strcpy(buf,strchr(w4,'{'));
else
buf[0]=0;
while(1){
for(j=strlen(buf)-1;j>=0 && isspace(buf[j]);j--);
if(j>=0 && buf[j]=='}')
break;
fgets(line,1020,fp);
if(feof(fp))
break;
if(strlen(buf)+strlen(line)>=size){
size+=65536;
buf=realloc(buf,size);
}
if(strchr(buf,'{')==NULL){
if(strchr(line,'{'))
strcpy(buf,strchr(line,'{'));
} else
strcat(buf,line);
}
map_data[i].npc[map_data[i].npc_num]->u.script=parse_script(buf);
add_block_npc(i,map_data[i].npc_num);
printf("script %s read done\n",w3);
map_data[i].npc_num++;
}
}
fclose(fp);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -