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

[vJass] Rect Utils No New Posts Rejected Codes & Snippets

Started by
Wareditor

0 Members and 1 Guest are viewing this topic.

[vJass] Rect Utils
on: July 21, 2012, 06:54:30 AM

* Introduction

So here is my first (public) script. Easy.
  • Allows to get a rect of a unit enters region event.
  • Allows to attach an Id to a rect.

* Code

Script :
Code: jass
  1. /===========================================================================
  2. // RECT UTILS v 1.2 By Wareditor - 21 / 07 / 12
  3. //===========================================================================
  4. //
  5. //  AIM :
  6. //
  7. //      * Add GetTriggeringRect()
  8. //      * Attach id to saved rect
  9. //     ¨* Easy "gate" system
  10. //
  11. //  API :
  12. //
  13. //      FUNCTIONS :
  14. //      * call SaveRect( rect, id )
  15. //          - Save a rect and attach a id to it.
  16. //            The GetTriggeringRect will only work with saved rect
  17. //
  18. //      * call ReleaseRect( rect )
  19. //          - Release a saved rect
  20. //
  21. //      * call RegionAddRectSaved( region, rect )
  22. //          - Add a saved rect to a region
  23. //            If you use the native, it will break the system in certain condition
  24. //
  25. //      * call SetRectId( rect, id )
  26. //          - Change the id of a saved rect
  27. //
  28. //      * call GetRectId( rect )
  29. //          - Return the id of the rect
  30. // 
  31. //      * call GetTriggeringRect()
  32. //          - Return the triggering rect of a Unit Enters Event and Leaves Event
  33. //
  34. //  COMMENTARY :
  35. //
  36. //      * Please give credits
  37. //
  38. //  REQUIRES :
  39. //
  40. //      * vJass + New Jass Helper
  41. //
  42. //
  43. //===========================================================================
  44. // Enjoy
  45. //===========================================================================
  46.  
  47. library RectUtils initializer Init
  48.  
  49.     //===========================================================================
  50.     // GLOBALS - NO TOUCH
  51.     //===========================================================================
  52.    
  53.     globals
  54.    
  55.         private hashtable hash
  56.         private region TriggerRegion = null
  57.         private trigger Trig = CreateTrigger()
  58.    
  59.     endglobals
  60.    
  61.     //===========================================================================
  62.     // GET TRIGGERING RECT
  63.     //===========================================================================
  64.    
  65.     function GetTriggeringRect takes nothing returns rect
  66.         return LoadRectHandle(hash, 1, GetHandleId(TriggerRegion))
  67.     endfunction
  68.    
  69.     private function EnterLeave takes nothing returns nothing
  70.         set TriggerRegion = GetTriggeringRegion()
  71.     endfunction
  72.    
  73.    
  74.     //===========================================================================
  75.     // ID
  76.     //===========================================================================
  77.    
  78.     function SetRectId takes rect r, integer id returns nothing
  79.         call SaveInteger(hash, 0, GetHandleId(r), id)
  80.     endfunction
  81.    
  82.     function GetRectId takes rect r returns integer
  83.         return LoadInteger(hash, 0, GetHandleId(r))
  84.     endfunction
  85.    
  86.     //===========================================================================
  87.     // SAVE
  88.     //===========================================================================
  89.    
  90.     function Saved takes rect r returns boolean
  91.         return LoadRegionHandle(hash, 2, GetHandleId(r)) != null
  92.     endfunction
  93.    
  94.     function SaveRect takes rect r, integer id returns nothing
  95.    
  96.         local region reg
  97.    
  98.         if LoadRegionHandle(hash, 2, GetHandleId(r)) == null then
  99.        
  100.             set reg = CreateRegion()
  101.             call RegionAddRect(reg, r)
  102.             call SaveInteger(hash, 0, GetHandleId(r), id)
  103.             call SaveRectHandle(hash, 1, GetHandleId(reg),  r)
  104.             call SaveRegionHandle(hash, 2, GetHandleId(r),  reg)
  105.             call TriggerRegisterEnterRegion(Trig, reg, null)
  106.             call TriggerRegisterLeaveRegion(Trig, reg, null)
  107.            
  108.         endif
  109.        
  110.     endfunction
  111.        
  112.     function ReleaseRect takes rect r returns nothing
  113.        
  114.         local integer i
  115.         local region reg
  116.        
  117.         if LoadRegionHandle(hash, 2, GetHandleId(r)) != null then
  118.        
  119.             set reg = LoadRegionHandle(hash, 2, GetHandleId(r))
  120.             call RegionClearRect(reg, r)
  121.             call RemoveSavedInteger(hash, 0, GetHandleId(r))
  122.             call RemoveSavedHandle(hash, 1, GetHandleId(reg))
  123.             call RemoveSavedHandle(hash, 2, GetHandleId(r))
  124.            
  125.         endif
  126.        
  127.     endfunction
  128.    
  129.     function RegionAddRectSaved takes region whichRegion, rect r returns nothing
  130.    
  131.         local region reg = LoadRegionHandle(hash, 2, GetHandleId(r))
  132.    
  133.         if reg != null then
  134.             call RegionAddRect(whichRegion, r)
  135.             call RegionClearRect(reg, r)//May seem useless but no
  136.             call RegionAddRect(reg, r)
  137.         endif
  138.    
  139.     endfunction
  140.    
  141.     //===========================================================================
  142.     // INITALIZATION
  143.     //===========================================================================
  144.  
  145.     private function Init takes nothing returns nothing
  146.         set hash = InitHashtable()
  147.         call TriggerAddAction(Trig, function EnterLeave)
  148.     endfunction
  149.    
  150. endlibrary
  151.  
  152. //===========================================================================
  153. // WAREDITOR.TLJ @ GMAIL.COM
  154. //===========================================================================

Demo :
Code: jass
  1. library Demo initializer Init requires RectUtils
  2.  
  3.     globals
  4.    
  5.         private trigger Trig
  6.         private region Gates
  7.    
  8.     endglobals
  9.  
  10.     struct gate
  11.    
  12.         private rect a
  13.         private rect b
  14.        
  15.         static method move takes nothing returns nothing
  16.        
  17.             local unit u = GetEnteringUnit()
  18.             local thistype this = GetRectId(GetTriggeringRect())
  19.            
  20.             call SetUnitX(u, GetRectCenterX(this.b))
  21.             call SetUnitY(u, GetRectCenterY(this.b))
  22.            
  23.         endmethod
  24.    
  25.         static method create takes rect a, rect b returns thistype
  26.        
  27.             local thistype this = thistype.allocate()
  28.                    
  29.             set this.a = a
  30.             set this.b = b
  31.            
  32.             call SaveRect(a, this)
  33.             call RegionAddRectSaved(Gates, a)
  34.    
  35.             return this
  36.        
  37.         endmethod
  38.        
  39.         private static method onInit takes nothing returns nothing
  40.        
  41.             set Gates = CreateRegion()
  42.             set Trig = CreateTrigger()
  43.             call TriggerRegisterEnterRegion(Trig, Gates, null)
  44.             call TriggerAddAction(Trig, function thistype.move)
  45.            
  46.         endmethod
  47.    
  48.     endstruct
  49.    
  50.     private function Init takes nothing returns nothing
  51.         call gate.create(gg_rct_a, gg_rct_b)//CONFIGURE
  52.     endfunction
  53.  
  54. endlibrary
  55.  

* Requirement


* Credits

  • Worldedit.free.fr
  • WC3Jass.comr
« Last Edit: July 21, 2012, 12:12:26 PM by Wareditor »



Re: [vJass] Rect Utils
Reply #1 on: July 21, 2012, 09:15:05 AM

Currently, this could crash the thread if someone calls GetTriggeringRect() at the wrong time.
The solution is to initially set the value of TriggerRegion in the globals to null.

edit
Also, making 5000 iterations on map init isn't a very good solution :/
What are the Pros of using this though? =o

The only utility I use for rects is this:

Code: jass
  1. /**************************************
  2. *
  3. *   RegisterRectEvent
  4. *   v1.0.0.0
  5. *   By Magtheridon96
  6. *
  7. *   - Used to register onEnter/onLeave code for a rect.
  8. *
  9. *   API:
  10. *   ----
  11. *
  12. *       - function RegisterRectEvent takes rect r, code onEnter, code onLeave returns nothing
  13. *           - Registers the codes onEnter and onLeave to the rect r.
  14. *
  15. **************************************/
  16. library RegisterRectEvent
  17.    
  18.     function RegisterRectEvent takes rect r, code onEnter, code onLeave returns nothing
  19.         local trigger t = null
  20.         local region rg = null
  21.         local boolean b = (onEnter != null)
  22.         local boolean c = (onLeave != null)
  23.         local boolean d = (b or c)
  24.        
  25.         if (d) then
  26.             set rg = CreateRegion()
  27.             call RegionAddRect(rg, r)
  28.         endif
  29.        
  30.         if (b) then
  31.             set t = CreateTrigger()
  32.             call TriggerRegisterEnterRegion(t, rg, null)
  33.             call TriggerAddCondition(t, Filter(onEnter))
  34.             set t = null
  35.         endif
  36.        
  37.         if (c) then
  38.             set t = CreateTrigger()
  39.             call TriggerRegisterLeaveRegion(t, rg, null)
  40.             call TriggerAddCondition(t, Filter(onLeave))
  41.             set t = null
  42.         endif
  43.        
  44.         if (d) then
  45.             set rg = null
  46.         endif
  47.     endfunction
  48.    
  49. endlibrary



Re: [vJass] Rect Utils
Reply #2 on: July 21, 2012, 10:15:20 AM

Hmmm, I think this code can be improved. First of all the looping though 5000 items is very inneficient. I'd go for using a hashtable, making the search faster.

In order to give more suggestions, could you give me an example of usage of your script?

Additionally, you should add this link for JNGP with the latest jass helper: https://wc3modding.info/4263/jass-newgen-pack-jngp/ or
Code: [Select]
[smg id=12 type=box] [smg id=12 type=box]


Re: [vJass] Rect Utils
Reply #3 on: July 21, 2012, 12:14:44 PM

Updated the code.
About the use, look at my demo.



Re: [vJass] Rect Utils
Reply #4 on: July 21, 2012, 08:01:01 PM

I've checked the new version of the code and now it looks better. I was just wondering about the releaserect, shouldn't be better to remove the rect and free that memory?


Re: [vJass] Rect Utils
Reply #5 on: August 19, 2012, 02:20:38 AM

Need to be graveyarded.
Rectwrap is a way better for what I wanted to do.



Re: [vJass] Rect Utils
Reply #6 on: August 20, 2012, 12:59:20 AM

Need to be graveyarded.
Rectwrap is a way better for what I wanted to do.

Graveyarded by request.


Re: [vJass] Rect Utils
Reply #7 on: August 31, 2015, 02:41:01 AM

Why wasn't the demo in a scope? Strange. Link to Rectwrap?



 

Started by moyack

Replies: 2
Views: 6144
WC3 Editing Tools

Started by azlier

Replies: 0
Views: 1789
Codes & Snippets

Started by Magtheridon96

Replies: 0
Views: 2026
Codes & Snippets

Started by nestharus

Replies: 0
Views: 2293
Jass Tutorials
Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...