• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] RectEvent

Status
Not open for further replies.
Level 29
Joined
Mar 10, 2009
Messages
5,016
As we all know creating a region in jass is a pain in the ass, coz we cant do it directly like in GUI...we need to add the region to a rect first in order to function, this on the other hand is very straightforward...

I just need suggestions on what to add and remove, pls dont ask me to use textmacros (I wont)...


JASS:
/*
===RectEvent
===by Mckill2009

Use GetFilterUnit() for the unit that's entering and leaving

API:
    static method enter takes rect r, code cond returns nothing
    static method leave takes rect r, code cond returns nothing
    static method removeEnter takes rect r returns nothing
    static method removeLeave takes rect r returns nothing
    static method destroyAll takes rect r returns nothing
*/

library RectEvent

struct RectEvent extends array
    private static hashtable ht = InitHashtable()
    private static integer enterID = 0
    private static integer leaveID = 10
    
    static method enter takes rect r, code cond returns nothing
        local integer rID = GetHandleId(r)
        local trigger t
        local region reg
        if HaveSavedHandle(ht,rID,enterID) then
            debug call BJDebugMsg("RectEvent.enter ERROR: Trying allocate a double rect!")       
        else
            set t = CreateTrigger()
            set reg = CreateRegion()
            call SaveTriggerHandle(ht,rID,enterID,t) //uses 0
            call SaveRegionHandle(ht,rID,enterID+1,reg) //uses 1
            call RegionAddRect(reg,r)
            call TriggerRegisterEnterRegion(t,reg,Filter(cond))
            set t = null
            set reg = null
        endif
    endmethod
    
    static method leave takes rect r, code cond returns nothing
        local integer rID = GetHandleId(r)
        local trigger t
        local region reg
        if HaveSavedHandle(ht,rID,leaveID) then
            debug call BJDebugMsg("RectEvent.leave ERROR: Trying allocate a double rect!")       
        else
            set t = CreateTrigger()
            set reg = CreateRegion()
            call SaveTriggerHandle(ht,rID,leaveID,t) //uses 10
            call SaveRegionHandle(ht,rID,leaveID+1,reg) //uses 11
            call RegionAddRect(reg,r)
            call TriggerRegisterLeaveRegion(t,reg,Filter(cond))
            set t = null
            set reg = null
        endif
    endmethod
    
    static method removeEnter takes rect r returns nothing
        local integer rID = GetHandleId(r)
        if HaveSavedHandle(ht,rID,enterID) then
            call DestroyTrigger(LoadTriggerHandle(ht,rID,enterID))
            call RegionClearRect(LoadRegionHandle(ht,rID,enterID+1),r)
            call RemoveSavedHandle(ht,rID,enterID)
        else
            debug call BJDebugMsg("RectEvent.removeEnter ERROR: Trying to remove an invalid rect!")            
        endif
    endmethod
    
    static method removeLeave takes rect r returns nothing
        local integer rID = GetHandleId(r)
        if HaveSavedHandle(ht,rID,leaveID) then
            call DestroyTrigger(LoadTriggerHandle(ht,rID,leaveID))
            call RegionClearRect(LoadRegionHandle(ht,rID,leaveID+1),r)
            call RemoveSavedHandle(ht,rID,leaveID)
        else
            debug call BJDebugMsg("RectEvent.removeLeave ERROR: Trying to remove an invalid rect!")            
        endif
    endmethod
    
    static method destroyAll takes rect r returns nothing
        local integer rID = GetHandleId(r)
        if HaveSavedHandle(ht,rID,enterID) or HaveSavedHandle(ht,rID,leaveID) then
            call DestroyTrigger(LoadTriggerHandle(ht,rID,enterID))
            call RegionClearRect(LoadRegionHandle(ht,rID,enterID+1),r)  
            call DestroyTrigger(LoadTriggerHandle(ht,rID,leaveID))
            call RegionClearRect(LoadRegionHandle(ht,rID,leaveID+1),r) 
            call FlushChildHashtable(ht,rID)            
        else
            debug call BJDebugMsg("RectEvent.destroyAll ERROR: Trying to destroy an invalid rect!")            
        endif   
    endmethod
endstruct

endlibrary

USAGE:
JASS:
struct test
    static method enterBIG takes nothing returns nothing
        call BJDebugMsg("ENTERING BIG") 
    endmethod
    
    static method leaveBIG takes nothing returns nothing
        call BJDebugMsg("LEAVING BIG") 
    endmethod
    
    static method onInit takes nothing returns nothing 
        call RectEvent.enter(gg_rct_RegBig,function thistype.enterBIG)
        call RectEvent.leave(gg_rct_RegBig,function thistype.leaveBIG)
    endmethod
endstruct
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
we need to add the region to a rect first in order to function, this on the other hand is very straightforward...
Sorry but that is illegal...

A rect is a rectangular area of the map.
A region is a collection of map cells.

A region can be filled with cells contained by a rect.
A rect cannot be made from a region as there is no restriction forcing a region to be rectangular.
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
@ DSG, I guess that is what he meant, but still you are correct.

Furthermore, I have to agree with Baassee, because there is a 256 limit on the amount of hashtables. Thus it would certainly be benificial to use Tables.

Then, why do you limit the amount of pieces of code to be assigned to each rect?
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
JASS:
            call DestroyTrigger(LoadTriggerHandle(ht,rID,enterID))
            call RegionClearRect(LoadRegionHandle(ht,rID,enterID+1),r)  
            call DestroyTrigger(LoadTriggerHandle(ht,rID,leaveID))
            call RegionClearRect(LoadRegionHandle(ht,rID,leaveID+1),r)
I mean this, destroying destroyed trigger?
 
Level 16
Joined
Oct 12, 2008
Messages
1,570
@DSG
what?, you mean one rect one full function/code?...

This is illegal from your point of view:

JASS:
call RectEvent.enter(myRect, function Enter1)
call RectEvent.enter(myRect, function Enter2)

I know it is not smart to do, but sometimes you want your different actions within different scopes of encapsulation, and thus you would want to enable adding different code to the same rect..
 
Status
Not open for further replies.
Top