1. The long-awaited results for Concept Art Contest #11 have finally been released!
    Dismiss Notice
  2. The mythological era has spawned some interesting characters around. Check them out and be sure to vote for them in the 30th Poll of the Texturing Contest.
    Dismiss Notice
  3. The 20th iteration of the Terraining Contest is upon us! Join and create exquisite Water Structures for it.
    Dismiss Notice
  4. Hivers united and created a bunch of 2v2 melee maps. Vote for the best in our Melee Mapping Contest #4 - Poll!
    Dismiss Notice
  5. Check out the Staff job openings thread.
    Dismiss Notice

IsUnitMoving()

Discussion in 'Graveyard' started by Freyleyes, Feb 24, 2010.

  1. Freyleyes

    Freyleyes

    Joined:
    Jun 28, 2008
    Messages:
    766
    Resources:
    1
    Spells:
    1
    Resources:
    1
    This is a little code snippet I created for a map I am creating. I could not find a working one anywhere.

    Description :

    This little piece of code checks if a unit is moving.

    Requires :

    • JNGP

    Show Code

    Code (vJASS):
    library IsUnitMoving initializer init

    globals

        private group   CHECKUNITS  = CreateGroup()
        private trigger MAINTRIGGER = CreateTrigger()
        private integer counter     = 0
        MoveData array ARRMove

    endglobals

    struct MoveData

        unit witchunit = null
        real cX        = 0
        real cY        = 0
        real nX        = 0
        real nY        = 0
        boolean moving = false

    endstruct

    private function GetIndex takes unit u returns integer

        local integer i = 0
       
        loop
       
            set i = i + 1
           
                if ARRMove[i].witchunit == u then
                    return i
                endif
           
            exitwhen i == counter
       
        endloop

        return 0
       
    endfunction

    function IsUnitMoving takes unit u returns boolean

        local integer i = GetIndex(u)

        return ARRMove[i].moving

    endfunction

    private function EnumUnits takes nothing returns nothing

        local integer i = GetIndex(GetEnumUnit())
       
        set ARRMove[i].cX = GetUnitX(GetEnumUnit())
        set ARRMove[i].cY = GetUnitY(GetEnumUnit())
       
        if (ARRMove[i].cX == ARRMove[i].nX) and (ARRMove[i].cY == ARRMove[i].nY) then
            set ARRMove[i].moving = false
        else
            set ARRMove[i].moving = true
        endif
       
        set ARRMove[i].nX = GetUnitX(GetEnumUnit())
        set ARRMove[i].nY = GetUnitY(GetEnumUnit())

    endfunction

    private function onLoop takes nothing returns nothing

        call ForGroup(CHECKUNITS, function EnumUnits)

    endfunction

    function AddUnit takes unit u returns nothing

        set counter = counter + 1
        set ARRMove[counter] = MoveData.create()
        set ARRMove[counter].witchunit = u
        set ARRMove[counter].cX = GetUnitX(u)
        set ARRMove[counter].cY = GetUnitY(u)
       
        call GroupAddUnit(CHECKUNITS, u)

    endfunction

    private function init takes nothing returns nothing

        call TriggerRegisterTimerEvent(MAINTRIGGER, 0.05, true)
        call TriggerAddAction(MAINTRIGGER, function onLoop)

    endfunction

    endlibrary


    Use
    Code (vJASS):
    AddUnit(WichUnit)
    to add a unit to the check list and use
    Code (vJASS):
    IsUnitMoving( unit WitchUnit)
    to check if that unit is moving.
     
  2. Anachron

    Anachron

    Joined:
    Sep 9, 2007
    Messages:
    6,167
    Resources:
    66
    Icons:
    49
    Packs:
    2
    Tools:
    1
    Maps:
    3
    Spells:
    9
    Tutorials:
    1
    JASS:
    1
    Resources:
    66
    You don't need a timer at all.
    Just register order issues, and change boolean on event.
     
  3. TriggerHappy

    TriggerHappy

    Code Moderator

    Joined:
    Jun 23, 2007
    Messages:
    3,578
    Resources:
    22
    Spells:
    11
    Tutorials:
    2
    JASS:
    9
    Resources:
    22
    I'm sorry, this cannot be approved.
    • You use O(n) searches which are very slow and are limited to 8190 registered units.
    • Like Anachron said, you could just trigger order events.
    • The fact you even need to register units is lame.
    • You don't even have recycling.
    • You're calling GetEnumUnit() five times when you could just store it in a local.
    • "AddUnit" is a to generic name.
    • What's the point of using the timer event? Why not just use a timer?
    • The struct should be private.
    • You could inline IsUnitMoving.
     
  4. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,419
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    You could just register order events, but that wouldn't detect SetUnitX/SetUnitY.

    EDIT: Although you could probably just hook SetUnitX/SetUnitY/SetUnitPosition and set the boolean to true. The rest you could probably just register the order events.
    EDIT2: Then again, you also need to know when the SetUnitXY/Pos stops. =P
     
  5. Anachron

    Anachron

    Joined:
    Sep 9, 2007
    Messages:
    6,167
    Resources:
    66
    Icons:
    49
    Packs:
    2
    Tools:
    1
    Maps:
    3
    Spells:
    9
    Tutorials:
    1
    JASS:
    1
    Resources:
    66
    Yes, additionally hook the SetUnitX/SetUnitY() natives and then use a timer with the delay of how long the unit is counted as being moved.
     
  6. azlier

    azlier

    Joined:
    Oct 3, 2008
    Messages:
    354
    Resources:
    4
    JASS:
    4
    Resources:
    4
    Please... never hook SetUnitX/Y. Those have to be, like, the most frequently called natives ever.

    Hooking those makes projectile systems and the like much slower. I prefer manual registry.

    Needs fixing.
     
  7. Anachron

    Anachron

    Joined:
    Sep 9, 2007
    Messages:
    6,167
    Resources:
    66
    Icons:
    49
    Packs:
    2
    Tools:
    1
    Maps:
    3
    Spells:
    9
    Tutorials:
    1
    JASS:
    1
    Resources:
    66
    Oh yes, didn't thought of the high usage in such systems.
     
  8. Freyleyes

    Freyleyes

    Joined:
    Jun 28, 2008
    Messages:
    766
    Resources:
    1
    Spells:
    1
    Resources:
    1
    I will update the Script asap. I am reality new at using vJass and don't really understand every thing yet. Thanks for the feedback, will try to make it right :D
     
  9. TriggerHappy

    TriggerHappy

    Code Moderator

    Joined:
    Jun 23, 2007
    Messages:
    3,578
    Resources:
    22
    Spells:
    11
    Tutorials:
    2
    JASS:
    9
    Resources:
    22
    It's been nearly two weeks so I am going to graveyard this. Send me a message when/if you're ready to revive it.