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

import wolfpack
from spells.spellfunctions import *
from math import floor

def onSpellSuccess( char, spell, type, target ):
	# target check is done at c++ code
	if not target.pos:
		return
	x = target.pos.x
	y = target.pos.y
	z = target.pos.z
	dx = abs( char.pos.x - x )
	dy = abs( char.pos.y - y )
	pos = ( x, y )
	# target LoS check is done in c++ core
	dir = int( char.directionto( pos ) )
	if( ( dir < 0 ) or ( dir > 7 ) ):
		char.socket.sysmessage( "Wrong target!" )
		return 1
	# we have only 2 directions
	if( dir == 1 ):
		if( dx > dy ):
			dir = 2
		else:
			dir = 0
	elif( dir == 3 ):
		if( dx > dy ):
			dir = 2
		else:
			dir = 4
	elif( dir == 5 ):
		if( dx > dy ):
			dir = 6
		else:
			dir = 4
	elif( dir == 7 ):
		if( dx > dy ):
			dir = 6
		else:
			dir = 0
	dir = floor( dir / 2 )
	dir = 1 - dir % 2
	duration = floor( 24 + 1.2 * char.skill[ MAGERY ] / 10 )
	for i in range( 0, 5 ):
		stone = additem( "80" )
		if( dir == 0 ):	# North-South
			stone.moveto( x, y - 2 + i )
		else:
			stone.moveto( x - 2 + i, y )
		stone.update()
		stone.addtimer( "wallofstone.restore", duration, ( stone.serial, ) )
	# effects
	char.soundeffect( 0x1f7 )
	return 1

def restore( args ):
	if( len( args ) < 1 ):
		return
	serial = args[0]
	stone = finditem( serial )
	if not stone:
		return
	stone.delete()
	return 1
