from toee import *
from utilities import *

def san_dialog( attachee, triggerer ):
	if (game.quests[16].state == qs_completed and game.quests[15].state == qs_completed): 
		triggerer.begin_dialog( attachee, 20 )
	elif (game.global_flags[41] == 1 or game.quests[16].state == qs_completed or game.quests[64].state == qs_completed): 
		triggerer.begin_dialog( attachee, 290 )
	elif (game.quests[17].state == qs_completed):
		triggerer.begin_dialog( attachee, 30 )
	elif (game.global_flags[31] == 1 or game.quests[64].state == qs_botched):
		triggerer.begin_dialog( attachee, 50 )
	elif (attachee.has_met(triggerer)):
		triggerer.begin_dialog( attachee, 70 )
	else:
		triggerer.begin_dialog( attachee, 1 )
	return SKIP_DEFAULT

def san_heartbeat( attachee, triggerer ):
	itemA = attachee.item_find(8010)
	if (itemA != OBJ_HANDLE_NULL and game.global_vars[750] == 0 and attachee.map == 5010):
		itemA.destroy()
#		create_item_in_inventory( 8021, attachee )
		game.new_sid = 0
	return RUN_DEFAULT

def san_start_combat( attachee, triggerer ):
	## THIS IS USED FOR BREAK FREE
	for obj in game.party[0].group_list():
		if (obj.distance_to(attachee) <= 3 and obj.stat_level_get(stat_hp_current) >= -9):
			return RUN_DEFAULT		
	while(attachee.item_find(8903) != OBJ_HANDLE_NULL):
		attachee.item_find(8903).destroy()
	if (attachee.d20_query(Q_Is_BreakFree_Possible)):
		create_item_in_inventory( 8903, attachee )
##		attachee.d20_send_signal(S_BreakFree)
	return RUN_DEFAULT

def switch_to_gremag( rannos, pc ):
	gremag = find_npc_near(rannos,8049)
	game.global_vars[750] = 1
	game.global_vars[751] = 1
	pc.begin_dialog(gremag,1010)
	rannos.turn_towards(gremag)
	gremag.turn_towards(rannos)
	return SKIP_DEFAULT

def san_enter_combat( attachee, triggerer ):
	if (triggerer.type == obj_t_pc):
		raimol = find_npc_near( attachee, 8050)
		if (raimol != OBJ_HANDLE_NULL):
			attachee.float_line(380,triggerer)
			leader = raimol.leader_get()
			if (leader != OBJ_HANDLE_NULL):
				leader.follower_remove(raimol)
			raimol.attack(triggerer)
	return RUN_DEFAULT

def san_dying( attachee, triggerer ):
	game.global_flags[814] = 1
	if (game.global_flags[815] == 1):
		for pc in game.party:
			if ( pc.reputation_has( 23 ) == 1):
				pc.reputation_remove( 23 )
		if (game.party[0].reputation_has(9) == 0):
			game.party[0].reputation_add(9)
	return RUN_DEFAULT

def san_resurrect( attachee, triggerer ):
	game.global_flags[814] = 0
	if (game.party[0].reputation_has(9) == 1):
		for pc in game.party:
			if ( pc.reputation_has( 23 ) == 0):
				pc.reputation_add( 23 )
		game.party[0].reputation_remove(9)
	return RUN_DEFAULT

def buff_npc( attachee, triggerer ):
	for obj in game.obj_list_vicinity(attachee.location,OLC_NPC):
		if (obj.name == 14607 and obj.leader_get() == OBJ_HANDLE_NULL):
			obj.cast_spell(spell_stoneskin, obj)
		if (obj.name == 14609 and obj.leader_get() == OBJ_HANDLE_NULL):
			obj.cast_spell(spell_shield_of_faith, obj)
	return RUN_DEFAULT

def buff_npc_two( attachee, triggerer ):
	for obj in game.obj_list_vicinity(attachee.location,OLC_NPC):
		if (obj.name == 14607 and obj.leader_get() == OBJ_HANDLE_NULL):
			obj.cast_spell(spell_improved_invisibility, obj)
		if (obj.name == 14609 and obj.leader_get() == OBJ_HANDLE_NULL):
			obj.cast_spell(spell_owls_wisdom, obj)
	return RUN_DEFAULT

def buff_npc_three( attachee, triggerer ):
	target = find_npc_near( attachee, 8049)
	for obj in game.obj_list_vicinity(attachee.location,OLC_NPC):
		if (obj.name == 14607 and obj.leader_get() == OBJ_HANDLE_NULL):
			obj.cast_spell(spell_endurance, attachee)
		if (obj.name == 14609 and obj.leader_get() == OBJ_HANDLE_NULL):
			obj.cast_spell(spell_endurance, target)
	return RUN_DEFAULT

def buff_npc_four( attachee, triggerer ):
	target = find_npc_near( attachee, 14606)
	for obj in game.obj_list_vicinity(attachee.location,OLC_NPC):
		if (obj.name == 14607 and obj.leader_get() == OBJ_HANDLE_NULL):
			obj.cast_spell(spell_endurance, attachee)
		if (obj.name == 14609 and obj.leader_get() == OBJ_HANDLE_NULL):
			obj.cast_spell(spell_endurance, target)
	return RUN_DEFAULT
