• 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] a little bit of math help

Status
Not open for further replies.
Level 23
Joined
Apr 16, 2012
Messages
4,041
Ive been working on something for past day and half now and It worked perfectly except for this:
When I pick units to group and then I check owners, it works fine but when I try to sort it it ever freezes to the "intial value" until there are units only of one player.
JASS:
function Loop takes nothing returns boolean
        local integer loop1 = 0
        local integer array unitswithin
        local unit first
        local integer loop2 = 0
        local texttag ttx
        local group g = CreateGroup()
        local integer winner
        local integer mainloop = 1 //up to stack(global variable)
        
        loop
            call GroupClear(g)
            call GroupEnumUnitsInRange(g, cpx[mainloop], cpy[mainloop], radius[mainloop], null)
//those are global arrays, this will loop trough all of them up to stack(another global)
            
            loop
                set first = FirstOfGroup(g)
                exitwhen first == null
                call GroupRemoveUnit(g, first)
                if not IsUnitType(first, UNIT_TYPE_DEAD) then
                    set unitswithin[GetPlayerId(GetOwningPlayer(first))] = unitswithin[GetPlayerId(GetOwningPlayer(first))] + 1
                endif
            endloop

            //now this is where the magic stops working

            loop
                set loop2 = loop1 + 1
                loop
                    if unitswithin[loop1] < unitswithin[loop2] then
                        set winner = loop2
                    endif
                    debug call BJDebugMsg(I2S(winner))
                    exitwhen loop2 == 12
                    set loop2 = loop2 + 1
                endloop
                exitwhen loop1 == 11
                set loop1 = loop1 + 1
            endloop
            exitwhen mainloop == stack
            set mainloop = mainloop + 1
        endloop 
        return false
endfunction
Ive also tried:
JASS:
//another:

            loop
                set loop2 = 0
                loop
                    if unitswithin[loop1] < unitswithin[loop2] then
                        set winner = loop2
                    endif
                    call BJDebugMsg(I2S(winner))
                    exitwhen loop2 == 12
                    set loop2 = loop2 + 1
                endloop
                exitwhen loop1 == 11
                set loop1 = loop1 + 1
            endloop

//as well as

            loop
                set loop2 = loop1 + 1
                loop
                    if unitswithin[loop1] < unitswithin[loop2] then
                        set winner = loop2
                    else
                        set winner = loop1
                    endif
                    call BJDebugMsg(I2S(winner))
                    exitwhen loop2 == 12
                    set loop2 = loop2 + 1
                endloop
                exitwhen loop1 == 11
                set loop1 = loop1 + 1
            endloop
now some examples:
lets say there is at map init 1 unit owned by Player 1(blue) at cpx[1], cpy[1] within radius[1], so the first time the winner is 1 as intended but if I rush it there with 3 units there are now 3 of mine, 1 of player 1(blue) so it should display 0 but it displays still 1 and only when all units of Player 2 are dead(in this case 1) it changes to 0

any help is appreciated
PS: this is not the whole trigger, the rest works as intended this is only part that doesnt work(the sorting) so dont worry about leaks or so
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
This is not sorting. It is finding the maximum value. Which can also be done in the FirstOfGroupLoop.
JASS:
function Loop takes nothing returns boolean
        local integer loop1 = 0
        local integer array unitswithin
        local unit first
        local integer loop2 = 0
        local texttag ttx
        local group g = CreateGroup()
        local integer winner
        local integer mainloop = 1 //up to stack(global variable)

        // Added Lines:
        local integer tempWinner = 0
        local integer tempMax = unitswithin[0]
        // End Added Lines

        loop
            call GroupClear(g)
            call GroupEnumUnitsInRange(g, cpx[mainloop], cpy[mainloop], radius[mainloop], null)
//those are global arrays, this will loop trough all of them up to stack(another global)
            
            loop
                set first = FirstOfGroup(g)
                exitwhen first == null
                call GroupRemoveUnit(g, first)
                if not IsUnitType(first, UNIT_TYPE_DEAD) then
                    set unitswithin[GetPlayerId(GetOwningPlayer(first))] = unitswithin[GetPlayerId(GetOwningPlayer(first))] + 1

                    //Added Lines:
                    if(unitswithin[GetPlayerId(GetOwningPlayer(first))] > tempMax)then
                        set tempMax = unitswithin[GetPlayerId(GetOwningPlayer(first))]
                        set tempWinner = GetPlayerId(GetOwningPlayer(first))
                    endif
                    // End Added Lines

                endif
            endloop

            // Added Line:
            set winner = tempWinner
            // End Added Line
           
            exitwhen mainloop == stack
            set mainloop = mainloop + 1
        endloop 
        return false
endfunction
 
Last edited:
Level 16
Joined
Oct 12, 2008
Messages
1,570
This, however will assume that all "unitswithin" variables have a non-negative value. If you want it to work for negative values as well, initialize tempMax and tempWinner to unitswithin[X] and X respectively, where X could be any number between 0 and 11 basically. This ensures that tempMax will not stay -1 when all the values of unitswithin are less than -1, and thus also ensures that tempWinner will not turn out as -1.

However, I guess they will never be negative, seeing as you named it "unitswithin", probably referring to the amount of units within some area.

Edit:

Edited the above algorithm to work for negative values of "unitswithin" as well..
 
Status
Not open for further replies.
Top