from toee import *
from utilities import *

def san_dialog( attachee, triggerer ):
	game.global_flags[70] = 1
	if (attachee.leader_get() != OBJ_HANDLE_NULL):
		triggerer.begin_dialog( attachee, 350 )
	elif ((triggerer.item_find(2202) != OBJ_HANDLE_NULL) or (triggerer.item_find(3008) != OBJ_HANDLE_NULL)):
		triggerer.begin_dialog( attachee, 330 )
	elif ((game.quests[32].state == qs_completed) and (game.global_flags[74] == 0)):
		triggerer.begin_dialog( attachee, 270 )
	elif (game.quests[31].state == qs_completed):
		triggerer.begin_dialog( attachee, 200 )
	elif (game.global_flags[73] == 1):
		triggerer.begin_dialog( attachee, 120 )
	elif (game.quests[63].state == qs_accepted):
		triggerer.begin_dialog( attachee, 700 )
	else:
		triggerer.begin_dialog( attachee, 1 )
	return SKIP_DEFAULT


def san_enter_combat( attachee, triggerer ):
	if (triggerer.type == obj_t_pc):
		leader = attachee.leader_get()
		if (leader != OBJ_HANDLE_NULL):
			leader.follower_remove(attachee)
		elmo = find_npc_near( attachee, 8000)
		if (elmo != OBJ_HANDLE_NULL):
			attachee.float_line(380,triggerer)
			leader = elmo.leader_get()
			if (leader != OBJ_HANDLE_NULL):
				leader.follower_remove(elmo)
			elmo.attack(triggerer)
	return RUN_DEFAULT		
		
def san_heartbeat( attachee, triggerer ):
	if (not game.combat_is_active()):
		if (game.global_flags[362] == 0):
			for obj in game.obj_list_vicinity(attachee.location,OLC_PC):
				if (is_safe_to_talk(attachee,obj)):
					if ((obj.item_find(2202) != OBJ_HANDLE_NULL) or (obj.item_find(3008) != OBJ_HANDLE_NULL)):
						obj.begin_dialog(attachee,330)
						game.global_flags[362] = 1
						return RUN_DEFAULT
		if (game.global_flags[72] == 0):
			for obj in game.obj_list_vicinity(attachee.location,OLC_NPC):
				if (obj.name == 8000):
					if (is_safe_to_talk(attachee,obj)):
						leader = obj.leader_get()
						if (leader == OBJ_HANDLE_NULL):
							leader = attachee.leader_get()
						if (leader != OBJ_HANDLE_NULL):
							leader.begin_dialog(attachee,400)	
							return RUN_DEFAULT
		if (game.global_flags[366] == 1):
			leader = attachee.leader_get()
			if (leader != OBJ_HANDLE_NULL):
				attachee.turn_towards(leader)
				attachee.float_line(12023,leader)
				leader.follower_remove(attachee)
				attachee.attack(leader)
				game.global_flags[366] = 0
				return RUN_DEFAULT
		if (game.global_flags[367] == 1):
			leader = attachee.leader_get()
			if (leader != OBJ_HANDLE_NULL):
				attachee.turn_towards(leader)
				attachee.float_line(10014,leader)
				leader.follower_remove(attachee)
				game.global_flags[367] = 0
				return RUN_DEFAULT
		Switch_NPC( attachee )
	return RUN_DEFAULT

def san_dying( attachee, triggerer ):
	attachee.float_line(12014,triggerer)
	if (attachee.leader_get() != OBJ_HANDLE_NULL):
		game.global_vars[29] = game.global_vars[29] + 1
	return RUN_DEFAULT

def san_join( attachee, triggerer ):
	for chest in game.obj_list_vicinity(attachee.location,OLC_CONTAINER):
		if (chest.name == 1202):
			chest.item_transfer_to( attachee, 2202 )
			chest.item_transfer_to( attachee, 3008 )
			itemA = attachee.item_find(4036)
			if (itemA != OBJ_HANDLE_NULL):
				itemA.destroy()
			itemB = attachee.item_find(6019)
			if (itemB != OBJ_HANDLE_NULL):
				itemB.destroy()
			attachee.item_wield_best_all()
			return RUN_DEFAULT
	return RUN_DEFAULT

def san_disband( attachee, triggerer ):
	for obj in triggerer.group_list():
		if (obj.name == 8021):
			triggerer.follower_remove(obj)
		if (obj.name == 8022):
			triggerer.follower_remove(obj)
	for pc in game.party:
		attachee.ai_shitlist_remove( pc )
		attachee.reaction_set( pc, 50 )
	return RUN_DEFAULT

def make_elmo_talk( attachee, triggerer, line):
	npc = find_npc_near(attachee,8000)
	if (npc != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(npc,line)
		npc.turn_towards(attachee)
		attachee.turn_towards(npc)
	else:
		triggerer.begin_dialog(attachee,410)
	return SKIP_DEFAULT

def make_saduj_talk( attachee, triggerer, line):
	npc = find_npc_near(attachee,14689)
	if (npc != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(npc,line)
		npc.turn_towards(attachee)
		attachee.turn_towards(npc)
	return SKIP_DEFAULT

def talk_to_screng( attachee, triggerer, line):
	npc = find_npc_near(attachee,8021)
	if (npc != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(npc,line)
		npc.turn_towards(attachee)
		attachee.turn_towards(npc)
	else:
		triggerer.begin_dialog(attachee,470)
	return SKIP_DEFAULT

def make_lila_talk( attachee, triggerer, line):
	npc = find_npc_near(attachee,14001)
	if (npc != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(npc,line)
		npc.turn_towards(attachee)
		attachee.turn_towards(npc)
	else:
		triggerer.begin_dialog(attachee,610)
	return SKIP_DEFAULT

def switch_to_thrommel( attachee, triggerer):
	npc = find_npc_near(attachee,8031)
	if (npc != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(npc,40)
		npc.turn_towards(attachee)
		attachee.turn_towards(npc)
	else:
		triggerer.begin_dialog(attachee,510)
	return SKIP_DEFAULT

def san_new_map( attachee, triggerer ):
	if ((attachee.map == 5066) or (attachee.map == 5067) or (attachee.map == 5140) or (attachee.map == 5079) or (attachee.map == 5080)):
		game.global_flags[73] = 1
		if (game.quests[31].state == qs_accepted):
			game.quests[31].state = qs_completed
	elif ((attachee.map == 5062) or (attachee.map == 5113) or (attachee.map == 5093)):
		game.global_flags[73] = 1
		leader = attachee.leader_get()
		if (leader != OBJ_HANDLE_NULL):
			if ((leader.stat_level_get(stat_alignment) == LAWFUL_EVIL) or (leader.stat_level_get(stat_alignment) == CHAOTIC_EVIL) or (leader.stat_level_get(stat_alignment) == NEUTRAL_EVIL)):
				percent = group_percent_hp(leader)
				if ((percent < 30) or (game.global_flags[74] == 1)):
					game.global_flags[366] = 1
	elif (attachee.map == 5051):
		if (((game.global_flags[73] == 1) and (game.quests[31].state == qs_unknown)) or (game.quests[31].state == qs_completed)):
			leader = attachee.leader_get()
			if (leader != OBJ_HANDLE_NULL):
				game.global_flags[367] = 1
	return RUN_DEFAULT	

################################################################################################
 ## Added by Cerulean the Blue

def get_switch_flag( attachee ): # Reads object field where switch flag is stored
	return attachee.obj_get_int( obj_f_npc_pad_i_5 )

def set_switch_flag( attachee, value ): # Sets switch flag in object field where it is stored.  Set to 1 in dialog if NPC is joining party.  Set to 0 in Switch_NPC after NPC has been added to party.
	attachee.obj_set_int( obj_f_npc_pad_i_5, value )
	return attachee.obj_get_int( obj_f_npc_pad_i_5 )

def Switch_NPC( attachee ):
	# Checks if script is being run by Advenrurer and if he has a leader
	if ( not ( Shopkeeper_Is( attachee ) ) and attachee.leader_get() == OBJ_HANDLE_NULL ):	
		# get handle on Shopkeeper
		Shopkeeper = Find_NPC( attachee, 'Shopkeeper' )		
		# If Shopkeeper is "on"  and switch is needed perform "on/off" switch to both and add Adventurer to party 
		if ( ( Shopkeeper != OBJ_HANDLE_NULL ) and ( get_switch_flag( Shopkeeper ) ) ):
			attachee.object_flag_unset(OF_OFF)
			Shopkeeper.object_flag_set(OF_OFF)
			game.party[0].follower_add( attachee )
			set_switch_flag( Shopkeeper, 0 )
		# Add Adventurer to party if Adventurer is already "on" and needs to be added
		elif ( get_switch_flag( attachee ) ):
			game.party[0].follower_add( attachee )
			set_switch_flag( attachee, 0 )
	# Check if script is being run by Shopkeeper
	elif Shopkeeper_Is( attachee ):
		# Gets handle on Adventurer
		Adventurer = Find_NPC( attachee, 'Adventurer' )
		# Check if Adventurer is "on" and is not in party and is in the same spot as Shopkeeper
		if ( Adventurer != OBJ_HANDLE_NULL and Adventurer.leader_get() == OBJ_HANDLE_NULL and location_to_axis( Adventurer.location ) == location_to_axis( attachee.location ) ):
			# Faces Adventurer in the same direction as Shopkeeper and performs "on.off" switch on both
			Adventurer.rotation = attachee.rotation
			attachee.object_flag_unset(OF_OFF)
			Adventurer.object_flag_set(OF_OFF)
	return attachee

def Shopkeeper_Is( npc ): # Returns 1 if npc is Shopkeeper and 0 if it is not.
	return ( npc.obj_get_int( obj_f_critter_description_unknown ) == 20081 )

def Find_NPC( obj , querry): # Finds the npc requested by querry
	for npc in game.obj_list_vicinity( obj.location, OLC_NPC ):
		if ( npc.name == 8014 ):
			if ( querry == 'Shopkeeper' and Shopkeeper_Is( npc ) ):
				return npc
			elif (querry == 'Adventurer' and not Shopkeeper_Is( npc ) ):
				return npc
	return OBJ_HANDLE_NULL

def hireling_add(npc, pc): # Created for Shiningted for KotB.  Not used here, but preserved here for posterity.
	pc.ai_follower_add(npc)
	game.timevent_add(pc.follower_remove, (npc), 259200000)

##################################################################################################
