function GroupPickRandomUnit takes group whichGroup returns unit
?What's wrong withfunction GroupPickRandomUnit takes group whichGroup returns unit
?
Edit: More to answer your question, you could recreate that function in a more optimized fashion if you want.
function GroupPickRandomUnit takes group whichGroup returns unit
// If the user wants the group destroyed, remember that fact and clear
// the flag, in case it is used again in the callback.
local boolean wantDestroy = bj_wantDestroyGroup
set bj_wantDestroyGroup = false
set bj_groupRandomConsidered = 0
set bj_groupRandomCurrentPick = null
call ForGroup(whichGroup, function GroupPickRandomUnitEnum)
// If the user wants the group destroyed, do so now.
if (wantDestroy) then
call DestroyGroup(whichGroup)
endif
return bj_groupRandomCurrentPick
endfunction
function GroupPickRandomUnitEnum takes nothing returns nothing
set bj_groupRandomConsidered = bj_groupRandomConsidered + 1
if (GetRandomInt(1,bj_groupRandomConsidered) == 1) then
set bj_groupRandomCurrentPick = GetEnumUnit()
endif
endfunction
bj_wantDestroyGroup
, you'll need to rewrite those functions. //! textmacro FirstOfGroupHead takes NAME, GROUP
loop
set $NAME$ = FirstOfGroup($GROUP$)
exitwhen $NAME$ == null
call GroupRemoveUnit($GROUP$, $NAME$)
//! endtextmacro
//! textmacro FirstOfGroupEnd
endloop
//! endtextmacro
function GroupPickRandomUnitEx takes group whichGroup returns unit
local integer array index// !
local integer counter = 0
local unit u
//! runtextmacro FirstOfGroupHead("u", "whichGroup")
set counter = counter + 1
set index[counter] = GetUnitUserData(u)
//! runtextmacro FirstOfGroupEnd()
if counter == 0 then
return null
endif
return GetUnitById(index[GetRandomInt(1, counter)])
endfunction
globals
unit array uArr
integer tempInt
endglobals
function RandomUnitFromGroupEnum takes nothing returns nothing
set tempInt = tempInt + 1
set uArr[tempInt] = GetEnumUnit()
endfunction
function RandomUnitFromGroup takes group g returns unit
set tempInt = 0
call ForGroup(g, function RandomUnitFromGroupEnum)
return uArr[GetRandomInt(1,tempInt)]
endfunction
globals
unit array uArr
integer tempInt
endglobals
function RandomUnitFromGroupEnum takes nothing returns nothing
set tempInt = tempInt + 1
set uArr[tempInt] = GetEnumUnit()
endfunction
function RandomUnitFromGroup takes group g returns unit
set tempInt = 0
set uArr[1] = null
call ForGroup(g, function RandomUnitFromGroupEnum)
return uArr[GetRandomInt(1,tempInt)]
endfunction
function GroupRandomUnit takes group g, boolean temphp returns unit
local unit u = null
local integer i = 0
local integer t = 0
local unit chosen = null
loop
set u = FirstOfGroup(g)
exitwhen u == null
if Unit_TempHP[GetUnitId(u)] > 0 and temphp == true then
call GroupAddUnit(TempGroup, u)
set i = i + 1
elseif temphp == false then
call GroupAddUnit(TempGroup, u)
set i = i + 1
endif
call GroupRemoveUnit(g, u)
endloop
set t = GetRandomInt(1, i)
loop
set u = FirstOfGroup(TempGroup)
exitwhen u == null
if i == t then
set chosen = u
endif
call GroupRemoveUnit(TempGroup, u)
set i = i - 1
endloop
return chosen
endfunction
function GroupRandomUnit takes group g, boolean temphp returns unit
local unit u = null
local integer t = 0
local integer curmax = 0
local unit chosen = null
loop
set u = FirstOfGroup(g)
exitwhen u == null
if (Unit_TempHP[GetUnitId(u)] > 0 or temphp == false then
set t = GetRandomInt(1,100)
if t>curmax then
set curmax=t
set chosen=u
endif
endif
call GroupAddUnit(TempGroup,u)
call GroupRemoveUnit(g, u)
endloop
loop
set u = FirstOfGroup(g)
exitwhen u == null
call GroupAddUnit(g,u)
call GroupRemoveUnit(TempGroup,u)
endloop
return chosen
endfunction
I don't like the BJ random unit func coz the first u has 100% chance to be picked the 2nd has 50 the 3rd 33 and so on...
u make a lot of unneeded steps, u don't need tempgroop coz g already IS function as a tempgroup
do just this
JASS:function GroupRandomUnit takes group g, boolean temphp returns unit local unit u = null local integer t = 0 local integer curmax = 0 local unit chosen = null loop set u = FirstOfGroup(g) exitwhen u == null if (Unit_TempHP[GetUnitId(u)] > 0 and temphp == true) or temphp == false then set t = GetRandomInt(1,100) if t>curmax then set curmax=t set chosen=u endif endif call GroupRemoveUnit(g, u) endloop return chosen endfunction
sry chobibo:/ I know what is it for I just forget to add to the lines...
JASS:function GroupRandomUnit takes group g, boolean temphp returns unit local unit u = null local integer t = 0 local integer curmax = 0 local unit chosen = FirstOfGroup(g) loop set u = FirstOfGroup(g) exitwhen u == null if (Unit_TempHP[GetUnitId(u)] > 0 or temphp == false then set t = GetRandomInt(1,100) if t>curmax then set curmax=t set chosen=u endif endif call GroupAddUnit(TempGroup,u) call GroupRemoveUnit(g, u) endloop loop set u = FirstOfGroup(g) exitwhen u == null call GroupAddUnit(g,u) call GroupRemoveUnit(TempGroup,u) endloop return chosen endfunction
loop
set u = FirstOfGroup(TempGroup)
exitwhen u == null
call GroupAddUnit(g,u)
call GroupRemoveUnit(TempGroup,u)
endloop
Shouldn't it be this in the end?
JASS:loop set u = FirstOfGroup(TempGroup) exitwhen u == null call GroupAddUnit(g,u) call GroupRemoveUnit(TempGroup,u) endloop