#----------------------------------------------------------------------------
# This file is part of WoWEmu server, Copyright (c) by wad, 2002-2005.
#
#############################################################################

set DEBUG 0

namespace eval WoWEmu {
   proc CalcXP { killer victim } {
        set victim_creature_type [GetCreatureType $victim]
        set killer_level [GetLevel $killer]
        set victim_level [GetLevel $victim]
   if {$victim_creature_type ==8} { return 0 }
   if {$killer_level >= 60} { return 0 }
   set lvldiff [expr {$victim_level-$killer_level}]
   if {$lvldiff <= -12} { return 0 }

   #  This formula below gives exp approximately 80% of the rested state experience
   #  as per Blizzard Official. To have it give 100% of the rested blizzard rate,
   #  change the 4 to a 5 in the expression below.
   #  set xp [expr {((4 * $victim_level) + 47)}]

   set xp [expr {((4 * $victim_level) + 47)}]
   if {$lvldiff < 0} { set xp [expr {$xp + ($xp*($lvldiff)/12)}] }
   if {$lvldiff > 0} { set xp [expr {$xp + ($xp/20*$lvldiff)}] }

   #  This expression prevent people from killing mobs higher than level 10
   #  to gain huge experience. Experience is 

   if {$lvldiff > 10} { 

	set lvldiff 10
	set xp [expr {((($killer_level + 10) * 4) + 94 + (($killer_level + 10) * 4/20*$lvldiff))}] 
	}

   return $xp
    } 
   proc CalcReputation { killer victim } {
        AddReputation $killer $victim -100
    }
    proc DamageReduction { player mob armor } {
        set level [GetLevel $mob]
	set plevel [GetLevel $player]
	set factor [expr {($plevel-$level-5)*.6}] 
        set denominator [expr {($level *125.0) +400.0 +$armor}]
        set damage_reduction [expr { $armor /$denominator}]
	if {$level >35} {set factor [expr {$factor*3}]}
	if {$factor < 0} {
		set damage_reduction [expr {$damage_reduction + ($damage_reduction *$factor)}] 
	}
        if {$damage_reduction <-8} { set damage_reduction-8 }
        if {$damage_reduction >1.0} { set damage_reduction 1.0 }
        return $damage_reduction
    }
}

namespace eval SpellEffects {
    proc SPELL_EFFECT_TELEPORT_UNITS { to from spellid } {
        puts "spell effect teleport $spellid"
        switch $spellid {
		31      { Teleport $to 1 -994.248169 -3830.104004 7.0 }
		33      { Teleport $to 0 -13923.823242 1313.069580 6.0 }
		34      { Teleport $to 0 -13828.068359 1299.230347 7.0 }
		35      { Teleport $to 0 -14002.620117 1271.615479 3 }
		427     { Teleport $to 0 2889.91 -811.148 160.332 }
		428     { Teleport $to 0 -11025.184570 1497.860596 43.195259 }
		442     { Teleport $to 0 -8929 -116 83 }
		444     { Teleport $to 0 -11409.933594 1968.416504 10.427084 }
		445     { Teleport $to 0 -10413.885742 -1136.884888 24.128809 }
		446     { Teleport $to -10531.167969 296.656219 30.964926 }
		665     { Teleport $to 0 -8960.140625 516.265686 96.356819 }
		1936    { Teleport $to 0 -6132 384 395.5 }
		3561    { Teleport $to 0 -8960.140625 516.265686 96.356819 }
		3562    { Teleport $to 0 -5032 -819 495 }
		3581    { Teleport $to 0 -5032 -819 495 }
		3563    { Teleport $to 0 1819.708374 238.789505 60.532143 }
		3577    { Teleport $to 0 1819.708374 238.789505 60.532143 }
		3565    { Teleport $to 1 9951.792969 2145.915771 1327.724854 }
		3578    { Teleport $to 1 9951.792969 2145.915771 1327.724854 }
		3566    { Teleport $to 1 -1391.0 140.0 22.478 }
		3579    { Teleport $to 1 -1391.0 140.0 22.478 }
		3567    { Teleport $to 1 1552.499268 -4420.658691 8.948024 }
		3580    { Teleport $to 1 1552.499268 -4420.658691 8.948024 }
		11362   { Teleport $to 90 -330 -2 -151 }
		11409   { Teleport $to 0 -14319.114258 444.477905 23.054321 }
		12509   { Teleport $to 1 4232.007813 -7802.841309 4.689385 }
		12510   { Teleport $to 1 4232.007813 -7802.841309 4.689385 }
		17334   { Teleport $to 0 -8960.140625 516.265686 96.356819 }
		17607   { Teleport $to 30 666.296021 -29.262074 51.118435 }
		17608   { Teleport $to 1 9951.792969 2145.915771 1327.724854 }
		17609   { Teleport $to 1 1552.499268 -4420.658691 8.948024 }
		17610   { Teleport $to 30 -1325.133545 -290.885803 90.726784 }
		17611   { Teleport $to 0 1819.708374 238.789505 60.532143 }
		18960   { Teleport $to 1 7980.842285 -2501.763428 487.576508 } 

		8690    { 
			set ss [GetBindpoint $from]
			set map [lindex $ss 0]
			set x [lindex $ss 1]
			set y [lindex $ss 2]
			set z [lindex $ss 3]
			Teleport $from $map $x $y $z
		}

            default { puts "Can't find teleport for spellid=$spellid" }
        }
    }
}

#############################################################################

set err ""
foreach script [glob scripts/tcl/*.tcl] { 
        if { $DEBUG } {
            source $script
        } else {
            if { [catch {source $script}] } {
                set err "$err\n Can't load script \[$script\]"
            }
        }
}

foreach dir [glob -type d scripts/tcl/*] { 
    foreach script [glob -directory $dir *.tcl] {
        if { $DEBUG } {
            source $script
        } else {
            if { [catch {source $script}] } {
                set err "$err\n Can't load script \[$script\]"
            }
        }
    }
}

if { $err == "" } {
    return "Init... done."
} else {
    return $err
}
