HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

function GroupGetSpecial() - Need some Help

08-14-2008, 04:06 PM#1
Fulla
Often I need to weed out units from groups, with a specific highest/lowest property. Such as highest health, lowest health etc. Examples:
  • A Resurrection spell - When I group the dead units, I'll want to get the highest level units first, so taurens get raised over grunts.
  • A Healing Wave spell - When transferring health to allies in a chain, I'll want to seek out lowest health units first prefably.
  • A Chain Mana Burn spell - When bouncing I'ld want to seek out enemy units with highest mana first prefably. You'd rather burn a guy with 115 mana than a guy with 13 mana.

So I thought what would be really neat is 1 simple group function to handle it all:
Collapse JASS:
function GroupGetSpecial takes group g, string property, boolean b returns unit

The boolean is simply, true = highest, false = lowest.
The properties could be:
Table:
PropertiesWhat they do?
UNIT_STATE_LIFEGet Highest/Lowest Health
UNIT_STATE_MAX_LIFEGet Highest/Lowest Health
UNIT_STATE_MANAGet Highest/Lowest Mana
UNIT_STATE_MAX_MANAGet Highest/Lowest Mana
UNIT_STATE_HERO_LEVELGet Highest/Lowest Hero Level
UNIT_STATE_UNIT_LEVELGet Highest/Lowest Unit Level
......
......
......

So anyways a quick simple function I whipped up, for my Soul Drain ability for Spell Olypmics, it simply returns unit with lowest health

Collapse JASS:
function GroupGetLowestHealth takes group g returns unit
    local unit d = null
    local unit lowestunit = null
    local real lowesthealth = 0
    local real health = 0
    local group f = CreateGroup()
    call GroupAddGroup(g, f)
    loop
        set d = FirstOfGroup(f)
        exitwhen d == null
        set health = GetWidgetLife(d)
        if lowestunit == null then
            set lowestunit = d
            set lowesthealth = health
        elseif health < lowesthealth then
            set lowestunit = d
            set lowesthealth = health
        endif
        call GroupRemoveUnit(f, d)
    endloop
    call DestroyGroup(f)
    return lowestunit
endfunction

So anyways, how do you think the best way of doing this would be?
It takes the property, then alters the condition to the appropriate one?
Collapse JASS:
set health = GetWidgetLife(d)
Gets altered to either of these:
Collapse JASS:
set health = GetUnitState(d, UNIT_STATE_MAX_LIFE)
set health = GetUnitState(d, UNIT_STATE_MAX_MANA)
How can this be done?

Thx for any help, I think this could be very useful to alot of ppl.
08-14-2008, 04:26 PM#2
Anitarf
Well, I have an idea and am coding it right now, but I need a better sounding name than GroupGetSpecial. GroupGetExtremeUnit perhaps? naw...
08-14-2008, 04:43 PM#3
Fulla
GroupGetSpecificUnit ?
08-14-2008, 04:44 PM#4
Anitarf
Hmm... no, I'll just go with GroupGetHighest and GroupGetLowest.
Edit: I'll throw in GroupGetNearest for good measure.
08-14-2008, 05:25 PM#5
Anitarf
Ok, I coded the first version, here it is. Suggestions on other properties I can implement are welcome.