Function MeleeTournamentFinishNowRuleA

Line
9773
Location
blizzard.j
Constant?
no
Type
function
Arguments
integer multiplier
Returns
nothing
9773
function MeleeTournamentFinishNowRuleA takes integer multiplier returns nothing
9774
    local integer array playerScore
9775
    local integer array teamScore
9776
    local force array   teamForce
9777
    local integer       teamCount
9778
    local integer       index
9779
    local player        indexPlayer
9780
    local integer       index2
9781
    local player        indexPlayer2
9782
    local integer       bestTeam
9783
    local integer       bestScore
9784
    local boolean       draw
9785
9786
    // Compute individual player scores
9787
    set index = 0
9788
    loop
9789
        set indexPlayer = Player(index)
9790
        if MeleeWasUserPlayer(indexPlayer) then
9791
            set playerScore[index] = GetTournamentScore(indexPlayer)
9792
            if playerScore[index] <= 0 then
9793
                set playerScore[index] = 1
9794
            endif
9795
        else
9796
            set playerScore[index] = 0
9797
        endif
9798
        set index = index + 1
9799
        exitwhen index == bj_MAX_PLAYERS
9800
    endloop
9801
9802
    // Compute team scores and team forces
9803
    set teamCount = 0
9804
    set index = 0
9805
    loop
9806
        if playerScore[index] != 0 then
9807
            set indexPlayer = Player(index)
9808
9809
            set teamScore[teamCount] = 0
9810
            set teamForce[teamCount] = CreateForce()
9811
9812
            set index2 = index
9813
            loop
9814
                if playerScore[index2] != 0 then
9815
                    set indexPlayer2 = Player(index2)
9816
9817
                    if PlayersAreCoAllied(indexPlayer, indexPlayer2) then
9818
                        set teamScore[teamCount] = teamScore[teamCount] + playerScore[index2]
9819
                        call ForceAddPlayer(teamForce[teamCount], indexPlayer2)
9820
                        set playerScore[index2] = 0
9821
                    endif
9822
                endif
9823
9824
                set index2 = index2 + 1
9825
                exitwhen index2 == bj_MAX_PLAYERS
9826
            endloop
9827
9828
            set teamCount = teamCount + 1
9829
        endif
9830
9831
        set index = index + 1
9832
        exitwhen index == bj_MAX_PLAYERS
9833
    endloop
9834
9835
    // The game is now over
9836
    set bj_meleeGameOver = true
9837
9838
    // There should always be at least one team, but continue to work if not
9839
    if teamCount != 0 then
9840
9841
        // Find best team score
9842
        set bestTeam = -1
9843
        set bestScore = -1
9844
        set index = 0
9845
        loop
9846
            if teamScore[index] > bestScore then
9847
                set bestTeam = index
9848
                set bestScore = teamScore[index]
9849
            endif
9850
9851
            set index = index + 1
9852
            exitwhen index == teamCount
9853
        endloop
9854
9855
        // Check whether the best team's score is 'multiplier' times better than
9856
        // every other team. In the case of multiplier == 1 and exactly equal team
9857
        // scores, the first team (which was randomly chosen by the server) will win.
9858
        set draw = false
9859
        set index = 0
9860
        loop
9861
            if index != bestTeam then
9862
                if bestScore < (multiplier * teamScore[index]) then
9863
                    set draw = true
9864
                endif
9865
            endif
9866
9867
            set index = index + 1
9868
            exitwhen index == teamCount
9869
        endloop
9870
9871
        if draw then
9872
            // Give draw to all players on all teams
9873
            set index = 0
9874
            loop
9875
                call ForForce(teamForce[index], function MeleeDoDrawEnum)
9876
9877
                set index = index + 1
9878
                exitwhen index == teamCount
9879
            endloop
9880
        else
9881
            // Give defeat to all players on teams other than the best team
9882
            set index = 0
9883
            loop
9884
                if index != bestTeam then
9885
                    call ForForce(teamForce[index], function MeleeDoDefeatEnum)
9886
                endif
9887
9888
                set index = index + 1
9889
                exitwhen index == teamCount
9890
            endloop
9891
9892
            // Give victory to all players on the best team
9893
            call ForForce(teamForce[bestTeam], function MeleeDoVictoryEnum)
9894
        endif
9895
    endif
9896
9897
endfunction