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] >= 9):
			triggerer.begin_dialog( attachee, 50 )
		elif (game.party_alignment == NEUTRAL_EVIL):
			triggerer.begin_dialog( attachee, 90 )
		elif (game.global_vars[5] <= 4):
			triggerer.begin_dialog( attachee, 30 )
		else:
			triggerer.begin_dialog( attachee, 40 )
	elif (game.party_alignment == NEUTRAL_GOOD or game.party_alignment == LAWFUL_GOOD or game.party_alignment == LAWFUL_NEUTRAL):
		game.global_vars[5] = 3
		triggerer.begin_dialog( attachee, 1 )
	elif (game.party_alignment == NEUTRAL_EVIL or game.party_alignment == CHAOTIC_EVIL or game.party_alignment == CHAOTIC_NEUTRAL):
		game.global_vars[5] = 5
		triggerer.begin_dialog( attachee, 20 )
	else:
		game.global_vars[5] = 4
		triggerer.begin_dialog( attachee, 10 )
	return SKIP_DEFAULT

def san_first_heartbeat( attachee, triggerer ):
	if (game.global_flags[816] == 1):
		attachee.object_flag_set(OF_OFF)
		return RUN_DEFAULT
	if (game.global_flags[21] == 1):
		attachee.object_flag_unset(OF_OFF)
	else:
		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[723] = 0
	return RUN_DEFAULT


def san_heartbeat( attachee, triggerer ):
#	if (game.global_flags[875] == 1 and game.global_flags[876] == 0 and game.quests[101].state != qs_completed and not anyone( triggerer.group_list(), "has_item", 12900 )):
#		game.global_flags[876] = 1
#		game.timevent_add( amii_dies, (), 140000000 )
#	if (game.global_vars[723] == 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[723] == 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[723] = game.global_vars[723] + 1
	return RUN_DEFAULT


def san_dying( attachee, triggerer ):
	game.global_flags[816] = 1
	game.global_flags[299] = 1
	if game.global_flags[296] == 1:
#		game.party[0].prestige_class_add[3]
		game.party[0].reputation_add( 24 )
		return RUN_DEFAULT
	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 san_enter_combat( attachee, triggerer ):
	if (game.global_flags[817] == 0 and game.global_flags[818] == 0):
		game.global_flags[818] = 1
		for target in game.obj_list_vicinity(attachee.location,OLC_PC):
			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)
		calmert = game.obj_create( 14011, location_from_axis (485L, 480L) )
		calmert.turn_towards(attachee)
		game.particles( "sp-Dimension Door", calmert )
		terjon = attachee.get_initiative()
		calmert.add_to_initiative()
		calmert.set_initiative( terjon )
		game.update_combat_ui()		
	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 amii_dies():
#	game.quests[101].state = qs_botched
	return RUN_DEFAULT


def create_calmert( attachee, triggerer ):
	calmert = game.obj_create( 14011, location_from_axis (485L, 480L) )
	calmert.turn_towards(attachee)
	game.particles( "sp-Dimension Door", calmert )		
	return RUN_DEFAULT


def switch_to_calmert( npc, pc ):
	calmert = find_npc_near(npc,20006)
	for target in game.obj_list_vicinity(npc.location,OLC_PC):
		if (calmert.distance_to(target) <= 20 and target.type == obj_t_pc):
			calmert.turn_towards(target)
			target.begin_dialog(calmert,2020)
			return SKIP_DEFAULT
	calmert.float_line(2020,triggerer)
	calmert.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_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	Change_Brooch( attachee, triggerer ):
	for pc in game.obj_list_vicinity( obj.location, OLC_NPC ):
		if ( pc.item_find(3003) != OBJ_HANDLE_NULL ):
			pc.begin_dialog(attachee, 1220)
			return SKIP_DEFAULT
	triggerer.begin_dialog(attachee, 1210)
	return SKIP_DEFAULT