[Snippet] FlyHeight

Scripts

13 years
edited 8 years
The documentation has a brief explanation as to why I made this.

Code

Code (jass) Select
/*************************************
*
*   FlyHeight
*   v1.0.0.1
*   By Magtheridon96
*
*   - Warcraft III movement types are pretty
*     bugged. It would be much better to write
*     them from scratch. This system allows
*     you to create flying units with changeable
*     heights.
*
*     All units must have Movement Type Foot.
*
*   Requires:
*   ---------
*
*       - UnitIndexer by Nestharus
*           - https://wc3modding.info/4607/unitindexer/
*
*       Optional:
*       ---------
*
*           - Table by Bribe
*               - https://wc3modding.info/4611/snippet-new-table/
*
*   API:
*   ----
*
*       - struct FlyHeight extends array
*
*           - static method operator [] takes unit u returns thistype
*               - Returns an instance of the struct given a unit
*
*           - static method setDefault takes integer unitType, real flyHeight returns nothing
*               - When a unit of a certain type enters the map, his height will be set
*                 to a default value input by the user using this function.
*
*           - method operator height takes nothing returns real
*               - Returns the height of a unit
*
*           - method operator height= takes real newHeight returns nothing
*               - Sets the height of a unit
*
*************************************/
library FlyHeight requires UnitIndexer, optional Table
   
    struct FlyHeight extends array
        private static real array h
       
        static if LIBRARY_Table then
            private static key defaultK
            private static Table default = defaultK
        else
            private static hashtable default = InitHashtable()
        endif
       
        method operator height takes nothing returns real
            return h[this]
        endmethod
       
        static method setDefault takes integer unitType, real defaultHeight returns nothing
            static if LIBRARY_Table then
                set default.real[unitType] = defaultHeight
            else
                call SaveReal(default, unitType, 0, defaultHeight)
            endif
        endmethod
       
        private static method haveDefault takes integer unitType returns boolean
            static if LIBRARY_Table then
                return default.real.has(unitType)
            else
                return HaveSavedReal(default, unitType, 0)
            endif
        endmethod
       
        private static method index takes nothing returns nothing
            local integer unitType = GetUnitTypeId(GetIndexedUnit())
           
            if haveDefault(unitType) then
                static if not LIBRARY_AutoFly then
                    if UnitAddAbility(GetIndexedUnit(), 'Amrf') then
                        call UnitRemoveAbility(GetIndexedUnit(), 'Amrf')
                    endif
                endif
                static if LIBRARY_Table then
                    set h[GetIndexedUnitId()] = default.real[unitType]
                else
                    set h[GetIndexedUnitId()] = LoadReal(default, unitType, 0)
                endif
                call SetUnitFlyHeight(GetIndexedUnit(), h[GetIndexedUnitId()], 0)
            endif
        endmethod
       
        implement UnitIndexStruct
       
        method operator height= takes real newHeight returns nothing
            if h[this] != newHeight then
                set h[this] = newHeight
                call SetUnitFlyHeight(this.unit, newHeight, 0)
            endif
        endmethod
    endstruct
   
endlibrary


Demo

Code (jass) Select
struct Demo extends array
    private static method onInit takes nothing returns nothing
        local unit peasant
        local unit hawk
           
        call FlyHeight.setDefault('hpea', 200)
        call FlyHeight.setDefault('hdhw', 150)
           
        set peasant = CreateUnit(Player(0), 'hpea', -256, 0, 270)
        set hawk = CreateUnit(Player(0), 'hdhw', 256, 0, 270)
           
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, R2SW(FlyHeight[peasant].height, 5, 1))
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, R2SW(FlyHeight[hawk].height, 5, 1))
           
        call TriggerSleepAction(5)
           
        set FlyHeight[peasant].height = 900
        set FlyHeight[hawk].height = 2
           
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, R2SW(FlyHeight[peasant].height, 5, 1))
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, R2SW(FlyHeight[hawk].height, 5, 1))
           
        set peasant = null
        set hawk = null
    endmethod
endstruct


What do you think? :D
13 years
Hmmm, any possiblity of a test map??
13 years
Nahh i search for this!! + REPT!!
13 years
There we go, I attached a map ^_^

By the way, I am never going to approve my own code snippets because that just isn't right :D
13 years
Quote from: Magtheridon96 on August 15, 2012, 10:23:46 PM
There we go, I attached a map ^_^

By the way, I am never going to approve my own code snippets because that just isn't right :D

And who told you that I'm going to impose that?? That's the reason that I'm here heheeee!!

And approvezorded!!!
13 years
But there are any spell's here?? ;) ;)