scope MountHorse initializer MountHorse
globals
// The ability's rawcode
private integer ABILITYCODE = 'A000'
// Thenext four abilities are the hero's abilities
private integer ABILITY1 = 'AEer'
private integer ABILITY2 = 'AEfn'
private integer ABILITY3 = 'AEah'
private integer ABILITY4 = 'AEtq'
// Unit type of mounted unit
private integer MOUNTED = 'Emfr'
// The effect when replacing a unit
private string EFFECT = "Abilities\\Spells\\NightElf\\Blink\\BlinkTarget.mdl"
private integer ItemsAmount = 0
private item Item
private integer array ItemCharges
private integer array ItemId
private hashtable MountHash = InitHashtable()
endglobals
private function SaveItems takes unit u returns nothing
local integer c = 0
set ItemsAmount = UnitInventoryCount(u)
loop
set c = c + 1
set ItemId[c] = GetItemTypeId(UnitItemInSlot(u, c))
set ItemCharges[c] = GetItemCharges(UnitItemInSlot(u, c))
exitwhen c == ItemsAmount
endloop
endfunction
private function GetItems takes unit who returns nothing
local integer c = 0
if ItemsAmount > 0 then
loop
set c = c + 1
set Item = CreateItem(ItemId[c], GetUnitX(who), GetUnitY(who))
call SetItemCharges(Item, ItemCharges[c])
call IssueTargetItemOrder(who, "smart", Item)
set ItemId[c] = 0
set ItemCharges[c] = 0
set Item = null
exitwhen c == ItemsAmount
endloop
endif
endfunction
private function ReplaceHorse takes unit u returns nothing
local real life = GetUnitState(u, UNIT_STATE_LIFE)
local real mana = GetUnitState(u, UNIT_STATE_MANA)
local integer level = GetUnitLevel(u)
local integer agi = GetHeroAgi(u, false)
local integer str = GetHeroStr(u, false)
local integer int = GetHeroInt(u, false)
local string name = GetHeroProperName(u)
local real x = GetUnitX(u)
local real y = GetUnitY(u)
local real f = GetUnitFacing(u)
local player p = GetOwningPlayer(u)
local string Effect = ""
local integer id = GetHandleId(u)
call SaveItems(u)
call DestroyEffect(AddSpecialEffect(Effect, x, y))
call RemoveUnit(u)
set u = CreateUnit(p, LoadInteger( MountHash , id , StringHash("type") ), x, y, f)
call GetItems(u)
set u = null
set p = null
endfunction
private function ReplaceHuman takes unit u returns nothing
local real life = GetUnitState(u, UNIT_STATE_LIFE)
local real mana = GetUnitState(u, UNIT_STATE_MANA)
local integer level = GetUnitLevel(u)
local integer agi = GetHeroAgi(u, false)
local integer str = GetHeroStr(u, false)
local integer int = GetHeroInt(u, false)
local string name = GetHeroProperName(u)
local real x = GetUnitX(u)
local real y = GetUnitY(u)
local real f = GetUnitFacing(u)
local player p = GetOwningPlayer(u)
local string Effect = ""
call SaveItems(u)
call DestroyEffect(AddSpecialEffect(Effect, x, y))
call RemoveUnit(u)
set u = CreateUnit(p, MOUNTED, x, y, f)
call GetItems(u)
set u = null
set p = null
endfunction
private function MountHorse_Actions takes nothing returns nothing
local unit u = GetTriggerUnit()
local unit v
local integer ut = GetUnitTypeId( u )
// Get the ability levels of the unit that gets replaced
local integer i1 = GetUnitAbilityLevel( u , ABILITY1 )
local integer i2 = GetUnitAbilityLevel( u , ABILITY2 )
local integer i3 = GetUnitAbilityLevel( u , ABILITY3 )
local integer i4 = GetUnitAbilityLevel( u , ABILITY4 )
local integer i5 = GetUnitAbilityLevel( u , ABILITYCODE )
// Get unspent skill points
local integer i6 = GetHeroSkillPoints( u )
local player p = GetTriggerPlayer()
local boolean b = IsUnitSelected( u , p )
// This block detects whether the unit is already mounted or not
// I used that BJ since it's good enough and I didn't want to recode things
if ut == MOUNTED then
call ReplaceHorse(u)
else
call ReplaceHuman(u)
call SaveInteger( MountHash , GetHandleId(bj_lastReplacedUnit) , StringHash("type") , ut )
endif
set v = bj_lastReplacedUnit
// Create effect
call DestroyEffect(AddSpecialEffectTarget( EFFECT , v , "origin" ) )
// This is jsut to make sure the hero has enough skill points to level abilities
call UnitModifySkillPoints( v , 100 )
// Then following loops make the new unit learn abilities to set them to correct level
loop
exitwhen (i1 == 0 )
call SelectHeroSkill( v , ABILITY1 )
set i1 = i1 - 1
endloop
loop
exitwhen (i2 == 0 )
call SelectHeroSkill( v , ABILITY2 )
set i2 = i2 - 1
endloop
loop
exitwhen (i3 == 0 )
call SelectHeroSkill( v , ABILITY3 )
set i3 = i3 - 1
endloop
loop
exitwhen (i4 == 0 )
call SelectHeroSkill( v , ABILITY4 )
set i4 = i4 - 1
endloop
loop
exitwhen (i5 == 0 )
call SelectHeroSkill( v , ABILITYCODE )
set i5 = i5 - 1
endloop
// Add the new unit to selection
if b == true and GetLocalPlayer() == p then
call SelectUnit( v , true )
endif
// Finally set the number of skill points to correct value
call UnitModifySkillPoints( v , i6 - GetHeroSkillPoints(v))
set u = null
set v = null
endfunction
private function Conditions takes nothing returns boolean
return GetSpellAbilityId() == ABILITYCODE
endfunction
function MountHorse takes nothing returns nothing
local trigger t = CreateTrigger( )
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
call TriggerAddCondition( t , Condition( function Conditions ) )
call TriggerAddAction( t , function MountHorse_Actions )
endfunction
endscope