• 🏆 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!

[Snippet] GetPointZ and UnitZ

Level 7
Joined
Apr 30, 2011
Messages
359
JASS:
//========================================================================================
//      
//      GetPointZ
//      -*- overcold_ice -*-
//      
//     -[*] Requirements:
//          - JNGP
//          - latest version of JassHelper
//      
//     -[*] API:
//      
//      function GetPointZ takes real x, real y returns real
//          returns the terrain z value of point [x, y]
//      
//========================================================================================
library GetPointZ
    
    globals
        private location _loc = Location(0, 0)
    endglobals
    
    function GetPointZ takes real x, real y returns real
        call MoveLocation(_loc, x, y)
        return GetLocationZ(_loc)
    endfunction
    
endlibrary
JASS:
//========================================================================================
//      
//      UnitZ
//      -*- overcold_ice -*-
//      
//     -[*] Requirements:
//          - JNGP
//          - latest version of JassHelper
//      
//     -[*] Optional Requirements:
//          - AutoFly
//              http://www.hiveworkshop.com/forums/jass-resources-412/snippet-autofly-unitindexer-version-195563/
//          - GetPointZ
//      
//          Adds the real UnitZ functions (not UnitFlyHeight)
//      
//     -[*] API:
//      
//      function GetUnitZ takes unit u returns real
//          returns the terrain z value of point [ux, uy] + its FlyHeight
//      function SetUnitZ takes unit u, real z returns nothing
//          sets the unit's FlyHeight to (z - ground z at [ux, uy])
//      
//========================================================================================
library UnitZ requires optional AutoFly, optional GetPointZ
    
    globals
        private location _loc = Location(0, 0)
    endglobals
    
    function GetUnitZ takes unit u returns real
        static if not LIBRARY_GetPointZ then
            call MoveLocation(_loc, x, y)
            return GetLocationZ(_loc) + GetUnitFlyHeight(u)
        else
            return GetPointZ(GetUnitX(u), GetUnitY(u)) + GetUnitFlyHeight(u)
        endif
    endfunction
    function SetUnitZ takes unit u, real z returns nothing
        static if not LIBRARY_AutoFly then
            if UnitAddAbility(u, 'Amrf') and UnitRemoveAbility(u, 'Amrf') then
            endif
        endif
        
        static if not LIBRARY_GetPointZ then
            call MoveLocation(_loc, GetUnitX(u), GetUnitY(u))
            call SetUnitFlyHeight(u, z - GetLocationZ(_loc), 0)
        else
            call SetUnitFlyHeight(u, z - GetPointZ(GetUnitX(u), GetUnitY(u)), 0)
        endif
    endfunction
    
endlibrary
 
Last edited:
JASS:
call UnitAddAbility(u, 'Amrf')
call UnitRemoveAbility(u, 'Amrf')
->
JASS:
if UnitAddAbility(u, 'Amrf') and UnitRemoveAbility(u, 'Amrf') then
endif

This has been done so many times before.
I can't count the number of times it's been done using my hands and feet and that should tell you something.

I don't know if I should approve it.
I can, and for modularity, I probably would, but why?
 
Level 7
Joined
Apr 30, 2011
Messages
359
@mag: thx for that 'if' correction

hmmm . . .
so that's it
you can gy this anyway, it's extremely small :)

i post it since i don't see anything like this around, i didn't check the small code snippets though
my reason is just because this thing helps my stuffs
and probably will help anyone else

moving loc to get ground z manually is tiring
and more when you use it in conjunction with unit's z
 
Top