Snippet Unit Detection

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
JASS:

--
    _   _   ___   ___
   | | | | |  _| | _ \ 
   | | | | | |_  || \ |
   | |_| | | |_  ||_/ |
   \_____/ |___| |___/
   
~Unit Events Detection~
  ~Version : v1.0.3~

Contains :
- Unit Events Detection : Basic (v1.0.0)
- Unit Events Detection : Enter n Leave (v1.0.3)
- Unit Events Detection : Pause n Invulnerable (v1.0.0)
- Unit Events Detection : Group Extension (v1.0.0)
- Unit Events Detection : Heal (v1.0.0)


JASS:

////////////////////////////////////////////////////////////////
//  _   _   ___   ___
// | | | | |  _| | _ \    UNIT EVENTS DETECTION : BASIC v1.0.0
// | | | | | |_  || \ |       by kingking
// | |_| | | |_  ||_/ |
// \_____/ |___| |___/
//  
//  This library is a basic for other UED components.
//
//  Functions Provided :
//  GetEventUnit() -> unit
//
//  Requires :
//  Jasshelper 0.9.K.0 or later
/////////////////////////////////////////////////////////////////
library UEDxBasic

    globals
        public unit u
    endglobals
    
    function GetEventUnit takes nothing returns unit
        return u
    endfunction
    
endlibrary


JASS:

////////////////////////////////////////////////////////////////
//  _   _   ___   ___
// | | | | |  _| | _ \    UNIT EVENTS DETECTION : ENTER n LEAVE v1.0.3
// | | | | | |_  || \ |       by kingking
// | |_| | | |_  ||_/ |
// \_____/ |___| |___/
//  
//  This library is the extension of Unit Events Detection.
//  It detects for units that enters, dies and get removed in game.
//
//  Functions Provided :
//  RegisterUnitDetected(trigger)
//  RegisterUnitDeath(trigger)
//  RegisterUnitRemoved(trigger)
//
//  Requires :
//  Event
//  Unit Events Detection : Basic
//  Jasshelper 0.9.K.0 or later
/////////////////////////////////////////////////////////////////
library UEDxEnL initializer Init requires Event, UEDxBasic
    
    globals
    //How faster the library search for new units.
        private constant real UNIT_DETECTION_PERIOD = 1.
    endglobals
    
    globals
        private trigger EnterDetection
        private Event EnterEvent
        private Event DeathEvent
        private Event RemoveUnitEvent
        private group Units
        private region World
    endglobals
    
    globals
        private unit temp
    endglobals
    
    function RegisterUnitDeath takes trigger whichtrigger returns integer
        return DeathEvent.register(whichtrigger)
    endfunction
    
    private function EnumGroup takes nothing returns nothing
        set temp = GetEnumUnit()
        if IsUnitType(temp, UNIT_TYPE_DEAD) == true and IsUnitType(temp,UNIT_TYPE_HERO) == false then
            set UEDxBasic_u = temp
            call DeathEvent.fire()
        endif
    endfunction
    
    private function CheckDeathUnits takes nothing returns nothing
        call ForGroup(Units,function EnumGroup)
    endfunction
    
    //Entering Event
    function RegisterUnitDetected takes trigger whichtrigger returns integer
        return EnterEvent.register(whichtrigger)
    endfunction
    
    private function ExecuteEnterEvent takes nothing returns boolean
        set UEDxBasic_u = GetTriggerUnit()
        call GroupAddUnit(Units,UEDxBasic_u)
        call EnterEvent.fire()
        return false
    endfunction
    
    private function FirePreplacedUnitsDetected takes nothing returns nothing
        set UEDxBasic_u = GetEnumUnit()
        call EnterEvent.fire()
    endfunction
    
    private function EnumPreplacedUnits takes nothing returns nothing
        call ForGroup(Units,function FirePreplacedUnitsDetected)
        call PauseTimer(GetExpiredTimer())
        call DestroyTimer(GetExpiredTimer())
    endfunction
    
    function RegisterUnitRemoved takes trigger whichtrigger returns integer
        return RemoveUnitEvent.register(whichtrigger)
    endfunction
    
    private function ExecuteRemoveEvent takes unit whichunit returns nothing
        set UEDxBasic_u = whichunit
        call RemoveUnitEvent.fire()
    endfunction
    
    hook RemoveUnit ExecuteRemoveEvent
    
     //Init
    private function RetTrue takes nothing returns boolean
        return true
    endfunction
    
    private function Init takes nothing returns nothing
        local conditionfunc rettrue = Condition(function RetTrue)
        local integer i = 15
        
        //Init Events
        set EnterEvent = Event.create()
        set DeathEvent = Event.create()
        set RemoveUnitEvent = Event.create()
        
        //Get preplaced units
        set Units = CreateGroup()
        loop
            call GroupEnumUnitsOfPlayer(Units,Player(i),rettrue)
        exitwhen i == 0
            set i = i - 1
        endloop
        //
        
        //Init Entering detection
        set World = CreateRegion()
        call RegionAddRect(World,bj_mapInitialPlayableArea)
        
        set EnterDetection = CreateTrigger()
        call TriggerRegisterEnterRegion(EnterDetection,World,rettrue)
        call TriggerAddCondition(EnterDetection,Condition(function ExecuteEnterEvent))
        //
        call TimerStart(CreateTimer(),0.,false,function EnumPreplacedUnits)
        call TimerStart(CreateTimer(),UNIT_DETECTION_PERIOD,true,function CheckDeathUnits)
        
        set rettrue = null
    endfunction
endlibrary


JASS:

////////////////////////////////////////////////////////////////
//  _   _   ___   ___
// | | | | |  _| | _ \    UNIT EVENTS DETECTION : PAUSE n INVULNERABLE v1.0.0
// | | | | | |_  || \ |       by kingking
// | |_| | | |_  ||_/ |
// \_____/ |___| |___/
//  
//  This library is the extension of Unit Events Detection.
//  It detects for pausing, unpausing, vulnerabling and invulnerabling event.
//
//  Functions Provided :
//  RegisterUnitInvulnerabled(trigger)
//  RegisterUnitPaused(trigger)
//  RegisterUnitVulnerabled(trigger)
//  RegisterUnitUnpaused(trigger)
//
//  Requires :
//  Event
//  Unit Events Detection : Basic
//  Jasshelper 0.9.K.0 or later
/////////////////////////////////////////////////////////////////
library UEDxPnI initializer Init requires UEDxBasic,Event

    globals
        private Event InvulnerableEvent
        private Event VulnerableEvent
        private Event PauseUnitEvent
        private Event UnpauseUnitEvent
    endglobals
    
    //Invulnerable/Vulnerable Event
    function RegisterUnitInvulnerabled takes trigger whichtrigger returns integer
        return InvulnerableEvent.register(whichtrigger)
    endfunction
    
    function RegisterUnitVulnerabled takes trigger whichtrigger returns integer
        return VulnerableEvent.register(whichtrigger)
    endfunction
    
    function ExecuteInvulnerableEvent takes unit whichunit, boolean flag returns nothing
        set UEDxBasic_u = whichunit
        if flag then
            call InvulnerableEvent.fire()
        else
            call VulnerableEvent.fire()
        endif
    endfunction
    
    hook SetUnitInvulnerable ExecuteInvulnerableEvent
    
    //Pause/Unpause Event
    function RegisterUnitPaused takes trigger whichtrigger returns integer
        return PauseUnitEvent.register(whichtrigger)
    endfunction

    function RegisterUnitUnpaused takes trigger whichtrigger returns integer
        return UnpauseUnitEvent.register(whichtrigger)
    endfunction
    
    function ExecuteUnitPauseEvent takes unit whichunit, boolean flag returns nothing
        set UEDxBasic_u = whichunit
        if flag then
            call PauseUnitEvent.fire()
        else
            call UnpauseUnitEvent.fire()
        endif
    endfunction
    
    hook PauseUnit ExecuteUnitPauseEvent
    
    private function Init takes nothing returns nothing
        set InvulnerableEvent = Event.create()
        set VulnerableEvent = Event.create()
        set PauseUnitEvent = Event.create()
        set UnpauseUnitEvent = Event.create()
    endfunction
endlibrary


JASS:

////////////////////////////////////////////////////////////////
//  _   _   ___   ___
// | | | | |  _| | _ \    UNIT EVENTS DETECTION : GROUP EXTENSION v1.0.0
// | | | | | |_  || \ |       by kingking
// | |_| | | |_  ||_/ |
// \_____/ |___| |___/
//  
//  This library is the extension of Unit Events Detection.
//  It detects for units that enters and leaves the group.
//
//  Functions Provided :
//  GetCurrentGroup() -> group (Replace the missing function in group enum callback.)
//  GetTriggerGroup() -> group (Triggered Group)
//  RegisterUnitEnterGroup(trigger)
//  RegisterUnitLeaveGroup(trigger)
//
//  Requires :
//  Event
//  Unit Events Detection : Basic
//  Jasshelper 0.9.K.0 or later
/////////////////////////////////////////////////////////////////
library UEDxGEx initializer Init requires UEDxBasic, Event
    
    globals
        private Event EnterGroupEvent
        private Event LeaveGroupEvent
        private group g
    endglobals
    
    //Group Extension
    function GetCurrentGroup takes nothing returns group
        return g
    endfunction
    
    function GetGroupEnumUnitsInRange takes group whichGroup, real x, real y, real radius, boolexpr filter returns nothing
        set g = whichGroup
    endfunction
    
    function GetGroupEnumUnitsInRangeCounted takes group whichGroup, real x, real y, real radius, boolexpr filter, integer countLimit returns nothing
        set g = whichGroup
    endfunction
    
    function GetGroupEnumUnitsInRangeOfLoc takes group whichGroup, location whichLocation, real radius, boolexpr filter returns nothing
        set g = whichGroup
    endfunction
    
    function GetGroupEnumUnitsInRangeOfLocCounted takes group whichGroup, location whichLocation, real radius, boolexpr filter, integer countLimit returns nothing
        set g = whichGroup
    endfunction
    
    function GetGroupEnumUnitsInRect takes group whichGroup, rect r, boolexpr filter returns nothing
        set g = whichGroup
    endfunction
    
    function GetGroupEnumUnitsInRectCounted takes group whichGroup, rect r, boolexpr filter, integer countLimit returns nothing
        set g = whichGroup
    endfunction
    
    function GetGroupEnumUnitsOfPlayer takes group whichGroup, player whichPlayer, boolexpr filter returns nothing
        set g = whichGroup
    endfunction
    
    function GetGroupEnumUnitsOfType takes group whichGroup, string unitname, boolexpr filter returns nothing
        set g = whichGroup
    endfunction
    
    function GetGroupEnumUnitsOfTypeCounted takes group whichGroup, string unitname, boolexpr filter, integer countLimit returns nothing
        set g = whichGroup
    endfunction
    
    function GetGroupEnumUnitsSelected takes group whichGroup, player whichPlayer, boolexpr filter returns nothing
        set g = whichGroup
    endfunction
    
    hook GroupEnumUnitsInRange GetGroupEnumUnitsInRange
    hook GroupEnumUnitsInRangeCounted GetGroupEnumUnitsInRangeCounted
    hook GroupEnumUnitsInRangeOfLoc GetGroupEnumUnitsInRangeOfLoc
    hook GroupEnumUnitsInRangeOfLocCounted GetGroupEnumUnitsInRangeOfLocCounted
    hook GroupEnumUnitsInRect GetGroupEnumUnitsInRect
    hook GroupEnumUnitsInRectCounted GetGroupEnumUnitsInRectCounted
    hook GroupEnumUnitsOfPlayer GetGroupEnumUnitsOfPlayer
    hook GroupEnumUnitsOfType GetGroupEnumUnitsOfType
    hook GroupEnumUnitsOfTypeCounted GetGroupEnumUnitsOfTypeCounted
    hook GroupEnumUnitsSelected GetGroupEnumUnitsSelected
    
    globals
        private group ge
    endglobals
    
    function GetTriggerGroup takes nothing returns group
        return ge
    endfunction
    
    function RegisterUnitEnterGroup takes trigger whichtrigger returns integer
        return EnterGroupEvent.register(whichtrigger)
    endfunction
    
    function RegisterUnitLeaveGroup takes trigger whichtrigger returns integer
        return LeaveGroupEvent.register(whichtrigger)
    endfunction
    
    private function ExecuteEnterGroupEvent takes group whichgroup, unit whichunit returns nothing
        set ge = whichgroup
        set UEDxBasic_u = whichunit
        call EnterGroupEvent.fire()
    endfunction
    
    private function ExecuteLeaveGroupEvent takes group whichgroup, unit whichunit returns nothing
        set ge = whichgroup
        set UEDxBasic_u = whichunit
        call LeaveGroupEvent.fire()
    endfunction
    
    hook GroupAddUnit ExecuteEnterGroupEvent
    hook GroupRemoveUnit ExecuteLeaveGroupEvent
    
    private function Init takes nothing returns nothing
        set EnterGroupEvent = Event.create()
        set LeaveGroupEvent = Event.create()
    endfunction
endlibrary


JASS:

////////////////////////////////////////////////////////////////
//  _   _   ___   ___
// | | | | |  _| | _ \    UNIT EVENTS DETECTION : HEAL v1.0.0
// | | | | | |_  || \ |       by kingking
// | |_| | | |_  ||_/ |
// \_____/ |___| |___/
//  
//  This library is the extension of Unit Events Detection.
//  It detects for healing event.
//
//  Functions Provided :
//  RegisterUnitHealed(trigger)
//  GetHealer() -> unit
//  GetHealAmount() -> real
//  UnitHealTarget(healer, target, real amount)
//
//  Requires :
//  Event
//  Unit Events Detection : Basic
//  Jasshelper 0.9.K.0 or later
/////////////////////////////////////////////////////////////////
library UEDxHeal initializer Init requires Event, UEDxBasic
    
    globals
        private Event HealEvent
    endglobals
    
    globals
        private unit healer
        private real healamount
    endglobals
    
    function GetHealer takes nothing returns unit
        return healer
    endfunction
    
    function GetHealAmount takes nothing returns real
        return healamount
    endfunction
    
    function RegisterUnitHealed takes trigger whichtrigger returns integer
        return HealEvent.register(whichtrigger)
    endfunction
    
    function UnitHealTarget takes unit from, unit targ, real amount returns boolean
        if IsUnitType(targ,UNIT_TYPE_DEAD) == false then//Prevent healing on death units
            call SetWidgetLife(targ,GetWidgetLife(targ) + amount)
            set healer = from
            set healamount = amount
            set UEDxBasic_u = targ
            call HealEvent.fire()
            return true
        endif
        return false
    endfunction
    
    private function Init takes nothing returns nothing
        set HealEvent = Event.create()
    endfunction
endlibrary
 

Nestharus

o-o
Reaction score
84
AIDS?

[ljass]hook RemoveUnit RemoveUnitDetection[/ljass]

: (

JASS:
loop
            call GroupEnumUnitsOfPlayer(DetectedUnits,Player(i),rtrue)
        exitwhen i == 0
            set i = i - 1
        endloop
        
        call TriggerSleepAction(.0) //Allow all triggers to be registered.
        call ForGroup(DetectedUnits,function FirePreplacedUnitsDetection)

You don't need that ForGroup in there : |

JASS:
boolexpr firePreplacedUnitsDetection = Condition(function FirePreplacedUnitsDetection)
call GroupEnumUnitsOfPlayer(DetectedUnits, Player(i), firePreplacedUnitsDetection)


Then [ljass]GetFilterUnit()[/ljass] and you can return true if you want to add it into the group. There's 0 reason for the ForGroup.

The TriggerSleepAction shouldn't be in there either, and you don't need a 0., pointless and just slows it down. The 0 is automatically promoted to a real, no need to promote it yourself.

Use a Timer at 0 to make it faster rather than TriggerSleepAction()

[ljass]if GetWidgetLife(thisunit) < .405 and IsUnitType(thisunit,UNIT_TYPE_HERO) == false then[/ljass]

Poor if statement. Units that are dead and have set life at above .405 after death would be registered as alive according to this.

[ljass]IsUnitType(u, UNIT_TYPE_DEAD) or GetUnitTypeId(u) == 0[/ljass]

Million other problems, but I think this is a good start.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
You don't need that ForGroup in there :
I tested. If TriggerSleepAction does not exist at there, the triggers does not fire on preplaced units.

Use a Timer at 0 to make it faster rather than TriggerSleepAction()
I was thinking of it when I write it. :p

JASS:
if GetWidgetLife(thisunit) &lt; .405 and IsUnitType(thisunit,UNIT_TYPE_HERO) == false then

May be I had to use SetUnitUserData to check it?
 

Nestharus

o-o
Reaction score
84
Always keep SetUnitUserData open for the map maker to use.

You don't need that ForGroup in there :
I tested. If TriggerSleepAction does not exist at there, the triggers does not fire on preplaced units.

Use a Timer at 0 to make it faster rather than TriggerSleepAction()
I was thinking of it when I write it.

You are telling me you cannot use a timer and have to use TriggerSleepAction()?
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
You are telling me you cannot use a timer and have to use TriggerSleepAction()?
It is able. =)

Posted a quick fix. I will add some events later.
Update #2 : Completed it, demo map uploaded as well.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Update #3 : Add some events detect. I had to split it into several parts because it is too messy for reading and users can choose the functions that wanted by them without importing long and messy script.
 

Nestharus

o-o
Reaction score
84
Better... but you do know that all those hooks are going to cripple any map that uses this right?
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Hooking RemoveUnit does not give you a unit leaves map event, by the way.
Yep, so I make it as UnitRemoved event.
After a delay, like 0.0s, the unit leaves the map.

In fact, if the unit is removed, you are unable to get it's data, like name, UnitUserData.
 

Jesus4Lyf

Good Idea™
Reaction score
397
You know, hook Events are cool, but are sadly unuseful. I don't see us approving a series of hook Events in a hurry.

>library UEDxGEx initializer Init requires UEDxBasic, Event
Nice sentiment and all about hooking stuff and storing things, but its a really bad way to code this.

I see you became familiar with Event recently. :D
And indeed, many things submitted these days just fire Events. But they're a little harder to create than these ones. ;)
JASS:
library UEDxBasic

    globals
        public unit u
    endglobals
    
    function GetEventUnit takes nothing returns unit
        return u
    endfunction
    
endlibrary

I kinda like this! XD (Honestly.)
 

Jesus4Lyf

Good Idea™
Reaction score
397
At the core of this it is not useful, because it does not detect when a unit leaves the map. Most of the Events are just triggered straight from hooks, meaning the user could call the things directly. Since it doesn't really add any functionality in a way that is appropriate, I'm going to graveyard this.

A Paladin would break this system tragically, mind you. Holy Light wouldn't fire the heal event, Divine Shield wouldn't fire the invulnerable event... etc.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top