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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

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