• 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.

[Solved] Help me with this code pls

Status
Not open for further replies.
Level 9
Joined
Jun 21, 2012
Messages
431
I want to list a group of units and order them from low to high depending on the distance which are from the source unit, but for some reason something is wrong in the code ... I would like that someone help me with this because the LoL and my work keeps me busy guys :V

JASS:
        call GroupEnumUnitsInRange(bj_lastCreatedGroup,sourcex,sourcey,AoE,null)
        loop
            set u[0]=FirstOfGroup(bj_lastCreatedGroup)
            exitwhen(null==u[0])
            
            call GroupRemoveUnit(bj_lastCreatedGroup,u[0])
            if(target!=u[0] and IsUnitEnemy(u[0],p))then
                set this.next=this.next+1
                set dx=GetUnitX(u[0])-sourcex
                set dy=GetUnitY(u[0])-sourcey
                //We keep the distance of each unit
                set d[this.next]=SquareRoot(dx*dx+dy*dy)
                set u[this.next]=u[0]
                
                if(this.next==maxCount)then
                    set u[0]=null
                    exitwhen true
                endif
            endif
        endloop

        loop
            set i[1]=0
            set i[0]=i[0]+1
            loop
                set i[1]=i[1]+1
                //then we search the distance of each unit low to high
                if(0==d[0] or (d[i[1]]<d[0] and null!=u[i[1]]))then
                    set d[0]=d[i[1]]
                    set i[2]=i[1]
                endif
                exitwhen(i[1]==this.next)
            endloop
            set this.data[i[0]].handle=u[i[2]]
            set u[i[2]]=null
            exitwhen(i[0]==this.next)
        endloop
 
Level 13
Joined
Nov 7, 2014
Messages
571
JASS:
globals
    group G = CreateGroup()
endglobals

struct Units_By_Distance extends array
    unit u
    real distance_squared

    static thistype next = 0
    static method create takes nothing returns thistype
        set next = next + 1
        if next >= 8191 then
            set next = 1
        endif
        return next
    endmethod
endstruct

function order_units_around_unit_by_distance_ascending takes unit src_u returns nothing
    local real src_x = GetUnitX(src_u)
    local real src_y = GetUnitY(src_u)
    local Units_By_Distance array ubds
    local integer ubds_count = 0
    local Units_By_Distance ubd
    local unit u
    local real dx
    local real dy
    local integer i
    local integer j

    call GroupEnumUnitsInRange(G, src_x, src_y, 600.0, null)
    call GroupRemoveUnit(G, src_u)
    loop
        set u = FirstOfGroup(G)
        exitwhen u == null
        call GroupRemoveUnit(G, u)

        set dx = GetUnitX(u) - src_x
        set dy = GetUnitY(u) - src_y

        set ubd = Units_By_Distance.create()
        set ubd.u = u
        set ubd.distance_squared = dx * dx + dy * dy

        set ubds_count = ubds_count + 1
        set ubds[ubds_count] = ubd
    endloop

    // insertion sort, 1 based array
    set i = 2
    loop
        exitwhen i > ubds_count
        set ubd = ubds[i]

        set j = i - 1
        loop
            exitwhen j < 1 or ubds[j].distance_squared <= ubd.distance_squared // ubds[j].distance_squared > ubd.distance_squared for descending order
            set ubds[j + 1] = ubds[j]
            set j = j - 1
        endloop

        set ubds[j + 1] = ubd

        set i = i + 1
    endloop

    // do stuff with units
    set i = 1
    loop
        exitwhen i > ubds_count
        set ubd = ubds[i]
        call BJDebugMsg(GetUnitName(ubd.u) + " (" + R2S(SquareRoot(ubd.distance_squared)) + ")")
        set i = i + 1
    endloop
endfunction
 
Status
Not open for further replies.
Top