from toee import *

from utilities import *

from __main__ import game

from Co8 import *

def san_heartbeat( attachee, triggerer ):
	if (game.leader.map == 5123):	# Shop Map
		money_script()
		attachee.destroy()
		
	if (attachee.map == 5093):
		game.areas[7] = 1
		attachee.destroy()
		
	if (attachee.map == 5112):
		game.areas[11] = 1
		attachee.destroy()
		
	if (attachee.map == 5113):
		game.areas[12] = 1
		attachee.destroy()
		
	if (attachee.map == 5091):	# Moathouse Cave Exit
		game.areas[8] = 1
#		if (game.story_state >= 2 and not(is_follower(8002) or is_follower(8004) or is_follower(8005) or is_follower(8010) or game.party_alignment == LAWFUL_EVIL)):
#			if ((game.global_flags[44] != 1) and (game.global_flags[45] != 1) and (game.global_flags[700] != 1) and (game.global_flags[37] == 1) and game.global_flags[70] != 1):
#				Big_Three_Ambush(attachee)
#			attachee.destroy()
		attachee.destroy()

			
	if (attachee.map == 5069 and game.global_vars[3] >= 2):	#The Deklo Grove
#		if (game.party_alignment == LAWFUL_EVIL or game.party_alignment == CHAOTIC_EVIL or game.party_alignment == NEUTRAL_EVIL):
#			drow_ring(attachee)
#			attachee.destroy()
#	
#		elif (game.party_alignment == LAWFUL_GOOD or game.party_alignment == CHAOTIC_GOOD or game.party_alignment == NEUTRAL_GOOD):
#			kob_ring_party(attachee)
#			attachee.destroy()
		attachee.destroy()

	
	return SKIP_DEFAULT

def san_first_heartbeat( attachee, triggerer ):
	if (game.leader.map == 5067):	#Temple Level 2
		bin_thr_dun_tht()
		
	if (game.leader.map == 5064 or game.leader.map == 5140):	#Temple Int and Level 3 Lower
		give_directions()
		
#	if (attachee.map == 5001):	# Hommlet Exterior
#		gob_zap()
#		
#	if (attachee.map == 5002):	#The Ruins of the Moathouse 
#		frog_ring()
		
#	gcr = group_challenge_rating()
		
#	if (attachee.map == 5114):	#Ogre Cave
#		if (game.global_flags[411] == 0 and game.quests[53].state == 0):
#			cave_orcs(attachee, gcr)
#			if (gcr >= 6):
#				cav_orcs_enc1(attachee)
#			else:
#				cav_orcs_enc(attachee, gcr)
			
	return RUN_DEFAULT
	
def Big_Three_Ambush(spawner):
	leader = game.party[0]
	n = 0
	while (n < 1): # Barbarian Gnolls
		npc = game.obj_create(14078, random_location(487, 490, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	n = 0
	while (n < 2): # Gnolls
		npc = game.obj_create(14079, random_location(487, 490, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	n = 0
	while (n < 3): # Gnolls2
		npc = game.obj_create(14080, random_location(487, 490, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	n = 0
	while (n < 3): # Mage Sniper Crossbowmen
		npc = game.obj_create(14314, random_location(487, 490, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	npc = game.obj_create(14642, random_location(487, 490, 5)) # Slightly better archer.
	npc.turn_towards(leader)
		
	zert = game.obj_create(14045, location_from_axis( 484, 488 ))
	remove_dagger(zert)
	zert.turn_towards(leader)
	turuko = game.obj_create(14041, location_from_axis( 487, 490 ))
	remove_dagger(turuko)
	turuko.turn_towards(leader)
	kobort = game.obj_create(14028, location_from_axis( 487, 490 ))
	remove_dagger(kobort)
	kobort.turn_towards(leader)
	leader.turn_towards(kobort)
	StopCombat(kobort, 0)
	leader.begin_dialog( kobort, 600 )

def remove_dagger(npc):
	dagger = npc.item_find_by_proto(4060)
	while dagger != OBJ_HANDLE_NULL:
		dagger.destroy()
		dagger = npc.item_find_by_proto(4060)
	return

def money_script():
	# money script by Agetian
	game.party[0].money_adj(-game.party[0].money_get())   # strip all money
	game.party[0].money_adj(game.random_range(20000, 30000))	#add 200-300 gold pocket money
	for pc in game.party:
		# get rid of an extra longsword (4036)
		lsw = pc.item_find_by_proto(4036)
		if lsw != OBJ_HANDLE_NULL:
			lsw.destroy()
		# get rid of an extra scimitar (4045)
		scm = pc.item_find_by_proto(4045)
		if scm != OBJ_HANDLE_NULL:
			scm.destroy()

		# give money per PHB (for each class)
		if (pc.stat_level_get(stat_level_barbarian) > 0):
			pc.money_adj(game.random_range(4000, 16000))
		if (pc.stat_level_get(stat_level_bard) > 0):
			pc.money_adj(game.random_range(4000, 16000))
		if (pc.stat_level_get(stat_level_cleric) > 0):
			pc.money_adj(game.random_range(5000, 20000))
		if (pc.stat_level_get(stat_level_druid) > 0):
			pc.money_adj(game.random_range(2000, 8000))
		if (pc.stat_level_get(stat_level_fighter) > 0):
			pc.money_adj(game.random_range(6000, 24000))
		if (pc.stat_level_get(stat_level_monk) > 0):
			pc.money_adj(game.random_range(5000, 20000))
		if (pc.stat_level_get(stat_level_paladin) > 0):
			pc.money_adj(game.random_range(6000, 24000))
		if (pc.stat_level_get(stat_level_ranger) > 0):
			pc.money_adj(game.random_range(6000, 24000))
		if (pc.stat_level_get(stat_level_rogue) > 0):
			pc.money_adj(game.random_range(5000, 20000))
		if (pc.stat_level_get(stat_level_sorcerer) > 0):
			pc.money_adj(game.random_range(3000, 12000))
		if (pc.stat_level_get(stat_level_wizard) > 0):
			pc.money_adj(game.random_range(3000, 12000))
	return

def random_location(x, y, range):
	print 'Generating Location'
	if (game.random_range(1,2) == 1) :
		loc_x = ( x + game.random_range(1,range) )
	else:
		loc_x = ( x - game.random_range(1,range) )
	if (game.random_range(1,2) == 1 ):
		loc_y = ( y + game.random_range(1,range) )
	else:
		loc_y = ( y - game.random_range(1,range) )
	location = location_from_axis( loc_x, loc_y )
	return location

def is_follower(name):
	for obj in game.party:
		if (obj.name == name):
			return 1
	return 0

def bin_thr_dun_tht():
	#bin_thr_dun_tht script by rufnredde - Function: Sets ggf 286 if you have been to level 2 and resets ggf 287 enter temple blindfolded if you have entered/exited normally ggv 55
	game.global_flags[286] = 1
	if (game.global_flags[287] == 1 and game.global_vars[55] >= 2 and game.global_flags[286] == 1):
		game.global_flags[287] = 0
	return
	
def give_directions():
	#give_directions script by rufnredde - Function: Checks to make sure you have entered the temple ggv 55 other than blindfolded ggf 287 and have been to level 2 ggf 286 allows you to give directions out of the temple
	if (game.global_flags[287] == 0 and game.global_vars[55] <= 1 and game.global_flags[286] == 0):
		game.global_vars[55] = 1
	if (game.global_flags[287] == 0 and game.global_vars[55] <=1 and game.global_flags[286] == 1):
		game.global_vars[55] = game.global_vars[55] + 1
	if (game.global_flags[287] == 1 and game.global_vars[55] <= 1):
		game.global_vars[55] = game.global_vars[55] + 1
	if (game.global_flags[287] == 1) and ((game.quests[53].state == qs_completed or game.quests[53].state == qs_accepted) or (game.global_flags[996] == 1 or game.global_flags[998] == 1)):
		game.global_vars[55] = game.global_vars[55] + 2
	if (game.global_flags[287] == 1 and game.global_vars[55] >= 2):
		game.global_flags[287] = 0
	return
	
def gob_zap():
	# gob_zap script by rufnredde get rid of goblin (14185) with Black Jays wifes ring in homlet
	gob_zapper = game.obj_create(14460, location_from_axis( 471, 261 ))
	gob = find_npc_near(gob_zapper, 14185)
	gob.destroy()
	gob_zapper.destroy()
	return
	
def frog_ring():
	# frog_ring script by rufnredde add Black Jays Wifes Ring to Moathouse Frog For Neutral Parties
	if ( (game.party_alignment == LAWFUL_NEUTRAL or game.party_alignment == CHAOTIC_NEUTRAL or game.party_alignment == TRUE_NEUTRAL) and game.global_flags[410] == 0):
		ring_givr = game.obj_create(14460, location_from_axis( 465, 499 ))
		frogi = find_npc_near(ring_givr, 14057 )
		create_item_in_inventory( 6270, frogi )
		game.global_flags[410] = 1
		ring_givr.destroy()
	return
	
def drow_ring(spawner):
	# drow_ring script by rufnredde add Black Jays Wifes Ring and Drow to Deklo Grove for Evil Parties
	if ((game.party_alignment == LAWFUL_EVIL or game.party_alignment == CHAOTIC_EVIL or game.party_alignment == NEUTRAL_EVIL) and game.global_flags[410] != 1):
		leader = game.party[0]
		Al = game.obj_create(14677, location_from_axis( 467, 451 ))
		create_item_in_inventory( 6270, Al )
		create_item_in_inventory( 9999, Al )
		Al.turn_towards(leader)
		leader.turn_towards(Al)
		StopCombat(Al, 0)
		leader.begin_dialog( Al, 100 )
		game.global_flags[410] = 1
	return
	
def kob_ring_party(spawner):
	# Kob_ring_party script by rufnredde, spawner on loan from Cerulean the Blue add Black Jays Wifes Ring and Kobolds to Deklo Grove for Good Parties
	if ((game.party_alignment == LAWFUL_GOOD or game.party_alignment == CHAOTIC_GOOD or game.party_alignment == NEUTRAL_GOOD) and game.global_flags[410] != 1):
		leader = game.party[0]
		npc = game.obj_create(14641, location_from_axis(469, 454)) #Kobold Sgt
		create_item_in_inventory( 6270, npc )
		npc.turn_towards(leader)
		npc.attack(leader)
		game.global_flags[410] = 1
		
	n = 0
	while (n < 6): #kobold
		npc = game.obj_create(14640, random_location(467, 451, 5))
		npc.turn_towards(leader)
		n = n + 1
		
	return

def cave_orcs(spawner, gcr):
	 #cave_orcs script by rufnredde, spawner on loan from Cerulean the Blue add an ogre cave prespawn with a band of orcs
	leader = game.party[0]
	oleader = game.obj_create( 14744, location_from_axis(483, 459)) #Orc Leader
	oleader.turn_towards(leader)
	oleader.attack(leader)
	if (gcr >= 6):
		shaman = game.obj_create( 14743, location_from_axis(504, 460)) #Orc Shaman
		ass = game.obj_create( 14747, location_from_axis(509, 473)) #Half-Orc Assassin
	else:
		shaman = game.obj_create( 14748, location_from_axis(504, 460)) #Orc Shaman Lite
		ass = game.obj_create( 14751, location_from_axis(509, 473)) #Half-Orc Assassin Lite
	shaman.turn_towards(leader)
	shaman.attack(leader)
	ass.turn_towards(leader)
	ass.attack(leader)
	booty = game.obj_create( 1027, location_from_axis(485, 449)) #chest
	create_item_in_inventory( 12043, booty )
	create_item_in_inventory( 12043, booty )
	create_item_in_inventory( 12043, booty )
	create_item_in_inventory( 12040, booty )
	create_item_in_inventory( 12039, booty )
	create_item_in_inventory( 8014, booty )
	create_item_in_inventory( 8014, booty )
	create_item_in_inventory( 8014, booty )
	game.global_flags[411] = 1
	if (game.global_vars[56] < 9 and game.quests[53].state > 0):
		game.quests[62].state = qs_botched
	
	return
	
def cav_orcs_enc(spawner, gcr):
	leader = game.party[0]
	if (gcr <= 2.5):
		numb = 1
		numb2 = 2
	else:
		numb = 2
		numb2 = 3
	n = 0
	while (n < numb): #Orc Mage-Sniper Archers
		npc = game.obj_create(14750, random_location(494, 454, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1
	n = 0
	while (n < numb2): #Orc Fighters
		npc = game.obj_create(14749, random_location(489, 473, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1
	n = 0
	while (n < numb): #Orc Leader Fighter Guards
		npc = game.obj_create(14749, random_location(482, 466, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1
	n = 0
	while (n < numb): #Orc Shaman Fighter Guards
		npc = game.obj_create( 14749, random_location(501, 457, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1

	return
	
def cav_orcs_enc1(spawner):
	leader = game.party[0]
	n = 0
	while (n < 2): #Orc Mage-Sniper Archers
		npc = game.obj_create(14746, random_location(494, 454, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1
	n = 0
	while (n < 3): #Orc Fighters
		npc = game.obj_create(14745, random_location(489, 473, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1
	n = 0
	while (n < 2): #Orc Leader Fighter Guards
		npc = game.obj_create(14745, random_location(482, 466, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1
	n = 0
	while (n < 2): #Orc Shaman Fighter Guards
		npc = game.obj_create( 14745, random_location(501, 457, 2))
		npc.item_wield_best_all()
		npc.turn_towards(leader)
		npc.attack(leader)
		n = n + 1
		
	return

