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
964
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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top