from __main__ import game;
from toee import *
from utilities import *

def san_dialog( attachee, triggerer ):
	if ( game.global_flags[198] == 1 ):
		triggerer.begin_dialog( attachee, 260 )
	elif (attachee.leader_get() != OBJ_HANDLE_NULL):
		if (game.global_flags[53] == 1):
			triggerer.begin_dialog( attachee, 320 )
		else:
			triggerer.begin_dialog( attachee, 220 )
	elif ( game.global_flags[52] == 1 ):
		triggerer.begin_dialog( attachee, 20 )
	elif ( game.global_flags[48] == 1 ):
		triggerer.begin_dialog( attachee, 1 )
	else:
		triggerer.begin_dialog( attachee, 10 )
	return SKIP_DEFAULT


def san_first_heartbeat( attachee, triggerer ):
	return RUN_DEFAULT

def san_heartbeat( attachee, triggerer ):
	return RUN_DEFAULT


def san_dying( attachee, triggerer ):
	if (attachee.leader_get() != OBJ_HANDLE_NULL):
		game.global_vars[29] = game.global_vars[29] + 1
		game.global_flags[37] = 1
		if ( game.story_state <= 1 ):
			game.story_state = 2
		return RUN_DEFAULT
	attachee.float_line(12014,triggerer)
	game.global_flags[37] = 1
	if ( game.story_state <= 1 ):
		game.story_state = 2
	for pc in game.party:
		if ( pc.reputation_has( 18 ) == 1):
			pc.reputation_remove( 18 )
	attachee.float_line(12014,triggerer)
	game.party[0].reputation_add( 15 )
	if ( game.global_flags[340] == 1 ):
		for obj in game.obj_list_vicinity(attachee.location,OLC_PC):
			if (is_safe_to_talk(attachee,obj)):
				obj.begin_dialog( attachee, 370 )
	elif ( game.global_flags[62] == 0 ):
		for obj in game.obj_list_vicinity(attachee.location,OLC_PC):
			if (is_safe_to_talk(attachee,obj)):
				obj.begin_dialog( attachee, 390 )
	return RUN_DEFAULT

def	san_disband( attachee, triggerer ):
	for pc in game.party:
		attachee.ai_shitlist_remove( pc )
		attachee.reaction_set( pc, 50 )
	for npc in game.obj_list_vicinity(attachee.location,OLC_NPC):
		for pc in game.party:
			npc.ai_shitlist_remove( pc )
			npc.reaction_set( pc, 50 )
	return RUN_DEFAULT

def san_join( attachee, triggerer ):
	if ( game.story_state <= 1 ):
		game.story_state = 2	
	if ( game.global_flags[340] == 1 ):
		triggerer.begin_dialog( attachee, 380 )
	return RUN_DEFAULT

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

def san_enter_combat( attachee, triggerer ):
	attachee.float_line(12058,triggerer)
	if (attachee.leader_get() == OBJ_HANDLE_NULL and (not attachee.has_wielded(1) or not attachee.has_wielded(4071))):
		attachee.item_wield_best_all()
		game.new_sid = 0
	return RUN_DEFAULT

def san_start_combat( attachee, triggerer ):
	if ( obj_percent_hp(attachee) < 50):
		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.begin_dialog( attachee, 160 )
			game.new_sid = 0
			return SKIP_DEFAULT
	return RUN_DEFAULT

def san_new_map( attachee, triggerer ):
	if (attachee.map == 5065):
		leader = attachee.leader_get()
		if (leader != OBJ_HANDLE_NULL):
			leader.begin_dialog( attachee, 270 )
	if ((attachee.map == 5113) and ( game.global_flags[200] == 0 )):
		game.global_flags[200] = 1
		leader = attachee.leader_get()
		if (leader != OBJ_HANDLE_NULL):
			leader.begin_dialog( attachee, 290 )
	if (attachee.map == 5066):
		leader = attachee.leader_get()
		if (leader != OBJ_HANDLE_NULL):
			leader.begin_dialog( attachee, 300 )
	return RUN_DEFAULT

def run_off( attachee, triggerer ):
	for pc in game.party:
		attachee.ai_shitlist_remove( pc )
		attachee.reaction_set( pc, 50 )
	attachee.runoff(attachee.location-3)
	return RUN_DEFAULT

def	demo_end_game( attachee, triggerer ):
	# play slides and end game
	return RUN_DEFAULT

def equip_transfer( attachee, triggerer ):
	itemA = attachee.item_find(4071)
	if (itemA != OBJ_HANDLE_NULL):
		itemA.destroy()
		create_item_in_inventory( 4071, triggerer )
	create_item_in_inventory( 7001, attachee )
	return RUN_DEFAULT