[JASS] Mathematical blackout... I can't see the solution, but it's out there!

Status
Not open for further replies.
Level 19
Joined
Oct 12, 2007
Messages
1,821
Hey there!

Maybe because I'm tired, or for whatever reason... I'm stuck.

What I'm doing:

- Defining units as a global array unit called 'ATTACKER[15]'
- A unit's speed is important for this and is calculated in an other function (I'm leaving out of this) called GetUnitSpeed(unit).
- The unit group 'ATTACKERS' consists out of a maximum of 16 units, so that's why the array value of ATTACKER[15] has 15 as it's highest number.
- Eventually I want this loop to check the speed of all the units in the ATTACKERS group and sort them based on their speed. ATTACKER[0] will be the fastest unit, and ATTACKER[15] will be the slowest, based on my 'GetUnitSpeed(unit)' calculations.

JASS:
loop /// Grouping the Attackers
    set u = FirstOfGroup(ATTACKERS)
    exitwhen u == null
        call GroupRemoveUnit(ATTACKERS, u)
        set i = 0
        loop
        exitwhen i == 16 or u == ATTACKER[i-1]
            if GetUnitSpeed(u) > GetUnitSpeed(ATTACKER[i-1]) and ATTACKER[i-1] != null and i > 0 then
                set ATTACKER[i-1] = ATTACKER[i]
                set u = ATTACKER[i-1]
            else
                set ATTACKER[i] = u
            endif
            set i = i + 1
        endloop //Fastest unit will be ATTACKER[0] and from there on the higher numbers will go up. Slowest is ATTACKER[15]
    endloop


Now my problem is:
I just noticed that with the current code the following scenario goes wrong:
- The last unit of the unit group is being checked (let's say this is the 16th unit) and he appears to be faster than all the other 15. Theoretically he should end up becoming ATTACKER[0] then, but with what I've made so far a unit will only shove up one spot so he will become ATTACKER[14] since 'i' was on 15, and I made him go to spot 'i-1'.
It might look simple, and it probably is... But my brain is broken and my headache blocks me from seeing the solution.

Is there anyone that wants to help me out or give me a hint to wake up my failing brains? I'd appreciate any help!

Thanks in advance.
 
Level 24
Joined
Aug 1, 2013
Messages
4,658
You start at 0.
You loop for each one until you came to number bigger than 15. (just simple loop)

You check if the speed is higher than the one before it, if so you switch both units.
Then you do it again and again and again until there are no more units left or when it is lower or equal.

You continue where you were in the original loop (meaning two different loop indexes).
And you do that until the end.

EDIT:
This would work:
JASS:
globals
    unit array unitArray
    real array speedArray
    
    unit tempUnit
    real tempReal
endglobals

function SortList takes integer unitCount returns nothing
    local integer index1 = 0
    local integer index2 = 0
    
    loop
        exitwhen index1 >= unitCount
        set index2 = index1
        
        loop
            exitwhen index2 <= 0
            
            //check if speedArray[index2] is lower than or equal to speedArray[index2-1]
            exitwhen speedArray[index2] <= speedArray[index2 -1]
            
            //Switch index2 and index2-1
            set tempUnit = unitArray[index2]
            set tempReal = speedArray[index2]
            set unitArray[index2] = unitArray[index2 -1]
            set speedArray[index2] = speedArray[index2 -1]
            set unitArray[index2 -1] = tempUnit
            set speedArray[index2 -1] = tempReal
            
            set index2 = index2 -1
        endloop
        
        set index1 = index1 +1
    endloop
endfunction

or

  • Sort List
    • Events
    • Conditions
    • Actions
      • For each (Integer Index1) from 0 to UnitCount, do (Actions)
        • Loop - Actions
          • Set Index2 = Index1
          • Custom script: loop
          • -------- - --------
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • Or - Any (Conditions) are true
                • Conditions
                  • Index2 Less than or equal to 0
                  • SpeedArray[Index2] Less than or equal to SpeedArray[(Index2 - 1)]
            • Then - Actions
              • Custom script: exitwhen true
            • Else - Actions
          • -------- - --------
          • -------- - --------
          • Set TempUnit = UnitArray[Index2]
          • Set TempReal = SpeedArray[Index2]
          • Set UnitArray[Index2] = UnitArray[(Index2 - 1)]
          • Set SpeedArray[Index2] = SpeedArray[(Index2 - 1)]
          • Set UnitArray[(Index2 - 1)] = TempUnit
          • Set SpeedArray[(Index2 - 1)] = TempReal
          • Set Index2 = (Index2 - 1)
          • -------- - --------
          • Custom script: endloop
 
Last edited:
Status
Not open for further replies.
Top