//***************************************************************************
//***** Mass Sleep
//***** Sleeps all units in an area.
//*****
//***** Area = 100 + 100 * level
//***** Duration = Change values in 'A001'
//***************************************************************************
scope MassSleep initializer Init
//***************************************************************************
//******************** SETUP ************************************************
//***************************************************************************
globals
private constant integer MAIN_SPELL_ID = 'A000'
private constant integer DUMMY_SPELL_ID = 'A001'
private constant integer DUMMY_ID = 'h000'
endglobals
private function Range takes integer level returns real
return 100. + level * 100. //see the "." after the numbers; numbers with "." are always real numbers
endfunction
private function Targets takes unit caster, unit target returns boolean
return IsUnitEnemy(target, GetOwningPlayer(caster)) and ( GetWidgetLife( target ) > 0.405 ) and ( IsUnitType( target, UNIT_TYPE_STRUCTURE ) == false ) and ( IsUnitType( target, UNIT_TYPE_MAGIC_IMMUNE ) == false )
//return IsUnitEnemy( target, GetOwningPlayer( caster ) ) and ( GetWidgetLife( target ) > 0.405 ) and ( IsUnitType( target, UNIT_TYPE_STRUCTURE ) == false ) and ( IsUnitType( target, UNIT_TYPE_MAGIC_IMMUNE ) == false ) and IsUnitEnemy( target, GetOwningPlayer( GetTriggerUnit() ) )
endfunction
//***************************************************************************
//****************** SETUP END **********************************************
//***************************************************************************
globals
private group victims
private boolexpr chooseVictims
private unit tempCaster
endglobals
//===========================================================================
private function Conditions takes nothing returns boolean
return GetSpellAbilityId() == MAIN_SPELL_ID
endfunction
//===========================================================================
private function Pick takes nothing returns boolean
//replaced "GetFilterUnits()" with "GetFilterUnit()"
//notice that this function only evaluates 1 unit at a time
return Targets( tempCaster, GetFilterUnit() )
endfunction
//===========================================================================
private function Actions takes nothing returns nothing
local unit caster = GetTriggerUnit()
//I added this new variable, to make code easier to read
local location spellLoc = GetSpellTargetLoc()
local real spellX = GetLocationX( spellLoc )
local real spellY = GetLocationY( spellLoc )
//You are using X and Y for SpellLoc, good job student!
//Working with X and Y is a lot faster
//then working with location. Always use X and Y if you can.
local integer level = GetUnitAbilityLevel( caster, MAIN_SPELL_ID )
local unit temp
local unit dummy
//local location unitLoc -> We don't need this anymore! X and Y for the win!
set tempCaster = caster
call GroupEnumUnitsInRange( victims, spellX, spellY, Range( level ), chooseVictims )
loop
//set temp = FirstOfGroup( g ) -> group "g" does not exist !
set temp = FirstOfGroup( victims )
//set unitLoc = GetUnitLoc( temp ) -> We don't need this anymore! X and Y for the win!
exitwhen ( temp == null )
//call GroupRemoveUnit( group, temp) -> you are working with a group called "victims". Don't forget its name !
call GroupRemoveUnit( victims, temp)
//set dummy = CreateNUnitsAtLoc( 1, DUMMY_ID, GetOwningPlayer( caster ), unitLoc, 0.00 )
//CreateNUnitsAtLoc -> returns a "group of several units" and not a single "unit"
//If you only want to create 1 unit, use CreateUnit(), it is a lot faster and better!
set dummy = CreateUnit(GetOwningPlayer(caster), DUMMY_ID, GetUnitX(temp), GetUnitY(temp), 0.00)
call UnitAddAbility( dummy, DUMMY_SPELL_ID )
//this line is new. Can you tell me what it does?
call SetUnitAbilityLevel(dummy, DUMMY_SPELL_ID, level)
//this stuff is Ok, but they are Bj's (evil in this case)
// I will optmize it
//call IssueTargetOrderBJ( dummy, "sleep", temp )
//call UnitApplyTimedLifeBJ( 1.50, 'BTLF', dummy )
call IssueTargetOrder(dummy, "sleep", temp)
call UnitApplyTimedLife(dummy, 'BTLF', 5.00)
//there you go, see how easy it was? Remember to always use "Function List" menu so you can see if BJ functions are evil or not!
endloop
call RemoveLocation( spellLoc )
//call RemoveLocation( unitLoc ) -> if we don't create it, we don't have to destroy it!
set spellLoc = null
//set unitLoc = null -> if we don't create it, we also don't beed to null it !
set caster = null
//added be Flame
set dummy = null //don't forget to null the dummy unit!
endfunction
//===========================================================================
private function Init takes nothing returns nothing
local trigger MassSleepTrg = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ( MassSleepTrg, EVENT_PLAYER_UNIT_SPELL_EFFECT )
call TriggerAddCondition( MassSleepTrg, Condition( function Conditions ) )
call TriggerAddAction( MassSleepTrg, function Actions )
//setting globals
//good job!
set victims = CreateGroup()
set chooseVictims = Condition( function Pick )
//Preloading ability
//good job!
set bj_lastCreatedUnit = CreateUnit( Player( PLAYER_NEUTRAL_PASSIVE ), DUMMY_ID, 0, 0, 0 )
//call UnitAddAbility( bj_lastCreatedUnit, SPELL_ID ) -> SPELL_ID does not exist! =P
call UnitAddAbility( bj_lastCreatedUnit, DUMMY_SPELL_ID )
call KillUnit( bj_lastCreatedUnit)
//Always comment your codes, it makes them easier to read !
endfunction
endscope