sunder2.1/clan/
sunder2.1/class/
sunder2.1/class/bak/
sunder2.1/doc/ideas/
sunder2.1/gods/
sunder2.1/log/
sunder2.1/msgbase/
sunder2.1/src/o/
sunder2.1/time/
/*
 *  The unique portions of SunderMud code as well as the integration efforts
 *  for code from other sources is based on the efforts of:
 *
 *  Lotherius (elfren@aros.net)
 *
 *  This code can only be used under the terms of the DikuMud, Merc,
 *  and ROM licenses. The same requirements apply to the changes that
 *  have been made.
 *
 * All other copyrights remain in place and in force.
*/


/* this procedure controls all mobile spell casting */

#include "everything.h"
#include "magic.h"

/* locals procedures */
CHAR_DATA          *target_harm args ( ( CHAR_DATA * ch ) );
CHAR_DATA          *target_aid args ( ( CHAR_DATA * ch ) );
bool is_friend     args ( ( CHAR_DATA * ch, CHAR_DATA * victim ) );

/* returns a target suitable for mayhem */
CHAR_DATA          *target_harm ( CHAR_DATA * ch )
{
    CHAR_DATA          *victim, *vch;
    int                 count;

    if ( ch == NULL || ch->in_room == NULL )
	return NULL;

    count = 0;
    victim = NULL;

    for ( vch = ch->in_room->people; vch != NULL; vch = vch->next_in_room )
    {
	if ( vch->fighting == ch && can_see ( ch, vch ) )
	{
	    if number_range ( 0, count ) == 0 ) victim = vch;
	    count++;
	}
    }

    return victim;
}


/* finds a friend to help */
CHAR_DATA *         target_aid ( CHAR_DATA * ch )
{
    CHAR_DATA *         friend, *fch;
    int                 count;

    if ( ch == NULL || ch->in_room == NULL )
	return NULL;

    count = 0;
    victim = NULL;

    for ( fch = ch->in_room->people; fch != NULL;
	  fch = fch->next_in_room )
    {
	if ( is_friend ( ch, fch ) && can_see ( ch, vch ) )
	{
	    if ( number_range ( 0, count ) == 0 )
		friend = fch;
	    count++;
	}
    }

    return friend;
}

/* checks to see if a mobile is friendly with a char or NPC */
bool                is_friend ( CHAR_DATA * ch,
				CHAR_DATA * victim )
{
    if ( ch->fighting != NULL &&
	 is_same_group ( victim, ch->fighting ) )
	return FALSE;

    if ( is_same_group ( ch, victim ) )
	return TRUE;

    /* check for assist_player */
    if ( IS_NPC ( ch ) && IS_SET ( ch->off_flags, ASSIST_PLAYERS )
	 && !IS_NPC ( victim ) )
	return TRUE;

    if ( !IS_NPC ( victim ) || IS_NPC ( ch ) )
	return FALSE;

    /* all cases below are NPC/NPC */

    /* charmees are not friends */
    if ( IS_AFFECTED ( ch, AFF_CHARM ) ||
	 IS_AFFECTED ( victim, AFF_CHARM ) )
	return FALSE;

    if ( IS_SET ( ch->off_flags, ASSIST_ALL ) )
	return TRUE;

    if ( IS_SET ( ch->off_flags, ASSIST_VNUM )
	 && ch->pIndexData == victim->pIndexData )
	return TRUE;


    if ( IS_SET ( ch->off_flags, ASSIST_RACE ) &&
	 ch->race == victim->race )
	return TRUE;

if ( IS_SET ( ch->off_flags, ASSIST_ALIGN )
	 && ( ( IS_GOOD ( ch ) && IS_GOOD ( victim ) )
		  || ( IS_EVIL ( ch ) && IS_EVIL ( victim ) )
		  || ( IS_NEUTRAL ( ch ) &&
			   IS_NEUTRAL ( victim ) ) ) return
	 TRUE; return FALSE;}