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

def san_dialog( attachee, triggerer ):
	attachee.turn_towards(triggerer)
	triggerer.begin_dialog( attachee, 1 )
	return SKIP_DEFAULT

def san_first_heartbeat(attachee, triggerer):
	if attachee.map == 5136:
		game.timevent_add(respawn, (attachee), 86400000 ) #86400000ms is 24 hours
		game.new_sid = 0
	return RUN_DEFAULT

def san_heartbeat( attachee, triggerer ):
	if (game.global_flags[990] == 0 ):
		attachee.object_flag_set(OF_OFF)
	if (game.global_flags[990] == 1 ):
		attachee.object_flag_unset(OF_OFF)
	while(attachee.item_find(8008) != OBJ_HANDLE_NULL):
		attachee.item_find(8008).destroy()
	while(attachee.item_find(8009) != OBJ_HANDLE_NULL):
		attachee.item_find(8009).destroy()
	while(attachee.item_find(8010) != OBJ_HANDLE_NULL):
		attachee.item_find(8010).destroy()
	while(attachee.item_find(8013) != OBJ_HANDLE_NULL):
		attachee.item_find(8013).destroy()
	while(attachee.item_find(8014) != OBJ_HANDLE_NULL):
		attachee.item_find(8014).destroy()
	while(attachee.item_find(8015) != OBJ_HANDLE_NULL):
		attachee.item_find(8015).destroy()
	while(attachee.item_find(8017) != OBJ_HANDLE_NULL):
		attachee.item_find(8017).destroy()
	while(attachee.item_find(8020) != OBJ_HANDLE_NULL):
		attachee.item_find(8020).destroy()
	while(attachee.item_find(8021) != OBJ_HANDLE_NULL):
		attachee.item_find(8021).destroy()
	while(attachee.item_find(8022) != OBJ_HANDLE_NULL):
		attachee.item_find(8022).destroy()
	while(attachee.item_find(8024) != OBJ_HANDLE_NULL):
		attachee.item_find(8024).destroy()
	while(attachee.item_find(8025) != OBJ_HANDLE_NULL):
		attachee.item_find(8025).destroy()
	while(attachee.item_find(8027) != OBJ_HANDLE_NULL):
		attachee.item_find(8027).destroy()
	while(attachee.item_find(8030) != OBJ_HANDLE_NULL):
		attachee.item_find(8030).destroy()
		create_item_in_inventory( 8904, attachee )
	if (not game.combat_is_active()):
		if (game.global_flags[144] == 0):
			if (find_npc_near( attachee, 8032 ) == OBJ_HANDLE_NULL):
				for obj in game.obj_list_vicinity(attachee.location,OLC_PC):
					if (not attachee.has_met(obj)):
						if (is_safe_to_talk(attachee,obj)):
							obj.begin_dialog(attachee,1)
#							game.new_sid = 0			## removed by Livonya
	if (game.combat_is_active()):
		return RUN_DEFAULT
	if (game.global_vars[720] == 0):
			attachee.cast_spell(spell_stoneskin, attachee)
			attachee.spells_pending_to_memorized()
	if (game.global_vars[720] == 4):
			attachee.cast_spell(spell_see_invisibility, attachee)
			attachee.spells_pending_to_memorized()
	if (game.global_vars[720] == 8):
			attachee.cast_spell(spell_false_life, attachee)
			attachee.spells_pending_to_memorized()
	if (game.global_vars[720] == 12):
			attachee.cast_spell(spell_mage_armor, attachee)
			attachee.spells_pending_to_memorized()
	game.global_vars[720] = game.global_vars[720] + 1	
	if (not attachee.has_spell_effects()):
		game.global_vars[720] = 1

	return RUN_DEFAULT

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