1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. The Lich King demands your service! We've reached the 19th edition of the Icon Contest. Come along and make some chilling servants for the one true king.
    Dismiss Notice
  4. The 4th SFX Contest has started. Be sure to participate and have a fun factor in it.
    Dismiss Notice
  5. The poll for the 21st Terraining Contest is LIVE. Be sure to check out the entries and vote for one.
    Dismiss Notice
  6. The results are out! Check them out.
    Dismiss Notice
  7. Don’t forget to sign up for the Hive Cup. There’s a 555 EUR prize pool. Sign up now!
    Dismiss Notice
  8. The Hive Workshop Cup contest results have been announced! See the maps that'll be featured in the Hive Workshop Cup tournament!
    Dismiss Notice
  9. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[Snippet] RegisterPlayerUnitEvent

Discussion in 'Graveyard' started by Magtheridon96, Sep 7, 2011.

  1. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    This system was made to replace that cumbersome GTrigger by Jesus4Lyf.
    Special thanks to Bribe, azlier and BBQ :)

    Code (vJASS):
    /**************************************************************
    *
    *   RegisterPlayerUnitEvent
    *   v5.1.0.1
    *   By Magtheridon96
    *
    *   I would like to give a special thanks to Bribe, azlier
    *   and BBQ for improving this library. For modularity, it only
    *   supports player unit events.
    *
    *   Functions passed to RegisterPlayerUnitEvent must either
    *   return a boolean (false) or nothing. (Which is a Pro)
    *
    *   Warning:
    *   --------
    *
    *       - Don't use TriggerSleepAction inside registered code.
    *       - Don't destroy a trigger unless you really know what you're doing.
    *
    *   API:
    *   ----
    *
    *       - function RegisterPlayerUnitEvent takes playerunitevent whichEvent, code whichFunction returns nothing
    *           - Registers code that will execute when an event fires.
    *       - function RegisterPlayerUnitEventForPlayer takes playerunitevent whichEvent, code whichFunction, player whichPlayer returns nothing
    *           - Registers code that will execute when an event fires for a certain player.
    *       - function GetPlayerUnitEventTrigger takes playerunitevent whichEvent returns trigger
    *           - Returns the trigger corresponding to ALL functions of a playerunitevent.
    *
    **************************************************************/

    library RegisterPlayerUnitEvent // Special Thanks to Bribe and azlier
        globals
            private trigger array t
        endglobals
       
        function RegisterPlayerUnitEvent takes playerunitevent p, code c returns nothing
            local integer i = GetHandleId(p)
            local integer k = 15
            if t[i] == null then
                set t[i] = CreateTrigger()
                loop
                    call TriggerRegisterPlayerUnitEvent(t[i], Player(k), p, null)
                    exitwhen k == 0
                    set k = k - 1
                endloop
            endif
            call TriggerAddCondition(t[i], Filter(c))
        endfunction
       
        function RegisterPlayerUnitEventForPlayer takes playerunitevent p, code c, player pl returns nothing
            local integer i = 16 * GetHandleId(p) + GetPlayerId(pl)
            if t[i] == null then
                set t[i] = CreateTrigger()
                call TriggerRegisterPlayerUnitEvent(t[i], pl, p, null)
            endif
            call TriggerAddCondition(t[i], Filter(c))
        endfunction
       
        function GetPlayerUnitEventTrigger takes playerunitevent p returns trigger
            return t[GetHandleId(p)]
        endfunction
    endlibrary


    Here's a vanilla Jass version:

    Code (vJASS):
    //**************************************************************
    //*
    //*   RegisterPlayerUnitEvent (Vanilla Jass)
    //*   v5.1.0.1
    //*   By Magtheridon96
    //*
    //*   I would like to give a special thanks to Bribe, azlier
    //*   and BBQ for improving this library. For modularity, it only
    //*   supports player unit events.
    //*
    //*   Functions passed to RegisterPlayerUnitEvent must either
    //*   return a boolean (false) or nothing. (Which is a Pro)
    //*
    //*   Implementation:
    //*   ---------------
    //*
    //*       - Copy all this script into a new trigger called "RegisterPlayerUnitEvent Jass"
    //*       - Create a trigger array variable called RPUE.
    //*       - Done.
    //*
    //*   Warning:
    //*   --------
    //*
    //*       - Don't use TriggerSleepAction inside registered code.
    //*       - Don't destroy a trigger unless you really know what you're doing.
    //*
    //*   API:
    //*   ----
    //*
    //*       - function RegisterPlayerUnitEvent takes playerunitevent whichEvent, code whichFunction returns nothing
    //*           - Registers code that will execute when an event fires.
    //*       - function RegisterPlayerUnitEventForPlayer takes playerunitevent whichEvent, code whichFunction, player whichPlayer returns nothing
    //*           - Registers code that will execute when an event fires for a certain player.
    //*       - function GetPlayerUnitEventTrigger takes playerunitevent whichEvent returns trigger
    //*           - Returns the trigger corresponding to ALL functions of a playerunitevent.
    //*
    //**************************************************************
    function RegisterPlayerUnitEvent takes playerunitevent p, code c returns nothing
        local integer i = GetHandleId(p)
        local integer k = 15
        if udg_RPUE[i] == null then
            set udg_RPUE[i] = CreateTrigger()
            loop
                call TriggerRegisterPlayerUnitEvent(udg_RPUE[i], Player(k), p, null)
                exitwhen k == 0
                set k = k - 1
            endloop
        endif
        call TriggerAddCondition(udg_RPUE[i], Filter(c))
    endfunction

    function RegisterPlayerUnitEventForPlayer takes playerunitevent p, code c, player pl returns nothing
        local integer i = 16 * GetHandleId(p) + GetPlayerId(pl)
        if udg_RPUE[i] == null then
            set udg_RPUE[i] = CreateTrigger()
            call TriggerRegisterPlayerUnitEvent(udg_RPUE[i], pl, p, null)
        endif
        call TriggerAddCondition(udg_RPUE[i], Filter(c))
    endfunction

    function GetPlayerUnitEventTrigger takes playerunitevent p returns trigger
        return udg_RPUE[GetHandleId(p)]
    endfunction
       
    function InitTrig_RegisterPlayerUnitEvent_Jass takes nothing returns nothing
    endfunction


    Feel free to comment..
     
    Last edited: Oct 8, 2012
  2. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    I haven't read your code, just your functions descriptions.
    Could you add an "EVERY" constant ? (a "random" negative value seems fine)

    I mean for orders sometimes we don't need to catch a specific order, just any order.
    Sure we could still create a trigger and add it this event, like the old good way.

    It would also be useful for the other events, but orders are the most obvious one.
     
  3. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    From what you told me, I understood that you want me to add an "Any Order" event.
    If that's true, sure i'll add it bro :)
     
  4. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    Well i'm sure it is useful for order events, but it could also be for other events, that's why i'm suggesting a constant integer.

    Code (vJASS):
    constant integer CommonEvent_ANY = -42


    Code (vJASS):
    ...

    call registerBeginCast(CommonEvent_ANY,<boolexpr>) // any ability
    call registerItemUse(CommonEvent_ANY,<boolexpr>) // any item


    Ofc the constant name could (must ?) be improved.
    Alternatively you could also create several constants, one for each event "type" (order, item, ...) but i personnaly don't like this idea.
     
  5. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    Ok.. I'll just remove those events in the struct and use this instead :p
     
  6. Nestharus

    Nestharus

    Joined:
    Jul 10, 2007
    Messages:
    6,146
    Resources:
    8
    Spells:
    3
    Tutorials:
    4
    JASS:
    1
    Resources:
    8
    Also, player specific and unit specific events would be nice.
     
  7. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    ;_;
    If you list them, it would be much easier for me >:p

    Come to think of it Troll-Brain, it would be more efficient if I keep the system the way it is and just add an "AnyOrder" event.
    That way, The entire API would inline :D
     
  8. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    But it would be less funny for us :/

    Like i said order events are only one example ...
    And seriously who care about register event inlines, it's just better to work on the API usage. (plz refrain your speed-freak)
     
  9. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    I don't know :S
    I'll add it and see how the public likes it :p
     
  10. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    Since when there is a public for the jass section ?
     
  11. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    Since the day I decided to link my resources in my sig >:D
     
  12. Troll-Brain

    Troll-Brain

    Joined:
    Apr 27, 2008
    Messages:
    2,372
    Resources:
    1
    JASS:
    1
    Resources:
    1
    Mouahahaha the jass section is more likely a semi-private flood forum.

    (Ok, end of the silly flood for me, i couldn't resist, sorry :p )
     
  13. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    Nestharus, Player-specific events would be way too messy =/
    I'll see what I can do, but if I go beyond 600 lines, NO DEAL XD
     
  14. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,053
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    This line is awkward:

    call UD.remove(i) // To avoid bugs after the handle id is recycled.

    The unit could die multiple times :/

    I think for death events you should let users just use Nestharus' Unit
    Event.

    This event-player-leave stuff is also strange to include in this library.
    >> call TriggerRegisterPlayerEvent(c,p,EVENT_PLAYER_LEAVE)

    I will show you how to make this a lot shorter code (works for all
    playerunitevents):

    Code (vJASS):
    function RegisterPlayerUnitEvent takes boolexpr condition, playerunitevent pu returns nothing
        globals
            private trigger array trigs
        endglobals
        local integer id = GetHandleId(pu)
        if trigs[id] == null then
            set trigs[id] = CreateTrigger()
            call TriggerRegisterAnyUnitEventBJ(trigs[id], pu)
        endif
        call TriggerAddCondition(trigs[id], condition)
    endfunction
     
    Last edited: Sep 8, 2011
  15. baassee

    baassee

    Joined:
    Nov 14, 2008
    Messages:
    3,220
    Resources:
    17
    Spells:
    14
    Tutorials:
    3
    Resources:
    17
    I am the public of the JASS section.

    Oo loooots of tables, I'm happy that it's not Vex's table. Anyway I'm very thankful for this as I hate the stupid GT (have modified it myself just because of that matter). Only thing that drives me mad but will probably not affect anyone else is the
    exitwhen i>15
    but I can make that constant myself and filter out nonplaying players aswell (so what I mean is that you should keep it). Brilliant.

    And the effect event should be in, just static if it out if the player already used Bribe's. (Or maybe merge with Bribe's? Sorry Bribe :D)
     
  16. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    Good idea :)
    Actually, the next version will be much shorter, and I won't be able to do this for ... reasons :p
     
  17. baassee

    baassee

    Joined:
    Nov 14, 2008
    Messages:
    3,220
    Resources:
    17
    Spells:
    14
    Tutorials:
    3
    Resources:
    17
    Go to bed god damn it. Stop be in such a rush all the time :)
     
  18. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    It's actually 12 am, so maybe I will go to bed soon since school starts in 2 weeks >:p
    I'm trying to get this to be very efficient and start only 1 thread per event.
    I'm using an instance of Table as an "EventMap".
    Each player will have an instance of the Event struct for each playerunitevent.
    There are only 2 player events I'm going to include here, so I'm doing something different for them :p
    I'm also going to include a "0.00 second elapsed game time event".

    edit
    Updated.
     
    Last edited: Sep 8, 2011
  19. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,053
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    Mag, you did "registerPlayerUnitEvent" wrong, the boolexpr is pretty
    useless because you get get data like GetSpellAbilityId() and the like.
    It only recognizes a filter unit.

    You also don't need a Table, because player unit events only go up to
    270 or something on the handle IDs, so you can use arrays. And you
    don't need a Table for the player id because that's going to be 0-15
    which is also fine of course. Trust me, the way I set it up in my first
    post is the shortest and sweetest way to do it ;)
     
  20. Magtheridon96

    Magtheridon96

    Joined:
    Dec 12, 2008
    Messages:
    6,006
    Resources:
    26
    Maps:
    1
    Spells:
    8
    Tutorials:
    7
    JASS:
    10
    Resources:
    26
    oh ok :)
    I'll fix this up really fast before anyone uses it xD

    edit

    Updated.
    I'm still using Table though.