Warcraft 3 documentation

vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

[System] GroupUtils

moyack · 900

0 Members and 1 Guest are viewing this topic.

[System] GroupUtils
on: December 29, 2011, 08:16:39 PM
Category: Units, Variables, System
Language: vJASS

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:

Code: jass
  1. /***************************************
  2. *
  3. *   GroupTools
  4. *   v1.1.2.2
  5. *   By Magtheridon96
  6. *
  7. *   - Original version by Rising_Dusk
  8. *
  9. *   - Recycles groups, and allows the
  10. *   enumeration of units while taking
  11. *   into account collision.
  12. *
  13. *   API:
  14. *   ----
  15. *
  16. *       - group ENUM_GROUP
  17. *
  18. *       - function NewGroup takes nothing returns group
  19. *       - function ReleaseGroup takes group g returns nothing
  20. *           - Get and release group handles
  21. *
  22. *       - function GroupRefresh takes group g returns nothing
  23. *           - Refresh a group so that null units are removed
  24. *
  25. *       - function GroupUnitsInArea takes group whichGroup, real x, real y, real radius returns nothing
  26. *           - Groups units while taking into account collision
  27. *
  28. ***************************************/
  29. library GroupTools
  30.    
  31.     globals
  32.         // The highest collision size you're using in your map.
  33.         private constant real MAX_COLLISION_SIZE = 197.
  34.         // Data Variables
  35.         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.     endglobals
  42.    
  43.     static if DEBUG_MODE then
  44.         private struct V extends array
  45.             debug static hashtable ht = InitHashtable()
  46.         endstruct
  47.     endif
  48.    
  49.     private function AE takes nothing returns nothing
  50.         if (f) then
  51.             call GroupClear(gT)
  52.             set f = false
  53.         endif
  54.         call GroupAddUnit(gT,GetEnumUnit())
  55.     endfunction
  56.    
  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.         endif
  68.     endfunction
  69.    
  70.     function NewGroup takes nothing returns group
  71.         if 0==gN then
  72.             return CreateGroup()
  73.         endif
  74.         set gN = gN - 1
  75.         debug call SaveBoolean(V.ht,GetHandleId(groups[gN]),0,false)
  76.         return groups[gN]
  77.     endfunction
  78.    
  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.     endfunction
  93.    
  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.         loop
  102.             set u = FirstOfGroup(ENUM_GROUP)
  103.             exitwhen null==u
  104.             if IsUnitInRangeXY(u,x,y,radius) then
  105.                 call GroupAddUnit(whichGroup,u)
  106.             endif
  107.             call GroupRemoveUnit(ENUM_GROUP,u)
  108.         endloop
  109.     endfunction
  110.    
  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..
« Last Edit: December 18, 2017, 01:53:59 AM by moyack »



 

Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)