from toee import *

from Co8 import *

def san_dialog( attachee, triggerer ):
	if ( game.global_flags[144] == 1 ):
		if (not attachee.has_met(triggerer)):
			triggerer.begin_dialog( attachee, 10 )
		else:
			triggerer.begin_dialog( attachee, 290 )
	elif ( game.quests[58].state >= qs_accepted ):
			triggerer.begin_dialog( attachee, 480 )
	elif ( attachee.has_met(triggerer) ):
			triggerer.begin_dialog( attachee, 490 )
	else:
			triggerer.begin_dialog( attachee, 1 )
	return SKIP_DEFAULT


def san_first_heartbeat( attachee, triggerer ):
	if (game.global_vars[754] == 1):
		return RUN_DEFAULT
	if (not game.combat_is_active()):
		game.global_vars[719] = 0
		game.global_vars[717] = 0
		for obj in game.obj_list_vicinity(attachee.location,OLC_PC):
			if (is_safe_to_talk(attachee,obj)):
				if ( game.global_vars[691] == 3 ):
					obj.turn_towards(attachee)		## added by Livonya
					attachee.turn_towards(obj)		## added by Livonya
					obj.begin_dialog( attachee, 40 )
				elif ( game.global_vars[691] == 2 ):
					obj.turn_towards(attachee)		## added by Livonya
					attachee.turn_towards(obj)		## added by Livonya
					obj.begin_dialog( attachee, 30 )
				elif ( game.global_vars[691] == 1 ):
					obj.turn_towards(attachee)		## added by Livonya
					attachee.turn_towards(obj)		## added by Livonya
					obj.begin_dialog( attachee, 20 )
				else:
					obj.turn_towards(attachee)		## added by Livonya
					attachee.turn_towards(obj)		## added by Livonya
					obj.begin_dialog(attachee,1)
				game.global_vars[754] = 1
##				game.new_sid = 0		## removed by Livonya
	return RUN_DEFAULT


def	san_heartbeat( attachee, triggerer ):
	if (game.combat_is_active()):
		return RUN_DEFAULT
#	else:
#		for obj in game.obj_list_vicinity(attachee.location,OLC_NPC):
#			if (obj.name == 14424 and obj.leader_get() == OBJ_HANDLE_NULL and game.global_vars[717] == 0):
#				obj.cast_spell(spell_mage_armor, obj)
#				obj.spells_pending_to_memorized()
#		for obj in game.obj_list_vicinity(attachee.location,OLC_NPC):
#			if (obj.name == 14424 and obj.leader_get() == OBJ_HANDLE_NULL and game.global_vars[717] == 4):
#				obj.cast_spell(spell_see_invisibility, obj)
#				obj.spells_pending_to_memorized()
#		for obj in game.obj_list_vicinity(attachee.location,OLC_NPC):
#			if (obj.name == 14424 and obj.leader_get() == OBJ_HANDLE_NULL and game.global_vars[717] == 8):
#				obj.cast_spell(spell_protection_from_arrows, obj)
#				obj.spells_pending_to_memorized()
#		for obj in game.obj_list_vicinity(attachee.location,OLC_NPC):
#			if (obj.name == 14424 and obj.leader_get() == OBJ_HANDLE_NULL and game.global_vars[717] == 12):
#				obj.cast_spell(spell_eagles_splendor, obj)
#				obj.spells_pending_to_memorized()
#		game.global_vars[717] = game.global_vars[717] + 1
	
#		if (game.global_vars[719] == 0):
#				attachee.cast_spell(spell_freedom_of_movement, attachee)
#				attachee.spells_pending_to_memorized()
#		if (game.global_vars[719] == 4):
#				attachee.cast_spell(spell_owls_wisdom, attachee)
#				attachee.spells_pending_to_memorized()
#		game.global_vars[719] = game.global_vars[719] + 1	
	
		for obj in game.obj_list_vicinity(attachee.location,OLC_PC):
			if (is_safe_to_talk(attachee,obj) and game.global_flags[812] == 0):
				if ( game.quests[58].state != qs_unknown ):
					game.global_flags[812] = 1
#					game.new_sid = 0				## Removed by Livonya
					return SKIP_DEFAULT
				elif ( game.global_vars[691] == 3 ):
					obj.turn_towards(attachee)		## added by Livonya
					attachee.turn_towards(obj)		## added by Livonya
					obj.begin_dialog( attachee, 40 )
				elif ( game.global_vars[691] == 2 ):
					obj.turn_towards(attachee)		## added by Livonya
					attachee.turn_towards(obj)		## added by Livonya
					obj.begin_dialog( attachee, 30 )
				elif ( game.global_vars[691] == 1 ):
					obj.turn_towards(attachee)		## added by Livonya
					attachee.turn_towards(obj)		## added by Livonya
					obj.begin_dialog( attachee, 20 )
				elif ( game.global_flags[144] == 1 ):
					if (not attachee.has_met(obj)):
						obj.turn_towards(attachee)		## added by Livonya
						attachee.turn_towards(obj)		## added by Livonya
						obj.begin_dialog( attachee, 10 )
					else:
						obj.turn_towards(attachee)		## added by Livonya
						attachee.turn_towards(obj)		## added by Livonya
						obj.begin_dialog( attachee, 290 )
				elif ( game.quests[58].state >= qs_accepted ):
					obj.turn_towards(attachee)		## added by Livonya
					attachee.turn_towards(obj)		## added by Livonya
					obj.begin_dialog( attachee, 480 )
				elif ( attachee.has_met(obj) ):
					obj.turn_towards(attachee)		## added by Livonya
					attachee.turn_towards(obj)		## added by Livonya
					obj.begin_dialog( attachee, 490 )
				else:
					obj.turn_towards(attachee)		## added by Livonya
					attachee.turn_towards(obj)		## added by Livonya
					obj.begin_dialog(attachee,1)
				game.global_flags[812] = 1
#				game.new_sid = 0				## Removed by Livonya
	return RUN_DEFAULT


def san_dying( attachee, triggerer ):
	game.global_flags[146] = 1
###############  Removed by Cerulean the Blue - Didn't work
	# found_pc = OBJ_HANDLE_NULL
	# for pc in game.party:
		# if pc.type == obj_t_pc:
			# found_pc = pc
			# attachee.ai_shitlist_remove( pc )
	# if found_pc != OBJ_HANDLE_NULL:
		# found_pc.turn_towards(attachee)		## added by Livonya
		# attachee.turn_towards(found_pc)		## added by Livonya
		# found_pc.begin_dialog( attachee, 190 )
		# game.new_sid = 0
		# return SKIP_DEFAULT
###############
	return RUN_DEFAULT

def san_resurrect( attachee, triggerer ):
	game.global_flags[146] = 0
	return RUN_DEFAULT

def san_end_combat( attachee, triggerer ):
	npc = find_npc_near(attachee,8001)
	if (npc != OBJ_HANDLE_NULL):
		game.global_flags[325] = 1
	npc = find_npc_near(attachee,8059)
	if (npc != OBJ_HANDLE_NULL):
		game.global_flags[325] = 1
	return RUN_DEFAULT

def san_start_combat( attachee, triggerer ):
	if ( obj_percent_hp(attachee) < 50):
		found_pc = OBJ_HANDLE_NULL
		StopCombat(attachee, 0)  # Added by Cerulean the Blue:  StopCombat is in Co8.py
		for pc in game.party:
			if pc.type == obj_t_pc:
				found_pc = pc
				attachee.ai_shitlist_remove( pc )
		if found_pc != OBJ_HANDLE_NULL:
			found_pc.turn_towards(attachee)		## added by Livonya
			attachee.turn_towards(found_pc)		## added by Livonya
			found_pc.begin_dialog( attachee, 190 )
			game.new_sid = 0
			return SKIP_DEFAULT
	return RUN_DEFAULT

def talk_Romag( attachee, triggerer, line):
	npc = find_npc_near(attachee,8037)
	if (npc != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(npc,line)
		npc.turn_towards(attachee)
		attachee.turn_towards(npc)
	else:
		triggerer.begin_dialog(attachee,520)
	return SKIP_DEFAULT

def	summon_Iuz( attachee, triggerer ):
	# needs to make Iuz appear near him
	Iuz = game.obj_create(14266, attachee.location-4)
################## Moved from Iuz's san_heartbeat script to here by Cerulean the Blue:  works here, didn't there.
	game.particles( "mon-iuz", Iuz )
	nearby_pc = OBJ_HANDLE_NULL
	for pc in game.obj_list_vicinity(Iuz.location,OLC_PC):
		if (nearby_pc == OBJ_HANDLE_NULL):
			nearby_pc = pc
		if (pc.item_find(2203) != OBJ_HANDLE_NULL):
			talk_Iuz( pc, pc, 1)
			return RUN_DEFAULT
	if (nearby_pc != OBJ_HANDLE_NULL):
		if (find_npc_near(Iuz,8032) != OBJ_HANDLE_NULL):
			talk_Iuz( attachee, nearby_pc, 100)
		else:
			talk_Iuz( nearby_pc, nearby_pc, 130)
###################
	return SKIP_DEFAULT

def talk_Iuz( attachee, triggerer, line):
	Iuz = find_npc_near(attachee,8042)
	if (Iuz != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(Iuz,line)
		Iuz.turn_towards(attachee)
		attachee.turn_towards(Iuz)
	else:
		triggerer.begin_dialog(attachee,30)
	return SKIP_DEFAULT

def	end_game( attachee, triggerer ):
	game.global_flags[339] = 1
	# play slides and end game
	set_join_slides( attachee, triggerer )
	game.moviequeue_play_end_game()
	return SKIP_DEFAULT

def	give_robes( attachee, triggerer ):
	for pc in game.party:
		create_item_in_inventory(6113,pc)
	return SKIP_DEFAULT