• 🏆 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] Order Types

Level 18
Joined
Jan 21, 2006
Messages
2,552
Order Types

In Warcraft III the possibilities for types of orders are limited to 3. They are:
  • Point
  • Target
  • Immediate/None

Also worth mentioning is the three different types of targets, which are:
  • Unit
  • Destructable
  • Item

It is often necessary to distinguish between these, for example you want your spell to perform a different set of actions if the target is a unit or a destructable (or if the order-type is a point target as opposed to a unit target) this library efficiently produces a result that can be used as a simple integer.

JASS:
library OrderTypes
/*******************************
 * Order Types Library
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 * by: Berb
 * Version: 1.0
 * 
 * Description:
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 *	The purpose of this library is to provide the user with an API function that allows
 *	them to easily (and efficiently) check which type of order the current event response
 *	is dealing with. It uses event responses such as "GetOrderPointLoc" so using this 
 *	outside of an event response action will not work.
 *
 * API:
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 *      • GetOrderType                  ~ Returns an integer constant representing the type
 *                                      of order that is in-process. 
 *      • GetAbilityOrderType           ~ Just like "GetOrderType" except for ability
 *                                      events rather than order events.
 *
 *      • GetOrderTargetType            ~ Returns an integer constant representing the type
 *                                      of target that the order was issued on.
 *      • GetAbilityOrderTargetType     ~ Just like "GetOrderTargetType" except for ability
 *                                      events rather than order events.
 *
 * Constants -------------------------------------------------------------------------------*/
    globals

        constant	integer     ORDER_TYPE_NONE     = 0
        constant	integer     ORDER_TYPE_POINT    = 1
        constant	integer     ORDER_TYPE_TARGET   = 2
        
        constant    integer     TARGET_TYPE_UNIT    = 0
        constant    integer     TARGET_TYPE_DEST    = 1
        constant    integer     TARGET_TYPE_ITEM    = 2
        constant    integer     TARGET_TYPE_NONE    = 3
        
    endglobals
/* -----------------------------------------------------------------------------------------
 * 
 */
    globals
        private     location	data__targetLoc		= null
    endglobals
				
    function GetAbilityOrderTargetType takes nothing returns integer
        if GetSpellTargetUnit() != null then
            return TARGET_TYPE_UNIT
        elseif GetSpellTargetItem() != null then
            return TARGET_TYPE_ITEM
        elseif GetSpellTargetDestructable() != null then
            return TARGET_TYPE_DEST
        endif
        return TARGET_TYPE_NONE
    endfunction
                
    function GetAbilityOrderType takes nothing returns integer   
        if GetSpellTargetUnit() != null     or /*
            */ GetSpellTargetItem() != null or /*
            */ GetSpellTargetDestructable() != null then
            return ORDER_TYPE_TARGET
        endif
        
        set data__targetLoc = GetSpellTargetLoc()    
        if data__targetLoc != null then
            call RemoveLocation(data__targetLoc)
            return ORDER_TYPE_POINT
        endif
        return ORDER_TYPE_NONE
    endfunction
        
    function GetOrderTargetType takes nothing returns integer
        if GetOrderTargetUnit() != null then
            return TARGET_TYPE_UNIT
        elseif GetOrderTargetItem() != null then
            return TARGET_TYPE_ITEM
        elseif GetOrderTargetDestructable() != null then
            return TARGET_TYPE_DEST
        endif
        return TARGET_TYPE_NONE
    endfunction
        
    function GetOrderType takes nothing returns integer
        if GetOrderTarget() != null then
            return ORDER_TYPE_TARGET
        endif
        
        set data__targetLoc = GetOrderPointLoc()
        if data__targetLoc != null then
            call RemoveLocation(data__targetLoc)
            return ORDER_TYPE_POINT
        endif
        return ORDER_TYPE_NONE
    endfunction
 /*
  *
  ******************************************************************************************/
 endlibrary

Initially this was written in zinc, but since I really don't find the zinc language attractive in an environment designed for vJass I converted it to plain ol' vJass.

I think this should be a little more appropriate for the JASS section than my previous two submissions, I thought many would find this useful and I couldn't find anything of the sort already submitted.
 
Last edited by a moderator:
Level 18
Joined
Jan 21, 2006
Messages
2,552
Well I used GetOrderPointLoc and GetSpellTargetLoc because they explicitly return (null) when there is an instant order, where as the (x, y) natives can return 0.00 in two possible scenarios. It also eliminates having to declare/compare two variables, instead I can just use the one. Since the location only needs to be removed if the location is not null, then I figure it saves a few steps.
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
I don't know where that mysterious debug function came from though. I'm going to remove it.

JASS:
    private function Error takes string msg returns nothing
        debug call BJDebugMsg(SCOPE_PREFIX+":|cffff0000Error: "+msg)
    endfunction

I don't remember what I was using this for, but it isn't used in the code at all. It's being removed from the script.
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
JASS:
    function GetOrderType takes nothing returns integer
        if GetOrderTarget() != null then
            return ORDER_TYPE_TARGET
        endif
        
        set data__targetLoc = GetOrderPointLoc()
        if data__targetLoc != null then
            call RemoveLocation(data__targetLoc)
            return ORDER_TYPE_POINT
        endif
        return ORDER_TYPE_NONE
    endfunction

The native GetTriggerEventId can be used instead of the location, in conjunction with EVENT_PLAYER_UNIT_ISSUED_ORDER and EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER and EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
Yeah, here would be the updated library with the slight optimization if you're interested:

JASS:
library OrderTypes
/*******************************
 * Order Types Library
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 * by: Berb
 * Version: 1.0
 * 
 * Description:
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 *    The purpose of this library is to provide the user with an API function that allows
 *    them to easily (and efficiently) check which type of order the current event response
 *    is dealing with. It uses event responses such as "GetOrderPointLoc" so using this 
 *    outside of an event response action will not work.
 *
 * API:
 * ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
 *      • GetOrderType                  ~ Returns an integer constant representing the type
 *                                      of order that is in-process. 
 *      • GetAbilityOrderType           ~ Just like "GetOrderType" except for ability
 *                                      events rather than order events.
 *
 *      • GetOrderTargetType            ~ Returns an integer constant representing the type
 *                                      of target that the order was issued on.
 *      • GetAbilityOrderTargetType     ~ Just like "GetOrderTargetType" except for ability
 *                                      events rather than order events.
 *
 * Constants -------------------------------------------------------------------------------*/
    globals

        constant    integer     ORDER_TYPE_NONE     = 0
        constant    integer     ORDER_TYPE_POINT    = 1
        constant    integer     ORDER_TYPE_TARGET   = 2
        
        constant    integer     TARGET_TYPE_UNIT    = 0
        constant    integer     TARGET_TYPE_DEST    = 1
        constant    integer     TARGET_TYPE_ITEM    = 2
        constant    integer     TARGET_TYPE_NONE    = 3
        
    endglobals
/* -----------------------------------------------------------------------------------------
 * 
 */
    globals
        private     location    data__targetLoc        = null
    endglobals
                
    function GetAbilityOrderTargetType takes nothing returns integer
        if GetSpellTargetUnit() != null then
            return TARGET_TYPE_UNIT
        elseif GetSpellTargetItem() != null then
            return TARGET_TYPE_ITEM
        elseif GetSpellTargetDestructable() != null then
            return TARGET_TYPE_DEST
        endif
        return TARGET_TYPE_NONE
    endfunction
                
    function GetAbilityOrderType takes nothing returns integer   
        if GetSpellTargetUnit() != null     or /*
            */ GetSpellTargetItem() != null or /*
            */ GetSpellTargetDestructable() != null then
            return ORDER_TYPE_TARGET
        endif
        
        set data__targetLoc = GetSpellTargetLoc()    
        if data__targetLoc != null then
            call RemoveLocation(data__targetLoc)
            return ORDER_TYPE_POINT
        endif
        return ORDER_TYPE_NONE
    endfunction
        
    function GetOrderTargetType takes nothing returns integer
        if GetOrderTargetUnit() != null then
            return TARGET_TYPE_UNIT
        elseif GetOrderTargetItem() != null then
            return TARGET_TYPE_ITEM
        elseif GetOrderTargetDestructable() != null then
            return TARGET_TYPE_DEST
        endif
        return TARGET_TYPE_NONE
    endfunction
        
    function GetOrderType takes nothing returns integer
        if GetOrderTarget() != null then
            return ORDER_TYPE_TARGET
        elseif GetTriggerEventId() == EVENT_PLAYER_UNIT_ISSUED_ORDER then
            return ORDER_TYPE_NONE
        endif
        return ORDER_TYPE_POINT
    endfunction
/*
 *
 ******************************************************************************************/
endlibrary

The downside is it doesn't recognize "EVENT_UNIT_ISSUED_ORDER" but using that event would be a total waste for obvious reasons (no one should be using it)
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
I'll try to remember to update the script when I get back. I haven't tested it though so I'll do that before jumping to the conclusion, unless you can tell me that it works - your word is good enough for me.
 
Top