
from toee import *
from utilities import *
from InventoryRespawn import *

switch_flag = obj_f_npc_pad_i_5

def san_dialog( attachee, triggerer ):
	if (attachee.leader_get() != OBJ_HANDLE_NULL):
		triggerer.begin_dialog( attachee, 330 )
	elif (game.global_flags[839] == 1):
		triggerer.begin_dialog(attachee,1300)
	elif (game.global_flags[835] == 1 and game.global_flags[37] == 0 and game.global_flags[842] == 1 and game.global_flags[839] == 0):
		triggerer.begin_dialog(attachee,1000)
	elif (game.party[0].reputation_has( 28 ) == 1): 
		triggerer.begin_dialog( attachee, 590 )
	elif (game.party[0].reputation_has( 27 ) == 1): 
		triggerer.begin_dialog( attachee, 11002 )
	else:
		triggerer.begin_dialog( attachee, 1 )
	return SKIP_DEFAULT

def san_first_heartbeat( attachee, triggerer ):
#	if (attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
#		game.global_vars[730] = 0
	if not (game.global_vars[34] & 1) and Shopkeeper_Is(attachee):
		game.timevent_add(respawn, (attachee), 86400000 ) #86400000ms is 24 hours
		game.global_vars[34] | 1
	return RUN_DEFAULT

def san_heartbeat( attachee, triggerer ):
	Switch_NPC( attachee ) ## Added by Cerulean the Blue
#	if (game.global_vars[730] == 0 and attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
#		attachee.cast_spell(spell_mage_armor, attachee)
#		attachee.spells_pending_to_memorized()
#		game.global_vars[730] = 1
	return RUN_DEFAULT

def san_new_map( attachee, triggerer ):
	if ( game.global_flags[195] == 1 ):
		game.leader.begin_dialog( attachee, 480 )
	return SKIP_DEFAULT

def san_dying( attachee, triggerer ):
	attachee.float_line(12014,triggerer)
	game.global_flags[336] = 1
	if (attachee.leader_get() == OBJ_HANDLE_NULL):
		game.global_vars[23] = game.global_vars[23] + 1
		if (game.global_vars[23] >= 2):
			game.party[0].reputation_add( 1 )
	else:
		game.global_vars[29] = game.global_vars[29] + 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 ) == 20000 )

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 == 8054 ):
			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)

def respawn(attachee):
	box = find_container_near(attachee,1001)
	RespawnInventory(box)
	game.timevent_add(respawn, (attachee), 86400000 ) #86400000ms is 24 hours
	return

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