• 🏆 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!

Index System

Status
Not open for further replies.
Level 3
Joined
Oct 17, 2013
Messages
29
JASS:
library IndexSystem initializer Init
/*==========================                 Adds Index to units in the Map======================================*/
/*================API===========================================================================================*/
/*=============function IsUnitIndexed takes unit u returns boolean          ====================================*/
/*=============   -Tells Whether a unit is Indexed                          ====================================*/
/*=============                                                             ====================================*/
/*=============function GetIndexCount takes nothing returns integer         ====================================*/
/*=============function GetUndexCount takes nothing returns integer         ====================================*/
/*=============   -Gets the Trigger Units Count                             ====================================*/
/*=============function IndexUnit takes unit u returns nothing              ====================================*/
/*=============function IndexUnit takes unit u returns nothing              ====================================*/
/*=============   -Adds Index to the Unit                                   ====================================*/
/*=============function UndexUnit takes unit u returns nothing              ====================================*/
/*=============   -Adds Undex to the Unit                                   ====================================*/
/*=============   -Undexed units won't be indexed                           ====================================*/
/*=============function DeUndexUnit takes unit u returns nothing            ====================================*/
/*=============   -Removes Undex to the Unit                                ====================================*/
/*=============  -Be Sure to Remove the Display actions once done testing   ====================================*/
/*=============  Details not updated.(Later)                                ====================================*/
/*==============================================================================================================*/
/*==============================================================================================================*/
    globals
        private boolean array Indexed
        private boolean array Dead
        private boolean array Undexed
        private group IndexUnits = null
    endglobals

    function IsUnitIndexed takes unit u returns boolean
        return Indexed[GetUnitUserData(u)]
    endfunction
    function GetUnitIndex takes unit u returns integer
        return GetUnitUserData(u)
    endfunction
    
    private function IndexCountTargets takes nothing returns boolean 
        return IsUnitAliveBJ(GetFilterUnit()) and Indexed[GetUnitIndex(GetFilterUnit())] and not Undexed[GetUnitIndex(GetFilterUnit())]
    endfunction
    
    function GetIndexCount takes nothing returns integer
        local group g = GetUnitsInRectMatching(GetPlayableMapRect(),Condition(function IndexCountTargets))
        local  integer i = CountUnitsInGroup(g)
        call DestroyGroup(g)
        set g = null
        return i
    endfunction
    
    private function UndexCountTargets takes nothing returns boolean 
        return IsUnitAliveBJ(GetFilterUnit()) and not Indexed[GetUnitIndex(GetFilterUnit())] and Undexed[GetUnitIndex(GetFilterUnit())]
    endfunction
    function GetUndexCount takes nothing returns integer
        local group g = GetUnitsInRectMatching(GetPlayableMapRect(),Condition(function IndexCountTargets))
        local  integer i = CountUnitsInGroup(g)
        call DestroyGroup(g)
        set g = null
        return i
    endfunction
    
    private function IndexTargets takes unit u returns boolean 
        return IsUnitAliveBJ(u) and not Indexed[GetUnitIndex(u)] and not Undexed[GetUnitIndex(u)]
    endfunction
    
    private function IndexCheckTargets takes nothing returns boolean 
        return Dead[GetUnitIndex(GetFilterUnit())]
    endfunction

    function IndexUnit takes unit u returns nothing
        local boolean b = false
        local integer i = GetIndexCount()
        local integer Di = 0
        loop
            if Dead[i] == true then
                set b = true
                set Di = i
            endif
            exitwhen i == 0
            set i = i - 1
        endloop
        if b == true then
            call SetUnitUserData(u,Di)
            set Dead[Di] = false
        else
            call SetUnitUserData(u,GetIndexCount() + 1)
        endif
        set Indexed[GetUnitIndex(u)] = true
        //test
        call DisplayTextToPlayer(Player(0),0.00,0.00,GetUnitName(u) + " = " + I2S(GetUnitIndex(u)) )
        //test end
    endfunction
    
    private function IndexGroup takes nothing returns nothing
        if IndexTargets(FirstOfGroup(IndexUnits)) then
            call IndexUnit(FirstOfGroup(IndexUnits))
            call GroupRemoveUnit(IndexUnits,FirstOfGroup(IndexUnits))
        endif
    endfunction
    
    private function IndexAllUnits takes nothing returns nothing
        set IndexUnits = GetUnitsInRectAll(GetPlayableMapRect())
        call ForGroup(IndexUnits,function IndexGroup)
        call DestroyGroup(IndexUnits)
        set IndexUnits = null
    endfunction
    
    function DeUndexUnit takes unit u returns nothing
        set Indexed[GetUnitIndex(u)] = false
        set Undexed[GetUnitIndex(u)] = true
    endfunction

    function UndexUnit takes unit u returns nothing
        set Indexed[GetUnitIndex(u)] = false
        set Undexed[GetUnitIndex(u)] = true
    endfunction

    private function AddIndexEnteringUnit takes nothing returns nothing
        if IndexTargets(GetEnteringUnit()) then
            call IndexUnit(GetEnteringUnit())
        endif
    endfunction
    
    private function RemoveIndex takes nothing returns nothing
        set Indexed[GetUnitIndex(GetDyingUnit())] = false
        set Dead[GetUnitIndex(GetDyingUnit())] = true
    endfunction
    
    
    private function Init takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterEnterRectSimple( t, bj_mapInitialPlayableArea )
        call TriggerAddAction(t, function AddIndexEnteringUnit)
        set t = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_DEATH )
        call TriggerAddAction(t, function RemoveIndex)
        call IndexAllUnits()
    endfunction

endlibrary
A Simple Indexer.
-Need Suggestions.
:vw_wtf:

.......
 

Attachments

  • IndexSystem.w3x
    21.8 KB · Views: 32
Last edited by a moderator:
Level 19
Joined
Mar 18, 2012
Messages
1,716
Reinventing the wheel ( Wikipedia link )

Please don't :/... No seriously we have multiple well working unit indexing systems.
UnitIndexer, GUI Unit Indexer, AIDS, UnitDex - UnitIndexer, ....

Why not just use one of the existing systems instead?

Using the unit death event is not an option, because you'll miss for example
units beeing removed. Think about hero type units aswell.
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
Best case scenario for a simple system is you end up with Unit Indexer GUI without the events. Worst case scenario is you have a poorer version of an existing resource.

I suggest you post your projects in Triggers & Scripts to get some feedback on them. The Lab is fine, but T&S has more active followers. There are a lot of things you can learn about JASS, especially how to avoid dynamic groups and the handle ID leaks BJ groups cause.
 
Status
Not open for further replies.
Top