HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Finding a Max

11-25-2008, 01:01 AM#1
Limb_Smasher
So I'm pretty new to JASS and came across a problem while trying to find a maximum value.
The only reason I know it's not working is because I try to display the maximum value with DisplayTextToPlayer and it won't show anything.

So there must be something wrong with my code.
Could someone take a look at it?

Collapse JASS:
function MAXabANDcd takes integer ab, integer cd returns integer
    if ab >= cd then
        return ab
    endif
    return cd
endfunction

function MAXefANDgh takes integer ef, integer gh returns integer
    if ef >= gh then
        return ef
    endif
    return gh
endfunction

function Maxab takes integer a, integer b returns integer
    if a >= b then
        return a
    endif
    return b
endfunction

function Maxcd takes integer c, integer d returns integer
    if c >= d then
        return c
    endif
    return d
endfunction

function Maxef takes integer e, integer f returns integer
    if e >= f then
        return e
    endif
    return f
endfunction

function Maxgh takes integer g, integer h returns integer    
    if g >= h then
        return g
    endif
    return h
endfunction

function Maximum takes integer a, integer b, integer c, integer d, integer e, integer f, integer g, integer h returns integer
    local integer ab
    local integer cd
    local integer ef
    local integer gh
    local integer abcd
    local integer efgh
    
    set ab = Maxab(a,b)
    set cd = Maxcd(c,d)
    set ef = Maxef(e,f)
    set gh = Maxgh(g,h)
    set abcd = MAXabANDcd(ab, cd)
    set efgh = MAXefANDgh(ef, gh)
    
    if abcd >= efgh then
        return abcd
    endif
    return efgh    
endfunction

Thanks!
11-25-2008, 01:25 AM#2
Deaod
Expand More efficient way:

Your code looks fine to me. Seems like your code for displaying the text isnt working. Which player are you displaying that text to?
11-25-2008, 02:14 AM#3
Zerzax
I was going to say, Limb Smasher, the functions you posted are all the same function with different names that are irrelevant if I'm correct. You're still just comparing maximums. Daeod's looks fine.
11-25-2008, 02:34 AM#4
DioD
some wierd jass from me

NOTE: there is something with endflag, check youself for good

Collapse JASS:
globals
    
    integer array Data
    integer SizeOfData = 0
    
endglobals

function AddData takes integer A,integer B,integer C,integer D returns nothing
    
    if A != 0 then
        set Data[SizeOfData] = A
        set SizeOfData = SizeOfData + 1
    endif
    
    if B != 0 then
        set Data[SizeOfData] = A
        set SizeOfData = SizeOfData + 1
    endif
    
    if C != 0 then
        set Data[SizeOfData] = A
        set SizeOfData = SizeOfData + 1
    endif
    
    if D != 0 then
        set Data[SizeOfData] = A
        set SizeOfData = SizeOfData + 1
    endif
    //just copypaste as much as you need, but you able to call loading multiple times.
    //to load 10 params call 3 times, last time leave C and D as zeros.
endfunction

function DropData takes nothing returns nothing
    set SizeOfData = 0
endfunction

function GetFlag takes integer Flag returns integer
    local integer Cycle = 1
    local integer SubResult = 0
    local integer Result = 0
    if SizeOfData == 0 then
        return 0
    endif
    if Flag == 0 then
        
        set SubResult = Data[0]
        
        loop
            
            exitwhen Cycle == SizeOfData - 1
            
            if Data[Cycle] > SubResult then
                set SubResult = Data[Cycle]
            endif
            set Cycle = Cycle + 1
            
        endloop
  
    endif
    return Result
endfunction
11-25-2008, 06:57 AM#5
Ammorth
All you need:
Collapse JASS:
function MaxInArray integer size returns integer
    local integer i = 0
    local integer max = i
    loop
        exitwhen i > size
        if SomeArray[i] > SomeArray[max] then
            set max = i
        endif
        set i = i + 1
    endloop
    return max
endfunction
Then all you have to do is store all your values in the array you loop through and call the function with the highest index you set in the array.
Collapse JASS:
function YourFunction takes nothing returns nothing
    set SomeArray[0] = SomeVal1
    set SomeArray[1] = SomeVal2
    set SomeArray[2] = SomeVal3
    set SomeArray[3] = SomeVal4
    set SomeArray[4] = SomeVal5
    set SomeArray[5] = SomeVal6
    set MaxValIndex = MaxInArray(5)
    set MaxVal = SomeArray[MaxValIndex]
    call BJDebugMsg("The max value is :"+I2S(MaxVal))
endfunction
11-25-2008, 07:17 AM#6
Spec
However, calculating maximum value on-the-fly would be better.
For example, we need to find leader with maximum kills. We should register event EVENT_PLAYER_UNIT_DEATH, and write something like that:
Collapse JASS:
globals
  integer max_kills = 0x00
  integer array kills
  integer max_leader = null
endglobals

function OnKill takes nothing returns nothing
  local integer i = GetPlayerId(GetOwningPlayer(GetKillingUnit()))
  set kills[i] = kills[i] + 1
  if kills[i] > max_kills then
    set max_kills = kills[i]
    set max_leader = i
  endif
endfunction

function InitTrig_Kill takes nothing returns nothing
// trigger event registration
endfunction
So if it's possible in your situation, I recommend you to use this.
11-25-2008, 08:21 AM#7
Captain Griffen
With your original code, you don't have to have all those functions - you could have just had one Max function and used it for all of them. The parameter name doesn't have to match up with the name of the variable being passed.
11-25-2008, 02:12 PM#8
Ammorth
Quote:
Originally Posted by Spec
Collapse JASS:
globals
  integer array kills
  integer max_leader = null
endglobals

function OnKill takes nothing returns nothing
  local integer i = GetPlayerId(GetOwningPlayer(GetKillingUnit()))
  set kills[i] = kills[i] + 1
  if kills[i] > kills[max_leader] then
    set max_leader = i
  endif
endfunction

function InitTrig_Kill takes nothing returns nothing
// trigger event registration
endfunction
otherwise if the max kills guys kills a unit, he won't update his max score. This way, it doesn't matter.
11-25-2008, 04:42 PM#9
Limb_Smasher
Thanks for all the feedback. I'm gonna test some of these out and see if they work alright. +rep to all that helped
11-26-2008, 12:42 AM#10
Monstah
Quote:
Originally Posted by DioD
some wierd jass from me

NOTE: there is something with endflag, check youself for good

Collapse JASS:
globals
    
    integer array Data
    integer SizeOfData = 0
    
endglobals

function AddData takes integer A,integer B,integer C,integer D returns nothing
    
    if A != 0 then
        set Data[SizeOfData] = A
        set SizeOfData = SizeOfData + 1
    endif
    
    if B != 0 then
        set Data[SizeOfData] = A
        set SizeOfData = SizeOfData + 1
    endif
    
    if C != 0 then
        set Data[SizeOfData] = A
        set SizeOfData = SizeOfData + 1
    endif
    
    if D != 0 then
        set Data[SizeOfData] = A
        set SizeOfData = SizeOfData + 1
    endif
    //just copypaste as much as you need, but you able to call loading multiple times.
    //to load 10 params call 3 times, last time leave C and D as zeros.
endfunction

function DropData takes nothing returns nothing
    set SizeOfData = 0
endfunction

function GetFlag takes integer Flag returns integer
    local integer Cycle = 1
    local integer SubResult = 0
    local integer Result = 0
    if SizeOfData == 0 then
        return 0
    endif
    if Flag == 0 then
        
        set SubResult = Data[0]
        
        loop
            
            exitwhen Cycle == SizeOfData - 1
            
            if Data[Cycle] > SubResult then
                set SubResult = Data[Cycle]
            endif
            set Cycle = Cycle + 1
            
        endloop
  
    endif
    return Result
endfunction

Hum... excuse me, kind sir, but whazzitdoo?

In other words, what the...?
11-26-2008, 05:03 AM#11
Limb_Smasher
OK so i'm encountering a problem.
At the start of the game, you have to pick a hero; then some triggers get turned off and some triggers get turned on.
Then my trigger which checks for min and max hits the fan and stops working.

It works fine until I pick a hero.
I triple checked and my trigger which checks the max and min values isn't even involved with the turning off and turning on of the triggers. Maybe there's a problem with my code?

Collapse JASS:
function MaxInArray takes nothing returns integer
    local integer i = 0
    local integer max = i
    local playerslotstate PSS
    loop
        exitwhen i > 7
        set PSS = GetPlayerSlotState(Player(i))
        if PSS == PLAYER_SLOT_STATE_PLAYING then
            if udg_Rating[i] >= udg_Rating[max] then
                set max = i
            endif
        endif
        set PSS = null
        set i = i + 1
    endloop
    return max
endfunction

function Maximum takes nothing returns integer
    local integer MaxValIndex
    local integer MaxVal
    
    set MaxValIndex = MaxInArray()
    set MaxVal = udg_Rating[MaxValIndex]
    return MaxVal
endfunction

function MinInArray takes nothing returns integer
    local integer i = 0
    local integer min = i
    local playerslotstate PSS
    loop
        exitwhen i > 7
        set PSS = GetPlayerSlotState(Player(i))
        if PSS == PLAYER_SLOT_STATE_PLAYING then    
            if udg_Rating[i] <= udg_Rating[min] then
                set min = i
            endif
        endif
        set PSS = null
        set i = i + 1
    endloop
    return min
endfunction

function Minimum takes nothing returns integer
    local integer MinValIndex
    local integer MinVal
    
    set MinValIndex = MinInArray()
    set MinVal = udg_Rating[MinValIndex]
    return MinVal
endfunction

function TotalRating takes nothing returns integer
    local integer total = 0
    local integer i = 0
    local playerslotstate PSS
    set udg_Player_Status = 0 //Number of players
    loop
        exitwhen i > 7
        set PSS = GetPlayerSlotState(Player(i))
        if PSS == PLAYER_SLOT_STATE_PLAYING then
            set total = total + udg_Rating[i]
            set udg_Player_Status = udg_Player_Status + 1
        endif
        set i = i + 1
        set PSS = null
    endloop
    return total
endfunction

function Trig_Rating_System_Actions takes nothing returns nothing
    local integer total = 0
    local integer i = 0
    local real avg
    local real norm
    local integer max
    local integer min

    set total = TotalRating()
    set avg = (I2R(total) / I2R(udg_Player_Status))
    set max = Maximum()
    set min = Minimum()
    set norm = (I2R(max + min) / 2.000)
    
    call DisplayTimedTextToForce(bj_FORCE_ALL_PLAYERS, 3, "|cffFF0202Avg|r |cff0041FFNorm|r")
    call DisplayTimedTextToForce(bj_FORCE_ALL_PLAYERS, 3, "|cffFF0202"+R2S(avg)+"|r |cff0041FF"+R2S(norm)+"|r")
endfunction

//===========================================================================
function InitTrig_Rating_System takes nothing returns nothing
    set gg_trg_Rating_System = CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_Rating_System, 6.00 )
    call TriggerAddAction( gg_trg_Rating_System, function Trig_Rating_System_Actions )
endfunction
11-26-2008, 05:31 PM#12
PitzerMike
The one-liner was still missing, also uses the bj-function since it's already there.

Collapse JASS:
function Maximum takes integer a, integer b, integer c, integer d, integer e, integer f, integer g, integer h returns integer
    return IMaxBJ(IMaxBJ(IMaxBJ(a, b), IMaxBJ(c, d)), IMaxBJ(IMaxBJ(e, f), IMaxBJ(g, h)))
endfunction

PS: This is concerning your original question. I don't know why your trigger doesn't work anymore.
11-27-2008, 01:51 AM#13
Ammorth
Can you explain what is happening when it starts to glitch up? Like what are the values in the array and what value is it returning.