• 🏆 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!

[Snippet] Enable/Disable attack v0.0.1

Requires:
- Ice fairy or something ? 0.0...

JASS:
//*********************Disable/Enable attack by Elphis v0.0.4*********************
//*********************Allow you disable/enable attack staus of unit*********************
//                      API:
//                          - function DisableAttack takes unit u returns nothing
//                          **Allow you disable attack of unit**
//                          - function EnableAttack takes unit u returns nothing
//                          **Enable attack of unit**
//                          - function DisableAttackAOE takes unit u,real radius returns nothing
//                          **Allow you disable attack of unit in wide Area**
//                          - function EnableAttackAOE takes unit u returns nothing
//                          **Enable attack of unit in wide Area**
//********************************************************************************************************
//********************************************************************************************************
//          - Installation:
//                                - Import/copy the required libraries and Attack Staus code to your map
//                                - Copy DummyUnit of this map into your map
//                                - Import/copy the custom ability to your map and change the ORDER, DISABLE_ABI_ID, BUFF_ID if needed
//                                - You may view the raw ID of the objects by pressing CTRL+D in the object editor
//                                - You may play with the configurables below
//
//          - Special Thank to TriggerHappy, who help me to improve this code.
//*************************************************************************************
//
library DisableAttack initializer Init
    
    globals
        private     constant        integer     ORDER          = 0xD0270
        private     constant        integer     SPELL_ID       = 'DISA' // Rawcode of Ability Disable, changes if needed
        private     constant        integer     BUFF_ID        = 'BDIS' // Rawcode of Buff Disable, changes if needed
        private     constant        integer     DUMMY_ID       = 'xeca' //Dummy rawcode
        
        private                     group       Swap           = CreateGroup()
        private                     group       Temp
        
        /*=Non - Configurables=*/
        private                     unit        Dummy
    endglobals

    function UnitDisableAttack takes unit u returns boolean
        if GetUnitAbilityLevel(u,BUFF_ID) > 0 then
            return false
        endif
        call SetUnitX(Dummy,GetUnitX(u))
        call SetUnitY(Dummy,GetUnitY(u))
        call IssueTargetOrderById(Dummy,ORDER,u)
        return true
    endfunction

    function UnitEnableAttack takes unit u returns boolean
        return UnitRemoveAbility(u,BUFF_ID)
    endfunction
    
    private function SwapGroup takes group Group returns nothing
        set Temp = Group
        set Group = Swap
        set Swap = Temp
    endfunction
    
    function GroupDisableAttack takes group g returns nothing
        local unit FoG
        loop
            set FoG=FirstOfGroup(g)
            exitwhen FoG==null
            //
            call UnitDisableAttack(FoG)
            //
            call GroupAddUnit(Swap,FoG)
            call GroupRemoveUnit(g,FoG)
        endloop
        
        call SwapGroup(g)
    endfunction
    
    function GroupEnableAttack takes group g returns nothing
        local unit FoG
        loop
            set FoG=FirstOfGroup(g)
            exitwhen FoG==null
            //
            call UnitEnableAttack(FoG)
            //
            call GroupAddUnit(Swap,FoG)
            call GroupRemoveUnit(g,FoG)
        endloop
        
        call SwapGroup(g)
    endfunction
    
    function Init takes nothing returns nothing

        set Dummy = CreateUnit(Player(15),DUMMY_ID,0.,0.,0.)
        
        if GetUnitAbilityLevel(Dummy,SPELL_ID) == 0 then
            call UnitAddAbility(Dummy,SPELL_ID)
        endif
    endfunction

endlibrary[/HIDDEN]
 

Attachments

  • Disable Attack.w3x
    25.7 KB · Views: 87
Last edited:
Level 10
Joined
Sep 19, 2011
Messages
527
JASS:
    function EnableAttackAOE takes real x,real y,real radius returns nothing
        local unit f
        call GroupEnumUnitsInRange(GROUP,x,y,radius,null)
        loop
            set f = FirstOfGroup(GROUP)
            exitwhen f == null
            if GetUnitAbilityLevel(f,BUFF_ID) >= 1 then
                call UnitRemoveAbility(f,BUFF_ID)
            endif
            call GroupRemoveUnit(GROUP,f)
        endloop
        set f = null
    endfunction

->

JASS:
    function EnableAttackAOE takes real x,real y,real radius returns nothing
        local unit f
        call GroupEnumUnitsInRange(GROUP,x,y,radius,null)
        loop
            set f = FirstOfGroup(GROUP)
            exitwhen f == null
            call EnableAttack(f)
            call GroupRemoveUnit(GROUP,f)
        endloop
    endfunction

edit:

function Init takes nothing returns nothing

->

private function Init takes nothing returns nothing
 
JASS:
    function EnableAttackAOE takes real x,real y,real radius returns nothing
        local unit f
        call GroupEnumUnitsInRange(GROUP,x,y,radius,null)
        loop
            set f = FirstOfGroup(GROUP)
            exitwhen f == null
            if GetUnitAbilityLevel(f,BUFF_ID) >= 1 then
                call UnitRemoveAbility(f,BUFF_ID)
            endif
            call GroupRemoveUnit(GROUP,f)
        endloop
        set f = null
    endfunction

->

JASS:
    function EnableAttackAOE takes real x,real y,real radius returns nothing
        local unit f
        call GroupEnumUnitsInRange(GROUP,x,y,radius,null)
        loop
            set f = FirstOfGroup(GROUP)
            exitwhen f == null
            call EnableAttack(f)
            call GroupRemoveUnit(GROUP,f)
        endloop
    endfunction

edit:

function Init takes nothing returns nothing

->

private function Init takes nothing returns nothing

Oh thanks, i forgot that ;)
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
if GetUnitAbilityLevel(f,BUFF_ID) >= 1 then

could be, because >= is (> or ==)

if GetUnitAbilityLevel(f,BUFF_ID) > 0 then

Also I'm that you can't have negative ability levels.

if GetUnitAbilityLevel(u,BUFF_ID) <= 0 then

or you can simply remove it, if the unit doesn't have the ability, nothing happens.

If it gets approved, there should be a DummyCaster version aswell, because we don't have xe.basic nor xe.cast in the Jass section. (Just my opinion)
 
Why are you using hex for the constants? Just use raw code. Also keep the objectmerger line and just remove the macro.

I modified the script to how I think it should look.
  • Added type prefixes to the API
  • Functions return boolean
  • Object merger line at the top
  • Group support
  • inlined UnitEnableAttack
  • Removed AOE functions. In my opinion it creates extra uneeded group because I think most people already have a global one they re-use in their maps. However people may end up creating extra boolean expressions whereas if the script supported it there would only be one filter handle. If you decide to keep AOE functionality then store the filter into a variable at the init function, and re-use that.
JASS:
library DisableAttack initializer Init

    //! objectmerger line here

    globals
        private     constant        integer     ORDER          = 0xD0270
        private     constant        integer     DISABLE_ABI_ID = 'DISA' // Rawcode of Ability Disable, changes if needed
        private     constant        integer     BUFF_ID        = 'BDIS' // Rawcode of Buff Disable, changes if needed
        private     constant        integer     DUMMY_ID       = 'xeca' //Dummy rawcode
        
        /*=Non - Configurables=*/
        private unit Dummy
    endglobals

    function UnitDisableAttack takes unit u returns boolean
        if GetUnitAbilityLevel(u,BUFF_ID) > 0x0 then
            return false
        endif
        call SetUnitX(Dummy,GetUnitX(u))
        call SetUnitY(Dummy,GetUnitY(u))
        call IssueTargetOrderById(Dummy,ORDER,u)
        return true
    endfunction

    function UnitEnableAttack takes unit u returns boolean
        return UnitRemoveAbility(u,BUFF_ID)
    endfunction
    
    private function GroupDisable takes nothing returns nothing
        call UnitDisableAttack(GetEnumUnit())
    endfunction
    
    private function GroupEnable takes nothing returns nothing
        call UnitEnableAttack(GetEnumUnit())
    endfunction
    
    function GroupDisableAttack takes group g returns nothing
        call ForGroup(g, function GroupDisable)
    endfunction
    
    function GroupEnableAttack takes group g returns nothing
        call ForGroup(g, function GroupEnable)
    endfunction
    
    function Init takes nothing returns nothing
        set Dummy = CreateUnit(Player(0xF),DUMMY_ID,0.,0.,0.)
    endfunction

endlibrary
 
Last edited:
Why are you using hex for the constants? Just use raw code. Also keep the objectmerger line and just remove the macro.

I modified the script to how I think it should look.
  • Added type prefixes to the API
  • Functions return boolean
  • Object merger line at the top
  • Group support
  • inlined UnitEnableAttack
  • Removed AOE functions. In my opinion it creates extra uneeded group because I think most people already have a global one they re-use in their maps. However people may end up creating extra boolean expressions whereas if the script supported it there would only be one filter handle. If you decide to keep AOE functionality then store the filter into a variable at the init function, and re-use that.
JASS:
library DisableAttack initializer Init

    //! objectmerger line here

    globals
        private     constant        integer     ORDER          = 0xD0270
        private     constant        integer     DISABLE_ABI_ID = 'DISA' // Rawcode of Ability Disable, changes if needed
        private     constant        integer     BUFF_ID        = 'BDIS' // Rawcode of Buff Disable, changes if needed
        private     constant        integer     DUMMY_ID       = 'xeca' //Dummy rawcode
        
        /*=Non - Configurables=*/
        private unit Dummy
    endglobals

    function UnitDisableAttack takes unit u returns boolean
        if GetUnitAbilityLevel(u,BUFF_ID) > 0x0 then
            return false
        endif
        call SetUnitX(Dummy,GetUnitX(u))
        call SetUnitY(Dummy,GetUnitY(u))
        call IssueTargetOrderById(Dummy,ORDER,u)
        return true
    endfunction

    function UnitEnableAttack takes unit u returns boolean
        return UnitRemoveAbility(u,BUFF_ID)
    endfunction
    
    private function GroupDisable takes nothing returns nothing
        call UnitDisableAttack(GetEnumUnit())
    endfunction
    
    private function GroupEnable takes nothing returns nothing
        call UnitEnableAttack(GetEnumUnit())
    endfunction
    
    function GroupDisableAttack takes group g returns nothing
        call ForGroup(g, function GroupDisable)
    endfunction
    
    function GroupEnableAttack takes group g returns nothing
        call ForGroup(g, function GroupEnable)
    endfunction
    
    function Init takes nothing returns nothing
        set Dummy = CreateUnit(Player(0xF),DUMMY_ID,0.,0.,0.)
    endfunction

endlibrary

Cool, thank ^^~
 
Top