1. Are you planning to upload your awesome spell or system to Hive? Please review the rules here.
    Dismiss Notice
  2. 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
  3. 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 haven't received your rank award? Then please contact the administration.
    Dismiss Notice
  4. Rubbed the right way, the genie is out of its lamp! The 12th Concept Art Contest Results have been announced.
    Dismiss Notice
  5. The Lich King has a new champion. The results for the 19th Icon Contest are here!
    Dismiss Notice
  6. Race against the odds and Reforge, Don't Refund. The 14th Techtree Contest has begun!
    Dismiss Notice
  7. 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.

Zwiebelchen's Threat System 2.7

Submitted by Zwiebelchen
This bundle is marked as approved. It works and satisfies the submission rules.
What is a threat system?
Basicly, it's a system that takes over Mob AI. The unit that dealt the most damage to the creep will be the one that gets attacked.
Although that is only half the truth, it should be enough to get the idea.

If you ever played World of Warcraft (as a mapper, you should have), chances are you know what a threat system does.
And this is an almost perfect replica of it.


Requirements:
- vJass preprocessor

What is different from this to other systems:
- First of all, this system is almost (if not completely) 100% fail-safe. It does checks on killed AND removed units and clears them automaticly.

- Second, it features a unique camp squad functionality, which ensures that units that are preplaced in camps will always attack and return together. If units return, they will be rendered invulnerable until they reach their original camp position again. If they can not reach their camping position within a certain duration, they will get instantly teleported back.
Like this, there is almost no more kiting or bug-abusing possible, as it was with other systems.

- Third: It does not block other Spellcasting systems. The system will only give orders to units, if the unit either has no order (i.e. auto-engage) or an "attack" order. If you want the unit to use a spell instead of attacking, you can do so whenever you want without having to fear interference of the system.
In fact, there is a special function provided so that people can create their own Spell-AI directly in a custom trigger

- Fourth: It provides a lot of useful functions and Getters (Like ApplyHealThreat or GetCombatState), to make triggering spells even more easy.

- Fifth: It is, by far, the fastest and most flexible threat system out there. I completely remastered the system from the last version and the entire script (except for certain enumerations that can not be avoided) is now O(1) complexity.


Version history:
2.7
- Fixed a bug that sometimes did not properly render units in-combat if pulled again right after returning to camp position

2.6
- Added a new function: ZTS_GetCombatTime, which returns the time an npc unit is currently in combat; returns 0 if the unit is currently out of combat or returning to camp position; does not work on player units

2.5
- Added a new boolean to the ZTS_AddThreatUnit command that determines wether the added unit shall be added to already fighting Creep Camps or only to non-fighting Creep Camps - this is interesting when you create encounters that summon units as you usually don't want those summoned units to create their own Creep Camp but want them to be added to the Boss' Creep Camp
- Fixed a small bug that sometimes caused linked Creep Camps

2.4
- Fixed a bug sometimes rendering units permanently invulnerable, when registering a unit in close range to currently retreating units

2.3
- Changed TriggerActions of dynamic triggers to TriggerConditions, to avoid nasty action leak
- Fixed GetThreatUnitAmount (did not return the correct value)

2.2b
- fixed a small logic bug with GetThreatUnitAmount and GetThreatUnitPosition

2.2
- Now uses "smart" order instead of "attack" to issue attack orders ... it turned out that the "smart" order returns false if the unit can not reach the target (for example when rooted) - weird, as it doesn't work for "attack" for some reason
- because of that, the AddRootAbility function and the Range Setter and Getters were removed, as they have become obsolete

2.1b
- fixed a small logical bug with GetCombatState sometimes returning a false positive

2.1
- rebuilt Update function to avoid some useless enumerations - depending on the number of PlayerUnits registered, the system should now be MULTIPLE TIMES faster - As a side effect, I could also remove some useless variables

2.0
- Initial release

Comments & Discussion:

Why hashtables instead of global arrays and structs?
- hashtables are not limited in terms of max size, unlike and array of structs with a unit array, which reaches the 8000 limit very fast ... this was basicly the most important point on that decision
- hashtables are more flexible and easier to use (Flush functions, etc.)
- hashtables were benchmarked to be only 60-80% slower than getting UnitUserData alone

Where is the sort function?
- version 2.0 and higher does not use sorting anymore; instead, when threat is applied to a unit, it uses an insertion method to keep the order of the list

What is new compared to pre 2.0 versions?
- Aside from the fact that the system now is a dozen times faster than before, I also improved the AI by using the "smart" instead of "attack" order. It turned out that - in a weird way - "smart" is indeed smarter than other orders, as it returns false if the unit can't reach the target (i.e. because of root)
- There is now a way to directly get an Order event by the threat system, to make creating spell-AI easier.

Does it matter how many units are registered to the system at the same time?
- In terms of speed, no; it only affects memory usage, but that should not have an impact on game performance at all, even with tousands of units registered - only the number of currently fighting units affects runtime

I still do not really understand how to use the system...
- just check the demo map and you'll get the idea

Keywords:
threat, aggro, wow, world of warcraft
Contents

Threat System v2.7 (Map)

Reviews
Moderator
17:56, 17th Jan 2010 TriggerHappy: Very good, and you know what my suggestions are. This can be approved in it's current state, though.
  1. 17:56, 17th Jan 2010
    TriggerHappy:

    Very good, and you know what my suggestions are.
    This can be approved in it's current state, though.
     
  2. Wasp

    Wasp

    Joined:
    Nov 16, 2009
    Messages:
    68
    Resources:
    0
    Resources:
    0
    Well done. May i will use it in my map.
    But one question, the hero with the most threat gets attacked, but do you have threat for every enemy creep, so that two people for example have threat from 2 different creeps. Or has every hero threat for all creeps, and the hero with the most threat gets attacked from all?
     
  3. Zwiebelchen

    Zwiebelchen

    Joined:
    Sep 17, 2009
    Messages:
    6,822
    Resources:
    12
    Models:
    5
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    JASS:
    4
    Resources:
    12
    Every enemy has its own threat list. You can see that in the demo map.
     
  4. Wasp

    Wasp

    Joined:
    Nov 16, 2009
    Messages:
    68
    Resources:
    0
    Resources:
    0
    Ok, and pls put the code in a spoiler.
     
  5. Justify

    Justify

    Joined:
    Mar 16, 2008
    Messages:
    935
    Resources:
    5
    Models:
    1
    Icons:
    1
    Spells:
    3
    Resources:
    5
    Well, I've created such a system a while ago, and it seems that you've put a lot of effort into it (I mean, I wrote it while starting with vJass and it was a mess :D).
    Still, I don't see the point with hashtables.
    You already said that they're "only" 60-80% slower.
    Regarding on how often they're called, I think AutoIndex would make this system much more performant.
    However, this is only a small part.
    I realy like it and it's well coded. Good work.
    Oh, and I just saw one thing:
    Why do you use "GetUnitTypeId==0" for a "null"-unit check? Check for == null?^^
     
  6. X-OMG-X

    X-OMG-X

    Joined:
    Nov 10, 2008
    Messages:
    1,942
    Resources:
    16
    Icons:
    1
    Skins:
    3
    Maps:
    2
    Spells:
    8
    Template:
    1
    Tutorials:
    1
    Resources:
    16
    Please put the code into hidden tag :)
     
  7. Zwiebelchen

    Zwiebelchen

    Joined:
    Sep 17, 2009
    Messages:
    6,822
    Resources:
    12
    Models:
    5
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    JASS:
    4
    Resources:
    12
    I know about that, but as I already wrote in the FAQ inside the initial post, I decided against it, mainly because pseudo 2-dimensional arrays have a HUGE drawback: They are limited to 8192. While this sounds a lot, it actually isn't, when you think about that each row multiplies with the number of columns.
    Let's say you have 100 units registered to the system - that means, that every array can only be 80 list entries long. With 200 registered units, it comes down to only 40 entries, etc.
    Sure, I could workaround that by just having those units directly around the player registered to the system, but that would come with huge drawbacks in terms of user-comfort.
    In the end, I prefered the flexibility of hashtables over 2-dimensional arrays and - let's face it - the system is fast enough for modern machines. The minor speed increase of arrays (even the most simple functions would only get a speed increase of like 2-3% when I'd upgrade to 2-d arrays) wasn't worth the flexibility loss.

    That's because Unit == null is unsafe since 1.24.
    References remain when the handle got removed from the game, if you do not properly null all global handle variables. This is called reference leak.

    You can check it out with the following function:
    Code (vJASS):

    globals
        unit U
    endglobals

    function testme takes nothing returns boolean
        set U = CreateUnit( ... )
        call RemoveUnit(U)
        return U == null
    endfunction
     

    The above function returns false.



    As I could not find a tag to hide the JASS code (spoiler and hide, such as hidden did not work), I removed the code from the post. You can check the code in the demomap better anyways, as its correctly colored and indended.
     
  8. Justify

    Justify

    Joined:
    Mar 16, 2008
    Messages:
    935
    Resources:
    5
    Models:
    1
    Icons:
    1
    Spells:
    3
    Resources:
    5
    For sure this return false, it always did?
    The reference is saved although it's removed, at least
    I thought so before 1.24 too
     
  9. Zwiebelchen

    Zwiebelchen

    Joined:
    Sep 17, 2009
    Messages:
    6,822
    Resources:
    12
    Models:
    5
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    JASS:
    4
    Resources:
    12
    And that's why var == null checks are not safe. QED.
     
  10. Justify

    Justify

    Joined:
    Mar 16, 2008
    Messages:
    935
    Resources:
    5
    Models:
    1
    Icons:
    1
    Spells:
    3
    Resources:
    5
    Depending on what you want to check.
    If you want to know if the unit still exists,
    you can check for it's life (or use UnitAlive, although its a bit bugged).
    Checking for null checks for the reference, which
    is only killed by nulling. Doesn't matter now ;)
     
  11. Hobbit_baron

    Hobbit_baron

    Joined:
    Apr 4, 2009
    Messages:
    24
    Resources:
    0
    Resources:
    0
    well done :) 5/5 very useful in lots of types of maps, great work!
     
  12. GurkenKinG

    GurkenKinG

    Joined:
    Jul 3, 2008
    Messages:
    10
    Resources:
    0
    Resources:
    0
    Amazing work, I can think of so many maps which definitively need this =) 5/5
     
  13. Zwiebelchen

    Zwiebelchen

    Joined:
    Sep 17, 2009
    Messages:
    6,822
    Resources:
    12
    Models:
    5
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    JASS:
    4
    Resources:
    12
    ... or I could just leave it as it is.
     
  14. Zwiebelchen

    Zwiebelchen

    Joined:
    Sep 17, 2009
    Messages:
    6,822
    Resources:
    12
    Models:
    5
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    JASS:
    4
    Resources:
    12
    Update! version 2.3

    - fixed a leak with dynamic triggers
    - fixed GetThreatUnitAmount
     
  15. Nyrano

    Nyrano

    Joined:
    Jan 20, 2010
    Messages:
    3
    Resources:
    0
    Resources:
    0
    Just a small question... Great system though, 5/5!

    It works perfect for unit-target abilities but it is not working for my AoE thunderclap-based ability. Must I adjust something to let it work for non-targeted AoE? =]
     
  16. Zwiebelchen

    Zwiebelchen

    Joined:
    Sep 17, 2009
    Messages:
    6,822
    Resources:
    12
    Models:
    5
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    JASS:
    4
    Resources:
    12
    Any more suggestions or comments?
     
  17. Zwiebelchen

    Zwiebelchen

    Joined:
    Sep 17, 2009
    Messages:
    6,822
    Resources:
    12
    Models:
    5
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    JASS:
    4
    Resources:
    12
    Hmm, depends on your spell trigger, of course. You'd have to pick all units in range of the target location and apply the threat to them.
     
  18. Frunck

    Frunck

    Joined:
    Nov 11, 2009
    Messages:
    172
    Resources:
    1
    Spells:
    1
    Resources:
    1
    5/5 For awesome system
     
  19. Forgotten_Warlord

    Forgotten_Warlord

    Joined:
    Nov 6, 2009
    Messages:
    1,379
    Resources:
    38
    Models:
    34
    Icons:
    4
    Resources:
    38
    Works great! 5/5 just one suggestion, maybe have a bar or something so in an rpg people can see the bar on the unit so they know who has the most threat :) I wouldn't use a multiboard in my rpg for threat :)