area/
build/testing/
log/
player/
player/backup/
/***************************************************************************
 *  Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer,        *
 *  Michael Seifert, Hans Henrik St{rfeldt, Tom Madsen, and Katja Nyboe.   *
 *                                                                         *
 *  Merc Diku Mud improvments copyright (C) 1992, 1993 by Michael          *
 *  Chastain, Michael Quan, and Mitchell Tse.                              *
 *                                                                         *
 *  In order to use any part of this Merc Diku Mud, you must comply with   *
 *  both the original Diku license in 'license.doc' as well the Merc       *
 *  license in 'license.txt'.  In particular, you may not remove either of *
 *  these copyright notices.                                               *
 *                                                                         *
 *  Much time and thought has gone into this software and you are          *
 *  benefitting.  We hope that you share your changes too.  What goes      *
 *  around, comes around.                                                  *
 ***************************************************************************/
#include <glib.h>

#if defined(macintosh)
#include <types.h>
#else
#include <sys/types.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <merc.h>
#include <tables.h>
#include <interp.h>
#include <fight.h>
extern void	set_fighting	args( ( CHAR_DATA *ch, CHAR_DATA *victim ) );

/*
 * Control the fights going on.
 * Called periodically by update_handler.
 */
void violence_update( void )
{
	CHAR_DATA *ch;
	CHAR_DATA *ch_next;
	CHAR_DATA *victim;
	CHAR_DATA *rch;
	CHAR_DATA *rch_next;
	CHAR_DATA *mount;
//	OBJ_DATA *to_obj;
	
	for ( ch = char_list; ch != NULL; ch = ch_next )
	{
		ch_next	= ch->next;
		
		if ( ( victim = ch->fighting ) == NULL || ch->in_room == NULL )
			continue;
		
		if ( ch->in_room != NULL && IS_RAFFECTED(ch->in_room,ROOM_AFF_REINA))
		{
			stop_fighting(ch,TRUE);
			continue;
		}

		if ( check_safe_imm(ch) )
		{
			stop_fighting(ch,TRUE);
		    continue;
		}

		if (check_pk(ch,victim,CHECK_DELAY))
		{
			stop_fighting(ch,TRUE);
			continue;
		}

		if (!IS_NPC(ch) && IS_ADDED(ch,ADDED_FRENZY) 
			&& ch->pcdata->frenzy_action == FRENZY_FLIGHT)
			do_flee(ch,"");

		if ( IS_AWAKE(ch) && IS_AWAKE(victim) && 
			ch->in_room == victim->in_room )
		{
			/* calls from here, because everyone actively fighting
			* (as in, not asleep and in the room, see "if" check above)
			* could pull a person into the fight, either against them
			* or against the oponent.  Also called before a person hits
			* so that no one gets drawn in after a fight ends due to death.
			*/
			/*crowd_brawl(ch);*/ 
			multi_hit( ch, victim, TYPE_UNDEFINED );
		}			
		else
        {
           //stop_fighting( ch, FALSE );
            continue;
		}
        
		if ( ( victim = ch->fighting ) == NULL )
			continue;
		
	/*
	 * Fun for the whole family!
	 */
		for ( rch = ch->in_room->people; rch != NULL; rch = rch_next )
		{
			rch_next = rch->next_in_room;
			
			if ( IS_AWAKE(rch) && rch->fighting == NULL )
			{
				
				if (HAS_DELAY(rch))
					continue;

				if ( IS_NPC(rch) && IS_SET(rch->act,ACT_GUARD) && rch->fighting == NULL)
				{
					/*KILL THE INFDELS!*/

					if (IS_NPC(ch) && !IS_NPC(victim) && !IS_SET(ch->act,ACT_ANIMAL)) 
					{
						set_fighting(rch,victim);
						do_say(rch,"Don't worry! We will protect you!");
						multi_hit( rch, victim, TYPE_UNDEFINED );
						continue;
					}

					if (IS_NPC(victim) && !IS_NPC(ch) && !IS_SET(ch->act,ACT_ANIMAL))
					{
						set_fighting(rch,ch);
						do_say(rch,"Don't worry! We will protect you!");
						multi_hit( rch, ch, TYPE_UNDEFINED );
						continue;
					}
				
				}
		/*
		 * Mount's auto-assist their riders and vice versa.
		 */
				if ( (mount = rch->mount) != NULL )
				{
					if (mount == ch) 
						multi_hit( rch, victim, TYPE_UNDEFINED );
					continue;
				}
		/*
		 * PC's auto-assist others in their group.
		 */
				if ( !IS_NPC(ch) || IS_AFFECTED(ch, AFF_CHARM) )
				{
					if ( ( !IS_NPC(rch) || IS_AFFECTED(rch, AFF_CHARM) )
						&&   is_same_group(ch, rch) )
						multi_hit( rch, victim, TYPE_UNDEFINED );
					continue;
				}
				
		/*
		 * NPC's assist NPC's of same type or 12.5% chance regardless.
		 */
				if ( IS_NPC(rch) && !IS_AFFECTED(rch, AFF_CHARM) )
				{
					if ( rch->pIndexData == ch->pIndexData
						||   number_bits( 3 ) == 0 )
					{
						CHAR_DATA *vch;
						CHAR_DATA *target = NULL;
						int number = 0;
						
						for ( vch = ch->in_room->people; vch; vch = vch->next )
						{
							if ( can_see( rch, vch )
								&&   is_same_group( vch, victim )
								&&   number_range( 0, number ) == 0 )
							{
								target = vch;
								number++;
							}
						}
						
						if ( target != NULL )
							multi_hit( rch, target, TYPE_UNDEFINED );
					}
				}
			}
		}
		
		if ( IS_NPC( ch ) )
		{
			if ( HAS_TRIGGER( ch, TRIG_FIGHT ) )
				mp_percent_trigger( ch, victim, NULL, NULL, TRIG_FIGHT );
			if ( HAS_TRIGGER( ch, TRIG_HPCNT ) )
				mp_hprct_trigger( ch, victim );
		}
		
		if ( HAS_PROG(ch->in_room, TRIG_FIGHT) )
        	rprog_percent_trigger(ch->in_room, ch, NULL, NULL, TRIG_FIGHT);
      	/*
		for ( to_obj = ch->in_room->contents; to_obj != NULL;	to_obj = to_obj->next_content )
       	{
			if ( HAS_PROG(to_obj->pIndexData, TRIG_FIGHT) )
         	 oprog_percent_trigger(to_obj, ch, NULL, NULL, TRIG_FIGHT);
    	}
    	*/
	}
	mudsetting->last_proc_logged = 43;
	
	return;
}