14 years
edited 8 years
Now that everyone's using FirstOfGroup loops, I re-evaluated GroupUtils by Rising_Dusk and found that most of the code was useless s***code now.
Here's a faster, cleaner, and 'better' version of it:
By the way, I didn't put an empty GroupEnumUnitsInArea function so that your libraries wouldn't
compile thus notifying you of the libraries you have to fix to run off of GroupUnitsInArea :p
Note: The use of groups is very avoidable in Warcraft III (Thank you Bribe).
The purpose of this is to improve the code of people who use groups anyways.
Feel free to comment..
Here's a faster, cleaner, and 'better' version of it:
1 /***************************************2 *3 * GroupTools4 * v1.1.2.25 * By Magtheridon966 *7 * - Original version by Rising_Dusk8 *9 * - Recycles groups, and allows the10 * enumeration of units while taking11 * into account collision.12 *13 * API:14 * ----15 *16 * - group ENUM_GROUP17 *18 * - function NewGroup takes nothing returns group19 * - function ReleaseGroup takes group g returns nothing20 * - Get and release group handles21 *22 * - function GroupRefresh takes group g returns nothing23 * - Refresh a group so that null units are removed24 *25 * - function GroupUnitsInArea takes group whichGroup, real x, real y, real radius returns nothing26 * - Groups units while taking into account collision27 *28 ***************************************/29 library GroupTools30
31 globals32 // The highest collision size you're using in your map.33 private constant real MAX_COLLISION_SIZE = 197.
34 // Data Variables35 private group array groups
36 private group gT = null
37 private integer gN = 0
38 private boolean f = false
39 // Global Group (Change it to CreateGroup() if you want)40 group ENUM_GROUP = bj_lastCreatedGroup
41 endglobals42
43 static if DEBUG_MODE then
44 private struct V extends array
45 debug static hashtable ht = InitHashtable()
46 endstruct47 endif48
49 private function AE takes nothing returns nothing
50 if (f) then
51 call GroupClear(gT)
52 set f = false
53 endif54 call GroupAddUnit(gT,GetEnumUnit())
55 endfunction56
57 function GroupRefresh takes group g returns nothing
58 debug if null==g then
59 debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"[GroupUtils]Error: Attempt to refresh null group!")
60 debug return
61 debug endif
62 set f = true
63 set gT = g
64 call ForGroup(gT,function AE)
65 if (f) then
66 call GroupClear(g)
67 endif68 endfunction69
70 function NewGroup takes nothing returns group
71 if 0==gN then
72 return CreateGroup()
73 endif74 set gN = gN - 1
75 debug call SaveBoolean(V.ht,GetHandleId(groups[gN]),0,false)
76 return groups[gN]
77 endfunction78
79 function ReleaseGroup takes group g returns nothing
80 debug if null==g then
81 debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"[GroupUtils]Error: Attempt to release null group!")
82 debug return
83 debug endif
84 debug if LoadBoolean(V.ht,GetHandleId(g),0) then
85 debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"[GroupUtils]Error: Double free!")
86 debug return
87 debug endif
88 debug call SaveBoolean(V.ht,GetHandleId(g),0,true)
89 call GroupClear(g)
90 set groups[gN] = g
91 set gN = gN + 1
92 endfunction93
94 function GroupUnitsInArea takes group whichGroup, real x, real y, real radius returns nothing
95 local unit u
96 debug if null==whichGroup then
97 debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"[GroupUtils]Error: Null group passed to GroupUnitsInArea!")
98 debug return
99 debug endif
100 call GroupEnumUnitsInRange(ENUM_GROUP,x,y,radius+MAX_COLLISION_SIZE,null)
101 loop102 set u = FirstOfGroup(ENUM_GROUP)
103 exitwhen null==u
104 if IsUnitInRangeXY(u,x,y,radius) then
105 call GroupAddUnit(whichGroup,u)
106 endif107 call GroupRemoveUnit(ENUM_GROUP,u)
108 endloop109 endfunction110
111 endlibrary
By the way, I didn't put an empty GroupEnumUnitsInArea function so that your libraries wouldn't
compile thus notifying you of the libraries you have to fix to run off of GroupUnitsInArea :p
Note: The use of groups is very avoidable in Warcraft III (Thank you Bribe).
The purpose of this is to improve the code of people who use groups anyways.
Feel free to comment..