from utilities import *

from toee import *

def san_dialog( attachee, triggerer ):
	if ( game.global_flags[28] == 1 and triggerer.reputation_has( 2 ) == 0 ):
		triggerer.reputation_add( 2 )
	if (attachee.has_met(triggerer)):
		if (game.global_vars[5] <= 7):
			attachee.turn_towards(triggerer)	## added by Livonya
			triggerer.begin_dialog( attachee, 10 )
		else:
			attachee.turn_towards(triggerer)	## added by Livonya
			triggerer.begin_dialog( attachee, 20 )
	else:
		attachee.turn_towards(triggerer)	## added by Livonya
		triggerer.begin_dialog( attachee, 1 )
	return SKIP_DEFAULT


def san_enter_combat( attachee, triggerer ):
	if (game.global_flags[816] == 0 and game.global_flags[818] == 0):
		game.global_flags[818] = 1
		for target in game.party[0].group_list():
			if (target.name != 20032 and attachee.distance_to(target) <= 20 and target.type == obj_t_pc):
				attachee.turn_towards(target)
				target.begin_dialog(attachee,2000)
				return SKIP_DEFAULT
		attachee.float_line(2010,triggerer)
		terjon = game.obj_create( 14007, location_from_axis (491L, 474L) )
		game.particles( "sp-Dimension Door", terjon )
		terjon.turn_towards(attachee)	
		calmert = attachee.get_initiative()
		terjon.add_to_initiative()
		terjon.set_initiative( calmert )
		game.update_combat_ui()	
		terjon.attack(triggerer)		
	for npc in game.party[0].group_list():
		if (npc.name == 20032 and npc.leader_get() != OBJ_HANDLE_NULL and game.global_flags[819] == 0):
			curr = npc.stat_level_get( stat_hp_current )
			if (curr >= 1): 
				for target in game.party[0].group_list():
					if (target.name != 20032 and npc.distance_to(target) <= 20 and target.type == obj_t_pc):
						npc.turn_towards(target)
						target.begin_dialog(npc,1000)
						return SKIP_DEFAULT
	return RUN_DEFAULT


def san_start_combat( attachee, triggerer ):	
	for npc in game.party[0].group_list():
		if (npc.name == 20032 and npc.leader_get() != OBJ_HANDLE_NULL and game.global_flags[819] == 0):
			curr = npc.stat_level_get( stat_hp_current )
			if (curr >= 1): 
				for target in game.party[0].group_list():
					if (target.name != 20032 and npc.distance_to(target) <= 20 and target.type == obj_t_pc):
						npc.turn_towards(target)
						target.begin_dialog(npc,1000)
						return SKIP_DEFAULT
	return RUN_DEFAULT


def create_terjon( attachee, triggerer ):
	terjon = game.obj_create( 14007, location_from_axis (491L, 474L) )
	terjon.turn_towards(attachee)
	game.particles( "sp-Dimension Door", terjon )		
	return RUN_DEFAULT


def switch_to_terjon( npc, pc ):
	terjon = find_npc_near(npc,20003)
	for target in game.obj_list_vicinity(npc.location,OLC_PC):
		if (terjon.distance_to(target) <= 20 and target.type == obj_t_pc):
			terjon.turn_towards(target)
			target.begin_dialog(terjon,2020)
			return SKIP_DEFAULT
	terjon.float_line(2020,triggerer)
	terjon.attack(pc)
	return RUN_DEFAULT


def look_spugnoir( attachee, triggerer ):
	for npc in game.party[0].group_list():
		if (npc.name == 20032 and npc.leader_get() != OBJ_HANDLE_NULL and game.global_flags[819] == 0):
			curr = npc.stat_level_get( stat_hp_current )
			if (curr >= 1): 
				for target in game.party[0].group_list():
					if (npc.distance_to(target) <= 20 and target.type == obj_t_pc and npc.name != 20032):
						npc.turn_towards(target)
						target.begin_dialog(npc,1000)
						return SKIP_DEFAULT
	attachee.attack(triggerer)		
	return RUN_DEFAULT


def san_first_heartbeat( attachee, triggerer ):
	if (game.global_flags[817] == 1):
		attachee.object_flag_set(OF_OFF)
		return RUN_DEFAULT
#	if (attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
#		game.global_vars[724] = 0
	return RUN_DEFAULT


def san_heartbeat( attachee, triggerer ):
#	if (game.global_vars[724] == 0 and attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
#		attachee.cast_spell(spell_death_ward, attachee)
#		attachee.spells_pending_to_memorized()
#	if (game.global_vars[724] == 4 and attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
#		attachee.cast_spell(spell_shield_of_faith, attachee)
#		attachee.spells_pending_to_memorized()
#	game.global_vars[724] = game.global_vars[724] + 1
	return RUN_DEFAULT


def san_dying( attachee, triggerer ):
	game.global_flags[817] = 1
	game.global_vars[23] = game.global_vars[23] + 1
	if (game.global_vars[23] >= 2):
		game.party[0].reputation_add( 1 )
	return RUN_DEFAULT


def beggar_cavanaugh( attachee, triggerer ):
	game.timevent_add( beggar_now, ( attachee, triggerer ), 86400000 )
	return RUN_DEFAULT


def beggar_now( attachee, triggerer ):
	game.global_flags[205] = 1
	game.global_vars[24] = game.global_vars[24] + 1
	if (triggerer.reputation_has( 5 ) == 0):
		triggerer.reputation_add( 5 )
	if ( game.global_vars[24] >= 3 and triggerer.reputation_has( 6 ) == 0 ):
		triggerer.reputation_add( 6 )
	return RUN_DEFAULT
