function Catch_Pokemon_Conditions takes nothing returns boolean
//the conditions of the trigger simply converted from GUI to Jass (It's not that optimized but why would I care that much ^.^?)
//ability being cast equal to Catch Pokemon
if ( not ( GetSpellAbilityId() == 'A007' ) ) then
return false
endif
//owner of target unit of ability being cast equal to neutral hostile
if ( not ( GetOwningPlayer(GetSpellTargetUnit()) == Player(PLAYER_NEUTRAL_AGGRESSIVE) ) ) then
return false
endif
//number of units in units in playable map area owned by owner of casting unit less then 7
if ( not ( CountUnitsInGroup(GetUnitsInRectOfPlayer(GetPlayableMapRect(), GetOwningPlayer(GetSpellAbilityUnit()))) < 7 ) ) then
return false
endif
return true
endfunction
//skip this function when reading this trigger for the first time, this function should be checked during the actions in the function underneath this one...
function QueueAnimation takes nothing returns nothing
local timer t = GetExpiredTimer() //set t is the timer being expired
local integer id = GetHandleId(t) //set id is the id of the expiring timer
local unit u = LoadUnitHandle(udg_Catch_Pokemon, id, 1) //load the pokeball unit from the hashtable where the parent key is the id of the expiring timer
local real duration = LoadReal(udg_Catch_Pokemon, id, 2) //load the real value from the hashtable where the parent key is the id of the expiring timer
if duration > 0 then //if the real value being loaded is bigger then 0, reset the pokeball unit animation and decrease the value being loaded by 1
call SetUnitAnimation(u, "spell")
call SaveReal(udg_Catch_Pokemon, id, 2, duration - 1)
else //if the real value being loaded is 0 (which means the timer was executed 6 times) then destroy the timer and flush the hashtable
call PauseTimer(t)
call DestroyTimer(t)
call FlushChildHashtable(udg_Catch_Pokemon, id)
endif
set u = null
endfunction
function Catch_Pokemon_Actions takes nothing returns nothing
local unit pokeball //the pokeball unit being created
local unit caught_pokemon = GetSpellTargetUnit() //the pokemon being targeted by the spell
local unit catching_trainer = GetSpellAbilityUnit() //the unit casting the ability
local real unit_facing //for calculating the angle which the pokeball unit faces, this is for optimization purposes
local timer t //a timer which will fire every 0.47 seconds for the pokeball animation to be reset. This will be done 6 times in a row before the timer is destroyed.
set udg_Catch_Pokemon = InitHashtable() //a hashtable to make sure the spell becomes MUI
//if the catching trainer has a pokeball and the unit being caught has less then or equal to 10% hp then
if (UnitHasItemOfTypeBJ(catching_trainer, 'I001')) and (GetUnitLifePercent(caught_pokemon) <=10) then
//remove the pokeball item from the catching trainer
call RemoveItem(GetItemOfTypeFromUnitBJ(catching_trainer, 'I001'))
//set the unit facing to the position of catching trainer
set unit_facing = bj_RADTODEG * Atan2(GetUnitY(caught_pokemon) - GetUnitY(catching_trainer), GetUnitX(caught_pokemon) - GetUnitX(catching_trainer))
//create the pokeball unit
set pokeball = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), 'N002', GetUnitX(catching_trainer), GetUnitY(catching_trainer), unit_facing)
//add a generic expiration timer of 2.94 seconds to the pokeball unit
call UnitApplyTimedLife(pokeball, 'BTLF', 2.94)
//hide the caught pokemon
call ShowUnit(caught_pokemon, false)
//set the animation for the pokeball unit to "spell"
call SetUnitAnimation(pokeball, "spell" )
//create the timer
set t = CreateTimer()
//start the timer and make it a continious timer which expires every 0.47 seconds. When the timer expires function QueueAnimation is executed (see function QueueAnimation)
call TimerStart(t, 0.47, true, function QueueAnimation)
//save the pokeball unit in a hashtable where the parent key is the id of the timer we just created. The child key is 1.
call SaveUnitHandle(udg_Catch_Pokemon, GetHandleId(t), 1, pokeball)
//save a real value of 6 in the hashtable under the same parent key with child key 2.
call SaveReal(udg_Catch_Pokemon, GetHandleId(t), 2, 6)
//set the owner of the pokemon being caught to the trainer catching the pokemon
call SetUnitOwner(caught_pokemon, GetOwningPlayer(catching_trainer), true )
//set the position of the caught pokemon to random point in Region 007 <gen>
call SetUnitPosition(caught_pokemon, GetLocationX(Location(GetRandomReal(GetRectMinX(gg_rct_Rect_007), GetRectMaxX(gg_rct_Rect_007)), GetRandomReal(GetRectMinY(gg_rct_Rect_007), GetRectMaxY(gg_rct_Rect_007)))), GetLocationY(Location(GetRandomReal(GetRectMinX(gg_rct_Rect_007), GetRectMaxX(gg_rct_Rect_007)), GetRandomReal(GetRectMinY(gg_rct_Rect_007), GetRectMaxY(gg_rct_Rect_007)))))
//show the caught pokemon
call ShowUnit(caught_pokemon, true)
//else if the catching trainer has a greatball and the unit being caught has less then or equal to 15% hp then
elseif (UnitHasItemOfTypeBJ(catching_trainer, 'I002')) and (GetUnitLifePercent(caught_pokemon) <=15) then
//same actions as in the first if block except a different item is removed from the catching trainer
call RemoveItem(GetItemOfTypeFromUnitBJ(catching_trainer, 'I002'))
set unit_facing = bj_RADTODEG * Atan2(GetUnitY(caught_pokemon) - GetUnitY(catching_trainer), GetUnitX(caught_pokemon) - GetUnitX(catching_trainer))
set pokeball = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), 'N002', GetUnitX(catching_trainer), GetUnitY(catching_trainer), unit_facing)
call UnitApplyTimedLife(pokeball, 'BTLF', 2.94)
call ShowUnit(caught_pokemon, false)
call SetUnitAnimation(pokeball, "spell" )
set t = CreateTimer()
call TimerStart(t, 0.47, true, function QueueAnimation)
call SaveUnitHandle(udg_Catch_Pokemon, GetHandleId(t), 1, pokeball)
call SaveReal(udg_Catch_Pokemon, GetHandleId(t), 2, 5)
call SetUnitOwner(caught_pokemon, GetOwningPlayer(catching_trainer), true )
call SetUnitPosition(caught_pokemon, GetLocationX(Location(GetRandomReal(GetRectMinX(gg_rct_Rect_007), GetRectMaxX(gg_rct_Rect_007)), GetRandomReal(GetRectMinY(gg_rct_Rect_007), GetRectMaxY(gg_rct_Rect_007)))), GetLocationY(Location(GetRandomReal(GetRectMinX(gg_rct_Rect_007), GetRectMaxX(gg_rct_Rect_007)), GetRandomReal(GetRectMinY(gg_rct_Rect_007), GetRectMaxY(gg_rct_Rect_007)))))
call ShowUnit(caught_pokemon, true)
//else if the catching trainer has a ultraball and the unit being caught has less then or equal to 20% hp then
elseif (UnitHasItemOfTypeBJ(catching_trainer, 'I003')) and (GetUnitLifePercent(caught_pokemon) <=20) then
//same actions as in the first if block except a different item is removed from the catching trainer
call RemoveItem(GetItemOfTypeFromUnitBJ(catching_trainer, 'I003'))
set unit_facing = bj_RADTODEG * Atan2(GetUnitY(caught_pokemon) - GetUnitY(catching_trainer), GetUnitX(caught_pokemon) - GetUnitX(catching_trainer))
set pokeball = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), 'N002', GetUnitX(catching_trainer), GetUnitY(catching_trainer), unit_facing)
call UnitApplyTimedLife(pokeball, 'BTLF', 2.94)
call ShowUnit(caught_pokemon, false)
call SetUnitAnimation(pokeball, "spell" )
set t = CreateTimer()
call TimerStart(t, 0.47, true, function QueueAnimation)
call SaveUnitHandle(udg_Catch_Pokemon, GetHandleId(t), 1, pokeball)
call SaveReal(udg_Catch_Pokemon, GetHandleId(t), 2, 5)
call SetUnitOwner(caught_pokemon, GetOwningPlayer(catching_trainer), true )
call SetUnitPosition(caught_pokemon, GetLocationX(Location(GetRandomReal(GetRectMinX(gg_rct_Rect_007), GetRectMaxX(gg_rct_Rect_007)), GetRandomReal(GetRectMinY(gg_rct_Rect_007), GetRectMaxY(gg_rct_Rect_007)))), GetLocationY(Location(GetRandomReal(GetRectMinX(gg_rct_Rect_007), GetRectMaxX(gg_rct_Rect_007)), GetRandomReal(GetRectMinY(gg_rct_Rect_007), GetRectMaxY(gg_rct_Rect_007)))))
call ShowUnit(caught_pokemon, true)
//else if the catching trainer has a masterball
elseif (UnitHasItemOfTypeBJ(catching_trainer, 'I004')) then
//same actions as in the first if block except a different item is removed from the catching trainer
call RemoveItem(GetItemOfTypeFromUnitBJ(catching_trainer, 'I004'))
set unit_facing = bj_RADTODEG * Atan2(GetUnitY(caught_pokemon) - GetUnitY(catching_trainer), GetUnitX(caught_pokemon) - GetUnitX(catching_trainer))
set pokeball = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), 'N002', GetUnitX(catching_trainer), GetUnitY(catching_trainer), unit_facing)
call UnitApplyTimedLife(pokeball, 'BTLF', 2.94)
call ShowUnit(caught_pokemon, false)
call SetUnitAnimation(pokeball, "spell" )
set t = CreateTimer()
call TimerStart(t, 0.47, true, function QueueAnimation)
call SaveUnitHandle(udg_Catch_Pokemon, GetHandleId(t), 1, pokeball)
call SaveReal(udg_Catch_Pokemon, GetHandleId(t), 2, 5)
call SetUnitOwner(caught_pokemon, GetOwningPlayer(catching_trainer), true )
call SetUnitPosition(caught_pokemon, GetLocationX(Location(GetRandomReal(GetRectMinX(gg_rct_Rect_007), GetRectMaxX(gg_rct_Rect_007)), GetRandomReal(GetRectMinY(gg_rct_Rect_007), GetRectMaxY(gg_rct_Rect_007)))), GetLocationY(Location(GetRandomReal(GetRectMinX(gg_rct_Rect_007), GetRectMaxX(gg_rct_Rect_007)), GetRandomReal(GetRectMinY(gg_rct_Rect_007), GetRectMaxY(gg_rct_Rect_007)))))
call ShowUnit(caught_pokemon, true)
endif
set pokeball = null
set caught_pokemon = null
set catching_trainer = null
endfunction
//===========================================================================
function InitTrig_Catch_Pokemon takes nothing returns nothing
//the actual trigger being created. Here all events, conditions and actions gets added to the trigger
local trigger t = CreateTrigger()
local integer index
set index = 0
loop
//add the event: A unit Starts the effect of an ability for each player
call TriggerRegisterPlayerUnitEvent(t, Player(index), EVENT_PLAYER_UNIT_SPELL_EFFECT, null)
set index = index + 1
exitwhen index == bj_MAX_PLAYER_SLOTS
endloop
//add the trigger conditions
call TriggerAddCondition( t, Condition( function Catch_Pokemon_Conditions ) )
//add the trigger actions
call TriggerAddAction( t, function Catch_Pokemon_Actions )
set t = null
endfunction