• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

RectData

JASS:
library RectData requires optional Table
/*******************************************************************
*
*   RectData v0.0.0.2
*   By Magtheridon96
*
*   Requirements (optional)
*   ------------
*
*       - Table [By Bribe] : hiveworkshop.com/forums/jass-functions-413/snippet-new-table-188084/
*
*   API
*   ---
*
*       struct RectData : array
*
*           real minX
*           real maxX
*           real minY
*           real maxY
*           real centerX
*           real centerY
*           
*           method toRegion :
*               - This returns the rect itself in the form of a region
*
*           method destroy :
*               - This destroys an instance of the struct
*
*           method getLength :
*               - Returns the length of the rect
*
*           method getWidth :
*               - Returns the width of the rect
*
*           method create :
*               - Creates a new instance of the struct given a rect
*
*********************************************************************/
    
    debug private module Init
        debug private static method onInit takes nothing returns nothing
            debug set thistype.data = Table.create()
        debug endmethod
    debug endmodule
    
    struct RectData extends array
        private static integer ic = 0
        private static integer ir = 0
        private thistype rn
        real minX
        real maxX
        real minY
        real maxY
        real centerX
        real centerY
        private region rg
        debug private static Table data
        method destroy takes nothing returns nothing
            call RemoveRegion(.rg)
            set .rg=null
            set .rn=ir
            set ir=this
        endmethod
        method toRegion takes nothing returns region
            return .rg
        endmethod
        method getLength takes nothing returns real
            return .maxX - .minX
        endmethod
        method getWidth takes nothing returns real
            return .maxY - .minY
        endmethod
        static method create takes rect r returns thistype
            local thistype this
            debug local integer i = GetHandleId(r)
            debug if not data.has(i) then
                if 0==ir then
                    set ic=ic+1
                    set this=ic
                else
                    set this=ir
                    set ir=.rn
                endif
                set .minX = GetRectMinX(r)
                set .minY = GetRectMinY(r)
                set .maxX = GetRectMaxX(r)
                set .maxY = GetRectMaxY(r)
                set .centerX = (.maxX + .minX) / 2
                set .centerY = (.maxY + .minY) / 2
                set .rg = CreateRegion()
                call RegionAddRect(.rg,r)
                debug set data[i] = this
            debug else
                debug call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,60,"[Rect Data]: Creating multiple instances of the struct RectData for the same rect.")
                debug return data[i]
            debug endif
            return this
        endmethod
        debug implement Init
    endstruct
    
endlibrary

This is a snippet that can be used to store rect data so that you dont have to constantly call GetRectCenterX, GetRectMinX, etc.. when registering "Unit enters region" events for a trigger.

Table is an optional requirement and is only used for debugging purposes.

I found it useful for my map, so I thought I'd post it here.

Feel free to comment.

Side Note: Most of it inlines anyways.
 
Last edited:
Actually, the reason I made it debug only was because if 2 systems were doing that, you'd be prompted while your debugging and fix it ^^

Btw, did you notice how I started using Nestharus' allocation/deallocation methods? :p
I'm never ever going to use normal structs EVER again xD

edit
Update: Optimized CenterX/Y calculation
 
Top