#################################################################
#   )      (\_     # WOLFPACK 13.0.0 Scripts                    #
#  ((    _/{  "-;  # Created by: khpae                          #
#   )).-" {{ ;"`   # Revised by:                                #
#  ( (  ;._ \\ ctr # Last Modification: Created                 #
#################################################################

import wolfpack
from wolfpack.consts import *
from wolfpack.utilities import *
from math import floor

#FIXME : poison damage interval
interval = 1000

def onSpellSuccess( char, spell, type, target ):
	# target check is done at c++ code
	defender = target.char
	if not defender:
		return

	agression( char, defender, spell )
	attacker = char
	reflected = magicReflection( char, defender )
	if( reflected == 1 ):
		attacker = defender
		defender = char
	# effects
	resist = resistSpell( attacker, defender, spell )

	defender.soundeffect( 0x206 )
	defender.effect( 0x374a )

	# disturb medi
	defender.disturb()
	# unfreeze - needed to be implemented

	magery = floor( char.skill[ MAGERY ] / 10 )
	duration = floor( ( 24 + magery * 1.2 ) * 1000 )
	poisonLevel = 1
	if( magery < 50 ):
		poisonLovel = 1
	elif( magery < 98 ):
		poisonLevel = 2
	else:
		poisonLevel = 3
	if( resist == 1 ):
		if( poisonLevel == 1 ):
			poisonLevel = 3
		elif( poisonLevel == 3 ):
			poisonLevel = 1

	# if poison elemental, this spell will heal it.
	defender.settag( "poisoned", poisonLevel )
	# FIXME : poison decrease hp every ? second
	defender.addtimer( "poison.restore", interval, ( defender.serial, duration ) )
	defender.socket.sysmessage( "You have been poisoned by %s!" % attacker.name )
	defender.update()
	return 1

def restore( args ):
	if( len( args ) < 1 ):
		return
	serial = args[0]
	char = findchar( serial )
	if not char:
		return 1
	if not char.hastag( "poisoned" ):
		return 1
	if( len( args ) < 2 ):
		char.socket.sysmessage( "poison script error. contact GM" )
		char.removetag( "poisoned" )
		return
	duration = int( args[1] )
	duration -= interval
	if( duration < 0 ):
		char.removetag( "poisoned" )
		char.update()
	else:
		poisonLevel = int( char.gettag( "poisoned" ) )
		damage = poisonLevel * 5
		char.health -= damage
		char.addtimer( "poison.restore", interval, ( serial, duration ) )
		char.updatehealth()
	return 1
