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 poll for Hive's 12th Concept Art Contest is up! Go cast your vote for your favourite genie!
    Dismiss Notice
  4. Travel to distant realms and encounter scenes unknown to the common folk. The Greatest of Adventures is upon us with the 8th Cinematic Contest. Join in on a fun ride.
    Dismiss Notice
  5. The 18th Icon Contest is ON! Choose any ingame unit and give him/her Hero abilities. Good luck to all.
    Dismiss Notice
  6. Contestants are to create a scene set in the Stone Age. Come and see what you can come up with. We wish you the best of luck!
    Dismiss Notice
  7. Colour outside the lines! Techtree Contest #13 is a go. The contest is optionally paired.
    Dismiss Notice
  8. Greetings cerebrates, our Swarm needs new spawners that will have numerous children. Join the HIVE's 31st Modeling Contest - Spawners and Spawned! The contest is optionally paired.
    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.

[vJASS] [SNIPPET] IssuedOrder

Discussion in 'Triggers & Scripts' started by Krogoth, May 6, 2013.

  1. Krogoth

    Krogoth

    Joined:
    Apr 5, 2011
    Messages:
    247
    Resources:
    0
    Resources:
    0
    It's a small configurable snippet for handy detection issued orders.

    Here you proc your global event onUnitIssuesOrder:
    Trigger Events
    Code (vJASS):
    //==============
    //=== Events ===
    //==============

    library Events
        function onUnitIssuesOrder takes nothing returns nothing
            call BJDebugMsg(Orders.String[IssuedOrder.id])
            if Orders.Type[IssuedOrder.id] == ORDER_TYPE_IMMEDIATE then
                call IssuePointOrderById(GetTriggerUnit(), ORDER_MOVE, 0, 0)
            endif
        endfunction
    endlibrary
    You may use
    - GetTriggerUnit()
    - IssuedOrder.id
    - Orders.Type[]
    - Orders.String[]
    to get some data about event.

    Here you write all data about orders you need in your map:
    Trigger Orders
    Code (vJASS):
    //==============
    //=== Orders ===
    //=== v0.900 ===

    library Orders
        globals
    //--- Settings ---
            private constant boolean STRINGS = true
    //----------------
            //Order types declaration
            constant integer ORDER_TYPE_IMMEDIATE = 0
            constant integer ORDER_TYPE_POINT = 1
            constant integer ORDER_TYPE_TARGET = 2
            constant integer ORDER_TYPE_SMART = 3
            constant integer ORDERS_OFFSET = 851970
            //Orders declaration
            constant integer ORDER_STOP = 2
            constant integer ORDER_MOVE = 1
            constant integer ORDER_ATTACK = 13
            constant integer ORDER_SMART = 16
            constant integer ORDER_HOLDPOSITION = 23
            constant integer ORDER_PATROL = 20
        endglobals
        struct Orders extends array
            readonly static integer array Type
            static if STRINGS then
                readonly static string array String
            endif
            private static method onInit takes nothing returns nothing
                //Orders data
                static if STRINGS then
                    set String[ORDER_STOP] = "stop"
                    set String[ORDER_MOVE] = "move"
                    set String[ORDER_ATTACK] = "attack"
                    set String[ORDER_SMART] = "smart"
                    set String[ORDER_HOLDPOSITION] = "holdposition"
                    set String[ORDER_PATROL] = "patrol"
                endif
                set Type[ORDER_STOP] = ORDER_TYPE_IMMEDIATE
                set Type[ORDER_MOVE] = ORDER_TYPE_SMART
                set Type[ORDER_ATTACK] = ORDER_TYPE_SMART
                set Type[ORDER_SMART] = ORDER_TYPE_SMART
                set Type[ORDER_HOLDPOSITION] = ORDER_TYPE_IMMEDIATE
                set Type[ORDER_PATROL] = ORDER_TYPE_SMART
            endmethod
        endstruct
    endlibrary
    As you see all in-game order integers become small, it seems more handy and efficient. Turn "STRINGS" false if you don't need to store strings for orders.

    Here is order detection:
    Trigger IssuedOrder
    Code (vJASS):
    //===================
    //=== IssuedOrder ===
    //===== v1.000 ======

    library IssuedOrder requires NExt
        struct IssuedOrder extends array
            readonly static integer id
            private static trigger t
           
            static method imitate takes unit u returns nothing
                if     Orders.Type[id] == ORDER_TYPE_IMMEDIATE then
                    call IssueImmediateOrderById(u, id + ORDERS_OFFSET)
                elseif Orders.Type[id] == ORDER_TYPE_POINT then
                    call IssuePointOrderById(u, id + ORDERS_OFFSET, GetOrderPointX(), GetOrderPointY())
                elseif Orders.Type[id] == ORDER_TYPE_TARGET then
                    call IssueTargetOrderById(u, id + ORDERS_OFFSET, GetOrderTarget())
                elseif Orders.Type[id] == ORDER_TYPE_SMART then
                    if GetOrderTarget() == null then
                        call IssuePointOrderById(u, id + ORDERS_OFFSET, GetOrderPointX(), GetOrderPointY())
                    else
                        call IssueTargetOrderById(u, id + ORDERS_OFFSET, GetOrderTarget())
                    endif
                endif
            endmethod

            private static method func takes nothing returns boolean
                set id = GetIssuedOrderId() - ORDERS_OFFSET
                static if LIBRARY_Events then
                    call onUnitIssuesOrder()
                endif
                return false
            endmethod
           
            private static method onInit takes nothing returns nothing
                set t = CreateTrigger()
                call TriggerRegisterAnyUnitEvent(t, EVENT_PLAYER_UNIT_ISSUED_ORDER, null)
                call TriggerRegisterAnyUnitEvent(t, EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER, null)
                call TriggerRegisterAnyUnitEvent(t, EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER, null)
                call TriggerAddCondition(t, Condition(function thistype.func))
            endmethod
        endstruct
    endlibrary
    You may use IssuedOrder.imitate(unit U) to make U doing the same order as detected.

    And support library that I use very often:
    Trigger NExt
    Code (vJASS):
    //======================
    //=======[ NExt ]=======
    //======= v0.502 =======

    library NExt
        globals
            constant integer MAX_PLAYER_INDEX = 13
        endglobals

        function GroupEnumUnits takes group whichGroup, boolexpr filter returns nothing
            local integer i = 0
            loop
                call GroupEnumUnitsOfPlayer(whichGroup, Player(i), filter)
                exitwhen i == MAX_PLAYER_INDEX
                set i = i + 1
            endloop
        endfunction
       
        function TriggerRegisterAnyUnitEvent takes trigger trig, playerunitevent whichEvent, boolexpr filter returns nothing
            local integer i = 0
            loop
                call TriggerRegisterPlayerUnitEvent(trig, Player(i), whichEvent, filter)
                exitwhen i == MAX_PLAYER_INDEX
                set i = i + 1
            endloop
        endfunction

        function MakePlayersAllyWithVision takes player p1, player p2 returns nothing
            call SetPlayerAlliance(p1, p2, ALLIANCE_PASSIVE, true)
            call SetPlayerAlliance(p1, p2, ALLIANCE_HELP_REQUEST, true)
            call SetPlayerAlliance(p1, p2, ALLIANCE_HELP_RESPONSE, true)
            call SetPlayerAlliance(p1, p2, ALLIANCE_SHARED_XP, true)
            call SetPlayerAlliance(p1, p2, ALLIANCE_SHARED_SPELLS, true)
            call SetPlayerAlliance(p1, p2, ALLIANCE_SHARED_VISION, true)
        endfunction
       
        function GetInventoryIndexOfItemType takes unit u, integer id returns integer
            local integer i
            local item it
            set i = 0
            loop
                set it = UnitItemInSlot(u, i)
                if it != null and GetItemTypeId(it) == id then
                    return i
                endif
                exitwhen i == 5
                set i = i + 1
            endloop
            return -1
        endfunction

        function Distance takes real x1, real y1, real x2, real y2 returns real
            local real dx = x1 - x2
            local real dy = y1 - y2
            return SquareRoot(dx * dx + dy * dy)
        endfunction    
    endlibrary

    Is it ok?
    How can I post it to Resources if it's ok?
     
    Last edited: May 6, 2013
  2. Zwiebelchen

    Zwiebelchen

    Joined:
    Sep 17, 2009
    Messages:
    6,791
    Resources:
    12
    Models:
    5
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    JASS:
    4
    Resources:
    12
    You can always just save the large integer numbers for the orders to constants.
    I don't see why an offset is needed if you use constants anyway.

    I could just use this:
    constant integer ORDER_STOP = 851972


    I also don't see why we need a string array. It's not like those strings are not intuitive by itself.

    The NExt library will probably never get approved because it's too small and belongs into "small code snippets".

    All in all, I think it provides no real advantage over the normal JASS natives.

    Also, Order.Type only works on those basic unit controls, which is a serious flaw in the concept.
     
  3. Krogoth

    Krogoth

    Joined:
    Apr 5, 2011
    Messages:
    247
    Resources:
    0
    Resources:
    0
    In my map I use a lot of arrays storing orders. So, if Warcraft creates arrays by pow(2), it seems more efficient.
    Direct address to array seems faster than creating functions.
    You can add orders by yourself, I did not do it because it's easy and I don't know what exact orders user needs. Though I can make documentation.
    For example, unit A gets order X. You just need unit B doing the same. Without my snippet you may write:
    Code (vJASS):
    if (GetIssuedOrderId() == '...') or ... then
       call IssueImmediateOrder(...)
    elseif (...)
       call IssueTargetOrder(...)
    elseif (...)
       call IssuePointOrder(...)
    else
       if blabla then
          call IssueTargetOrder(...)
       else
          call IssuePointOrder(...)
       endif
    endif

    (last one for "smart" order)
    With my snippet is would be like:
    Code (vJASS):
    if Orders.Type[IssuedOrder.id] == ORDER_TYPE_IMMEDIATE then
       call IssueImmediateOrder(...)
    elseif Orders.Type[IssuedOrder.id] == ORDER_TYPE_TARGET then
       call IssueTargetOrder(...)
    elseif Orders.Type[IssuedOrder.id] == ORDER_TYPE_POINT then
       call IssuePointOrder(...)
    else
       if blabla then
          call IssueTargetOrder(...)
       else
          call IssuePointOrder(...)
       endif
    endif

    Added:
    Added IssuedOrder.imitate(unit) method.
     
    Last edited: May 6, 2013
  4. Almia

    Almia

    Joined:
    Apr 24, 2012
    Messages:
    4,860
    Resources:
    35
    Spells:
    30
    Tutorials:
    4
    JASS:
    1
    Resources:
    35
    We already have order ids:
    http://www.hiveworkshop.com/forums/jass-resources-412/repo-order-ids-197002/
    So you don't need to write those constants because they are useless
    Btw,if you are going to submit this kind of resource,please visit Jass Resources Forum and go to Submissions sub-forum:http://www.hiveworkshop.com/forums/submissions-414/

    Struct Order shall extend array because you are not using the default alloc,
    extends array
    removes the generated code.
    Struct globals must be camel-cased (ex.
     static integer array hello
    instead of
    Hello


    Its useless to create your own TriggerRegisterAnyUnitEvent,we already have
    TriggerRegisterAnyUnitEventBJ
    ,also,it would look nice if you take
    code
    instead of
    trigger
     
  5. Krogoth

    Krogoth

    Joined:
    Apr 5, 2011
    Messages:
    247
    Resources:
    0
    Resources:
    0
    Thanks, I forgot about that.
    That is red-colored, it hurts, my own - black. Also I limit MAX_PLAYER_INDEX by myself. (Some maps need only 2 players, that 8 lines make 7 times less instances for cycle)

    Added:
    How can I do it?