from toee import *

def OnBeginSpellCast( spell ):
	print "Charm Person OnBeginSpellCast"
	print "spell.target_list=", spell.target_list
	print "spell.caster=", spell.caster, " caster.level= ", spell.caster_level
	game.particles( "sp-enchantment-conjure", spell.caster )

def	OnSpellEffect( spell ):
	print "Charm Person OnSpellEffect"

	spell.duration = 600 * spell.caster_level
	target_item = spell.target_list[0]

	if not target_item.obj.is_friendly( spell.caster ):
		if (target_item.obj.is_category_type( mc_type_humanoid )) and (target_item.obj.get_size < STAT_SIZE_LARGE):

			if not target_item.obj.saving_throw_spell( spell.dc, D20_Save_Will, D20STD_F_NONE, spell.caster, spell.id, spell.id ):
				# saving throw unsuccessful
				target_item.obj.float_mesfile_line( 'mes\\spell.mes', 30002 )

				spell.caster.ai_follower_add( target_item.obj )

				target_item.obj.condition_add_with_args( 'sp-Charm Person', spell.id, spell.duration, target_item.obj.hit_dice_num )
				target_item.partsys_id = game.particles( 'sp-Charm Person', target_item.obj )

				# add target to initiative, just in case
				target_item.obj.add_to_initiative()
				game.update_combat_ui()

			else:

				# saving throw successful
				target_item.obj.float_mesfile_line( 'mes\\spell.mes', 30001 )

				game.particles( 'Fizzle', target_item.obj )
				spell.target_list.remove_target( target_item.obj )

		else:
			# not a humanoid
			target_item.obj.float_mesfile_line( 'mes\\spell.mes', 30000 )
			target_item.obj.float_mesfile_line( 'mes\\spell.mes', 31004 )

			game.particles( 'Fizzle', target_item.obj )
			spell.target_list.remove_target( target_item.obj )

	else:

		# can't target friendlies
		game.particles( 'Fizzle', target_item.obj )
		spell.target_list.remove_target( target_item.obj )

	spell.spell_end( spell.id )

def OnBeginRound( spell ):
	print "Charm Person OnBeginRound"

def OnEndSpellCast( spell ):
	print "Charm Person OnEndSpellCast"