Function help

Posted by MattJ820 on Mon 13 Apr 2015 01:41 AM — 8 posts, 30,812 views.

#0
Hello guys,

I'm trying to write my first function and I'm getting some strange results.

if I type "bountyscan" with no argument, I'm getting a "They aren't here" response rather than the Usage bountyscan <target>

If I bounty scan an NPC, I'm getting the Indetification No match response rather than the you can't scan NPC response.

If I try to bounty scan myself or another player, I get an emergency copy over that prevents the crash so I don' have a core to work with. This is my first function that I pieced together after a view C tutorials and reviewing other functions within bounty.c

Can someone help me figure out what I'm doing wrong?

/* Allows bounty hunters to hunt players by identifying them with a scanner to see if they'e on the bounty list
for situations where they have not been introduced. Object may be required to be worn later..not sure. */
void do_bountyscan ( CHAR_DATA *ch, char *argument)
{
char arg[MAX_STRING_LENGTH];
BOUNTY_DATA *bounty;
CHAR_DATA *victim;

bounty = get_disintegration( victim->name );

if ( IS_NPC(victim))
{
send_to_char( "You cannot bountyscan an NPC\n\r", ch );
return;
}

if ((victim = get_char_room(ch, argument)) == NULL)
{
send_to_char("They aren't here.\n\r", ch);
return;
}

if (argument[0] == '\0' )
{
send_to_char( "Usage: bountyscan <target>\n\r", ch );
return;
}

if ( ch == victim )
{
send_to_char( "Why scan yourself, would you kill yourself for the money?\n\r", ch );
return;
}

if ( bounty != NULL )
{
ch_printf( ch, "Identification: Match! %s has a bounty worth %ld credits.\n\r", victim->name, bounty->amount );
return;
}

if (bounty == NULL)
{
send_to_char( "Identification: No Match!\n\r", ch );
return;
}
}
Amended on Mon 13 Apr 2015 03:12 AM by MattJ820
USA #1
Purely for ease of reading ...
/* Allows bounty hunters to hunt players by identifying them with a scanner to see if they'e on the bounty list
for situations where they have not been introduced. Object may be required to be worn later..not sure. */
void do_bountyscan ( CHAR_DATA *ch, char *argument)
{
char arg[MAX_STRING_LENGTH];
BOUNTY_DATA *bounty;
CHAR_DATA *victim;

bounty = get_disintegration( victim->name );

if ( IS_NPC(victim))
	{
	send_to_char( "You cannot bountyscan an NPC\n\r", ch );
	return;
	}

if ((victim = get_char_room(ch, argument)) == NULL)
	{
	send_to_char("They aren't here.\n\r", ch);
	return;
	}

if (argument[0] == '\0' )
	{
	send_to_char( "Usage: bountyscan <target>\n\r", ch );
	return;
	}

if ( ch == victim )
	{
	send_to_char( "Why scan yourself, would you kill yourself for the money?\n\r", ch );
	return;
	}

if ( bounty != NULL )
	{
	ch_printf( ch, "Identification: Match! %s has a bounty worth %ld credits.\n\r", victim->name, bounty->amount );
	return;
	}

if (bounty == NULL)
	{
	send_to_char( "Identification: No Match!\n\r", ch );
	return;
	}
}
USA #2
Ok, now that we can read it more easily, let's see if we can find the problem .....

At first glance, I don't see anything obviously wrong in the code, though that doesn't mean much if you missed a declaration. The hotboot you're experiencing would seem to indicate a missed declaration somewhere and while you aren't getting a core dump, you could try running in gdb to get more info on the background processes prior to the hotboot.

As for your scan without an arg returning the "wrong" message, it's a perfectly valid response against a null argument. Take a look at fight.c or magic.c or skills.c and compare the code for checking targets in the room against what you did here.
Amended on Mon 13 Apr 2015 03:55 AM by Meerclar
Australia Forum Administrator #3
I think it's the order you are doing it. Check for no argument, before you look up the person in the room.
#4
Changing the order worked. I don't really understand why the order matters, but I guess it does. Proof is in the pudding. Do you have any insight into why so I can understand? I would think the computer would go through all the ifchecks one after another and deliver the appropriate action regardless of the order.

I did it in this order

no argument

victim not in room

victim = self

victim = npc

no bounty

bounty
Australia Forum Administrator #5
Well, you are calling "get_char_room" with an empty argument. It is probably written to respond "they aren't here" in that situation. Otherwise what else could it do? It returns the "victim" variable, so if there is no victim you would then need to test that to be NULL or not.
Australia Forum Administrator #6
Your original code had this, for example:


CHAR_DATA *victim;

bounty = get_disintegration( victim->name );

if ( IS_NPC(victim))
	{
	send_to_char( "You cannot bountyscan an NPC\n\r", ch );
	return;
	}



At that point "victim" does not exist, because you haven't found who the victim is, and thus you are lucky it didn't crash when you tried to get the victim name.
#7
That makes sense, thank you.