Jump to content
  • 0
akka

Trying to figure out if there is a player near a mob.

Question

Hi, i've been trying to figure out how to check if there is a player near a mob. Here is the code im at right now:

 

int pc_isnear_sub(struct block_list *bl, va_list args){ int* isnear = va_arg(args, int*); struct map_session_data *sd = (struct map_session_data *)bl; if (sd->sc.data[SC_TRICKDEAD] ||        sd->sc.data[SC_HIDING] ||        sd->sc.data[SC_CLOAKING] ||        pc_isinvisible(sd) ||        sd->vd.class_ == INVISIBLE_CLASS ||        pc_isdead(sd))    {        return 0;    }    else{        *isnear++;    }    return 1;}int pc_isnear_mob(struct mob_data *md) {    int *isnear = 0;    //map->foreachinarea(pc_isnear_sub, md->bl.m, md->bl.x - AREA_SIZE, md->bl.y - AREA_SIZE, md->bl.x + AREA_SIZE, md->bl.y + AREA_SIZE, BL_PC,isnear, isnear);    map->foreachinrange((pc_isnear_sub), &md->bl, AREA_SIZE, BL_PC, isnear);    if (isnear > 0){        return true;    }    return false;}

But it seems like pc_isnear_sub isn't even being called, can anyone tell me what im doing wrong? :/

Edited by akka

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Just by doing a quick look there's something strange going on, why are you using a pointer?

int *isnear = 0;
It looks as in pc_isnear_sub you are just using this pointer to point things that are in stack, so when this function returns it will point to something that can't really be accessed and will probably raise a SEGFAULT.
if (isnear > 0){
This line is just checking if there's an address set to '*isnear', not if the dereferenced value is bigger than 0.

Also you are incrementing the pointer here not the dereferenced value:

*isnear++;
So you should do something like:
int pc_isnear_sub(struct block_list *bl, va_list args){	int *isnear;	struct map_session_data *sd;	isnear = va_arg(args, int*);	sd = (struct map_session_data *)bl;	if( isnear == NULL || sd == NULL )		return 0;	if (sd->sc.data[SC_TRICKDEAD] ||		sd->sc.data[SC_HIDING] ||		sd->sc.data[SC_CLOAKING] ||		pc_isinvisible(sd) ||		sd->vd.class_ == INVISIBLE_CLASS ||		pc_isdead(sd)		)	{		return 0;	}	(*isnear)++;	return 1;}bool pc_isnear_mob( struct mob_data *md ){	int isnear;	isnear = 0;	map->foreachinrange((pc_isnear_sub), &md->bl, AREA_SIZE, BL_PC, &isnear);	return (isnear > 0)?true:false;}
Regards.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.