• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] how to clean more this function?

Status
Not open for further replies.

ABM

ABM

Level 7
Joined
Jul 13, 2005
Messages
279
Hi everyone,

[Jass=]
if ( UnitHasItemOfTypeBJ(target, 'I0A8') == true ) then
set x = GetUnitX(target)
set y = GetUnitY(target)
set udg_Loc = GetUnitLoc(target)
set udg_TempGroup = GetUnitsInRangeOfLocMatching(300.00, udg_Loc, Condition(function Trig_Condition))
if ( IsUnitGroupEmptyBJ(udg_TempGroup) == false ) then
call RemoveItem( GetItemOfTypeFromUnitBJ(target, 'I0A8') )
call SetPlayerState(GetOwningPlayer(target), PLAYER_STATE_RESOURCE_LUMBER, GetPlayerState(GetOwningPlayer(target), PLAYER_STATE_RESOURCE_LUMBER) + -20)
call ReplaceUnitBJ( FirstOfGroup(udg_TempGroup), 'n01D', bj_UNIT_STATE_METHOD_RELATIVE )
call SetUnitOwner( GetLastReplacedUnitBJ(), GetOwningPlayer(target), true )
call DestroyGroup(udg_TempGroup)
call CreateItem( 'texp', x, y )
call CreateUnit( GetOwningPlayer(target), 'h02X', x, y, bj_UNIT_FACING )
call RemoveLocation(udg_Loc)
call UnitRemoveAbility( gg_unit_h00F_0043, 'A0HE' )
call SetUnitState( gg_unit_h00F_0043, UNIT_STATE_MANA, ( GetUnitState( gg_unit_h00F_0043, UNIT_STATE_MANA) + 1 ) )
endif
endif
[/code]

there are some function in red wich i don't know how to replace...
 
Level 37
Joined
Mar 6, 2006
Messages
9,243
This is what inside ReplaceUnitBJ. It is not completely bad function. You can cut out what you don't need.
JASS:
function ReplaceUnitBJ takes unit whichUnit, integer newUnitId, integer unitStateMethod returns unit
    local unit    oldUnit = whichUnit
    local unit    newUnit
    local boolean wasHidden
    local integer index
    local item    indexItem
    local real    oldRatio

    // If we have bogus data, don't attempt the replace.
    if (oldUnit == null) then
        set bj_lastReplacedUnit = oldUnit
        return oldUnit
    endif

    // Hide the original unit.
    set wasHidden = IsUnitHidden(oldUnit)
    call ShowUnit(oldUnit, false)

    // Create the replacement unit.
    if (newUnitId == 'ugol') then
        set newUnit = CreateBlightedGoldmine(GetOwningPlayer(oldUnit), GetUnitX(oldUnit), GetUnitY(oldUnit), GetUnitFacing(oldUnit))
    else
        set newUnit = CreateUnit(GetOwningPlayer(oldUnit), newUnitId, GetUnitX(oldUnit), GetUnitY(oldUnit), GetUnitFacing(oldUnit))
    endif

    // Set the unit's life and mana according to the requested method.
    if (unitStateMethod == bj_UNIT_STATE_METHOD_RELATIVE) then
        // Set the replacement's current/max life ratio to that of the old unit.
        // If both units have mana, do the same for mana.
        if (GetUnitState(oldUnit, UNIT_STATE_MAX_LIFE) > 0) then
            set oldRatio = GetUnitState(oldUnit, UNIT_STATE_LIFE) / GetUnitState(oldUnit, UNIT_STATE_MAX_LIFE)
            call SetUnitState(newUnit, UNIT_STATE_LIFE, oldRatio * GetUnitState(newUnit, UNIT_STATE_MAX_LIFE))
        endif

        if (GetUnitState(oldUnit, UNIT_STATE_MAX_MANA) > 0) and (GetUnitState(newUnit, UNIT_STATE_MAX_MANA) > 0) then
            set oldRatio = GetUnitState(oldUnit, UNIT_STATE_MANA) / GetUnitState(oldUnit, UNIT_STATE_MAX_MANA)
            call SetUnitState(newUnit, UNIT_STATE_MANA, oldRatio * GetUnitState(newUnit, UNIT_STATE_MAX_MANA))
        endif
    elseif (unitStateMethod == bj_UNIT_STATE_METHOD_ABSOLUTE) then
        // Set the replacement's current life to that of the old unit.
        // If the new unit has mana, do the same for mana.
        call SetUnitState(newUnit, UNIT_STATE_LIFE, GetUnitState(oldUnit, UNIT_STATE_LIFE))
        if (GetUnitState(newUnit, UNIT_STATE_MAX_MANA) > 0) then
            call SetUnitState(newUnit, UNIT_STATE_MANA, GetUnitState(oldUnit, UNIT_STATE_MANA))
        endif
    elseif (unitStateMethod == bj_UNIT_STATE_METHOD_DEFAULTS) then
        // The newly created unit should already have default life and mana.
    elseif (unitStateMethod == bj_UNIT_STATE_METHOD_MAXIMUM) then
        // Use max life and mana.
        call SetUnitState(newUnit, UNIT_STATE_LIFE, GetUnitState(newUnit, UNIT_STATE_MAX_LIFE))
        call SetUnitState(newUnit, UNIT_STATE_MANA, GetUnitState(newUnit, UNIT_STATE_MAX_MANA))
    else
        // Unrecognized unit state method - ignore the request.
    endif

    // Mirror properties of the old unit onto the new unit.
    //call PauseUnit(newUnit, IsUnitPaused(oldUnit))
    call SetResourceAmount(newUnit, GetResourceAmount(oldUnit))

    // If both the old and new units are heroes, handle their hero info.
    if (IsUnitType(oldUnit, UNIT_TYPE_HERO) and IsUnitType(newUnit, UNIT_TYPE_HERO)) then
        call SetHeroXP(newUnit, GetHeroXP(oldUnit), false)

        set index = 0
        loop
            set indexItem = UnitItemInSlot(oldUnit, index)
            if (indexItem != null) then
                call UnitRemoveItem(oldUnit, indexItem)
                call UnitAddItem(newUnit, indexItem)
            endif

            set index = index + 1
            exitwhen index >= bj_MAX_INVENTORY
        endloop
    endif

    // Remove or kill the original unit.  It is sometimes unsafe to remove
    // hidden units, so kill the original unit if it was previously hidden.
    if wasHidden then
        call KillUnit(oldUnit)
        call RemoveUnit(oldUnit)
    else
        call RemoveUnit(oldUnit)
    endif

    set bj_lastReplacedUnit = newUnit
    return newUnit
endfunction

if ( IsUnitGroupEmptyBJ(udg_TempGroup) == false ) then
->
if FirstOfGroup(udg_TempGroup) != null then

GetLastReplacedUnitBJ() -> bj_lastReplacedUnit

Instead of the item type bjs, you could use this a bit modified:
JASS:
function GetInventoryIndexOfItemTypeBJ takes unit whichUnit, integer itemId returns integer
    local integer index
    local item    indexItem

    set index = 0
    loop
        set indexItem = UnitItemInSlot(whichUnit, index)
        if (indexItem != null) and (GetItemTypeId(indexItem) == itemId) then
            return index + 1
        endif

        set index = index + 1
        exitwhen index >= bj_MAX_INVENTORY
    endloop
    return 0
endfunction
Except you return the item, and then use the item in place of the second item type bj.
 

ABM

ABM

Level 7
Joined
Jul 13, 2005
Messages
279
ok here is some fixing i made using your recommendation....

[Jass=]
function Trig_Condition takes nothing returns boolean
return ( GetUnitTypeId(GetFilterUnit()) == 'n01B' )
endfunction

function Trig_Sleep takes nothing returns nothing
local unit target = GetTriggerUnit()
local unit caster
local real x
local real y
local integer i = 0
local item thekey
local boolean key = false
local integer number = GetPlayerId(GetOwningPlayer(GetTriggerUnit()))
if ( UnitHasBuffBJ(target, 'B003') == false ) and ( GetUnitState( target,UNIT_STATE_MANA) >= 1.00 ) and ( udg_Coma[number+1] == false ) then
set udg_Sleeping[number+1] = true
call SetUnitState( udg_Basic_Crafting[number+1], UNIT_STATE_MANA, 10.00)
set x = GetUnitX(target)
set y = GetUnitY(target)
set caster = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), 'ndwm', x, y, bj_UNIT_FACING)
call UnitAddAbility( caster, 'A0M9' )
call TriggerSleepAction( 0.50 )
call IssueTargetOrder( caster, "creepthunderbolt", target )
call TriggerSleepAction( 0.10 )
set udg_MB_Sleep[number+1] = "Yes"
if ( UnitHasBuffBJ(target, 'B016') == true ) then
call UnitAddAbility( target, 'A04L' )
set udg_Sleep_Troll[number+1] = 'A04L'
call DisplayTimedTextToPlayer( GetOwningPlayer(target), 0, 0, 10.00, "TRIGSTR_3616" )
elseif ( UnitHasBuffBJ(target, 'B01O') == true ) then
call UnitAddAbility( target, 'A04F' )
set udg_Sleep_Troll[number+1] = 'A04F'
call DisplayTimedTextToPlayer( GetOwningPlayer(target), 0, 0, 10.00, "TRIGSTR_3589" )
elseif ( UnitHasBuffBJ(target, 'B01Q') == true ) then
call UnitAddAbility( target, 'A04R' )
set udg_Sleep_Troll[number+1] = 'A04R'
call DisplayTimedTextToPlayer( GetOwningPlayer(target), 0, 0, 10.00, "TRIGSTR_3344" )
elseif ( UnitHasBuffBJ(target, 'B01P') == true ) then
call UnitAddAbility( target, 'A04S' )
set udg_Sleep_Troll[number+1] = 'A04S'
call DisplayTimedTextToPlayer( GetOwningPlayer(target), 0, 0, 10.00, "TRIGSTR_2639" )
elseif ( GetUnitTypeId(target) == 'H001' ) then
call UnitAddAbility( target, 'A0HF' )
set udg_Sleep_Troll[number+1] = 'A0HF'
elseif ( GetUnitTypeId(target) == 'H002' ) then
call UnitAddAbility( target, 'A0HW' )
set udg_Sleep_Troll[number+1] = 'A0HW'
elseif ( GetUnitTypeId(target) == 'H000' ) or ( GetUnitTypeId(target) == 'H003' ) then
call UnitAddAbility( target, 'A0HX' )
set udg_Sleep_Troll[number+1] = 'A0HX'
endif
if ( udg_Heart_Snake[number+1] == true ) or ( GetRandomInt(1, 10) == 1 ) then
if ( UnitHasBuffBJ(target, 'B00H') == true ) then
call UnitRemoveBuffBJ( 'B00H', target )
elseif ( UnitHasBuffBJ(target, 'B00I') == true ) then
call UnitRemoveBuffBJ( 'B00I', target )
elseif ( UnitHasBuffBJ(target, 'Brej') == true ) then
call UnitRemoveBuffBJ( 'Brej', target )
endif
endif
loop
exitwhen i > 5
if ( GetItemTypeId(UnitItemInSlot(GetTriggerUnit(), i)) == 'I0A8' ) then
set key = true
set thekey = UnitItemInSlot(GetTriggerUnit(), i)
endif
set i = i + 1
endloop
if key == true ) then
set x = GetUnitX(target)
set y = GetUnitY(target
set udg_Loc = GetUnitLoc(target)
set udg_TempGroup = GetUnitsInRangeOfLocMatching(300.00, udg_Loc, Condition(function Trig_Condition))
if FirstOfGroup(udg_TempGroup) != null then
call RemoveItem( thekey )
call SetPlayerState(GetOwningPlayer(target), PLAYER_STATE_RESOURCE_LUMBER, GetPlayerState(GetOwningPlayer(target), PLAYER_STATE_RESOURCE_LUMBER) + -20)
call ReplaceUnitBJ( FirstOfGroup(udg_TempGroup), 'n01D', bj_UNIT_STATE_METHOD_RELATIVE )
call SetUnitOwner( bj_lastReplacedUnit , GetOwningPlayer(target), true )
call DestroyGroup(udg_TempGroup)
call CreateItem( 'texp', x, y )
call CreateUnit( GetOwningPlayer(target), 'h02X', x, y, bj_UNIT_FACING )
call RemoveLocation(udg_Loc)
call UnitRemoveAbility( gg_unit_h00F_0043, 'A0HE' )
call SetUnitState( gg_unit_h00F_0043, UNIT_STATE_MANA, ( GetUnitState( gg_unit_h00F_0043, UNIT_STATE_MANA) + 1 ) )
endif
endif
else
call DisplayTimedTextToPlayer( GetOwningPlayer(target), 0, 0, 10.00, "TRIGSTR_2504" )
endif
set target = null
set caster = null
endfunction

//===========================================================================
function InitTrig_Sleep_Dummy takes nothing returns nothing
set gg_trg_Sleep_Dummy = CreateTrigger( )
call TriggerAddAction( gg_trg_Sleep_Dummy, function Trig_Sleep )
endfunction[/code]

i also added the full trigger, to see if there is more i could do...
what about the unit in range? is it ok to keep the matching things, or better to make it differently?
and what about unit has buff of type?

EDIT:
i added local item to replace the "GetInventoryIndexOfItemTypeBJ"
 
Level 14
Joined
Jun 27, 2008
Messages
1,325
Not that inlining BJDebugMsg makes any sense.

does make sense:
JASS:
function BJDebugMsg2 takes string msg returns nothing
    call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, msg)
endfunction

no loop
pinguin.gif
 
Or just ctrl-click the function.

Not that inlining BJDebugMsg makes any sense.

Hey, nice trick!

Inlining BJDebugMsg only doesn't make sense if you're using it for debugging.

I guess technically changing BJDebugMsg(string) to DisplayTextToPlayer(GetLocalPlayer(),0.,0.,string) isn't exactly inlining though.

does make sense:
JASS:
function BJDebugMsg2 takes string msg returns nothing
    call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, msg)
endfunction

no loop
pinguin.gif

Yes, but that doesn't mean you should use this BJDebugMsg2() function - it's every bit as bad as other wrapper functions (like GetUnitAbilityLevelSwapped)
 

ABM

ABM

Level 7
Joined
Jul 13, 2005
Messages
279
ok so after all the inlining and replacement i get this...

[Jass=]
function Trig_Condition takes nothing returns boolean
return ( GetUnitTypeId(GetFilterUnit()) == 'n01B' )
endfunction

function Trig_Sleep takes nothing returns nothing
local unit target = GetTriggerUnit()
local unit caster
local real x
local real y
local integer i = 0
local item thekey
local boolean key = false
local integer number = GetPlayerId(GetOwningPlayer(GetTriggerUnit())) + 1
if (GetUnitAbilityLevel(target, 'B003') > 0) and ( GetUnitState( target,UNIT_STATE_MANA) >= 1.00 ) and ( udg_Coma[number] == false ) then
set udg_Sleeping[number] = true
call SetUnitState( udg_Basic_Crafting[number], UNIT_STATE_MANA, 10.00)
set x = GetUnitX(target)
set y = GetUnitY(target)
set caster = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), 'ndwm', x, y, bj_UNIT_FACING)
call UnitAddAbility( caster, 'A0M9' )
call TriggerSleepAction( 0.50 )
call IssueTargetOrder( caster, "creepthunderbolt", target )
call TriggerSleepAction( 0.10 )
set udg_MB_Sleep[number] = "Yes"
if (GetUnitAbilityLevel(target, 'B016') > 0) then
call UnitAddAbility( target, 'A04L' )
set udg_Sleep_Troll[number] = 'A04L'
call DisplayTimedTextToPlayer( GetOwningPlayer(target), 0, 0, 10.00, "TRIGSTR_3616" )
elseif (GetUnitAbilityLevel(target, 'B01O') > 0) then
call UnitAddAbility( target, 'A04F' )
set udg_Sleep_Troll[number] = 'A04F'
call DisplayTimedTextToPlayer( GetOwningPlayer(target), 0, 0, 10.00, "TRIGSTR_3589" )
elseif (GetUnitAbilityLevel(target, 'B01Q') > 0) then
call UnitAddAbility( target, 'A04R' )
set udg_Sleep_Troll[number] = 'A04R'
call DisplayTimedTextToPlayer( GetOwningPlayer(target), 0, 0, 10.00, "TRIGSTR_3344" )
elseif (GetUnitAbilityLevel(target, 'B01P') > 0) then
call UnitAddAbility( target, 'A04S' )
set udg_Sleep_Troll[number] = 'A04S'
call DisplayTimedTextToPlayer( GetOwningPlayer(target), 0, 0, 10.00, "TRIGSTR_2639" )
elseif ( GetUnitTypeId(target) == 'H001' ) then
call UnitAddAbility( target, 'A0HF' )
set udg_Sleep_Troll[number] = 'A0HF'
elseif ( GetUnitTypeId(target) == 'H002' ) then
call UnitAddAbility( target, 'A0HW' )
set udg_Sleep_Troll[number] = 'A0HW'
elseif ( GetUnitTypeId(target) == 'H000' ) or ( GetUnitTypeId(target) == 'H003' ) then
call UnitAddAbility( target, 'A0HX' )
set udg_Sleep_Troll[number] = 'A0HX'
endif
if ( udg_Heart_Snake[number] == true ) or ( GetRandomInt(1, 10) == 1 ) then
if (GetUnitAbilityLevel(target, 'B00H') > 0) then
call UnitRemoveBuffBJ( 'B00H', target )
elseif (GetUnitAbilityLevel(target, 'B00I') > 0) then
call UnitRemoveBuffBJ( 'B00I', target )
elseif (GetUnitAbilityLevel(target, 'Brej') > 0) then
call UnitRemoveBuffBJ( 'Brej', target )
endif
endif
loop
exitwhen i > 5
if ( GetItemTypeId(UnitItemInSlot(GetTriggerUnit(), i)) == 'I0A8' ) then
set key = true
set thekey = UnitItemInSlot(GetTriggerUnit(), i)
endif
set i = i + 1
endloop
if ( key == true ) then
set x = GetUnitX(target)
set y = GetUnitY(target)
set udg_Loc = GetUnitLoc(target)
set udg_TempGroup = GetUnitsInRangeOfLocMatching(300.00, udg_Loc, Condition(function Trig_Condition))
if FirstOfGroup(udg_TempGroup) != null then
call RemoveItem( thekey )
call SetPlayerState(GetOwningPlayer(target), PLAYER_STATE_RESOURCE_LUMBER, GetPlayerState(GetOwningPlayer(target), PLAYER_STATE_RESOURCE_LUMBER) + -20)
call ReplaceUnitBJ( FirstOfGroup(udg_TempGroup), 'n01D', bj_UNIT_STATE_METHOD_RELATIVE )
call SetUnitOwner( bj_lastReplacedUnit , GetOwningPlayer(target), true )
call DestroyGroup(udg_TempGroup)
call CreateItem( 'texp', x, y )
call CreateUnit( GetOwningPlayer(target), 'h02X', x, y, bj_UNIT_FACING )
call RemoveLocation(udg_Loc)
call UnitRemoveAbility( gg_unit_h00F_0043, 'A0HE' )
call SetUnitState( gg_unit_h00F_0043, UNIT_STATE_MANA, ( GetUnitState( gg_unit_h00F_0043, UNIT_STATE_MANA) + 1 ) )
endif
endif
else
call DisplayTimedTextToPlayer( GetOwningPlayer(target), 0, 0, 10.00, "TRIGSTR_2504" )
endif
set target = null
set caster = null
set thekey = null
endfunction

//===========================================================================
function InitTrig_Sleep_Dummy takes nothing returns nothing
set gg_trg_Sleep_Dummy = CreateTrigger( )
call TriggerAddAction( gg_trg_Sleep_Dummy, function Trig_Sleep )
endfunction
[/code]

call UnitRemoveBuffBJ( 'Brej', target ) i don't know what to do with this....?
set udg_TempGroup = GetUnitsInRangeOfLocMatching(300.00, udg_Loc, Condition(function Trig_Condition)) and i don't see how to change this one...is it needed to replace it?
 
call UnitRemoveBuffBJ( 'Brej', target ) i don't know what to do with this....?

If you control+click the BJ function, you will see what it comprises. This is an essential skill in inlining.

set udg_TempGroup = GetUnitsInRangeOfLocMatching(300.00, udg_Loc, Condition(function Trig_Condition)) and i don't see how to change this one

Read this: http://www.hiveworkshop.com/forums/...on-using-first-group-loop-enumeration-223140/

...is it needed to replace it?

To make it work? No. To submit it as a public resource to the hive? Yes.
 

ABM

ABM

Level 7
Joined
Jul 13, 2005
Messages
279
what about this function...

[Jass=]
function InitTrig_Monster_Eat_Damage takes nothing returns nothing
set gg_trg_Monster_Eat_Damage = CreateTrigger( )
call DisableTrigger( gg_trg_Monster_Eat_Damage )
call TriggerRegisterTimerEventPeriodic( gg_trg_Monster_Eat_Damage, 5.00 )
call TriggerAddAction( gg_trg_Monster_Eat_Damage, function Trig_Monster_Damage )
endfunction
[/code]

[Jass=]
function TriggerRegisterTimerEventPeriodic takes trigger trig, real timeout returns event
return TriggerRegisterTimerEvent(trig, timeout, true)
endfunction
[/code]

replacing TriggerRegisterTimerEventPeriodic( gg_trg_Monster_Eat_Damage, 5.00 ) as the event by:
TriggerRegisterTimerEvent(gg_trg_Monster_Eat_Damage, 5.00, true)
would that work? would it screw the event action of periodic event activation every 5 sec?
 
Status
Not open for further replies.
Top