13 years
edited 8 years
Description
This library aims to provide a simple way to attach data to units via unit indexing. It offers the minimal functions required to work. This library is the result of thinking about this proposal.
Requirements:
- Alloc 2
Actual Code
Changelog:
Feel free to comment.
This library aims to provide a simple way to attach data to units via unit indexing. It offers the minimal functions required to work. This library is the result of thinking about this proposal.
Requirements:
- Alloc 2
Actual Code
1 /******************************************************************2 * SIMPLE UNIT INDEXER V1.3 *3 * By moyack *4 * 2012 *5 * =================================== *6 * Exclusive resource from wc3jass.com *7 * =================================== *8 ******************************************************************/9 library UnitIndexer initializer init requires Alloc
10 /*11 PURPOSE: Index units for easy attacking data to them12 13 FUNCTIONS:14 - GetUnitIndex: Takes a unit and returns its index (integer)15 - GetIndexUnit: Takes an integer and returns the unit indexed16 with that integer.17
18 The following functions are meant to be used when a unit is about19 to be deindexed and removed from the game, via RemoveUnit() or when20 it dies.21 22 - AddUnitEndEventCondition: Adds a contitional boolexpr variable23 - AddUnitEndEventAction: Adds an action code24 - GetDeindexedIndex: Returns the index of the removed unit25 - GetDeindexedUnit: Returns the unit about to remove26 27 ADVANTAGES:28 - Very easy to use29 - Inline friendly30 - It uses arrays instead of Hashtables31 - You can set the max amount of units in your map.32
33 DISADVANTAGE:34 - Uses Unit's user data, so if in your map you use this feature in35 your units, this system will put issues.36 37 CONFIGURATION: "SIZE" Constant.38 Here you can define the maximum amount of units that your game will manage.39 If you REAAALLLY need that, please set the SIZE variable with the required 40 amount, else DO NOT MODIFY ANYTHING.41 42 CREDITS:43 - Magtheridon96 & Troll-Brain for his nice support and help me notice some44 flaws in the code.45 */46 globals47 private constant integer SIZE = 8190 // Here you set the array size, just if you need it, ok??
48 private integer I = 0 // Stores the deallocated unit index...
49 private unit U = null // Stores the deallocated unit...
50 private trigger T = null // a trigger which will be fired when a unit is deallocated.
51 endglobals52 53 private struct data extends array [SIZE]
54 unit u
55
56 implement Alloc57
58 method destroy takes nothing returns nothing
59 set I = this
60 set U = .u
61 if T != null then
62 if TriggerEvaluate(T) then
63 call TriggerExecute(T)
64 endif65 endif66 call SetUnitUserData(.u, 0)
67 set .u = null
68 call .deallocate()
69 endmethod70
71 static method create takes unit u returns thistype
72 local thistype this
73 if GetUnitUserData(u) < 1 then
74 set this = thistype.allocate()
75 call SetUnitUserData(u, integer(this))
76 else77 set this = thistype(GetUnitUserData(u))
78 endif79 set this.u = u
80 return this
81 endmethod82 endstruct83 84 // public functions85 function GetUnitIndex takes unit u returns integer
86 return GetUnitUserData(u)
87 endfunction88 89 function GetIndexUnit takes integer index returns unit
90 return data(index).u
91 endfunction92 93 function GetDeindexedIndex takes nothing returns integer
94 return I95 endfunction96 97 function GetDeindexedUnit takes nothing returns unit
98 return U99 endfunction100 101 function AddUnitEndEventCondition takes boolexpr b returns triggercondition
102 if T == null then
103 set T = CreateTrigger()
104 endif105 return TriggerAddCondition(T, b)
106 endfunction107 108 function AddUnitEndEventAction takes code c returns triggeraction
109 if T == null then
110 set T = CreateTrigger()
111 endif112 return TriggerAddAction(T, c)
113 endfunction114 // end public functions115 116 private function ClearUnit takes unit u returns nothing
117 call data(GetUnitIndex(u)).destroy()
118 endfunction119 120 hook RemoveUnit ClearUnit
121 122 private function indexit takes nothing returns boolean
123 return data.create(GetTriggerUnit()) < 0 // just to make it boolean :)
124 endfunction125 126 private function indexit2 takes nothing returns boolean
127 return data.create(GetFilterUnit()) < 0 // just to make it boolean :)
128 endfunction129 130 private function dead takes nothing returns boolean
131 if not IsUnitType(GetTriggerUnit(), UNIT_TYPE_HERO) then
132 call ClearUnit(GetTriggerUnit())
133 endif134 return false
135 endfunction136 137 private function init takes nothing returns nothing
138 local trigger t = CreateTrigger()
139 local rect r = GetWorldBounds()
140 call TriggerRegisterEnterRectSimple(t, r)
141 call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SUMMON)
142 call TriggerAddCondition(t, Condition(function indexit))
143 call GroupEnumUnitsInRect(bj_lastCreatedGroup, r, Condition(function indexit2))
144 set t = CreateTrigger()
145 call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_DEATH)
146 call TriggerAddCondition(t, Condition(function dead))
147 set t = null
148 set r = null
149 endfunction150 151 endlibrary
Changelog:
- 1.0: Initial release.
- 1.1: Added detection of units removed via RemoveUnit()
- 1.2: Added detection of resurrected units.
- 1.3: Added functionality to trigger a condition or a function when a unit is about to be removed from the game, via RemoveUnit() or when it dies. This can be useful when you need to do something with the unit about to be removed like clearing any data attachment done to it. I don't plan to add more features, only optimization unless a good reason makes merits to an addition.
Feel free to comment.

