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

akka

New member
Messages
8
Points
0
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? :/
 
Last edited by a moderator:
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.
Code:
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:

Code:
*isnear++;
So you should do something like:
Code:
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.
 
Thanks I really appreciate your help. I made a read up on pointers and got it to work! 

 
  • Upvote
Reactions: pan
Back
Top