//-------------------------------------------------------------------------------
function SetHeroAvailability takes integer i returns nothing
local integer tier2 = CountUnitsInGroup(udg_HeroLimit_tier2Group[i])
local integer tier3 = CountUnitsInGroup(udg_HeroLimit_tier3Group[i])
if udg_HeroLimit_heroCount[i] == 3 then
call SetUnitOwner( udg_HeroLimit_dummy[i], Player(15), false )
elseif udg_HeroLimit_heroCount[i]==2 then
if tier3==0 then
call SetUnitOwner( udg_HeroLimit_dummy[i], Player(15), false )
else
call SetUnitOwner( udg_HeroLimit_dummy[i], Player(i), false )
endif
elseif udg_HeroLimit_heroCount[i]==1 then
if tier2==0 then
call SetUnitOwner( udg_HeroLimit_dummy[i], Player(15), false )
else
call SetUnitOwner( udg_HeroLimit_dummy[i], Player(i), false )
endif
elseif udg_HeroLimit_heroCount[i]==0 then
call SetUnitOwner( udg_HeroLimit_dummy[i], Player(i), false )
endif
endfunction
//-------------------------------------------------------------------------------
function IsAnAltar takes integer unitId returns boolean
local integer a=1
loop
exitwhen udg_HeroLimit_altar[a] == null
if unitId == udg_HeroLimit_altar[a] then
return true
endif
set a=a+1
endloop
return false
endfunction
//-------------------------------------------------------------------------------
function IsTier2 takes integer unitId returns boolean
local integer a=1
loop
exitwhen udg_HeroLimit_tier2[a] == null
if unitId == udg_HeroLimit_tier2[a] then
return true
endif
set a=a+1
endloop
return false
endfunction
//-------------------------------------------------------------------------------
function IsTier3 takes integer unitId returns boolean
local integer a=1
loop
exitwhen udg_HeroLimit_tier3[a] == null
if unitId == udg_HeroLimit_tier3[a] then
return true
endif
set a=a+1
endloop
return false
endfunction
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
function Trig_Training_Actions takes nothing returns nothing
local integer h = GetHandleId(GetTriggerEventId())
local unit u = GetTriggerUnit()
local integer unitId = GetUnitTypeId(u)
local integer playerId = GetPlayerId(GetOwningPlayer(u))
// [32]begins ------------------------------------------------------------
if h==32 and IsAnAltar(unitId) then
call SetUnitOwner(udg_HeroLimit_dummy[playerId], Player(15), false) //block, change to passive owner
set udg_HeroLimit_heroDuringTrain[playerId] = udg_HeroLimit_heroDuringTrain[playerId] + 1
call GroupAddUnit(udg_HeroLimit_workingAltars[playerId], u)
// [33]cancels --------------------------------------------------------
elseif h==33 and IsAnAltar(unitId) then
set udg_HeroLimit_heroDuringTrain[playerId] = udg_HeroLimit_heroDuringTrain[playerId] - 1
call GroupRemoveUnit(udg_HeroLimit_workingAltars[playerId], GetTriggerUnit())
call SetHeroAvailability(playerId)
// [34]finishes -------------------------------------------------------
elseif h==34 and IsUnitType(GetTrainedUnit(), UNIT_TYPE_HERO) then
set udg_HeroLimit_heroCount[playerId] = udg_HeroLimit_heroCount[playerId] + 1
set udg_HeroLimit_heroDuringTrain[playerId] = udg_HeroLimit_heroDuringTrain[playerId] - 1
call GroupRemoveUnit(udg_HeroLimit_workingAltars[playerId], GetTriggerUnit())
call SetHeroAvailability(playerId)
endif
set u=null
endfunction
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
function Trig_SoldConstructedUpgradesDies_Act takes nothing returns boolean
local integer h = GetHandleId(GetTriggerEventId())
local unit u = null
local integer unitId
local integer playerId
if h==269 then // [269]unit sells a unit (tavern) ---------------------------------------------
set u = GetSoldUnit()
set playerId = GetPlayerId(GetOwningPlayer(u))
if IsUnitType(u, UNIT_TYPE_HERO) then
set udg_HeroLimit_heroCount[playerId] = udg_HeroLimit_heroCount[playerId] + 1
call SetHeroAvailability(playerId)
endif
elseif h==28 then // [28]finishes construction ---------------------------------------------
set u = GetConstructedStructure()
set unitId = GetUnitTypeId(u)
set playerId = GetPlayerId(GetOwningPlayer(u))
if IsTier2(unitId) then
call GroupAddUnit(udg_HeroLimit_tier2Group[playerId], u)
if udg_HeroLimit_heroDuringTrain[playerId] == 0 then
call SetHeroAvailability(playerId)
endif
elseif IsTier3(unitId) then
call GroupAddUnit(udg_HeroLimit_tier3Group[playerId], u)
if udg_HeroLimit_heroDuringTrain[playerId] == 0 then
call SetHeroAvailability(playerId)
endif
endif
elseif h==31 then // [31]finishes an upgrade -----------------------------------------------
set u = GetTriggerUnit()
set unitId = GetUnitTypeId(u)
set playerId = GetPlayerId(GetOwningPlayer(u))
if IsTier2(unitId) then
call GroupAddUnit(udg_HeroLimit_tier2Group[playerId], u)
if udg_HeroLimit_heroDuringTrain[playerId] == 0 then
call SetHeroAvailability(playerId)
endif
elseif IsTier3(unitId) then
call GroupAddUnit(udg_HeroLimit_tier3Group[playerId], u)
if udg_HeroLimit_heroDuringTrain[playerId] == 0 then
call SetHeroAvailability(playerId)
endif
endif
elseif h==20 then // [20]structures dies -----------------------------------------------
set u = GetDyingUnit()
set playerId = GetPlayerId(GetOwningPlayer(u))
if IsUnitInGroup(u, udg_HeroLimit_tier2Group[playerId]) or IsUnitInGroup(u, udg_HeroLimit_tier3Group[playerId]) then
call GroupRemoveUnit(udg_HeroLimit_tier2Group[playerId], u)
call GroupRemoveUnit(udg_HeroLimit_tier3Group[playerId], u)
call SetHeroAvailability(playerId)
elseif IsUnitInGroup(u, udg_HeroLimit_workingAltars[playerId]) then
set udg_HeroLimit_heroDuringTrain[playerId] = udg_HeroLimit_heroDuringTrain[playerId] - 1
call GroupRemoveUnit(udg_HeroLimit_workingAltars[playerId], GetTriggerUnit())
call SetHeroAvailability(playerId)
endif
endif
set u=null
return false
endfunction
//===========================================
//look for preplaced heroes and structures:
function HeroLimit_CheckPreplaced takes nothing returns nothing
local group ug = CreateGroup()
local unit u = null
local integer a=0
loop // 12 players:
exitwhen a==bj_MAX_PLAYERS //12
//---
call GroupEnumUnitsOfPlayer(ug, Player(a), null)
loop
set u = FirstOfGroup(ug)
exitwhen u == null
if IsUnitType(u, UNIT_TYPE_HERO) then
set udg_HeroLimit_heroCount[a] = udg_HeroLimit_heroCount[a] + 1
elseif IsTier2(GetUnitTypeId(u)) then
call GroupAddUnit(udg_HeroLimit_tier2Group[a], u)
elseif IsTier3(GetUnitTypeId(u)) then
call GroupAddUnit(udg_HeroLimit_tier3Group[a], u)
endif
call GroupRemoveUnit(ug, u)
endloop
call GroupClear (ug)
call SetHeroAvailability(a)
//---
set a=a+1
endloop
call DestroyGroup(ug)
set ug=null
set u=null
endfunction
//==================================================================
//==================================================================
function InitTrig_HeroLimitSystem takes nothing returns nothing
local integer a=0
local trigger t1 = CreateTrigger()
local trigger t2 = CreateTrigger()
loop
exitwhen a==bj_MAX_PLAYERS
set udg_HeroLimit_dummy[a] = CreateUnit(Player(a), udg_HeroLimit_dummyId, 0.00, 0.00, 0.00)
set udg_HeroLimit_heroCount[a] = 0
set udg_HeroLimit_heroDuringTrain[a] = 0
set a=a+1
endloop
call TriggerRegisterAnyUnitEventBJ(t1, EVENT_PLAYER_UNIT_TRAIN_START ) //begins training a unit
call TriggerRegisterAnyUnitEventBJ(t1, EVENT_PLAYER_UNIT_TRAIN_CANCEL ) // cancels training a unit
call TriggerRegisterAnyUnitEventBJ(t1, EVENT_PLAYER_UNIT_TRAIN_FINISH ) // finishes training a unit
call TriggerAddAction(t1, function Trig_Training_Actions)
call TriggerRegisterAnyUnitEventBJ(t2, EVENT_PLAYER_UNIT_SELL ) // unit sells a unit
call TriggerRegisterAnyUnitEventBJ(t2, EVENT_PLAYER_UNIT_CONSTRUCT_FINISH ) // unit finishes construction
call TriggerRegisterAnyUnitEventBJ(t2, EVENT_PLAYER_UNIT_UPGRADE_FINISH ) // finishes an upgrade
call TriggerRegisterAnyUnitEventBJ(t2, EVENT_PLAYER_UNIT_DEATH ) // structure dies
call TriggerAddCondition(t2, Condition( function Trig_SoldConstructedUpgradesDies_Act))
set t2=null
set t1=null
endfunction