• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] Unit Indexing System

Status
Not open for further replies.
Level 8
Joined
Feb 3, 2013
Messages
277
Hi all I've been trying to learn more about structs and currently trying to recreate an unit indexer for the sake of practice.
The index works just fine, but the de - index won't go through - what am I doing wrong?

JASS:
scope JassUnitIndexer initializer init 

    globals
        real indexEvent = 0.00
        
        unit array units
        integer array prev
        integer array next
        integer array rn
        integer ic = 0
    endglobals
            
    private function indexUnits takes unit u returns nothing
        local integer this = rn[0]
        
        if this == 0 then
            set ic = ic + 1
            set this = ic
        else
            set rn[0] = rn[this]
        endif
        
        set next[this] = 0
        set prev[this] = prev[0]
        set next[prev[this]] = this
        set prev[0] = this
        
        set units[this] = u
        call SetUnitUserData(units[this], this)
        set indexEvent = 1.00
        set indexEvent = 0.00
        call BJDebugMsg("UNIT HAS BEEN INDEXED! UNIT " + GetUnitName(u) + " HAS BEEN INDEXED AS NUMBER " + I2S(this) + "!")
    endfunction
    
    private function initializeIndex takes nothing returns boolean
        local unit lvUnit = GetTriggerUnit()
        local integer this
        
        set this = next[0]
        loop
            if GetUnitState(units[this], UNIT_STATE_LIFE) == 0 and units[this] != null then
                call BJDebugMsg("Instance number " + I2S(this) + ", " + GetUnitName(units[this]) + " is being recycled!")
                set units[this] = null
                set prev[next[this]] = prev[this]
                set next[prev[this]] = next[this]
                set rn[this] = rn[0]
                set rn[0] = this
                set indexEvent = 2.00
                set indexEvent = 0.00
                set this = 0
            endif
            exitwhen next[this] == 0 or (GetUnitState(units[this], UNIT_STATE_LIFE) == 0 and units[this] != null)
            set this = next[this]
        endloop
        
        call indexUnits(lvUnit)
        
        set lvUnit = null
        return false 
    endfunction
    
    private function init takes nothing returns nothing
        local trigger t = CreateTrigger()
        local rect lvRect = GetWorldBounds()
        local region lvReg = CreateRegion()
        local group g = CreateGroup()
        local unit fog 
        
        call GroupEnumUnitsInRect(g, lvRect, null)
        loop
            set fog = FirstOfGroup(g)
            exitwhen fog == null
            call indexUnits(fog)
            call GroupRemoveUnit(g, fog)
        endloop
        
        call RegionAddRect(lvReg, lvRect)
        
        call TriggerRegisterEnterRegion(t, lvReg, null)
        call TriggerAddCondition(t, function initializeIndex)
        
        call DestroyGroup(g)
        set g = null
        set t = null
    endfunction
    
endscope

Okay i think its working now, but I'm not 100% sure..
 
Last edited:
Level 31
Joined
Jul 10, 2007
Messages
6,306
Ok...

JASS:
        set next[this] = 0
        set prev[this] = prev[0]
        set next[prev[this]] = this
        set prev[0] = this

set this.next = 0
set this.prev = 0.prev

this.prev now points to whatever is before 0, which is the last node on the list

then this.prev.next = this

this.prev points to the last node, so

lastNode.next = this

and then 0.prev = this, meaning that the new last is this

looks perfect =)

but you could just as easily use next[prev[0]], and the literal number will be a little bit more efficient than a variable

however, using the variable is less error prone, not as easy to make mistakes =). Fine tuning like this usually leads to mistakes.

Looking good on your list ; p.


Why are you deindexing when a unit's life is 0? Shouldn't you deindex if GetUnitTypeId is 0?

Also, you should exit the loop when this == 0 or GetUnitTypeId(units[this]) != 0 ;)
 
Status
Not open for further replies.
Top