Snippet UnitSpeedTracking

Komaqtion

You can change this now in User CP.
Reaction score
469
Hi again ;)

Just a small script which will track the absolute movement-speed of units by using a single timer (Copied T32's module for this, so people can use different periods for T32 and this. Though if enough people think this is unecessary then I'll just use T32 instead ;)).

Ok, here's the small code :D

JASS:
library UnitSpeedTracking requires AIDS

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//*********************************************************************************************************//
//@@///////////////////////////////// *// UnitSpeedTracking \\* /////////////////////////////////////////@@//
//@@                                                                                                     @@//
//@@                               Made by , Komaqtion @ TheHelper.net                                   @@//
//@@                                                                                                     @@//
//@@                                                                                                     @@//
//@@                                         Purpose:                                                    @@//
//@@                                                                                                     @@//
//@@             # The purpose of this small snippet is to be able to get a unit's absolute              @@//
//@@               speed, and accurately, too <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite2" alt=";)" title="Wink    ;)" loading="lazy" data-shortname=";)" />                                                         @@//
//@@                                                                                                     @@//
//@@                                         Usage:                                                      @@//
//@@                                                                                                     @@//
//@@             # Now, the functions provided by this small snippet is:                                 @@//
//@@                                                                                                     @@//
//@@   -&gt;                   function GetUnitSpeed takes unit speeder returns real                   &lt;-   @@//
//@@                                                                                                     @@//
//@@               - &quot;unit speeder&quot; -&gt; This is the unit you wish to find out the speed of                @@//
//@@               - &quot;returns real&quot; -&gt; This function will return the current speed                       @@//
//@@                 the unit is currently moving at, used as &quot;range-per-second&quot;.                        @@//
//@@                                                                                                     @@//
//@@                                                                                                     @@//
//@@                                     Requirements:                                                   @@//
//@@                                                                                                     @@//
//@@             # This snippet&#039;s only requirement is vJASS compilement, which is                        @@//
//@@               easiest achieved by downloading JASS Newgen Pack, at                                  @@//
//@@               <a href="http://www.thehelper.net/forums/showthread.php?t=73936" class="link link--internal">http://www.thehelper.net/forums/showthread.php?t=73936</a>                                @@//
//@@               You&#039;ll also have to update JASS Helper to the latest version...                       @@//
//@@                                                                                                     @@//
//@@                                                                                                     @@//
//@@                                        Credits:                                                     @@//
//@@                                                                                                     @@//
//@@             #  - Jesus4Lyf, for his T32 module which I&#039;ve copied for the                            @@//
//@@                 timer-efficiency of using a single timer for it all ! <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick Out Tongue    :p" loading="lazy" data-shortname=":p" />                            @@//
//@@                                                                                                     @@//
//@@             # And credits, if you use this that is, is not needed to give me                        @@//
//@@               though it&#039;s always welcome <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite2" alt=";)" title="Wink    ;)" loading="lazy" data-shortname=";)" />                                                         @@//
//@@                                                                                                     @@//
//@@/////////////////////////////////////////////////////////////////////////////////////////////////////@@//
//*********************************************************************************************************//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // Small but important CONFIGURATION AREA !

    globals
        private constant real TRACK_PERIOD = 0.01 // This is the period of which the speed of each unit in-game is refreshed
    endglobals
    
    // END OF CONFIGURATION !!!! <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick Out Tongue    :p" loading="lazy" data-shortname=":p" />
    
    private struct UnitSpeedData extends array
        //! runtextmacro AIDS()
        
        timer trackTimer
        
        real unitX
        real unitY
        real currentSpeed
        
        private thistype next
        private thistype prev
        
        static trigger trackingTrigger = CreateTrigger()
        
        private method AIDS_onCreate takes nothing returns nothing
            set unitX = GetUnitX( unit )
            set unitY = GetUnitY( unit )
            set currentSpeed = 0
            
            set thistype(0).next.prev = this
            set this.next = thistype(0).next
            set thistype(0).next  =this
            set this.prev = thistype(0)
        endmethod
        
        private static method SpeedTracking takes nothing returns boolean
            local real x
            local real y
            local real difx
            local real dify
            local real dist
            
            local thistype this = thistype(0).next
            
            loop
                exitwhen this == 0
                
                set x = GetUnitX( unit )
                set y = GetUnitY( unit )
                set difx = x - unitX
                set dify = y - unitY
                set dist = SquareRoot( difx * difx + dify * dify )
                
                set currentSpeed = dist / TRACK_PERIOD
                set unitX = x
                set unitY = y
                
                set this = next
            endloop
            
            return false
        endmethod
        
        private method AIDS_onDestroy takes nothing returns nothing            
            set this.prev.next = this.next
            set this.next.prev = this.prev
        endmethod
        
        private static method EvaluateStack takes nothing returns nothing
            call TriggerEvaluate( thistype.trackingTrigger )
        endmethod
            
        private static method AIDS_onInit takes nothing returns nothing
            call TimerStart( CreateTimer(), TRACK_PERIOD, true, function thistype.EvaluateStack )
            call TriggerAddCondition( thistype.trackingTrigger, Condition( function thistype.SpeedTracking ) )
        endmethod
            
    endstruct
    
    function GetUnitSpeed takes unit speeder returns real
        return UnitSpeedData( GetUnitId( speeder ) ).currentSpeed
    endfunction
    
endlibrary


Might not be too useful, but can be extended quite a bit I'd believe ;)
 

Sevion

The DIY Ninja
Reaction score
413
This could bug. If you attempt to get a unit's speed and he's not moving.

Do an if: [ljass]if ( not (dist < currentSpeed * TRACK_PERIOD and dist == 0) ) then[/ljass]
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
A simple Blink will break your code.
And... practically usage? It serves no practically usage for spells, IMO.
 

Komaqtion

You can change this now in User CP.
Reaction score
469
This could bug. If you attempt to get a unit's speed and he's not moving.

Do an if: if ( not (dist < currentSpeed * TRACK_PERIOD and dist == 0) ) then

Why would it ? Wouldn't it return 0. ? :S

I vote this should use T32 instead.
That is to say that you remove the ability for users to specify the period.

I did already write all this functionality inside Status, but did not expose the ability to get the movement of a unit... :)

One or two more votes on this and I'll change it ;)

So wait, if you use this on a non moving unit, it returns 0?
That could be a feature =P

That's what I'd believe ;)

What happened to GetUnitMoveSpeed ?

Well, this will work for units moved with triggers, like projectiles and such but only 0.01 seconds after it's started to move...
That's why I would suggest (Though might be too much work for such a little thing) to set the projectiles "currentSpeed" variable to the speed it's supposed to have when it moves...

A simple Blink will break your code.
And... practically usage? It serves no practically usage for spells, IMO.

Yeah, I just came to think of this too...
And a quite lame workaround for the moment could be that I add a boolean, like 'trackMovement', which you set to 'false' when you start to cast a spell, and back to true when it finishes...
But as said, it's quite lame but if anyone has a better solution please inform me of it ;)

And, as said if you think this whole thing is just unecessary, then maybe you can try to come up with some other features which this could use to make it a bit more interesting ;)
 

Sevion

The DIY Ninja
Reaction score
413
I vote that it uses T32. Done :)

No, it wouldn't return 0 if you put all of your currentSpeed code into the if block.

Projectiles generally have a speed variable. You have to give projectiles a speed anyhow. If you wanted it, you could store it and use it later.

You could also put in the AIDS_filter [ljass]return GetUnitAbilityLevel(GetFilterUnit(), 'amov') != 0[/ljass]
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Ok then :p I'll use T32 for it ;)

No, it wouldn't return 0 if you put all of your currentSpeed code into the if block.

I mean't it should return 0. normally when a unit isn't moving, so why remove that ? :S
 

Sevion

The DIY Ninja
Reaction score
413
I suppose you and I are on different pages as to what this snippet is for.

What I'm looking at is that this snippet is for replacing [ljass]GetUnitMoveSpeed[/ljass] when you're expecting triggered movement. The native function returns the unit's move speed regardless of whether or not the unit is moving.

That is what I believed you intended this system to do. Get a unit's move speed without it needing to be moving while getting the speed.
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Actually no, I intended this to be used to get the actual current speed of the unit...

But why don't I do some kind of "average-speed", so I add the current speed each period (Unless it's 0 ;)) to a variable and then divide it by the amount of perioed gone by :eek:
Think this would be good ? :S
 

Sevion

The DIY Ninja
Reaction score
413
Say a unit doesn't move for about 10 seconds after creation. That's 1000 ticks with a currentSpeed = 0. Then he moves. Say he has 522 speed (max). 522/1000 == average speed. Wait.... no it isn't.....

Why would you need average speed? In what case scenario could you ever need it?
 

Komaqtion

You can change this now in User CP.
Reaction score
469
Say a unit doesn't move for about 10 seconds after creation. That's 1000 ticks with a currentSpeed = 0. Then he moves. Say he has 522 speed (max). 522/1000 == average speed. Wait.... no it isn't.....

Well, you only do this if he is movin,g meaning 'dist' is greater than 0 ;)

Why would you need average speed? In what case scenario could you ever need it?

Dunno really :S
But, someone could need it sometime :p
Maybe you could add a command for it, so you do it under a time or something :S

and what happends if you teleport that unit, this will mark a hight speed.

As said before, this is a bug I'm not sure how to fix... :(

Check if the distance is less than say. 500? Or even 200.

Well, this isn't a vvery good solution either, is it...
I mean, you can still blink 200. range :(
 

Romek

Super Moderator
Reaction score
963
I don't think this serves any purpose. In those few occasions when [ljass]GetUnitMoveSpeed[/ljass] doesn't suffice, the speed is most likely already stored or is easily obtainable in one way or another.

You used the example of a projectile; If anybody wanted to get the speed of a triggered projectile, they'd almost certainly store it in the projectile struct which they're sure to have. It'd be a simple array read in that case too.

Besides that, there're also many issues with something like this, which have been mentioned above.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top