HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

[Help] Converting for "efficiency"

10-05-2008, 03:09 AM#1
Tide-Arc Ephemera
Hidden information:
Hello, it's me again! Anyway... I don't code too much so "efficiency" isn't totally asserted, though I just assume Jass > GUI in terms of speed (and power in some cases).

Anyway, here's the original trigger:

Trigger:
Swap Interact
Collapse Events
Unit - A unit Starts the effect of an ability
Collapse Conditions
(Ability being cast) Equal to (==) Interract
Collapse Actions
Set MovesTaken = (MovesTaken + 1)
Set TempPoint = (Position of (Target unit of ability being cast))
Custom script: set bj_wantDestroyGroup = true
Collapse Unit Group - Pick every unit in (Units within 200.00 of TempPoint) and do (Actions)
Collapse Loop - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Unit-type of (Picked unit)) Equal to (==) On
Collapse Then - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Unit-type of (Picked unit)) Not equal to (!=) Puzzle Solver
(Unit-type of (Picked unit)) Not equal to (!=) Randomizer
Collapse Then - Actions
Unit - Replace (Picked unit) with a Off using The old unit's relative life and mana
Unit - Change ownership of (Last replaced unit) to Player 12 (Brown) and Change color
Else - Actions
Collapse Else - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
(Unit-type of (Picked unit)) Not equal to (!=) Puzzle Solver
(Unit-type of (Picked unit)) Not equal to (!=) Randomizer
Collapse Then - Actions
Unit - Replace (Picked unit) with a On using The old unit's relative life and mana
Unit - Change ownership of (Last replaced unit) to Player 10 (Light Blue) and Change color
Else - Actions
Custom script: call RemoveLocation(udg_TempPoint)
Set Integer = 0
Set TempGroup = (Units of type Off)
Collapse Unit Group - Pick every unit in TempGroup and do (Actions)
Collapse Loop - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
((Picked unit) is hidden) Equal to (==) False
Collapse Then - Actions
Set Integer = (Integer + 1)
Else - Actions
Collapse If (All Conditions are True) then do (Then Actions) else do (Else Actions)
Collapse If - Conditions
Integer Equal to (==) NumLights[Integer2]
Collapse Then - Actions
Set Integer2 = (Integer2 + 1)
Trigger - Run Yay <gen> (checking conditions)
Else - Actions
Custom script: call DestroyGroup(udg_TempGroup)

And here's my Jass trigger:
Collapse JASS:
function Trig_Swap_I_Conditions takes nothing returns boolean
    if GetSpellAbilityId() == 'A000' then
        return true
    endif
    return false
endfunction

function Trig_Swap_I_Actions takes nothing returns nothing
    local integer i = 0
    local integer i2 = 0
    local group g = CreateGroup()
    local unit u = GetTriggerUnit()
    local real x = 0
    local real y = 0
    set udg_MovesTaken = udg_MovesTaken + 1
    call GroupEnumUnitsInRange(g, GetUnitX(u), GetUnitY(u), 200., null)
    loop
    exitwhen u==null
        set u = FirstOfGroup(g)
        if GetUnitTypeId(u) == 'h001' and GetUnitTypeId(u) != 'h002' then
            set x = GetUnitX(u)
            set y = GetUnitY(u)
            call ShowUnit(u, false)
            call KillUnit(u)
            call CreateUnit(Player(9), 'h000', x, y, 270)
        elseif GetUnitTypeId(u) == 'h000' and GetUnitTypeId(u) != 'h002' then
            set x = GetUnitX(u)
            set y = GetUnitY(u)
            call ShowUnit(u, false)
            call KillUnit(u)
            call CreateUnit(Player(11), 'h001', x, y, 270)
        endif
        call GroupRemoveUnit(g, u)
    endloop
    call DestroyGroup(g)
    set u = GetTriggerUnit()
    set g = CreateGroup()
    call GroupEnumUnitsOfType(g, "On", null)
    loop
    exitwhen u==null
        set u = FirstOfGroup(g)
        set i = i + 1
        call GroupRemoveUnit(g, u)
    endloop
    if i == udg_NumLights[udg_Integer2] then
        set udg_Integer2 = udg_Integer2 + 1
    endif
    call DestroyGroup(g)
    set u = null
    set g = null
endfunction

//===========================================================================
function InitTrig_Swap_I takes nothing returns nothing
    set gg_trg_Swap_I = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Swap_I, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( gg_trg_Swap_I, Condition( function Trig_Swap_I_Conditions ) )
    call TriggerAddAction( gg_trg_Swap_I, function Trig_Swap_I_Actions )
endfunction

I had a go at inlining Replace Unit but it didn't turn out very well.

What's meant to happen (and does happen with the GUI one):

1. Person interacts with an object
2. All objects within X (including targeted one) turn from A's to B's and all B's turn into A's

Now what happens with the Jass one is:
1. Person interracts with an object
2. Some (I think it's all of the appropriate ones) turn from A's to B's and all B's turn into A's
3 (if A). Some A's pop up
3 (if B). Some B's pop up

I think it has something to do with my hodge-podge replacement, but could anybody tell me what I've done wrong?

Thanks ahead!


EDIT!
Problem solved over IRC.
10-05-2008, 10:07 AM#2
Tide-Arc Ephemera
Sorry to bump a thread so young but I ended up having a new problem along the road...

Collapse JASS:
function Trig_Swap_I_Actions takes nothing returns nothing
    local integer i = 0
    local integer i2 = 0
    local group g = CreateGroup()
    local group g2 = CreateGroup()
    local unit u = GetSpellTargetUnit()
    local real x = 0
    local real y = 0
    set udg_MovesTaken = udg_MovesTaken + 1
    call GroupEnumUnitsInRange(g, GetUnitX(u), GetUnitY(u), 200., null)
    call GroupRemoveUnit(g, gg_unit_E000_0021)
    call KillUnit(GetAttacker())
    loop
    exitwhen u==null
        set u = FirstOfGroup(g)
        if GetUnitTypeId(u) == 'h001' and GetUnitTypeId(u) != 'h002' then
            set x = GetUnitX(u)
            set y = GetUnitY(u)
            call ReplaceUnitBJ(u, 'h000', bj_UNIT_STATE_METHOD_ABSOLUTE)
            call SetUnitOwner(u, Player(9), true)
        elseif GetUnitTypeId(u) == 'h000' and GetUnitTypeId(u) != 'h002' then
            set x = GetUnitX(u)
            set y = GetUnitY(u)
            call ReplaceUnitBJ(u, 'h001', bj_UNIT_STATE_METHOD_ABSOLUTE)
            call SetUnitOwner(u, Player(11), true)
        endif
        call GroupRemoveUnit(g, u)
    endloop
    call DestroyGroup(g)
    set u = GetTriggerUnit()
    call GroupEnumUnitsOfType(g2, 'On', null)
    loop
    exitwhen u == null
        set u = FirstOfGroup(g2)
        if GetUnitTypeId(u) == 'h001' then 
            set i = i + 1
        endif
    endloop
    call BJDebugMsg("Lights: "+I2S(i))
    if i == 0 then
        set udg_Integer2 = udg_Integer2 + 1
    endif
    call DestroyGroup(g)
    set u = null
    set g = null
endfunction

It's telling me that line has an "Expected expression" and I don't know what to do, I had it before but now it's not working for some reason.
10-05-2008, 10:48 AM#3
Flame_Phoenix
What is 'On' ?? To which unit is it refereeing to ?
Maybe you will have to use something like H2S ... not sure though, need more info.
10-05-2008, 10:49 AM#4
Spec
Because the type of argument is NOT integer, but string:
Collapse JASS:
native GroupEnumUnitsOfType takes group whichGroup, string unitname, boolexpr filter returns nothing
So that should be right variant, of course, if you have such unit name in OE:
Collapse JASS:
call GroupEnumUnitsOfType(g2, "On", null)
10-05-2008, 10:53 AM#5
Tide-Arc Ephemera
Oh shit, I keep forgetting that uses strings. Warghabargle, all the other ones use integers!

Thanks!

EDIT!
New problem!
Collapse JASS:
function Trig_Init_Actions takes nothing returns nothing
    local unit u
    local integer i = 3
    local integer i2 = 21
    call GroupEnumUnitsInRect(udg_AllStage, gg_rct_FourthStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[5], gg_rct_FourthStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[3], gg_rct_FourthStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[2], gg_rct_ThirdStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[1], gg_rct_SecondStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[0], gg_rct_FirstStage, null)
    set udg_NumLights[3] = 96
    set udg_NumLights[2] = 65
    set udg_NumLights[1] = 40
    set udg_NumLights[0] = 21
    loop
        call GroupRemoveGroup(udg_StageLevel[i - 1], udg_StageLevel[i])
        set i = i - 1
    exitwhen i == 0
    endloop
    call GroupRemoveGroup(udg_StageLevel[0], udg_StageLevel[5])
    loop
        set u = FirstOfGroup(udg_StageLevel[5])
        call ShowUnitHide(u)
        call GroupRemoveUnit(udg_StageLevel[5], u)
    exitwhen u == null
    endloop
     
     // ....

endfunction

I have a grid of On lights, it's meant to hide the lights that aren't part of the first stage. Thing is, this trigger isn't hiding them. The GUI equivalent did.

Trigger:
Initialization
Collapse Events
Map initialization
Conditions
Collapse Actions
Set StageLevel[6] = (Units in FourthStage <gen>)
Set StageLevel[5] = (Units in FourthStage <gen>)
Set StageLevel[3] = (Units in FourthStage <gen>)
Set StageLevel[2] = (Units in ThirdStage <gen>)
Set StageLevel[1] = (Units in SecondStage <gen>)
Set StageLevel[0] = (Units in FirstStage <gen>)
Set NumLights[3] = 96
Set NumLights[2] = 65
Set NumLights[1] = 40
Set NumLights[0] = 21
Unit Group - Remove Puzzle Solver 0021 <gen> from StageLevel[6]
Collapse For each (Integer A) from 0 to 5, do (Actions)
Collapse Loop - Actions
Unit Group - Remove Puzzle Solver 0021 <gen> from StageLevel[(Integer A)]
Collapse For each (Integer A) from 0 to 2, do (Actions)
Collapse Loop - Actions
Unit Group - Remove all units of StageLevel[(Integer A)] from StageLevel[3]
Collapse For each (Integer A) from 0 to 1, do (Actions)
Collapse Loop - Actions
Unit Group - Remove all units of StageLevel[(Integer A)] from StageLevel[2]
Unit Group - Remove all units of StageLevel[0] from StageLevel[1]
Unit Group - Remove all units of StageLevel[0] from StageLevel[6]
Collapse Unit Group - Pick every unit in StageLevel[6] and do (Actions)
Collapse Loop - Actions
Unit - Hide (Picked unit)

.....
10-05-2008, 11:43 AM#6
Malf
Collapse JASS:
call GroupEnumUnitsInRect(udg_AllStage, gg_rct_FourthStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[5], gg_rct_FourthStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[3], gg_rct_FourthStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[2], gg_rct_ThirdStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[1], gg_rct_SecondStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[0], gg_rct_FirstStage, null)

Those will crash Macs. Don't use null, use a function that returns true instead.
10-05-2008, 12:00 PM#7
Flame_Phoenix
Rep all people who helped ya xD

Quote:
Those will crash Macs. Don't use null, use a function that returns true instead.
So, that why, warcraft wouldn't be played in macs, because they would always be crashing. I think that the bad thing is using "Filter(null)".. i think "null" is ok, not sure though.
10-05-2008, 12:09 PM#8
Tide-Arc Ephemera
Quote:
Originally Posted by Malf
Collapse JASS:
call GroupEnumUnitsInRect(udg_AllStage, gg_rct_FourthStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[5], gg_rct_FourthStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[3], gg_rct_FourthStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[2], gg_rct_ThirdStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[1], gg_rct_SecondStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[0], gg_rct_FirstStage, null)

Those will crash Macs. Don't use null, use a function that returns true instead.

I've seen this used EVERYWHERE. Not to sound accusing, but where's the support for your statement?

EDIT!
Also, I think I'd know where Mac would crash. Also, what would these trick functions look like?
10-05-2008, 02:00 PM#9
Pyrogasm
Malf is incorrect. That does not cause a crash; the culprit is this:
Collapse JASS:
function Filter takes nothing returns nothing
    call Blah()
endfunction

call GroupEnumUnitsInRect(udg_TempGroup, udg_MyRect, Condition(function Filter))
However, the null boolexpr argument does cause a slight leak, if you remember the thread by grim001 a while back. I'd link to it but I'm literally running out the door right now.
10-05-2008, 02:11 PM#10
Tide-Arc Ephemera
I've read that but he was running it at like 1000 times per second right? Unless my puzzle takes 5 days to finish and you're taking like 2 moves per second, it's highly unlikely that it will matter in my map.
10-05-2008, 02:17 PM#11
Spec
Try this, it would work, if I understood problem)
Collapse JASS:
function RemoveUnits takes nothing returns nothing
  call GroupRemoveUnit(udg_StageLevel[5], GetEnumUnit())
endfunction
    
function HideUnits takes nothing returns nothing
  call ShowUnit(GetEnumUnit(), false)
endfunction
    
function Trig_Init_Actions takes nothing returns nothing
  local integer i = 0
  call GroupEnumUnitsInRect(udg_AllStage,      gg_rct_FourthStage, null)
  call GroupEnumUnitsInRect(udg_StageLevel[5], gg_rct_FirstStage,  null) // note the difference
  call GroupEnumUnitsInRect(udg_StageLevel[3], gg_rct_FourthStage, null)
  call GroupEnumUnitsInRect(udg_StageLevel[2], gg_rct_ThirdStage,  null)
  call GroupEnumUnitsInRect(udg_StageLevel[1], gg_rct_SecondStage, null)
  call GroupEnumUnitsInRect(udg_StageLevel[0], gg_rct_FirstStage,  null)
  loop
    set i = i + 1
    exitwhen i > 3
    call ForGroup(udg_StageLevel[i], function RemoveUnits) 
  endloop
  call ForGroup(udg_StageLevel[5], function HideUnits)
  call DestroyGroup(udg_StageLevel[5])
  // . . .
endfunction
10-05-2008, 04:31 PM#12
Tide-Arc Ephemera
I need StageLevel[5] to be the opposite of [0], so you're almost right there.

Also, from what I've heard, ForGroup calls are evil. I'll give it a shot when I get World Editor access.

EDIT!
Now this may sound like a really silly off topic request, but could somebody get me a benchmark for both square root and power?
Yes, I am aware of how stupid this is but that's why it's tiny-text!
10-05-2008, 05:09 PM#13
Flame_Phoenix
Quote:
I need StageLevel[5] to be the opposite of [0], so you're almost right there.

Also, from what I've heard, ForGroup calls are evil. I'll give it a shot when I get World Editor access.
No, ForGroup is, by all means, the fastest and more efficient(?) way you can do stuff with units in groups. Vex made a benchmark an year ago that shows that ForGroup is even faster than the "f = FirstOfGroup(g)" thing.

About the benchmark, I know a benchmark for Pow, but not for Square.
Anyway, this test may help you creating your own benchmark(see the last posts):
http://www.wc3campaigns.net/showthread.php?t=102881
10-05-2008, 05:57 PM#14
the-thingy
Quote:
No, ForGroup is, by all means, the fastest and more efficient(?) way you can do stuff with units in groups
You could go one step further and handle everything within the GroupEnum filter, and return false, which would be faster still - probably not alot of difference (unless you drool over maximum possible efficiency) but it does shorten the code and make it a little neater
10-06-2008, 02:45 AM#15
Tide-Arc Ephemera
I just need that tidbit for something else and I don't want to start a new topic about it. I'll get to work on my triggers now.

EDIT!
Pheonix, your link is broken.

EDIT!
Oh shit I am so sorry, I had "Run ON Map Initialization" unchecked, it was checked when I started but wasn't later on.
+Rep to whoever helped a mistaken problem

EDIT!
Apparently I'm not out of the woods yet.

Collapse JASS:
function Trig_Init_Actions takes nothing returns nothing
    local integer i = 4
    local integer i2 = 0
    local unit u
    local group g = CreateGroup()
    call GroupEnumUnitsInRect(udg_AllStage, gg_rct_FifthStage, null)
    call GroupEnumUnitsInRect(udg_StageLevel[5], gg_rct_FifthStage, null )
    call GroupEnumUnitsInRect(udg_StageLevel[4], gg_rct_FifthStage, null )
    call GroupEnumUnitsInRect(udg_StageLevel[3], gg_rct_FourthStage, null )
    call GroupEnumUnitsInRect(udg_StageLevel[2], gg_rct_ThirdStage, null )
    call GroupEnumUnitsInRect(udg_StageLevel[1], gg_rct_SecondStage, null )
    call GroupEnumUnitsInRect(udg_StageLevel[0], gg_rct_FirstStage, null )

    loop
        call GroupRemoveGroup(udg_StageLevel[i - 1], udg_StageLevel[i])
        set i = i - 1
    exitwhen i == 0
    endloop
    call GroupRemoveGroup(udg_StageLevel[0], udg_StageLevel[5])

    call BJDebugMsg("AllStage = " + I2S(CountUnitsInGroup(udg_AllStage)))
    call BJDebugMsg("Stage[0] = " + I2S(CountUnitsInGroup(udg_StageLevel[0])))
    call BJDebugMsg("Stage[1] = " + I2S(CountUnitsInGroup(udg_StageLevel[1])))
    call BJDebugMsg("Stage[2] = " + I2S(CountUnitsInGroup(udg_StageLevel[2])))
    call BJDebugMsg("Stage[3] = " + I2S(CountUnitsInGroup(udg_StageLevel[3])))
    call BJDebugMsg("Stage[4] = " + I2S(CountUnitsInGroup(udg_StageLevel[4])))
    call BJDebugMsg("Stage[5] = " + I2S(CountUnitsInGroup(udg_StageLevel[5])))

    loop
        set u = FirstOfGroup(udg_StageLevel[5])
        call ShowUnit(u, false)
        call GroupRemoveUnit(udg_StageLevel[5], u)
    exitwhen u==null
    endloop

    call DestroyGroup(udg_StageLevel[5])

    // .....

endfunction


Problems:
- Units STILL not hiding (for some god-knows-why reason)
- Groups have the following number of units in them
AllStage = 96
Stage[0] = 8
Stage[1] = 13
Stage[2] = 0
Stage[3] = 0
Stage[4] = 0
Stage[5] = 0 <-- That's why things aren't hiding, but I don't know why that's 0. Heck, even when I don't do anything to it it's zero.